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"
44 #include "wx/richtext/richtextsizepage.h"
46 #include "wx/listimpl.cpp"
47 #include "wx/arrimpl.cpp"
49 WX_DEFINE_LIST(wxRichTextObjectList
)
50 WX_DEFINE_LIST(wxRichTextLineList
)
52 // Switch off if the platform doesn't like it for some reason
53 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
55 // Use GetPartialTextExtents for platforms that support it natively
56 #define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
58 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
60 // Helper classes for floating layout
61 struct wxRichTextFloatRectMap
63 wxRichTextFloatRectMap(int sY
, int eY
, int w
, wxRichTextObject
* obj
)
73 wxRichTextObject
* anchor
;
76 WX_DEFINE_SORTED_ARRAY(wxRichTextFloatRectMap
*, wxRichTextFloatRectMapArray
);
78 int wxRichTextFloatRectMapCmp(wxRichTextFloatRectMap
* r1
, wxRichTextFloatRectMap
* r2
)
80 return r1
->startY
- r2
->startY
;
83 class wxRichTextFloatCollector
86 wxRichTextFloatCollector(const wxRect
& availableRect
);
87 ~wxRichTextFloatCollector();
89 // Collect the floating objects info in the given paragraph
90 void CollectFloat(wxRichTextParagraph
* para
);
91 void CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
);
93 // Return the last paragraph we collected
94 wxRichTextParagraph
* LastParagraph();
96 // Given the start y position and the height of the line,
97 // find out how wide the line can be
98 wxRect
GetAvailableRect(int startY
, int endY
);
100 // Given a floating box, find its fit position
101 int GetFitPosition(int direction
, int start
, int height
) const;
102 int GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const;
104 // Find the last y position
105 int GetLastRectBottom();
107 // Draw the floats inside a rect
108 void Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
);
110 // HitTest the floats
111 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, int flags
);
113 // Get floating object count
114 int GetFloatingObjectCount() const { return m_left
.GetCount() + m_right
.GetCount(); }
116 // Get floating objects
117 bool GetFloatingObjects(wxRichTextObjectList
& objects
) const;
120 bool DeleteFloat(wxRichTextObject
* obj
);
122 // Do we have this float already?
123 bool HasFloat(wxRichTextObject
* obj
);
125 bool HasFloats() const { return m_left
.GetCount() >0 || m_right
.GetCount() > 0; }
127 static int SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
);
129 static int GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
);
131 static void FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
);
133 static void DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
);
135 static int HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, int flags
);
138 wxRichTextFloatRectMapArray m_left
;
139 wxRichTextFloatRectMapArray m_right
;
141 wxRect m_availableRect
;
142 wxRichTextParagraph
* m_para
;
146 bool wxRichTextFloatCollector::DeleteFloat(wxRichTextObject
* obj
)
149 for (i
= 0; i
< m_left
.GetCount(); i
++)
151 if (m_left
[i
]->anchor
== obj
)
157 for (i
= 0; i
< m_right
.GetCount(); i
++)
159 if (m_right
[i
]->anchor
== obj
)
168 // Do we have this float already?
169 bool wxRichTextFloatCollector::HasFloat(wxRichTextObject
* obj
)
172 for (i
= 0; i
< m_left
.GetCount(); i
++)
174 if (m_left
[i
]->anchor
== obj
)
179 for (i
= 0; i
< m_right
.GetCount(); i
++)
181 if (m_right
[i
]->anchor
== obj
)
189 // Get floating objects
190 bool wxRichTextFloatCollector::GetFloatingObjects(wxRichTextObjectList
& objects
) const
193 for (i
= 0; i
< m_left
.GetCount(); i
++)
194 objects
.Append(m_left
[i
]->anchor
);
195 for (i
= 0; i
< m_right
.GetCount(); i
++)
196 objects
.Append(m_right
[i
]->anchor
);
202 * Binary search helper function
203 * The argument point is the Y coordinate, and this fuction
204 * always return the floating rect that contain this coordinate
205 * or under this coordinate.
207 int wxRichTextFloatCollector::SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
)
209 int end
= array
.GetCount() - 1;
222 int mid
= (start
+ end
) / 2;
223 if (array
[mid
]->startY
<= point
&& array
[mid
]->endY
>= point
)
225 else if (array
[mid
]->startY
> point
)
230 else if (array
[mid
]->endY
< point
)
240 int wxRichTextFloatCollector::GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
)
243 int len
= array
.GetCount();
245 wxASSERT(index
>= 0 && index
< len
);
247 if (array
[index
]->startY
< startY
&& array
[index
]->endY
> startY
)
248 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
249 while (index
< len
&& array
[index
]->startY
<= endY
)
251 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
258 wxRichTextFloatCollector::wxRichTextFloatCollector(const wxRect
& rect
) : m_left(wxRichTextFloatRectMapCmp
), m_right(wxRichTextFloatRectMapCmp
)
260 m_availableRect
= rect
;
264 void wxRichTextFloatCollector::FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
)
266 int len
= array
.GetCount();
267 for (int i
= 0; i
< len
; i
++)
271 wxRichTextFloatCollector::~wxRichTextFloatCollector()
273 FreeFloatRectMapArray(m_left
);
274 FreeFloatRectMapArray(m_right
);
277 int wxRichTextFloatCollector::GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const
279 if (array
.GetCount() == 0)
282 int i
= SearchAdjacentRect(array
, start
);
284 while (i
< (int) array
.GetCount())
286 if (array
[i
]->startY
- last
>= height
)
288 last
= array
[i
]->endY
;
295 int wxRichTextFloatCollector::GetFitPosition(int direction
, int start
, int height
) const
297 if (direction
== wxTEXT_BOX_ATTR_FLOAT_LEFT
)
298 return GetFitPosition(m_left
, start
, height
);
299 else if (direction
== wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
300 return GetFitPosition(m_right
, start
, height
);
303 wxASSERT("Never should be here");
308 // Adds a floating image to the float collector.
309 // The actual positioning is done by wxRichTextParagraph::LayoutFloat.
310 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
)
312 int direction
= floating
->GetFloatDirection();
314 wxPoint pos
= floating
->GetPosition();
315 wxSize size
= floating
->GetCachedSize();
316 wxRichTextFloatRectMap
*map
= new wxRichTextFloatRectMap(pos
.y
, pos
.y
+ size
.y
, size
.x
, floating
);
319 case wxTEXT_BOX_ATTR_FLOAT_NONE
:
322 case wxTEXT_BOX_ATTR_FLOAT_LEFT
:
323 // Just a not-enough simple assertion
324 wxASSERT (m_left
.Index(map
) == wxNOT_FOUND
);
327 case wxTEXT_BOX_ATTR_FLOAT_RIGHT
:
328 wxASSERT (m_right
.Index(map
) == wxNOT_FOUND
);
333 wxASSERT("Unrecognised float attribute.");
339 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
)
341 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
344 wxRichTextObject
* floating
= node
->GetData();
346 if (floating
->IsFloating())
348 CollectFloat(para
, floating
);
351 node
= node
->GetNext();
357 wxRichTextParagraph
* wxRichTextFloatCollector::LastParagraph()
362 wxRect
wxRichTextFloatCollector::GetAvailableRect(int startY
, int endY
)
364 int widthLeft
= 0, widthRight
= 0;
365 if (m_left
.GetCount() != 0)
367 int i
= SearchAdjacentRect(m_left
, startY
);
368 if (i
< (int) m_left
.GetCount())
369 widthLeft
= GetWidthFromFloatRect(m_left
, i
, startY
, endY
);
371 if (m_right
.GetCount() != 0)
373 int j
= SearchAdjacentRect(m_right
, startY
);
374 if (j
< (int) m_right
.GetCount())
375 widthRight
= GetWidthFromFloatRect(m_right
, j
, startY
, endY
);
378 // TODO: actually we want to use the actual image positions to find the
379 // available remaining space, since the image might not be right up against
380 // the left or right edge of the container.
381 return wxRect(widthLeft
+ m_availableRect
.x
, 0, m_availableRect
.width
- widthLeft
- widthRight
, 0);
384 int wxRichTextFloatCollector::GetLastRectBottom()
387 int len
= m_left
.GetCount();
389 ret
= ret
> m_left
[len
-1]->endY
? ret
: m_left
[len
-1]->endY
;
391 len
= m_right
.GetCount();
393 ret
= ret
> m_right
[len
-1]->endY
? ret
: m_right
[len
-1]->endY
;
399 void wxRichTextFloatCollector::DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
402 int end
= rect
.y
+ rect
.height
;
404 i
= SearchAdjacentRect(array
, start
);
405 if (i
< 0 || i
>= (int) array
.GetCount())
407 j
= SearchAdjacentRect(array
, end
);
408 if (j
< 0 || j
>= (int) array
.GetCount())
409 j
= array
.GetCount() - 1;
412 wxRichTextObject
* obj
= array
[i
]->anchor
;
413 wxRichTextRange r
= obj
->GetRange();
414 obj
->Draw(dc
, r
, selection
, wxRect(obj
->GetPosition(), obj
->GetCachedSize()), descent
, style
);
419 void wxRichTextFloatCollector::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
421 if (m_left
.GetCount() > 0)
422 DrawFloat(m_left
, dc
, range
, selection
, rect
, descent
, style
);
423 if (m_right
.GetCount() > 0)
424 DrawFloat(m_right
, dc
, range
, selection
, rect
, descent
, style
);
427 int wxRichTextFloatCollector::HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, int WXUNUSED(flags
))
430 if (array
.GetCount() == 0)
431 return wxRICHTEXT_HITTEST_NONE
;
432 i
= SearchAdjacentRect(array
, pt
.y
);
433 if (i
< 0 || i
>= (int) array
.GetCount())
434 return wxRICHTEXT_HITTEST_NONE
;
435 if (!array
[i
]->anchor
->IsShown())
436 return wxRICHTEXT_HITTEST_NONE
;
438 wxPoint point
= array
[i
]->anchor
->GetPosition();
439 wxSize size
= array
[i
]->anchor
->GetCachedSize();
440 if (point
.x
<= pt
.x
&& point
.x
+ size
.x
>= pt
.x
441 && point
.y
<= pt
.y
&& point
.y
+ size
.y
>= pt
.y
)
443 textPosition
= array
[i
]->anchor
->GetRange().GetStart();
444 * obj
= array
[i
]->anchor
;
445 if (pt
.x
> (pt
.x
+ pt
.x
+ size
.x
) / 2)
446 return wxRICHTEXT_HITTEST_BEFORE
;
448 return wxRICHTEXT_HITTEST_AFTER
;
451 return wxRICHTEXT_HITTEST_NONE
;
454 int wxRichTextFloatCollector::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, int flags
)
456 int ret
= HitTestFloat(m_left
, dc
, pt
, textPosition
, obj
, flags
);
457 if (ret
== wxRICHTEXT_HITTEST_NONE
)
459 ret
= HitTestFloat(m_right
, dc
, pt
, textPosition
, obj
, flags
);
464 // Helpers for efficiency
465 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
467 // JACS: did I do this some time ago when testing? Should we re-enable it?
469 const wxFont
& font1
= dc
.GetFont();
470 if (font1
.IsOk() && font
.IsOk())
472 if (font1
.GetPointSize() == font
.GetPointSize() &&
473 font1
.GetFamily() == font
.GetFamily() &&
474 font1
.GetStyle() == font
.GetStyle() &&
475 font1
.GetWeight() == font
.GetWeight() &&
476 font1
.GetUnderlined() == font
.GetUnderlined() &&
477 font1
.GetFamily() == font
.GetFamily() &&
478 font1
.GetFaceName() == font
.GetFaceName())
485 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
487 const wxPen
& pen1
= dc
.GetPen();
488 if (pen1
.IsOk() && pen
.IsOk())
490 if (pen1
.GetWidth() == pen
.GetWidth() &&
491 pen1
.GetStyle() == pen
.GetStyle() &&
492 pen1
.GetColour() == pen
.GetColour())
498 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
500 const wxBrush
& brush1
= dc
.GetBrush();
501 if (brush1
.IsOk() && brush
.IsOk())
503 if (brush1
.GetStyle() == brush
.GetStyle() &&
504 brush1
.GetColour() == brush
.GetColour())
512 * This is the base for drawable objects.
515 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
517 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
525 wxRichTextObject::~wxRichTextObject()
529 void wxRichTextObject::Dereference()
537 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
540 m_maxSize
= obj
.m_maxSize
;
541 m_minSize
= obj
.m_minSize
;
543 m_range
= obj
.m_range
;
544 m_ownRange
= obj
.m_ownRange
;
545 m_attributes
= obj
.m_attributes
;
546 m_properties
= obj
.m_properties
;
547 m_descent
= obj
.m_descent
;
551 // Get/set the top-level container of this object.
552 wxRichTextParagraphLayoutBox
* wxRichTextObject::GetContainer() const
554 const wxRichTextObject
* p
= this;
559 return wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
566 void wxRichTextObject::SetMargins(int margin
)
568 SetMargins(margin
, margin
, margin
, margin
);
571 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
573 GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(leftMargin
, wxTEXT_ATTR_UNITS_PIXELS
);
574 GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(rightMargin
, wxTEXT_ATTR_UNITS_PIXELS
);
575 GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(topMargin
, wxTEXT_ATTR_UNITS_PIXELS
);
576 GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(bottomMargin
, wxTEXT_ATTR_UNITS_PIXELS
);
579 int wxRichTextObject::GetLeftMargin() const
581 return GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue();
584 int wxRichTextObject::GetRightMargin() const
586 return GetAttributes().GetTextBoxAttr().GetMargins().GetRight().GetValue();
589 int wxRichTextObject::GetTopMargin() const
591 return GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue();
594 int wxRichTextObject::GetBottomMargin() const
596 return GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().GetValue();
599 // Calculate the available content space in the given rectangle, given the
600 // margins, border and padding specified in the object's attributes.
601 wxRect
wxRichTextObject::GetAvailableContentArea(wxDC
& dc
, const wxRect
& outerRect
) const
603 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
604 marginRect
= outerRect
;
605 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
609 // Invalidate the buffer. With no argument, invalidates whole buffer.
610 void wxRichTextObject::Invalidate(const wxRichTextRange
& invalidRange
)
612 if (invalidRange
!= wxRICHTEXT_NONE
)
614 SetCachedSize(wxDefaultSize
);
615 SetMaxSize(wxDefaultSize
);
616 SetMinSize(wxDefaultSize
);
620 // Convert units in tenths of a millimetre to device units
621 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
626 scale
= GetBuffer()->GetScale();
627 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
, scale
);
632 // Convert units in tenths of a millimetre to device units
633 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
, double scale
)
635 // There are ppi pixels in 254.1 "1/10 mm"
637 double pixels
= ((double) units
* (double)ppi
) / 254.1;
641 // If the result is very small, make it at least one pixel in size.
642 if (pixels
== 0 && units
> 0)
648 // Convert units in pixels to tenths of a millimetre
649 int wxRichTextObject::ConvertPixelsToTenthsMM(wxDC
& dc
, int pixels
) const
654 scale
= GetBuffer()->GetScale();
656 return ConvertPixelsToTenthsMM(dc
.GetPPI().x
, p
, scale
);
659 int wxRichTextObject::ConvertPixelsToTenthsMM(int ppi
, int pixels
, double scale
)
661 // There are ppi pixels in 254.1 "1/10 mm"
663 double p
= double(pixels
);
668 int units
= int( p
* 254.1 / (double) ppi
);
672 // Draw the borders and background for the given rectangle and attributes.
673 // Width and height are taken to be the outer margin size, not the content.
674 bool wxRichTextObject::DrawBoxAttributes(wxDC
& dc
, wxRichTextBuffer
* buffer
, const wxRichTextAttr
& attr
, const wxRect
& boxRect
, int flags
)
676 // Assume boxRect is the area around the content
677 wxRect marginRect
= boxRect
;
678 wxRect contentRect
, borderRect
, paddingRect
, outlineRect
;
680 GetBoxRects(dc
, buffer
, attr
, marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
682 // Margin is transparent. Draw background from margin.
683 if (attr
.HasBackgroundColour() || (flags
& wxRICHTEXT_DRAW_SELECTED
))
686 if (flags
& wxRICHTEXT_DRAW_SELECTED
)
688 // TODO: get selection colour from control?
689 colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
692 colour
= attr
.GetBackgroundColour();
695 wxBrush
brush(colour
);
699 dc
.DrawRectangle(marginRect
);
702 if (flags
& wxRICHTEXT_DRAW_GUIDELINES
)
704 wxRichTextAttr editBorderAttr
= attr
;
705 // TODO: make guideline colour configurable
706 editBorderAttr
.GetTextBoxAttr().GetBorder().SetColour(*wxLIGHT_GREY
);
707 editBorderAttr
.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS
);
708 editBorderAttr
.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID
);
710 DrawBorder(dc
, buffer
, editBorderAttr
.GetTextBoxAttr().GetBorder(), borderRect
, flags
);
713 if (attr
.GetTextBoxAttr().GetBorder().IsValid())
714 DrawBorder(dc
, buffer
, attr
.GetTextBoxAttr().GetBorder(), borderRect
);
716 if (attr
.GetTextBoxAttr().GetOutline().IsValid())
717 DrawBorder(dc
, buffer
, attr
.GetTextBoxAttr().GetOutline(), outlineRect
);
723 bool wxRichTextObject::DrawBorder(wxDC
& dc
, wxRichTextBuffer
* buffer
, const wxTextAttrBorders
& attr
, const wxRect
& rect
, int WXUNUSED(flags
))
725 int borderLeft
= 0, borderRight
= 0, borderTop
= 0, borderBottom
= 0;
726 wxTextAttrDimensionConverter
converter(dc
, buffer
? buffer
->GetScale() : 1.0);
728 if (attr
.GetLeft().IsValid() && attr
.GetLeft().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE
)
730 borderLeft
= converter
.GetPixels(attr
.GetLeft().GetWidth());
731 wxColour
col(attr
.GetLeft().GetColour());
733 // If pen width is > 1, resorts to a solid rectangle.
736 int penStyle
= wxSOLID
;
737 if (attr
.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
739 else if (attr
.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
740 penStyle
= wxLONG_DASH
;
741 wxPen
pen(col
, 1, penStyle
);
743 dc
.DrawLine(rect
.x
, rect
.y
, rect
.x
, rect
.y
+ rect
.height
);
746 else if (borderLeft
> 1)
752 dc
.DrawRectangle(rect
.x
, rect
.y
, borderLeft
, rect
.height
);
756 if (attr
.GetRight().IsValid() && attr
.GetRight().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE
)
758 borderRight
= converter
.GetPixels(attr
.GetRight().GetWidth());
760 wxColour
col(attr
.GetRight().GetColour());
762 // If pen width is > 1, resorts to a solid rectangle.
763 if (borderRight
== 1)
765 int penStyle
= wxSOLID
;
766 if (attr
.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
768 else if (attr
.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
769 penStyle
= wxLONG_DASH
;
770 wxPen
pen(col
, 1, penStyle
);
772 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1);
775 else if (borderRight
> 1)
781 dc
.DrawRectangle(rect
.x
- borderRight
, rect
.y
, borderRight
, rect
.height
);
785 if (attr
.GetTop().IsValid() && attr
.GetTop().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE
)
787 borderTop
= converter
.GetPixels(attr
.GetTop().GetWidth());
789 wxColour
col(attr
.GetTop().GetColour());
791 // If pen width is > 1, resorts to a solid rectangle.
794 int penStyle
= wxSOLID
;
795 if (attr
.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
797 else if (attr
.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
798 penStyle
= wxLONG_DASH
;
799 wxPen
pen(col
, 1, penStyle
);
801 dc
.DrawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
);
804 else if (borderTop
> 1)
810 dc
.DrawRectangle(rect
.x
, rect
.y
, rect
.width
, borderTop
);
814 if (attr
.GetBottom().IsValid() && attr
.GetBottom().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE
)
816 borderBottom
= converter
.GetPixels(attr
.GetBottom().GetWidth());
817 wxColour
col(attr
.GetTop().GetColour());
819 // If pen width is > 1, resorts to a solid rectangle.
820 if (borderBottom
== 1)
822 int penStyle
= wxSOLID
;
823 if (attr
.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
825 else if (attr
.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
826 penStyle
= wxLONG_DASH
;
827 wxPen
pen(col
, 1, penStyle
);
829 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
832 else if (borderBottom
> 1)
838 dc
.DrawRectangle(rect
.x
, rect
.y
- rect
.height
- borderBottom
, rect
.width
, borderBottom
);
845 // Get the various rectangles of the box model in pixels. You can either specify contentRect (inner)
846 // or marginRect (outer), and the other must be the default rectangle (no width or height).
847 // Note that the outline doesn't affect the position of the rectangle, it's drawn in whatever space
850 // | Margin | Border | Padding | CONTENT | Padding | Border | Margin |
852 bool wxRichTextObject::GetBoxRects(wxDC
& dc
, wxRichTextBuffer
* buffer
, const wxRichTextAttr
& attr
, wxRect
& marginRect
, wxRect
& borderRect
, wxRect
& contentRect
, wxRect
& paddingRect
, wxRect
& outlineRect
)
854 int borderLeft
= 0, borderRight
= 0, borderTop
= 0, borderBottom
= 0;
855 int outlineLeft
= 0, outlineRight
= 0, outlineTop
= 0, outlineBottom
= 0;
856 int paddingLeft
= 0, paddingRight
= 0, paddingTop
= 0, paddingBottom
= 0;
857 int marginLeft
= 0, marginRight
= 0, marginTop
= 0, marginBottom
= 0;
859 wxTextAttrDimensionConverter
converter(dc
, buffer
? buffer
->GetScale() : 1.0);
861 if (attr
.GetTextBoxAttr().GetMargins().GetLeft().IsValid())
862 marginLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetLeft());
863 if (attr
.GetTextBoxAttr().GetMargins().GetRight().IsValid())
864 marginRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetRight());
865 if (attr
.GetTextBoxAttr().GetMargins().GetTop().IsValid())
866 marginTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetTop());
867 if (attr
.GetTextBoxAttr().GetMargins().GetLeft().IsValid())
868 marginBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetBottom());
870 if (attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth().IsValid())
871 borderLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth());
872 if (attr
.GetTextBoxAttr().GetBorder().GetRight().GetWidth().IsValid())
873 borderRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetRight().GetWidth());
874 if (attr
.GetTextBoxAttr().GetBorder().GetTop().GetWidth().IsValid())
875 borderTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetTop().GetWidth());
876 if (attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth().IsValid())
877 borderBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetBottom().GetWidth());
879 if (attr
.GetTextBoxAttr().GetPadding().GetLeft().IsValid())
880 paddingLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetLeft());
881 if (attr
.GetTextBoxAttr().GetPadding().GetRight().IsValid())
882 paddingRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetRight());
883 if (attr
.GetTextBoxAttr().GetPadding().GetTop().IsValid())
884 paddingTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetTop());
885 if (attr
.GetTextBoxAttr().GetPadding().GetBottom().IsValid())
886 paddingBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetBottom());
888 if (attr
.GetTextBoxAttr().GetOutline().GetLeft().GetWidth().IsValid())
889 outlineLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetLeft().GetWidth());
890 if (attr
.GetTextBoxAttr().GetOutline().GetRight().GetWidth().IsValid())
891 outlineRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetRight().GetWidth());
892 if (attr
.GetTextBoxAttr().GetOutline().GetTop().GetWidth().IsValid())
893 outlineTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetTop().GetWidth());
894 if (attr
.GetTextBoxAttr().GetOutline().GetBottom().GetWidth().IsValid())
895 outlineBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetBottom().GetWidth());
897 int leftTotal
= marginLeft
+ borderLeft
+ paddingLeft
;
898 int rightTotal
= marginRight
+ borderRight
+ paddingRight
;
899 int topTotal
= marginTop
+ borderTop
+ paddingTop
;
900 int bottomTotal
= marginBottom
+ borderBottom
+ paddingBottom
;
902 if (marginRect
!= wxRect())
904 contentRect
.x
= marginRect
.x
+ leftTotal
;
905 contentRect
.y
= marginRect
.y
+ topTotal
;
906 contentRect
.width
= marginRect
.width
- (leftTotal
+ rightTotal
);
907 contentRect
.height
= marginRect
.height
- (topTotal
+ bottomTotal
);
911 marginRect
.x
= contentRect
.x
- leftTotal
;
912 marginRect
.y
= contentRect
.y
- topTotal
;
913 marginRect
.width
= contentRect
.width
+ (leftTotal
+ rightTotal
);
914 marginRect
.height
= contentRect
.height
+ (topTotal
+ bottomTotal
);
917 borderRect
.x
= marginRect
.x
+ marginLeft
;
918 borderRect
.y
= marginRect
.y
+ marginTop
;
919 borderRect
.width
= marginRect
.width
- (marginLeft
+ marginRight
);
920 borderRect
.height
= marginRect
.height
- (marginTop
+ marginBottom
);
922 paddingRect
.x
= marginRect
.x
+ marginLeft
+ borderLeft
;
923 paddingRect
.y
= marginRect
.y
+ marginTop
+ borderTop
;
924 paddingRect
.width
= marginRect
.width
- (marginLeft
+ marginRight
+ borderLeft
+ borderRight
);
925 paddingRect
.height
= marginRect
.height
- (marginTop
+ marginBottom
+ borderTop
+ borderBottom
);
927 // The outline is outside the margin and doesn't influence the overall box position or content size.
928 outlineRect
.x
= marginRect
.x
- outlineLeft
;
929 outlineRect
.y
= marginRect
.y
- outlineTop
;
930 outlineRect
.width
= marginRect
.width
+ (outlineLeft
+ outlineRight
);
931 outlineRect
.height
= marginRect
.height
+ (outlineTop
+ outlineBottom
);
936 // Get the total margin for the object in pixels, taking into account margin, padding and border size
937 bool wxRichTextObject::GetTotalMargin(wxDC
& dc
, wxRichTextBuffer
* buffer
, const wxRichTextAttr
& attr
, int& leftMargin
, int& rightMargin
,
938 int& topMargin
, int& bottomMargin
)
940 // Assume boxRect is the area around the content
941 wxRect contentRect
, marginRect
, borderRect
, paddingRect
, outlineRect
;
942 marginRect
= wxRect(0, 0, 1000, 1000);
944 GetBoxRects(dc
, buffer
, attr
, marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
946 leftMargin
= contentRect
.GetLeft() - marginRect
.GetLeft();
947 rightMargin
= marginRect
.GetRight() - contentRect
.GetRight();
948 topMargin
= contentRect
.GetTop() - marginRect
.GetTop();
949 bottomMargin
= marginRect
.GetBottom() - contentRect
.GetBottom();
954 // Returns the rectangle which the child has available to it given restrictions specified in the
955 // child attribute, e.g. 50% width of the parent, 400 pixels, x position 20% of the parent, etc.
956 wxRect
wxRichTextObject::AdjustAvailableSpace(wxDC
& dc
, wxRichTextBuffer
* buffer
, const wxRichTextAttr
& WXUNUSED(parentAttr
), const wxRichTextAttr
& childAttr
, const wxRect
& availableParentSpace
)
958 wxRect rect
= availableParentSpace
;
961 scale
= buffer
->GetScale();
963 wxTextAttrDimensionConverter
converter(dc
, scale
, availableParentSpace
.GetSize());
965 if (childAttr
.GetTextBoxAttr().GetWidth().IsValid())
966 rect
.width
= converter
.GetPixels(childAttr
.GetTextBoxAttr().GetWidth());
968 if (childAttr
.GetTextBoxAttr().GetHeight().IsValid())
969 rect
.height
= converter
.GetPixels(childAttr
.GetTextBoxAttr().GetHeight());
971 // Can specify either left or right for the position (we're assuming we can't
972 // set the left and right edges to effectively set the size. Would we want to do that?)
973 if (childAttr
.GetTextBoxAttr().GetPosition().GetLeft().IsValid())
975 rect
.x
= rect
.x
+ converter
.GetPixels(childAttr
.GetTextBoxAttr().GetPosition().GetLeft());
977 else if (childAttr
.GetTextBoxAttr().GetPosition().GetRight().IsValid())
979 int x
= converter
.GetPixels(childAttr
.GetTextBoxAttr().GetPosition().GetRight());
980 if (childAttr
.GetTextBoxAttr().GetPosition().GetRight().GetPosition() == wxTEXT_BOX_ATTR_POSITION_RELATIVE
)
981 rect
.x
= availableParentSpace
.x
+ availableParentSpace
.width
- rect
.width
;
986 if (childAttr
.GetTextBoxAttr().GetPosition().GetTop().IsValid())
988 rect
.y
= rect
.y
+ converter
.GetPixels(childAttr
.GetTextBoxAttr().GetPosition().GetTop());
990 else if (childAttr
.GetTextBoxAttr().GetPosition().GetBottom().IsValid())
992 int y
= converter
.GetPixels(childAttr
.GetTextBoxAttr().GetPosition().GetBottom());
993 if (childAttr
.GetTextBoxAttr().GetPosition().GetBottom().GetPosition() == wxTEXT_BOX_ATTR_POSITION_RELATIVE
)
994 rect
.y
= availableParentSpace
.y
+ availableParentSpace
.height
- rect
.height
;
1002 // Dump to output stream for debugging
1003 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
1005 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
1006 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");
1007 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");
1010 // Gets the containing buffer
1011 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
1013 const wxRichTextObject
* obj
= this;
1014 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
1015 obj
= obj
->GetParent();
1016 return wxDynamicCast(obj
, wxRichTextBuffer
);
1019 // Get the absolute object position, by traversing up the child/parent hierarchy
1020 wxPoint
wxRichTextObject::GetAbsolutePosition() const
1022 wxPoint pt
= GetPosition();
1024 wxRichTextObject
* p
= GetParent();
1027 pt
= pt
+ p
->GetPosition();
1034 // Hit-testing: returns a flag indicating hit test details, plus
1035 // information about position
1036 int wxRichTextObject::HitTest(wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, wxRichTextObject
** contextObj
, int WXUNUSED(flags
))
1039 return wxRICHTEXT_HITTEST_NONE
;
1041 wxRect rect
= GetRect();
1042 if (pt
.x
>= rect
.x
&& pt
.x
< rect
.x
+ rect
.width
&&
1043 pt
.y
>= rect
.y
&& pt
.y
< rect
.y
+ rect
.height
)
1046 *contextObj
= GetParentContainer();
1047 textPosition
= GetRange().GetStart();
1048 return wxRICHTEXT_HITTEST_ON
;
1051 return wxRICHTEXT_HITTEST_NONE
;
1054 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
1055 // lays out the object again using the maximum ('best') size
1056 bool wxRichTextObject::LayoutToBestSize(wxDC
& dc
, wxRichTextBuffer
* buffer
,
1057 const wxRichTextAttr
& parentAttr
, const wxRichTextAttr
& attr
, const wxRect
& availableParentSpace
,
1060 wxRect availableChildRect
= AdjustAvailableSpace(dc
, buffer
, parentAttr
, attr
, availableParentSpace
);
1061 wxRect originalAvailableRect
= availableChildRect
;
1062 Layout(dc
, availableChildRect
, style
);
1064 wxSize maxSize
= GetMaxSize();
1066 // Don't ignore if maxSize.x is zero, since we need to redo the paragraph's lines
1068 if (!attr
.GetTextBoxAttr().GetWidth().IsValid() && maxSize
.x
< availableChildRect
.width
/* && maxSize.x > 0 */)
1070 // Redo the layout with a fixed, minimum size this time.
1071 Invalidate(wxRICHTEXT_ALL
);
1072 wxRichTextAttr
newAttr(attr
);
1073 newAttr
.GetTextBoxAttr().GetWidth().SetValue(maxSize
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
1074 newAttr
.GetTextBoxAttr().GetWidth().SetPosition(wxTEXT_BOX_ATTR_POSITION_ABSOLUTE
);
1076 availableChildRect
= AdjustAvailableSpace(dc
, buffer
, parentAttr
, newAttr
, availableParentSpace
);
1078 // If a paragraph, align the whole paragraph.
1079 // Problem with this: if we're limited by a floating object, a line may be centered
1080 // w.r.t. the smaller resulting box rather than the actual available width.
1081 if (attr
.HasAlignment() && !GetContainer()->GetFloatCollector()->HasFloats()) // FIXME: aligning whole paragraph not compatible with floating objects
1083 // centering, right-justification
1084 if (GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1086 availableChildRect
.x
= (originalAvailableRect
.GetWidth() - availableChildRect
.GetWidth())/2 + availableChildRect
.x
;
1088 else if (GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1090 availableChildRect
.x
= availableChildRect
.x
+ originalAvailableRect
.GetWidth() - availableChildRect
.GetWidth();
1094 Layout(dc
, availableChildRect
, style
);
1108 // Move the object recursively, by adding the offset from old to new
1109 void wxRichTextObject::Move(const wxPoint
& pt
)
1116 * wxRichTextCompositeObject
1117 * This is the base for drawable objects.
1120 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
1122 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
1123 wxRichTextObject(parent
)
1127 wxRichTextCompositeObject::~wxRichTextCompositeObject()
1132 /// Get the nth child
1133 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
1135 wxASSERT ( n
< m_children
.GetCount() );
1137 return m_children
.Item(n
)->GetData();
1140 /// Append a child, returning the position
1141 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
1143 m_children
.Append(child
);
1144 child
->SetParent(this);
1145 return m_children
.GetCount() - 1;
1148 /// Insert the child in front of the given object, or at the beginning
1149 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
1153 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
1154 m_children
.Insert(node
, child
);
1157 m_children
.Insert(child
);
1158 child
->SetParent(this);
1163 /// Delete the child
1164 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
1166 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
1169 wxRichTextObject
* obj
= node
->GetData();
1170 m_children
.Erase(node
);
1179 /// Delete all children
1180 bool wxRichTextCompositeObject::DeleteChildren()
1182 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1185 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
1187 wxRichTextObject
* child
= node
->GetData();
1188 child
->Dereference(); // Only delete if reference count is zero
1190 node
= node
->GetNext();
1191 m_children
.Erase(oldNode
);
1197 /// Get the child count
1198 size_t wxRichTextCompositeObject::GetChildCount() const
1200 return m_children
.GetCount();
1204 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
1206 wxRichTextObject::Copy(obj
);
1210 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
1213 wxRichTextObject
* child
= node
->GetData();
1214 wxRichTextObject
* newChild
= child
->Clone();
1215 newChild
->SetParent(this);
1216 m_children
.Append(newChild
);
1218 node
= node
->GetNext();
1222 /// Hit-testing: returns a flag indicating hit test details, plus
1223 /// information about position
1224 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, wxRichTextObject
** contextObj
, int flags
)
1227 return wxRICHTEXT_HITTEST_NONE
;
1229 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1232 wxRichTextObject
* child
= node
->GetData();
1234 if (child
->IsShown() && child
->IsTopLevel() && (flags
& wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
))
1236 // Just check if we hit the overall object
1237 int ret
= child
->wxRichTextObject::HitTest(dc
, pt
, textPosition
, obj
, contextObj
, flags
);
1238 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
1241 else if (child
->IsShown())
1243 int ret
= child
->HitTest(dc
, pt
, textPosition
, obj
, contextObj
, flags
);
1244 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
1248 node
= node
->GetNext();
1251 return wxRICHTEXT_HITTEST_NONE
;
1254 /// Finds the absolute position and row height for the given character position
1255 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
1257 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1260 wxRichTextObject
* child
= node
->GetData();
1262 // Don't recurse if the child is a top-level object,
1263 // such as a text box, because the character position will no longer
1264 // apply. By definition, a top-level object has its own range of
1265 // character positions.
1266 if (!child
->IsTopLevel() && child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
1269 node
= node
->GetNext();
1276 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
1278 long current
= start
;
1279 long lastEnd
= current
;
1287 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1290 wxRichTextObject
* child
= node
->GetData();
1293 child
->CalculateRange(current
, childEnd
);
1296 current
= childEnd
+ 1;
1298 node
= node
->GetNext();
1303 // A top-level object always has a range of size 1,
1304 // because its children don't count at this level.
1306 m_range
.SetRange(start
, start
);
1308 // An object with no children has zero length
1309 if (m_children
.GetCount() == 0)
1311 m_ownRange
.SetRange(0, lastEnd
);
1317 // An object with no children has zero length
1318 if (m_children
.GetCount() == 0)
1321 m_range
.SetRange(start
, end
);
1325 /// Delete range from layout.
1326 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
1328 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1332 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
1333 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1335 // Delete the range in each paragraph
1337 // When a chunk has been deleted, internally the content does not
1338 // now match the ranges.
1339 // However, so long as deletion is not done on the same object twice this is OK.
1340 // If you may delete content from the same object twice, recalculate
1341 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
1342 // adjust the range you're deleting accordingly.
1344 if (!obj
->GetRange().IsOutside(range
))
1346 // No need to delete within a top-level object; just removing this object will do fine
1347 if (!obj
->IsTopLevel())
1348 obj
->DeleteRange(range
);
1350 // Delete an empty object, or paragraph within this range.
1351 if (obj
->IsEmpty() ||
1352 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
1354 // An empty paragraph has length 1, so won't be deleted unless the
1355 // whole range is deleted.
1356 RemoveChild(obj
, true);
1366 /// Get any text in this object for the given range
1367 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
1370 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1373 wxRichTextObject
* child
= node
->GetData();
1374 wxRichTextRange childRange
= range
;
1375 if (!child
->GetRange().IsOutside(range
))
1377 childRange
.LimitTo(child
->GetRange());
1379 wxString childText
= child
->GetTextForRange(childRange
);
1383 node
= node
->GetNext();
1389 /// Get the child object at the given character position
1390 wxRichTextObject
* wxRichTextCompositeObject::GetChildAtPosition(long pos
) const
1392 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1395 wxRichTextObject
* child
= node
->GetData();
1396 if (child
->GetRange().GetStart() == pos
)
1398 node
= node
->GetNext();
1403 /// Recursively merge all pieces that can be merged.
1404 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
1406 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1409 wxRichTextObject
* child
= node
->GetData();
1410 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
1412 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
1414 composite
->Defragment();
1416 if (node
->GetNext())
1418 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
1419 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
1421 nextChild
->Dereference();
1422 m_children
.Erase(node
->GetNext());
1424 // Don't set node -- we'll see if we can merge again with the next
1428 node
= node
->GetNext();
1431 node
= node
->GetNext();
1434 node
= node
->GetNext();
1437 // Delete any remaining empty objects, but leave at least one empty object per composite object.
1438 if (GetChildCount() > 1)
1440 node
= m_children
.GetFirst();
1443 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1444 wxRichTextObject
* child
= node
->GetData();
1445 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
1447 if (child
->IsEmpty())
1449 child
->Dereference();
1450 m_children
.Erase(node
);
1455 node
= node
->GetNext();
1462 /// Dump to output stream for debugging
1463 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
1465 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1468 wxRichTextObject
* child
= node
->GetData();
1469 child
->Dump(stream
);
1470 node
= node
->GetNext();
1474 /// Get/set the object size for the given range. Returns false if the range
1475 /// is invalid for this object.
1476 bool wxRichTextCompositeObject::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
1478 if (!range
.IsWithin(GetRange()))
1483 wxArrayInt childExtents
;
1490 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1493 wxRichTextObject
* child
= node
->GetData();
1494 if (!child
->GetRange().IsOutside(range
))
1496 // Floating objects have a zero size within the paragraph.
1497 if (child
->IsFloating())
1502 if (partialExtents
->GetCount() > 0)
1503 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
1507 partialExtents
->Add(0 /* zero size */ + lastSize
);
1514 wxRichTextRange rangeToUse
= range
;
1515 rangeToUse
.LimitTo(child
->GetRange());
1516 if (child
->IsTopLevel())
1517 rangeToUse
= child
->GetOwnRange();
1519 int childDescent
= 0;
1521 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
1522 // but it's only going to be used after caching has taken place.
1523 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
1525 childDescent
= child
->GetDescent();
1526 childSize
= child
->GetCachedSize();
1528 sz
.y
= wxMax(sz
.y
, childSize
.y
);
1529 sz
.x
+= childSize
.x
;
1530 descent
= wxMax(descent
, childDescent
);
1532 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
1534 sz
.y
= wxMax(sz
.y
, childSize
.y
);
1535 sz
.x
+= childSize
.x
;
1536 descent
= wxMax(descent
, childDescent
);
1538 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange() || child
->IsTopLevel()))
1540 child
->SetCachedSize(childSize
);
1541 child
->SetDescent(childDescent
);
1547 if (partialExtents
->GetCount() > 0)
1548 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
1553 for (i
= 0; i
< childExtents
.GetCount(); i
++)
1555 partialExtents
->Add(childExtents
[i
] + lastSize
);
1565 node
= node
->GetNext();
1571 // Invalidate the buffer. With no argument, invalidates whole buffer.
1572 void wxRichTextCompositeObject::Invalidate(const wxRichTextRange
& invalidRange
)
1574 wxRichTextObject::Invalidate(invalidRange
);
1576 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1579 wxRichTextObject
* child
= node
->GetData();
1580 if (invalidRange
!= wxRICHTEXT_ALL
&& invalidRange
!= wxRICHTEXT_NONE
&& child
->GetRange().IsOutside(invalidRange
))
1584 else if (child
->IsTopLevel())
1586 if (invalidRange
== wxRICHTEXT_NONE
)
1587 child
->Invalidate(wxRICHTEXT_NONE
);
1589 child
->Invalidate(wxRICHTEXT_ALL
); // All children must be invalidated if within parent range
1592 child
->Invalidate(invalidRange
);
1593 node
= node
->GetNext();
1597 // Move the object recursively, by adding the offset from old to new
1598 void wxRichTextCompositeObject::Move(const wxPoint
& pt
)
1600 wxPoint oldPos
= GetPosition();
1602 wxPoint offset
= pt
- oldPos
;
1604 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1607 wxRichTextObject
* child
= node
->GetData();
1608 wxPoint childPos
= child
->GetPosition() + offset
;
1609 child
->Move(childPos
);
1610 node
= node
->GetNext();
1616 * wxRichTextParagraphLayoutBox
1617 * This box knows how to lay out paragraphs.
1620 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextCompositeObject
)
1622 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
1623 wxRichTextCompositeObject(parent
)
1628 wxRichTextParagraphLayoutBox::~wxRichTextParagraphLayoutBox()
1630 if (m_floatCollector
)
1632 delete m_floatCollector
;
1633 m_floatCollector
= NULL
;
1637 /// Initialize the object.
1638 void wxRichTextParagraphLayoutBox::Init()
1642 // For now, assume is the only box and has no initial size.
1643 m_range
= wxRichTextRange(0, -1);
1644 m_ownRange
= wxRichTextRange(0, -1);
1646 m_invalidRange
= wxRICHTEXT_ALL
;
1649 m_partialParagraph
= false;
1650 m_floatCollector
= NULL
;
1653 void wxRichTextParagraphLayoutBox::Clear()
1657 if (m_floatCollector
)
1658 delete m_floatCollector
;
1659 m_floatCollector
= NULL
;
1660 m_partialParagraph
= false;
1664 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
1668 wxRichTextCompositeObject::Copy(obj
);
1670 m_partialParagraph
= obj
.m_partialParagraph
;
1671 m_defaultAttributes
= obj
.m_defaultAttributes
;
1674 // Gather information about floating objects; only gather floats for those paragraphs that
1675 // will not be formatted again due to optimization, after which floats will be gathered per-paragraph
1677 bool wxRichTextParagraphLayoutBox::UpdateFloatingObjects(const wxRect
& availableRect
, wxRichTextObject
* untilObj
)
1679 if (m_floatCollector
!= NULL
)
1680 delete m_floatCollector
;
1681 m_floatCollector
= new wxRichTextFloatCollector(availableRect
);
1682 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1683 // Only gather floats up to the point we'll start formatting paragraphs.
1684 while (untilObj
&& node
&& node
->GetData() != untilObj
)
1686 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1687 wxASSERT (child
!= NULL
);
1689 m_floatCollector
->CollectFloat(child
);
1690 node
= node
->GetNext();
1696 // Returns the style sheet associated with the overall buffer.
1697 wxRichTextStyleSheet
* wxRichTextParagraphLayoutBox::GetStyleSheet() const
1699 return GetBuffer() ? GetBuffer()->GetStyleSheet() : (wxRichTextStyleSheet
*) NULL
;
1702 // Get the number of floating objects at this level
1703 int wxRichTextParagraphLayoutBox::GetFloatingObjectCount() const
1705 if (m_floatCollector
)
1706 return m_floatCollector
->GetFloatingObjectCount();
1711 // Get a list of floating objects
1712 bool wxRichTextParagraphLayoutBox::GetFloatingObjects(wxRichTextObjectList
& objects
) const
1714 if (m_floatCollector
)
1716 return m_floatCollector
->GetFloatingObjects(objects
);
1723 void wxRichTextParagraphLayoutBox::UpdateRanges()
1727 start
= GetRange().GetStart();
1729 CalculateRange(start
, end
);
1733 int wxRichTextParagraphLayoutBox::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, wxRichTextObject
** contextObj
, int flags
)
1736 return wxRICHTEXT_HITTEST_NONE
;
1738 int ret
= wxRICHTEXT_HITTEST_NONE
;
1739 if (m_floatCollector
&& (flags
& wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
) == 0)
1740 ret
= m_floatCollector
->HitTest(dc
, pt
, textPosition
, obj
, flags
);
1742 if (ret
== wxRICHTEXT_HITTEST_NONE
)
1743 return wxRichTextCompositeObject::HitTest(dc
, pt
, textPosition
, obj
, contextObj
, flags
);
1751 /// Draw the floating objects
1752 void wxRichTextParagraphLayoutBox::DrawFloats(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
1754 if (m_floatCollector
)
1755 m_floatCollector
->Draw(dc
, range
, selection
, rect
, descent
, style
);
1758 void wxRichTextParagraphLayoutBox::MoveAnchoredObjectToParagraph(wxRichTextParagraph
* from
, wxRichTextParagraph
* to
, wxRichTextObject
* obj
)
1763 from
->RemoveChild(obj
);
1764 to
->AppendChild(obj
);
1768 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
1773 wxRect
thisRect(GetPosition(), GetCachedSize());
1776 if (selection
.IsValid() && GetParentContainer() != this && selection
.WithinSelection(GetRange().GetStart(), GetParentContainer()))
1777 flags
|= wxRICHTEXT_DRAW_SELECTED
;
1779 // Don't draw guidelines if at top level
1780 int theseFlags
= flags
;
1782 theseFlags
&= ~wxRICHTEXT_DRAW_GUIDELINES
;
1783 DrawBoxAttributes(dc
, GetBuffer(), GetAttributes(), thisRect
, theseFlags
);
1785 DrawFloats(dc
, range
, selection
, rect
, descent
, style
);
1786 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1789 wxRichTextObject
* child
= node
->GetData();
1791 if (child
&& !child
->GetRange().IsOutside(range
))
1793 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
1794 wxRichTextRange childRange
= range
;
1795 if (child
->IsTopLevel())
1797 childRange
= child
->GetOwnRange();
1800 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
1805 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
1810 child
->Draw(dc
, childRange
, selection
, rect
, descent
, style
);
1813 node
= node
->GetNext();
1818 /// Lay the item out
1819 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1821 SetPosition(rect
.GetPosition());
1826 wxRect availableSpace
;
1827 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
1829 // If only laying out a specific area, the passed rect has a different meaning:
1830 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
1831 // so that during a size, only the visible part will be relaid out, or
1832 // it would take too long causing flicker. As an approximation, we assume that
1833 // everything up to the start of the visible area is laid out correctly.
1836 wxRect
rect2(0, 0, rect
.width
, rect
.height
);
1837 availableSpace
= GetAvailableContentArea(dc
, rect2
);
1839 // Invalidate the part of the buffer from the first visible line
1840 // to the end. If other parts of the buffer are currently invalid,
1841 // then they too will be taken into account if they are above
1842 // the visible point.
1844 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
1846 startPos
= line
->GetAbsoluteRange().GetStart();
1848 Invalidate(wxRichTextRange(startPos
, GetOwnRange().GetEnd()));
1852 availableSpace
= GetAvailableContentArea(dc
, rect
);
1855 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1856 wxRichTextObject::GetTotalMargin(dc
, GetBuffer(), GetAttributes(), leftMargin
, rightMargin
,
1857 topMargin
, bottomMargin
);
1862 // The maximum paragraph maximum width, so we can set the overall maximum width for this object
1863 int maxMaxWidth
= 0;
1865 // The maximum paragraph minimum width, so we can set the overall minimum width for this object
1866 int maxMinWidth
= 0;
1868 // If we have vertical alignment, we must recalculate everything.
1869 bool hasVerticalAlignment
= (GetAttributes().GetTextBoxAttr().HasVerticalAlignment() &&
1870 (GetAttributes().GetTextBoxAttr().GetVerticalAlignment() > wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP
));
1872 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1874 bool layoutAll
= true;
1876 // Get invalid range, rounding to paragraph start/end.
1877 wxRichTextRange invalidRange
= GetInvalidRange(true);
1879 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
1882 if (invalidRange
== wxRICHTEXT_ALL
|| hasVerticalAlignment
)
1884 else // If we know what range is affected, start laying out from that point on.
1885 if (invalidRange
.GetStart() >= GetOwnRange().GetStart())
1887 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
1890 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
1891 wxRichTextObjectList::compatibility_iterator previousNode
;
1893 previousNode
= firstNode
->GetPrevious();
1898 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
1899 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
1902 // Now we're going to start iterating from the first affected paragraph.
1910 // Gather information about only those floating objects that will not be formatted,
1911 // after which floats will be gathered per-paragraph during layout.
1912 UpdateFloatingObjects(availableSpace
, node
? node
->GetData() : (wxRichTextObject
*) NULL
);
1914 // A way to force speedy rest-of-buffer layout (the 'else' below)
1915 bool forceQuickLayout
= false;
1919 // Assume this box only contains paragraphs
1921 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1922 // Unsure if this is needed
1923 // wxCHECK_MSG( child, false, wxT("Unknown object in layout") );
1925 if (child
&& child
->IsShown())
1927 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
1928 if ( !forceQuickLayout
&&
1930 child
->GetLines().IsEmpty() ||
1931 !child
->GetRange().IsOutside(invalidRange
)) )
1933 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
1934 // lays out the object again using the minimum size
1935 child
->LayoutToBestSize(dc
, GetBuffer(),
1936 GetAttributes(), child
->GetAttributes(), availableSpace
, style
&~wxRICHTEXT_LAYOUT_SPECIFIED_RECT
);
1938 // Layout must set the cached size
1939 availableSpace
.y
+= child
->GetCachedSize().y
;
1940 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1941 maxMinWidth
= wxMax(maxMinWidth
, child
->GetMinSize().x
);
1942 maxMaxWidth
= wxMax(maxMaxWidth
, child
->GetMaxSize().x
);
1944 // If we're just formatting the visible part of the buffer,
1945 // and we're now past the bottom of the window, and we don't have any
1946 // floating objects (since they may cause wrapping to change for the rest of the
1947 // the buffer), start quick layout.
1948 if (!hasVerticalAlignment
&& formatRect
&& child
->GetPosition().y
> rect
.GetBottom() && GetFloatingObjectCount() == 0)
1949 forceQuickLayout
= true;
1953 // We're outside the immediately affected range, so now let's just
1954 // move everything up or down. This assumes that all the children have previously
1955 // been laid out and have wrapped line lists associated with them.
1956 // TODO: check all paragraphs before the affected range.
1958 int inc
= availableSpace
.y
- child
->GetPosition().y
;
1962 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1965 if (child
->GetLines().GetCount() == 0)
1967 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
1968 // lays out the object again using the minimum size
1969 child
->LayoutToBestSize(dc
, GetBuffer(),
1970 GetAttributes(), child
->GetAttributes(), availableSpace
, style
&~wxRICHTEXT_LAYOUT_SPECIFIED_RECT
);
1972 //child->Layout(dc, availableChildRect, style);
1975 child
->Move(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
1977 availableSpace
.y
+= child
->GetCachedSize().y
;
1978 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1979 maxMinWidth
= wxMax(maxMinWidth
, child
->GetMinSize().x
);
1980 maxMaxWidth
= wxMax(maxMaxWidth
, child
->GetMaxSize().x
);
1983 node
= node
->GetNext();
1989 node
= node
->GetNext();
1992 node
= m_children
.GetLast();
1993 if (node
&& node
->GetData()->IsShown())
1995 wxRichTextObject
* child
= node
->GetData();
1996 // maxHeight = (child->GetPosition().y - GetPosition().y) + child->GetCachedSize().y;
1997 maxHeight
= child
->GetPosition().y
- (GetPosition().y
+ topMargin
) + child
->GetCachedSize().y
;
2000 maxHeight
= 0; // topMargin + bottomMargin;
2002 // TODO: (also in para layout) should set the
2003 // object's size to an absolute one if specified,
2004 // but if not specified, calculate it from content.
2006 // We need to add back the margins etc.
2008 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
2009 contentRect
= wxRect(wxPoint(0, 0), wxSize(maxWidth
, maxHeight
));
2010 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
2011 SetCachedSize(marginRect
.GetSize());
2014 // The maximum size is the greatest of all maximum widths for all paragraphs.
2016 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
2017 contentRect
= wxRect(wxPoint(0, 0), wxSize(maxMaxWidth
, maxHeight
)); // Actually max height is a lie, we can't know it
2018 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
2019 SetMaxSize(marginRect
.GetSize());
2022 // The minimum size is the greatest of all minimum widths for all paragraphs.
2024 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
2025 contentRect
= wxRect(wxPoint(0, 0), wxSize(maxMinWidth
, maxHeight
)); // Actually max height is a lie, we can't know it
2026 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
2027 SetMinSize(marginRect
.GetSize());
2030 if (GetAttributes().GetTextBoxAttr().HasVerticalAlignment() &&
2031 (GetAttributes().GetTextBoxAttr().GetVerticalAlignment() > wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP
))
2034 int leftOverSpace
= availableSpace
.height
- topMargin
- bottomMargin
- maxHeight
;
2035 if (leftOverSpace
> 0)
2037 if (GetAttributes().GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_CENTRE
)
2039 yOffset
= (leftOverSpace
/2);
2041 else if (GetAttributes().GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_BOTTOM
)
2043 yOffset
= leftOverSpace
;
2047 // Move all the children to vertically align the content
2048 // This doesn't take into account floating objects, unfortunately.
2051 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2054 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2056 child
->Move(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ yOffset
));
2058 node
= node
->GetNext();
2063 m_invalidRange
= wxRICHTEXT_NONE
;
2068 /// Get/set the size for the given range.
2069 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
2073 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
2074 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
2076 // First find the first paragraph whose starting position is within the range.
2077 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2080 // child is a paragraph
2081 wxRichTextObject
* child
= node
->GetData();
2082 const wxRichTextRange
& r
= child
->GetRange();
2084 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
2090 node
= node
->GetNext();
2093 // Next find the last paragraph containing part of the range
2094 node
= m_children
.GetFirst();
2097 // child is a paragraph
2098 wxRichTextObject
* child
= node
->GetData();
2099 const wxRichTextRange
& r
= child
->GetRange();
2101 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
2107 node
= node
->GetNext();
2110 if (!startPara
|| !endPara
)
2113 // Now we can add up the sizes
2114 for (node
= startPara
; node
; node
= node
->GetNext())
2116 // child is a paragraph
2117 wxRichTextObject
* child
= node
->GetData();
2118 const wxRichTextRange
& childRange
= child
->GetRange();
2119 wxRichTextRange rangeToFind
= range
;
2120 rangeToFind
.LimitTo(childRange
);
2122 if (child
->IsTopLevel())
2123 rangeToFind
= child
->GetOwnRange();
2127 int childDescent
= 0;
2128 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
2130 descent
= wxMax(childDescent
, descent
);
2132 sz
.x
= wxMax(sz
.x
, childSize
.x
);
2133 sz
.y
+= childSize
.y
;
2135 if (node
== endPara
)
2144 /// Get the paragraph at the given position
2145 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
2150 // First find the first paragraph whose starting position is within the range.
2151 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2154 // child is a paragraph
2155 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2156 // wxASSERT (child != NULL);
2160 // Return first child in buffer if position is -1
2164 if (child
->GetRange().Contains(pos
))
2168 node
= node
->GetNext();
2173 /// Get the line at the given position
2174 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
2179 // First find the first paragraph whose starting position is within the range.
2180 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2183 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
2184 if (obj
->GetRange().Contains(pos
))
2186 // child is a paragraph
2187 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
2188 // wxASSERT (child != NULL);
2192 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2195 wxRichTextLine
* line
= node2
->GetData();
2197 wxRichTextRange range
= line
->GetAbsoluteRange();
2199 if (range
.Contains(pos
) ||
2201 // If the position is end-of-paragraph, then return the last line of
2202 // of the paragraph.
2203 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
2206 node2
= node2
->GetNext();
2211 node
= node
->GetNext();
2214 int lineCount
= GetLineCount();
2216 return GetLineForVisibleLineNumber(lineCount
-1);
2221 /// Get the line at the given y pixel position, or the last line.
2222 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
2224 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2227 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2228 // wxASSERT (child != NULL);
2232 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2235 wxRichTextLine
* line
= node2
->GetData();
2237 wxRect
rect(line
->GetRect());
2239 if (y
<= rect
.GetBottom())
2242 node2
= node2
->GetNext();
2246 node
= node
->GetNext();
2250 int lineCount
= GetLineCount();
2252 return GetLineForVisibleLineNumber(lineCount
-1);
2257 /// Get the number of visible lines
2258 int wxRichTextParagraphLayoutBox::GetLineCount() const
2262 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2265 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2266 // wxASSERT (child != NULL);
2269 count
+= child
->GetLines().GetCount();
2271 node
= node
->GetNext();
2277 /// Get the paragraph for a given line
2278 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
2280 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
2283 /// Get the line size at the given position
2284 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
2286 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
2289 return line
->GetSize();
2292 return wxSize(0, 0);
2296 /// Convenience function to add a paragraph of text
2297 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxRichTextAttr
* paraStyle
)
2299 // Don't use the base style, just the default style, and the base style will
2300 // be combined at display time.
2301 // Divide into paragraph and character styles.
2303 wxRichTextAttr defaultCharStyle
;
2304 wxRichTextAttr defaultParaStyle
;
2306 // If the default style is a named paragraph style, don't apply any character formatting
2307 // to the initial text string.
2308 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2310 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2312 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
2315 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
2317 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
2318 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
2320 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
2326 return para
->GetRange();
2329 /// Adds multiple paragraphs, based on newlines.
2330 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxRichTextAttr
* paraStyle
)
2332 // Don't use the base style, just the default style, and the base style will
2333 // be combined at display time.
2334 // Divide into paragraph and character styles.
2336 wxRichTextAttr defaultCharStyle
;
2337 wxRichTextAttr defaultParaStyle
;
2339 // If the default style is a named paragraph style, don't apply any character formatting
2340 // to the initial text string.
2341 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2343 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2345 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
2348 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
2350 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
2351 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
2353 wxRichTextParagraph
* firstPara
= NULL
;
2354 wxRichTextParagraph
* lastPara
= NULL
;
2356 wxRichTextRange
range(-1, -1);
2359 size_t len
= text
.length();
2361 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
2370 wxChar ch
= text
[i
];
2371 if (ch
== wxT('\n') || ch
== wxT('\r'))
2375 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
2376 plainText
->SetText(line
);
2378 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
2383 line
= wxEmptyString
;
2394 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
2395 plainText
->SetText(line
);
2400 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
2403 /// Convenience function to add an image
2404 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxRichTextAttr
* paraStyle
)
2406 // Don't use the base style, just the default style, and the base style will
2407 // be combined at display time.
2408 // Divide into paragraph and character styles.
2410 wxRichTextAttr defaultCharStyle
;
2411 wxRichTextAttr defaultParaStyle
;
2413 // If the default style is a named paragraph style, don't apply any character formatting
2414 // to the initial text string.
2415 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2417 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2419 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
2422 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
2424 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
2425 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
2427 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
2429 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
2433 return para
->GetRange();
2437 /// Insert fragment into this box at the given position. If partialParagraph is true,
2438 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
2441 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
2443 // First, find the first paragraph whose starting position is within the range.
2444 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
2447 wxRichTextAttr originalAttr
= para
->GetAttributes();
2449 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
2451 // Now split at this position, returning the object to insert the new
2452 // ones in front of.
2453 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
2455 // Special case: partial paragraph, just one paragraph. Might be a small amount of
2456 // text, for example, so let's optimize.
2458 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
2460 // Add the first para to this para...
2461 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
2465 // Iterate through the fragment paragraph inserting the content into this paragraph.
2466 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
2467 wxASSERT (firstPara
!= NULL
);
2469 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
2472 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
2477 para
->AppendChild(newObj
);
2481 // Insert before nextObject
2482 para
->InsertChild(newObj
, nextObject
);
2485 objectNode
= objectNode
->GetNext();
2492 // Procedure for inserting a fragment consisting of a number of
2495 // 1. Remove and save the content that's after the insertion point, for adding
2496 // back once we've added the fragment.
2497 // 2. Add the content from the first fragment paragraph to the current
2499 // 3. Add remaining fragment paragraphs after the current paragraph.
2500 // 4. Add back the saved content from the first paragraph. If partialParagraph
2501 // is true, add it to the last paragraph added and not a new one.
2503 // 1. Remove and save objects after split point.
2504 wxList savedObjects
;
2506 para
->MoveToList(nextObject
, savedObjects
);
2508 // 2. Add the content from the 1st fragment paragraph.
2509 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
2513 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
2514 wxASSERT(firstPara
!= NULL
);
2516 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
2517 para
->SetAttributes(firstPara
->GetAttributes());
2519 // Save empty paragraph attributes for appending later
2520 // These are character attributes deliberately set for a new paragraph. Without this,
2521 // we couldn't pass default attributes when appending a new paragraph.
2522 wxRichTextAttr emptyParagraphAttributes
;
2524 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
2526 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
2527 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
2531 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
2534 para
->AppendChild(newObj
);
2536 objectNode
= objectNode
->GetNext();
2539 // 3. Add remaining fragment paragraphs after the current paragraph.
2540 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
2541 wxRichTextObject
* nextParagraph
= NULL
;
2542 if (nextParagraphNode
)
2543 nextParagraph
= nextParagraphNode
->GetData();
2545 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
2546 wxRichTextParagraph
* finalPara
= para
;
2548 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
2550 // If there was only one paragraph, we need to insert a new one.
2553 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
2554 wxASSERT( para
!= NULL
);
2556 finalPara
= (wxRichTextParagraph
*) para
->Clone();
2559 InsertChild(finalPara
, nextParagraph
);
2561 AppendChild(finalPara
);
2566 // If there was only one paragraph, or we have full paragraphs in our fragment,
2567 // we need to insert a new one.
2570 finalPara
= new wxRichTextParagraph
;
2573 InsertChild(finalPara
, nextParagraph
);
2575 AppendChild(finalPara
);
2578 // 4. Add back the remaining content.
2582 finalPara
->MoveFromList(savedObjects
);
2584 // Ensure there's at least one object
2585 if (finalPara
->GetChildCount() == 0)
2587 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
2588 text
->SetAttributes(emptyParagraphAttributes
);
2590 finalPara
->AppendChild(text
);
2594 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
2595 finalPara
->SetAttributes(firstPara
->GetAttributes());
2596 else if (finalPara
&& finalPara
!= para
)
2597 finalPara
->SetAttributes(originalAttr
);
2605 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
2608 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
2609 wxASSERT( para
!= NULL
);
2611 AppendChild(para
->Clone());
2620 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
2621 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
2622 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
2624 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
2627 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
2628 wxASSERT( para
!= NULL
);
2630 if (!para
->GetRange().IsOutside(range
))
2632 fragment
.AppendChild(para
->Clone());
2637 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
2638 if (!fragment
.IsEmpty())
2640 wxRichTextRange
topTailRange(range
);
2642 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
2643 wxASSERT( firstPara
!= NULL
);
2645 // Chop off the start of the paragraph
2646 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
2648 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
2649 firstPara
->DeleteRange(r
);
2651 // Make sure the numbering is correct
2653 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
2655 // Now, we've deleted some positions, so adjust the range
2657 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
2660 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
2661 wxASSERT( lastPara
!= NULL
);
2663 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
2665 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
2666 lastPara
->DeleteRange(r
);
2668 // Make sure the numbering is correct
2670 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
2672 // We only have part of a paragraph at the end
2673 fragment
.SetPartialParagraph(true);
2677 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
2678 // We have a partial paragraph (don't save last new paragraph marker)
2679 fragment
.SetPartialParagraph(true);
2681 // We have a complete paragraph
2682 fragment
.SetPartialParagraph(false);
2689 /// Given a position, get the number of the visible line (potentially many to a paragraph),
2690 /// starting from zero at the start of the buffer.
2691 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
2698 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2701 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2702 // wxASSERT( child != NULL );
2706 if (child
->GetRange().Contains(pos
))
2708 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2711 wxRichTextLine
* line
= node2
->GetData();
2712 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2714 if (lineRange
.Contains(pos
) || pos
== lineRange
.GetStart())
2716 // If the caret is displayed at the end of the previous wrapped line,
2717 // we want to return the line it's _displayed_ at (not the actual line
2718 // containing the position).
2719 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
2720 return lineCount
- 1;
2727 node2
= node2
->GetNext();
2729 // If we didn't find it in the lines, it must be
2730 // the last position of the paragraph. So return the last line.
2734 lineCount
+= child
->GetLines().GetCount();
2737 node
= node
->GetNext();
2744 /// Given a line number, get the corresponding wxRichTextLine object.
2745 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
2749 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2752 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2753 // wxASSERT(child != NULL);
2757 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
2759 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2762 wxRichTextLine
* line
= node2
->GetData();
2764 if (lineCount
== lineNumber
)
2769 node2
= node2
->GetNext();
2773 lineCount
+= child
->GetLines().GetCount();
2776 node
= node
->GetNext();
2783 /// Delete range from layout.
2784 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
2786 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2788 wxRichTextParagraph
* firstPara
= NULL
;
2791 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2792 // wxASSERT (obj != NULL);
2794 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
2798 // Delete the range in each paragraph
2800 if (!obj
->GetRange().IsOutside(range
))
2802 // Deletes the content of this object within the given range
2803 obj
->DeleteRange(range
);
2805 wxRichTextRange thisRange
= obj
->GetRange();
2806 wxRichTextAttr thisAttr
= obj
->GetAttributes();
2808 // If the whole paragraph is within the range to delete,
2809 // delete the whole thing.
2810 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
2812 // Delete the whole object
2813 RemoveChild(obj
, true);
2816 else if (!firstPara
)
2819 // If the range includes the paragraph end, we need to join this
2820 // and the next paragraph.
2821 if (range
.GetEnd() <= thisRange
.GetEnd())
2823 // We need to move the objects from the next paragraph
2824 // to this paragraph
2826 wxRichTextParagraph
* nextParagraph
= NULL
;
2827 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
2828 nextParagraph
= obj
;
2831 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
2833 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
2836 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
2838 wxRichTextAttr nextParaAttr
;
2839 if (applyFinalParagraphStyle
)
2841 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
2842 // not the next one.
2843 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
2844 nextParaAttr
= thisAttr
;
2846 nextParaAttr
= nextParagraph
->GetAttributes();
2849 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
2851 // Move the objects to the previous para
2852 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
2856 wxRichTextObject
* obj1
= node1
->GetData();
2858 firstPara
->AppendChild(obj1
);
2860 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
2861 nextParagraph
->GetChildren().Erase(node1
);
2866 // Delete the paragraph
2867 RemoveChild(nextParagraph
, true);
2870 // Avoid empty paragraphs
2871 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
2873 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
2874 firstPara
->AppendChild(text
);
2877 if (applyFinalParagraphStyle
)
2878 firstPara
->SetAttributes(nextParaAttr
);
2891 /// Get any text in this object for the given range
2892 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
2896 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2899 wxRichTextObject
* child
= node
->GetData();
2900 if (!child
->GetRange().IsOutside(range
))
2902 wxRichTextRange childRange
= range
;
2903 childRange
.LimitTo(child
->GetRange());
2905 wxString childText
= child
->GetTextForRange(childRange
);
2909 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
2914 node
= node
->GetNext();
2920 /// Get all the text
2921 wxString
wxRichTextParagraphLayoutBox::GetText() const
2923 return GetTextForRange(GetOwnRange());
2926 /// Get the paragraph by number
2927 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
2929 if ((size_t) paragraphNumber
>= GetChildCount())
2932 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
2935 /// Get the length of the paragraph
2936 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
2938 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2940 return para
->GetRange().GetLength() - 1; // don't include newline
2945 /// Get the text of the paragraph
2946 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
2948 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2950 return para
->GetTextForRange(para
->GetRange());
2952 return wxEmptyString
;
2955 /// Convert zero-based line column and paragraph number to a position.
2956 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
2958 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
2961 return para
->GetRange().GetStart() + x
;
2967 /// Convert zero-based position to line column and paragraph number
2968 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
2970 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
2974 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2977 wxRichTextObject
* child
= node
->GetData();
2981 node
= node
->GetNext();
2985 *x
= pos
- para
->GetRange().GetStart();
2993 /// Get the leaf object in a paragraph at this position.
2994 /// Given a line number, get the corresponding wxRichTextLine object.
2995 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
2997 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
3000 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
3004 wxRichTextObject
* child
= node
->GetData();
3005 if (child
->GetRange().Contains(position
))
3008 node
= node
->GetNext();
3010 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
3011 return para
->GetChildren().GetLast()->GetData();
3016 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
3017 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3019 bool characterStyle
= false;
3020 bool paragraphStyle
= false;
3022 if (style
.IsCharacterStyle())
3023 characterStyle
= true;
3024 if (style
.IsParagraphStyle())
3025 paragraphStyle
= true;
3027 wxRichTextBuffer
* buffer
= GetBuffer();
3029 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
3030 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
3031 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
3032 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
3033 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
3034 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
3036 // Apply paragraph style first, if any
3037 wxRichTextAttr
wholeStyle(style
);
3039 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && buffer
->GetStyleSheet())
3041 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
3043 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(buffer
->GetStyleSheet()));
3046 // Limit the attributes to be set to the content to only character attributes.
3047 wxRichTextAttr
characterAttributes(wholeStyle
);
3048 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
3050 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && buffer
->GetStyleSheet())
3052 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
3054 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(buffer
->GetStyleSheet()));
3057 // If we are associated with a control, make undoable; otherwise, apply immediately
3060 bool haveControl
= (buffer
->GetRichTextCtrl() != NULL
);
3062 wxRichTextAction
* action
= NULL
;
3064 if (haveControl
&& withUndo
)
3066 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, buffer
, this, buffer
->GetRichTextCtrl());
3067 action
->SetRange(range
);
3068 action
->SetPosition(buffer
->GetRichTextCtrl()->GetCaretPosition());
3071 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3074 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3075 // wxASSERT (para != NULL);
3077 if (para
&& para
->GetChildCount() > 0)
3079 // Stop searching if we're beyond the range of interest
3080 if (para
->GetRange().GetStart() > range
.GetEnd())
3083 if (!para
->GetRange().IsOutside(range
))
3085 // We'll be using a copy of the paragraph to make style changes,
3086 // not updating the buffer directly.
3087 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
3089 if (haveControl
&& withUndo
)
3091 newPara
= new wxRichTextParagraph(*para
);
3092 action
->GetNewParagraphs().AppendChild(newPara
);
3094 // Also store the old ones for Undo
3095 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
3100 // If we're specifying paragraphs only, then we really mean character formatting
3101 // to be included in the paragraph style
3102 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
3106 // Removes the given style from the paragraph
3107 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
3109 else if (resetExistingStyle
)
3110 newPara
->GetAttributes() = wholeStyle
;
3115 // Only apply attributes that will make a difference to the combined
3116 // style as seen on the display
3117 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes(true));
3118 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
3121 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
3125 // When applying paragraph styles dynamically, don't change the text objects' attributes
3126 // since they will computed as needed. Only apply the character styling if it's _only_
3127 // character styling. This policy is subject to change and might be put under user control.
3129 // Hm. we might well be applying a mix of paragraph and character styles, in which
3130 // case we _do_ want to apply character styles regardless of what para styles are set.
3131 // But if we're applying a paragraph style, which has some character attributes, but
3132 // we only want the paragraphs to hold this character style, then we _don't_ want to
3133 // apply the character style. So we need to be able to choose.
3135 if (!parasOnly
&& (characterStyle
|charactersOnly
) && range
.GetStart() != newPara
->GetRange().GetEnd())
3137 wxRichTextRange
childRange(range
);
3138 childRange
.LimitTo(newPara
->GetRange());
3140 // Find the starting position and if necessary split it so
3141 // we can start applying a different style.
3142 // TODO: check that the style actually changes or is different
3143 // from style outside of range
3144 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
3145 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
3147 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
3148 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
3150 firstObject
= newPara
->SplitAt(range
.GetStart());
3152 // Increment by 1 because we're apply the style one _after_ the split point
3153 long splitPoint
= childRange
.GetEnd();
3154 if (splitPoint
!= newPara
->GetRange().GetEnd())
3158 if (splitPoint
== newPara
->GetRange().GetEnd())
3159 lastObject
= newPara
->GetChildren().GetLast()->GetData();
3161 // lastObject is set as a side-effect of splitting. It's
3162 // returned as the object before the new object.
3163 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
3165 wxASSERT(firstObject
!= NULL
);
3166 wxASSERT(lastObject
!= NULL
);
3168 if (!firstObject
|| !lastObject
)
3171 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
3172 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
3174 wxASSERT(firstNode
);
3177 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
3181 wxRichTextObject
* child
= node2
->GetData();
3185 // Removes the given style from the paragraph
3186 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
3188 else if (resetExistingStyle
)
3189 child
->GetAttributes() = characterAttributes
;
3194 // Only apply attributes that will make a difference to the combined
3195 // style as seen on the display
3196 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes(), true));
3197 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
3200 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
3203 if (node2
== lastNode
)
3206 node2
= node2
->GetNext();
3212 node
= node
->GetNext();
3215 // Do action, or delay it until end of batch.
3216 if (haveControl
&& withUndo
)
3217 buffer
->SubmitAction(action
);
3222 // Just change the attributes for this single object.
3223 void wxRichTextParagraphLayoutBox::SetStyle(wxRichTextObject
* obj
, const wxRichTextAttr
& textAttr
, int flags
)
3225 wxRichTextBuffer
* buffer
= GetBuffer();
3226 bool withUndo
= flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
;
3227 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
3228 bool haveControl
= (buffer
->GetRichTextCtrl() != NULL
);
3230 wxRichTextAction
*action
= NULL
;
3231 wxRichTextAttr newAttr
= obj
->GetAttributes();
3232 if (resetExistingStyle
)
3235 newAttr
.Apply(textAttr
);
3237 if (haveControl
&& withUndo
)
3239 action
= new wxRichTextAction(NULL
, _("Change Object Style"), wxRICHTEXT_CHANGE_ATTRIBUTES
, buffer
, obj
->GetContainer(), buffer
->GetRichTextCtrl());
3240 action
->SetRange(obj
->GetRange().FromInternal());
3241 action
->SetPosition(buffer
->GetRichTextCtrl()->GetCaretPosition());
3242 action
->MakeObject(obj
);
3244 action
->GetAttributes() = newAttr
;
3247 obj
->GetAttributes() = newAttr
;
3249 if (haveControl
&& withUndo
)
3250 buffer
->SubmitAction(action
);
3253 /// Get the text attributes for this position.
3254 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
3256 return DoGetStyle(position
, style
, true);
3259 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3261 return DoGetStyle(position
, style
, false);
3264 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
3265 /// context attributes.
3266 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxRichTextAttr
& style
, bool combineStyles
)
3268 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
3270 if (style
.IsParagraphStyle())
3272 obj
= GetParagraphAtPosition(position
);
3277 // Start with the base style
3278 style
= GetAttributes();
3280 // Apply the paragraph style
3281 wxRichTextApplyStyle(style
, obj
->GetAttributes());
3284 style
= obj
->GetAttributes();
3291 obj
= GetLeafObjectAtPosition(position
);
3296 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
3297 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
3300 style
= obj
->GetAttributes();
3308 static bool wxHasStyle(long flags
, long style
)
3310 return (flags
& style
) != 0;
3313 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
3315 bool wxRichTextParagraphLayoutBox::CollectStyle(wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
3317 currentStyle
.CollectCommonAttributes(style
, clashingAttr
, absentAttr
);
3322 /// Get the combined style for a range - if any attribute is different within the range,
3323 /// that attribute is not present within the flags.
3324 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
3326 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3328 style
= wxRichTextAttr();
3330 wxRichTextAttr clashingAttr
;
3331 wxRichTextAttr absentAttrPara
, absentAttrChar
;
3333 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
3336 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3337 if (para
&& !(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
3339 if (para
->GetChildren().GetCount() == 0)
3341 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes(true /* use box attributes */);
3343 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
3347 wxRichTextRange
paraRange(para
->GetRange());
3348 paraRange
.LimitTo(range
);
3350 // First collect paragraph attributes only
3351 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes();
3352 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
3353 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
3355 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
3359 wxRichTextObject
* child
= childNode
->GetData();
3360 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
3362 wxRichTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes(), true /* include box attributes */);
3364 // Now collect character attributes only
3365 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
3367 CollectStyle(style
, childStyle
, clashingAttr
, absentAttrChar
);
3370 childNode
= childNode
->GetNext();
3374 node
= node
->GetNext();
3379 /// Set default style
3380 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxRichTextAttr
& style
)
3382 m_defaultAttributes
= style
;
3386 /// Test if this whole range has character attributes of the specified kind. If any
3387 /// of the attributes are different within the range, the test fails. You
3388 /// can use this to implement, for example, bold button updating. style must have
3389 /// flags indicating which attributes are of interest.
3390 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
3393 int matchingCount
= 0;
3395 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3398 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3399 // wxASSERT (para != NULL);
3403 // Stop searching if we're beyond the range of interest
3404 if (para
->GetRange().GetStart() > range
.GetEnd())
3405 return foundCount
== matchingCount
&& foundCount
!= 0;
3407 if (!para
->GetRange().IsOutside(range
))
3409 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
3413 wxRichTextObject
* child
= node2
->GetData();
3414 // Allow for empty string if no buffer
3415 wxRichTextRange childRange
= child
->GetRange();
3416 if (childRange
.GetLength() == 0 && GetRange().GetLength() == 1)
3417 childRange
.SetEnd(childRange
.GetEnd()+1);
3419 if (!childRange
.IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
3422 wxRichTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
3424 if (wxTextAttrEqPartial(textAttr
, style
))
3428 node2
= node2
->GetNext();
3433 node
= node
->GetNext();
3436 return foundCount
== matchingCount
&& foundCount
!= 0;
3439 /// Test if this whole range has paragraph attributes of the specified kind. If any
3440 /// of the attributes are different within the range, the test fails. You
3441 /// can use this to implement, for example, centering button updating. style must have
3442 /// flags indicating which attributes are of interest.
3443 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
3446 int matchingCount
= 0;
3448 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3451 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3452 // wxASSERT (para != NULL);
3456 // Stop searching if we're beyond the range of interest
3457 if (para
->GetRange().GetStart() > range
.GetEnd())
3458 return foundCount
== matchingCount
&& foundCount
!= 0;
3460 if (!para
->GetRange().IsOutside(range
))
3462 wxRichTextAttr textAttr
= GetAttributes();
3463 // Apply the paragraph style
3464 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
3467 if (wxTextAttrEqPartial(textAttr
, style
))
3472 node
= node
->GetNext();
3474 return foundCount
== matchingCount
&& foundCount
!= 0;
3477 void wxRichTextParagraphLayoutBox::Reset()
3481 wxRichTextBuffer
* buffer
= GetBuffer();
3482 if (buffer
&& buffer
->GetRichTextCtrl())
3484 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, buffer
->GetRichTextCtrl()->GetId());
3485 event
.SetEventObject(buffer
->GetRichTextCtrl());
3486 event
.SetContainer(this);
3488 buffer
->SendEvent(event
, true);
3491 AddParagraph(wxEmptyString
);
3493 InvalidateHierarchy(wxRICHTEXT_ALL
);
3496 /// Invalidate the buffer. With no argument, invalidates whole buffer.
3497 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
3499 wxRichTextCompositeObject::Invalidate(invalidRange
);
3501 DoInvalidate(invalidRange
);
3504 // Do the (in)validation for this object only
3505 void wxRichTextParagraphLayoutBox::DoInvalidate(const wxRichTextRange
& invalidRange
)
3507 if (invalidRange
== wxRICHTEXT_ALL
)
3509 m_invalidRange
= wxRICHTEXT_ALL
;
3511 // Already invalidating everything
3512 else if (m_invalidRange
== wxRICHTEXT_ALL
)
3517 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
3518 m_invalidRange
.SetStart(invalidRange
.GetStart());
3519 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
3520 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
3524 // Do the (in)validation both up and down the hierarchy
3525 void wxRichTextParagraphLayoutBox::InvalidateHierarchy(const wxRichTextRange
& invalidRange
)
3527 Invalidate(invalidRange
);
3529 if (invalidRange
!= wxRICHTEXT_NONE
)
3531 // Now go up the hierarchy
3532 wxRichTextObject
* thisObj
= this;
3533 wxRichTextObject
* p
= GetParent();
3536 wxRichTextParagraphLayoutBox
* l
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
3538 l
->DoInvalidate(thisObj
->GetRange());
3546 /// Get invalid range, rounding to entire paragraphs if argument is true.
3547 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
3549 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
3550 return m_invalidRange
;
3552 wxRichTextRange range
= m_invalidRange
;
3554 if (wholeParagraphs
)
3556 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
3558 range
.SetStart(para1
->GetRange().GetStart());
3559 // floating layout make all child should be relayout
3560 range
.SetEnd(GetOwnRange().GetEnd());
3565 /// Apply the style sheet to the buffer, for example if the styles have changed.
3566 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3568 wxASSERT(styleSheet
!= NULL
);
3574 wxRichTextAttr
attr(GetBasicStyle());
3575 if (GetBasicStyle().HasParagraphStyleName())
3577 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
3580 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
3581 SetBasicStyle(attr
);
3586 if (GetBasicStyle().HasCharacterStyleName())
3588 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
3591 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
3592 SetBasicStyle(attr
);
3597 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3600 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3601 // wxASSERT (para != NULL);
3605 // Combine paragraph and list styles. If there is a list style in the original attributes,
3606 // the current indentation overrides anything else and is used to find the item indentation.
3607 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
3608 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
3609 // exception as above).
3610 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
3611 // So when changing a list style interactively, could retrieve level based on current style, then
3612 // set appropriate indent and apply new style.
3616 if (para
->GetAttributes().HasOutlineLevel())
3617 outline
= para
->GetAttributes().GetOutlineLevel();
3618 if (para
->GetAttributes().HasBulletNumber())
3619 num
= para
->GetAttributes().GetBulletNumber();
3621 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
3623 int currentIndent
= para
->GetAttributes().GetLeftIndent();
3625 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
3626 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
3627 if (paraDef
&& !listDef
)
3629 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
3632 else if (listDef
&& !paraDef
)
3634 // Set overall style defined for the list style definition
3635 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
3637 // Apply the style for this level
3638 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
3641 else if (listDef
&& paraDef
)
3643 // Combines overall list style, style for level, and paragraph style
3644 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
3648 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
3650 int currentIndent
= para
->GetAttributes().GetLeftIndent();
3652 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
3654 // Overall list definition style
3655 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
3657 // Style for this level
3658 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
3662 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
3664 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
3667 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
3673 para
->GetAttributes().SetOutlineLevel(outline
);
3675 para
->GetAttributes().SetBulletNumber(num
);
3678 node
= node
->GetNext();
3680 return foundCount
!= 0;
3684 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3686 wxRichTextBuffer
* buffer
= GetBuffer();
3687 wxRichTextStyleSheet
* styleSheet
= buffer
->GetStyleSheet();
3689 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
3690 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
3691 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
3692 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
3694 // Current number, if numbering
3697 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
3699 // If we are associated with a control, make undoable; otherwise, apply immediately
3702 bool haveControl
= (buffer
->GetRichTextCtrl() != NULL
);
3704 wxRichTextAction
* action
= NULL
;
3706 if (haveControl
&& withUndo
)
3708 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, buffer
, this, buffer
->GetRichTextCtrl());
3709 action
->SetRange(range
);
3710 action
->SetPosition(buffer
->GetRichTextCtrl()->GetCaretPosition());
3713 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3716 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3717 // wxASSERT (para != NULL);
3719 if (para
&& para
->GetChildCount() > 0)
3721 // Stop searching if we're beyond the range of interest
3722 if (para
->GetRange().GetStart() > range
.GetEnd())
3725 if (!para
->GetRange().IsOutside(range
))
3727 // We'll be using a copy of the paragraph to make style changes,
3728 // not updating the buffer directly.
3729 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
3731 if (haveControl
&& withUndo
)
3733 newPara
= new wxRichTextParagraph(*para
);
3734 action
->GetNewParagraphs().AppendChild(newPara
);
3736 // Also store the old ones for Undo
3737 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
3744 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
3745 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
3747 // How is numbering going to work?
3748 // If we are renumbering, or numbering for the first time, we need to keep
3749 // track of the number for each level. But we might be simply applying a different
3751 // In Word, applying a style to several paragraphs, even if at different levels,
3752 // reverts the level back to the same one. So we could do the same here.
3753 // Renumbering will need to be done when we promote/demote a paragraph.
3755 // Apply the overall list style, and item style for this level
3756 wxRichTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
3757 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
3759 // Now we need to do numbering
3762 newPara
->GetAttributes().SetBulletNumber(n
);
3767 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
3769 // if def is NULL, remove list style, applying any associated paragraph style
3770 // to restore the attributes
3772 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
3773 newPara
->GetAttributes().SetLeftIndent(0, 0);
3774 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
3776 // Eliminate the main list-related attributes
3777 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
);
3779 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
3781 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
3784 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
3791 node
= node
->GetNext();
3794 // Do action, or delay it until end of batch.
3795 if (haveControl
&& withUndo
)
3796 buffer
->SubmitAction(action
);
3801 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3803 wxRichTextBuffer
* buffer
= GetBuffer();
3804 if (buffer
&& buffer
->GetStyleSheet())
3806 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->FindListStyle(defName
);
3808 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
3813 /// Clear list for given range
3814 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3816 return SetListStyle(range
, NULL
, flags
);
3819 /// Number/renumber any list elements in the given range
3820 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3822 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
3825 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
3826 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
3827 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3829 wxRichTextBuffer
* buffer
= GetBuffer();
3830 wxRichTextStyleSheet
* styleSheet
= buffer
->GetStyleSheet();
3832 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
3833 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
3835 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
3838 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
3840 // Max number of levels
3841 const int maxLevels
= 10;
3843 // The level we're looking at now
3844 int currentLevel
= -1;
3846 // The item number for each level
3847 int levels
[maxLevels
];
3850 // Reset all numbering
3851 for (i
= 0; i
< maxLevels
; i
++)
3853 if (startFrom
!= -1)
3854 levels
[i
] = startFrom
-1;
3855 else if (renumber
) // start again
3858 levels
[i
] = -1; // start from the number we found, if any
3861 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
3863 // If we are associated with a control, make undoable; otherwise, apply immediately
3866 bool haveControl
= (buffer
->GetRichTextCtrl() != NULL
);
3868 wxRichTextAction
* action
= NULL
;
3870 if (haveControl
&& withUndo
)
3872 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, buffer
, this, buffer
->GetRichTextCtrl());
3873 action
->SetRange(range
);
3874 action
->SetPosition(buffer
->GetRichTextCtrl()->GetCaretPosition());
3877 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3880 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3881 // wxASSERT (para != NULL);
3883 if (para
&& para
->GetChildCount() > 0)
3885 // Stop searching if we're beyond the range of interest
3886 if (para
->GetRange().GetStart() > range
.GetEnd())
3889 if (!para
->GetRange().IsOutside(range
))
3891 // We'll be using a copy of the paragraph to make style changes,
3892 // not updating the buffer directly.
3893 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
3895 if (haveControl
&& withUndo
)
3897 newPara
= new wxRichTextParagraph(*para
);
3898 action
->GetNewParagraphs().AppendChild(newPara
);
3900 // Also store the old ones for Undo
3901 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
3906 wxRichTextListStyleDefinition
* defToUse
= def
;
3909 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
3910 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
3915 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
3916 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
3918 // If we've specified a level to apply to all, change the level.
3919 if (specifiedLevel
!= -1)
3920 thisLevel
= specifiedLevel
;
3922 // Do promotion if specified
3923 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
3925 thisLevel
= thisLevel
- promoteBy
;
3932 // Apply the overall list style, and item style for this level
3933 wxRichTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
3934 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
3936 // OK, we've (re)applied the style, now let's get the numbering right.
3938 if (currentLevel
== -1)
3939 currentLevel
= thisLevel
;
3941 // Same level as before, do nothing except increment level's number afterwards
3942 if (currentLevel
== thisLevel
)
3945 // A deeper level: start renumbering all levels after current level
3946 else if (thisLevel
> currentLevel
)
3948 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
3952 currentLevel
= thisLevel
;
3954 else if (thisLevel
< currentLevel
)
3956 currentLevel
= thisLevel
;
3959 // Use the current numbering if -1 and we have a bullet number already
3960 if (levels
[currentLevel
] == -1)
3962 if (newPara
->GetAttributes().HasBulletNumber())
3963 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
3965 levels
[currentLevel
] = 1;
3969 levels
[currentLevel
] ++;
3972 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
3974 // Create the bullet text if an outline list
3975 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3978 for (i
= 0; i
<= currentLevel
; i
++)
3980 if (!text
.IsEmpty())
3982 text
+= wxString::Format(wxT("%d"), levels
[i
]);
3984 newPara
->GetAttributes().SetBulletText(text
);
3990 node
= node
->GetNext();
3993 // Do action, or delay it until end of batch.
3994 if (haveControl
&& withUndo
)
3995 buffer
->SubmitAction(action
);
4000 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4002 wxRichTextBuffer
* buffer
= GetBuffer();
4003 if (buffer
->GetStyleSheet())
4005 wxRichTextListStyleDefinition
* def
= NULL
;
4006 if (!defName
.IsEmpty())
4007 def
= buffer
->GetStyleSheet()->FindListStyle(defName
);
4008 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
4013 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4014 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4017 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
4018 // to NumberList with a flag indicating promotion is required within one of the ranges.
4019 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
4020 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
4021 // We start renumbering from the para after that different para we found. We specify that the numbering of that
4022 // list position will start from 1.
4023 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
4024 // We can end the renumbering at this point.
4026 // For now, only renumber within the promotion range.
4028 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
4031 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4033 wxRichTextBuffer
* buffer
= GetBuffer();
4034 if (buffer
->GetStyleSheet())
4036 wxRichTextListStyleDefinition
* def
= NULL
;
4037 if (!defName
.IsEmpty())
4038 def
= buffer
->GetStyleSheet()->FindListStyle(defName
);
4039 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
4044 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
4045 /// position of the paragraph that it had to start looking from.
4046 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
4048 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
4051 wxRichTextBuffer
* buffer
= GetBuffer();
4052 wxRichTextStyleSheet
* styleSheet
= buffer
->GetStyleSheet();
4053 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
4055 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
4058 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
4059 // int thisLevel = def->FindLevelForIndent(thisIndent);
4061 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
4063 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
4064 if (previousParagraph
->GetAttributes().HasBulletName())
4065 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
4066 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
4067 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
4069 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
4070 attr
.SetBulletNumber(nextNumber
);
4074 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
4075 if (!text
.IsEmpty())
4077 int pos
= text
.Find(wxT('.'), true);
4078 if (pos
!= wxNOT_FOUND
)
4080 text
= text
.Mid(0, text
.Length() - pos
- 1);
4083 text
= wxEmptyString
;
4084 if (!text
.IsEmpty())
4086 text
+= wxString::Format(wxT("%d"), nextNumber
);
4087 attr
.SetBulletText(text
);
4101 * wxRichTextParagraph
4102 * This object represents a single paragraph (or in a straight text editor, a line).
4105 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextCompositeObject
)
4107 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
4109 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxRichTextAttr
* style
):
4110 wxRichTextCompositeObject(parent
)
4113 SetAttributes(*style
);
4116 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* paraStyle
, wxRichTextAttr
* charStyle
):
4117 wxRichTextCompositeObject(parent
)
4120 SetAttributes(*paraStyle
);
4122 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
4125 wxRichTextParagraph::~wxRichTextParagraph()
4131 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
4136 // Currently we don't merge these attributes with the parent, but we
4137 // should consider whether we should (e.g. if we set a border colour
4138 // for all paragraphs). But generally box attributes are likely to be
4139 // different for different objects.
4140 wxRect paraRect
= GetRect();
4141 DrawBoxAttributes(dc
, GetBuffer(), GetAttributes(), paraRect
);
4143 wxRichTextAttr attr
= GetCombinedAttributes();
4145 // Draw the bullet, if any
4146 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
4148 if (attr
.GetLeftSubIndent() != 0)
4150 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
4151 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
4153 wxRichTextAttr
bulletAttr(GetCombinedAttributes());
4155 // Combine with the font of the first piece of content, if one is specified
4156 if (GetChildren().GetCount() > 0)
4158 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
4159 if (!firstObj
->IsFloatable() && firstObj
->GetAttributes().HasFont())
4161 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
4165 // Get line height from first line, if any
4166 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : NULL
;
4169 int lineHeight
wxDUMMY_INITIALIZE(0);
4172 lineHeight
= line
->GetSize().y
;
4173 linePos
= line
->GetPosition() + GetPosition();
4178 if (bulletAttr
.HasFont() && GetBuffer())
4179 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
4181 font
= (*wxNORMAL_FONT
);
4183 wxCheckSetFont(dc
, font
);
4185 lineHeight
= dc
.GetCharHeight();
4186 linePos
= GetPosition();
4187 linePos
.y
+= spaceBeforePara
;
4190 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
4192 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
4194 if (wxRichTextBuffer::GetRenderer())
4195 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
4197 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
4199 if (wxRichTextBuffer::GetRenderer())
4200 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
4204 wxString bulletText
= GetBulletText();
4206 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
4207 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
4212 // Draw the range for each line, one object at a time.
4214 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
4217 wxRichTextLine
* line
= node
->GetData();
4218 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4220 // Lines are specified relative to the paragraph
4222 wxPoint linePosition
= line
->GetPosition() + GetPosition();
4224 // Don't draw if off the screen
4225 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
4227 wxPoint objectPosition
= linePosition
;
4228 int maxDescent
= line
->GetDescent();
4230 // Loop through objects until we get to the one within range
4231 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
4236 wxRichTextObject
* child
= node2
->GetData();
4238 if (!child
->IsFloating() && child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
4240 // Draw this part of the line at the correct position
4241 wxRichTextRange
objectRange(child
->GetRange());
4242 objectRange
.LimitTo(lineRange
);
4245 if (child
->IsTopLevel())
4247 objectSize
= child
->GetCachedSize();
4248 objectRange
= child
->GetOwnRange();
4252 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4253 if (i
< (int) line
->GetObjectSizes().GetCount())
4255 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
4261 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
4265 // Use the child object's width, but the whole line's height
4266 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
4267 child
->Draw(dc
, objectRange
, selection
, childRect
, maxDescent
, style
);
4269 objectPosition
.x
+= objectSize
.x
;
4272 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
4273 // Can break out of inner loop now since we've passed this line's range
4276 node2
= node2
->GetNext();
4280 node
= node
->GetNext();
4286 // Get the range width using partial extents calculated for the whole paragraph.
4287 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
4289 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
4291 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
4294 int leftMostPos
= 0;
4295 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
4296 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
4298 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
4300 int w
= rightMostPos
- leftMostPos
;
4305 /// Lay the item out
4306 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
4308 // Deal with floating objects firstly before the normal layout
4309 wxRichTextBuffer
* buffer
= GetBuffer();
4311 wxRichTextFloatCollector
* collector
= GetContainer()->GetFloatCollector();
4312 wxASSERT(collector
);
4313 LayoutFloat(dc
, rect
, style
, collector
);
4315 wxRichTextAttr attr
= GetCombinedAttributes();
4319 // Increase the size of the paragraph due to spacing
4320 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
4321 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
4322 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
4323 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
4324 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
4326 int lineSpacing
= 0;
4328 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
4329 if (attr
.HasLineSpacing() && attr
.GetLineSpacing() > 0 && attr
.GetFont().Ok())
4331 wxCheckSetFont(dc
, attr
.GetFont());
4332 lineSpacing
= (int) (double(dc
.GetCharHeight()) * (double(attr
.GetLineSpacing())/10.0 - 1.0));
4335 // Start position for each line relative to the paragraph
4336 int startPositionFirstLine
= leftIndent
;
4337 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
4339 // If we have a bullet in this paragraph, the start position for the first line's text
4340 // is actually leftIndent + leftSubIndent.
4341 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
4342 startPositionFirstLine
= startPositionSubsequentLines
;
4344 long lastEndPos
= GetRange().GetStart()-1;
4345 long lastCompletedEndPos
= lastEndPos
;
4347 int currentWidth
= 0;
4348 SetPosition(rect
.GetPosition());
4350 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
4353 int maxHeight
= currentPosition
.y
;
4358 int lineDescent
= 0;
4360 wxRichTextObjectList::compatibility_iterator node
;
4362 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4364 wxArrayInt partialExtents
;
4367 int paraDescent
= 0;
4369 // This calculates the partial text extents
4370 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, rect
.GetPosition(), & partialExtents
);
4372 node
= m_children
.GetFirst();
4375 wxRichTextObject
* child
= node
->GetData();
4377 //child->SetCachedSize(wxDefaultSize);
4378 child
->Layout(dc
, rect
, style
);
4380 node
= node
->GetNext();
4387 // We may need to go back to a previous child, in which case create the new line,
4388 // find the child corresponding to the start position of the string, and
4391 wxRect availableRect
;
4393 node
= m_children
.GetFirst();
4396 wxRichTextObject
* child
= node
->GetData();
4398 // If floating, ignore. We already laid out floats.
4399 // Also ignore if empty object, except if we haven't got any
4401 if (child
->IsFloating() || !child
->IsShown() ||
4402 (child
->GetRange().GetLength() == 0 && maxHeight
> spaceBeforePara
)
4405 node
= node
->GetNext();
4409 // If this is e.g. a composite text box, it will need to be laid out itself.
4410 // But if just a text fragment or image, for example, this will
4411 // do nothing. NB: won't we need to set the position after layout?
4412 // since for example if position is dependent on vertical line size, we
4413 // can't tell the position until the size is determined. So possibly introduce
4414 // another layout phase.
4416 // We may only be looking at part of a child, if we searched back for wrapping
4417 // and found a suitable point some way into the child. So get the size for the fragment
4420 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
4421 long lastPosToUse
= child
->GetRange().GetEnd();
4422 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
4424 if (lineBreakInThisObject
)
4425 lastPosToUse
= nextBreakPos
;
4428 int childDescent
= 0;
4430 int startOffset
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
4431 availableRect
= wxRect(rect
.x
+ startOffset
, rect
.y
+ currentPosition
.y
,
4432 rect
.width
- startOffset
- rightIndent
, rect
.height
);
4434 if (child
->IsTopLevel())
4436 wxSize oldSize
= child
->GetCachedSize();
4438 child
->Invalidate(wxRICHTEXT_ALL
);
4439 child
->SetPosition(wxPoint(0, 0));
4441 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
4442 // lays out the object again using the minimum size
4443 // The position will be determined by its location in its line,
4444 // and not by the child's actual position.
4445 child
->LayoutToBestSize(dc
, buffer
,
4446 GetAttributes(), child
->GetAttributes(), availableRect
, style
);
4448 if (oldSize
!= child
->GetCachedSize())
4450 partialExtents
.Clear();
4452 // Recalculate the partial text extents since the child object changed size
4453 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
4457 // Problem: we need to layout composites here for which we need the available width,
4458 // but we can't get the available width without using the float collector which
4459 // needs to know the object height.
4461 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
4463 childSize
= child
->GetCachedSize();
4464 childDescent
= child
->GetDescent();
4468 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4469 // Get height only, then the width using the partial extents
4470 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
4471 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
4473 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
4478 int loopIterations
= 0;
4480 // If there are nested objects that need to lay themselves out, we have to do this in a
4481 // loop because the height of the object may well depend on the available width.
4482 // And because of floating object positioning, the available width depends on the
4483 // height of the object and whether it will clash with the floating objects.
4484 // So, we see whether the available width changes due to the presence of floating images.
4485 // If it does, then we'll use the new restricted width to find the object height again.
4486 // If this causes another restriction in the available width, we'll try again, until
4487 // either we lose patience or the available width settles down.
4492 wxRect oldAvailableRect
= availableRect
;
4494 // Available width depends on the floating objects and the line height.
4495 // Note: the floating objects may be placed vertically along the two side of
4496 // buffer, so we may have different available line widths with different
4497 // [startY, endY]. So, we can't determine how wide the available
4498 // space is until we know the exact line height.
4499 lineDescent
= wxMax(childDescent
, maxDescent
);
4500 lineAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
4501 lineHeight
= lineDescent
+ lineAscent
;
4502 wxRect floatAvailableRect
= collector
->GetAvailableRect(rect
.y
+ currentPosition
.y
, rect
.y
+ currentPosition
.y
+ lineHeight
);
4504 // Adjust availableRect to the space that is available when taking floating objects into account.
4506 if (floatAvailableRect
.x
+ startOffset
> availableRect
.x
)
4508 int newX
= floatAvailableRect
.x
+ startOffset
;
4509 int newW
= availableRect
.width
- (newX
- availableRect
.x
);
4510 availableRect
.x
= newX
;
4511 availableRect
.width
= newW
;
4514 if (floatAvailableRect
.width
< availableRect
.width
)
4515 availableRect
.width
= floatAvailableRect
.width
;
4517 currentPosition
.x
= availableRect
.x
- rect
.x
;
4519 if (child
->IsTopLevel() && loopIterations
<= 20)
4521 if (availableRect
!= oldAvailableRect
)
4523 wxSize oldSize
= child
->GetCachedSize();
4525 //child->SetCachedSize(wxDefaultSize);
4526 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
4527 // lays out the object again using the minimum size
4528 child
->Invalidate(wxRICHTEXT_ALL
);
4529 child
->LayoutToBestSize(dc
, buffer
,
4530 GetAttributes(), child
->GetAttributes(), availableRect
, style
);
4531 childSize
= child
->GetCachedSize();
4532 childDescent
= child
->GetDescent();
4533 //child->SetPosition(availableRect.GetPosition());
4535 if (oldSize
!= child
->GetCachedSize())
4537 partialExtents
.Clear();
4539 // Recalculate the partial text extents since the child object changed size
4540 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
4543 // Go around the loop finding the available rect for the given floating objects
4554 // 1) There was a line break BEFORE the natural break
4555 // 2) There was a line break AFTER the natural break
4556 // 3) It's the last line
4557 // 4) The child still fits (carry on) - 'else' clause
4559 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableRect
.width
))
4561 (childSize
.x
+ currentWidth
> availableRect
.width
)
4563 ((childSize
.x
+ currentWidth
<= availableRect
.width
) && !node
->GetNext())
4567 if (child
->IsTopLevel())
4569 // We can move it to the correct position at this point
4570 child
->Move(GetPosition() + wxPoint(currentWidth
, currentPosition
.y
));
4573 long wrapPosition
= 0;
4574 if ((childSize
.x
+ currentWidth
<= availableRect
.width
) && !node
->GetNext() && !lineBreakInThisObject
)
4575 wrapPosition
= child
->GetRange().GetEnd();
4578 // Find a place to wrap. This may walk back to previous children,
4579 // for example if a word spans several objects.
4580 // Note: one object must contains only one wxTextAtrr, so the line height will not
4581 // change inside one object. Thus, we can pass the remain line width to the
4582 // FindWrapPosition function.
4583 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableRect
.width
, wrapPosition
, & partialExtents
))
4585 // If the function failed, just cut it off at the end of this child.
4586 wrapPosition
= child
->GetRange().GetEnd();
4589 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
4590 if (wrapPosition
<= lastCompletedEndPos
)
4591 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
4593 // Line end position shouldn't be the same as the end, or greater.
4594 if (wrapPosition
>= GetRange().GetEnd())
4595 wrapPosition
= GetRange().GetEnd()-1;
4597 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
4599 // Let's find the actual size of the current line now
4601 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
4603 /// Use previous descent, not the wrapping descent we just found, since this may be too big
4604 /// for the fragment we're about to add.
4605 childDescent
= maxDescent
;
4607 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4608 if (!child
->IsEmpty())
4610 // Get height only, then the width using the partial extents
4611 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
4612 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
4616 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
4618 currentWidth
= actualSize
.x
;
4619 maxDescent
= wxMax(childDescent
, maxDescent
);
4620 maxAscent
= wxMax(actualSize
.y
-childDescent
, maxAscent
);
4621 lineHeight
= maxDescent
+ maxAscent
;
4623 if (lineHeight
== 0 && buffer
)
4625 wxFont
font(buffer
->GetFontTable().FindFont(attr
));
4626 wxCheckSetFont(dc
, font
);
4627 lineHeight
= dc
.GetCharHeight();
4630 if (maxDescent
== 0)
4633 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
4637 wxRichTextLine
* line
= AllocateLine(lineCount
);
4639 // Set relative range so we won't have to change line ranges when paragraphs are moved
4640 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
4641 line
->SetPosition(currentPosition
);
4642 line
->SetSize(wxSize(currentWidth
, lineHeight
));
4643 line
->SetDescent(maxDescent
);
4645 maxHeight
= currentPosition
.y
+ lineHeight
;
4647 // Now move down a line. TODO: add margins, spacing
4648 currentPosition
.y
+= lineHeight
;
4649 currentPosition
.y
+= lineSpacing
;
4652 maxWidth
= wxMax(maxWidth
, currentWidth
+startOffset
);
4657 // TODO: account for zero-length objects, such as fields
4658 // wxASSERT(wrapPosition > lastCompletedEndPos);
4660 lastEndPos
= wrapPosition
;
4661 lastCompletedEndPos
= lastEndPos
;
4665 if (wrapPosition
< GetRange().GetEnd()-1)
4667 // May need to set the node back to a previous one, due to searching back in wrapping
4668 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
4669 if (childAfterWrapPosition
)
4670 node
= m_children
.Find(childAfterWrapPosition
);
4672 node
= node
->GetNext();
4675 node
= node
->GetNext();
4677 // Apply paragraph styles such as alignment to the wrapped line
4678 ApplyParagraphStyle(line
, attr
, availableRect
, dc
);
4682 // We still fit, so don't add a line, and keep going
4683 currentWidth
+= childSize
.x
;
4684 maxDescent
= wxMax(childDescent
, maxDescent
);
4685 maxAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
4686 lineHeight
= maxDescent
+ maxAscent
;
4688 maxWidth
= wxMax(maxWidth
, currentWidth
+startOffset
);
4689 lastEndPos
= child
->GetRange().GetEnd();
4691 node
= node
->GetNext();
4695 //wxASSERT(!(lastCompletedEndPos != -1 && lastCompletedEndPos < GetRange().GetEnd()-1));
4697 // Remove remaining unused line objects, if any
4698 ClearUnusedLines(lineCount
);
4700 // We need to add back the margins etc.
4702 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
4703 contentRect
= wxRect(wxPoint(0, 0), wxSize(maxWidth
, currentPosition
.y
+ spaceAfterPara
));
4704 GetBoxRects(dc
, buffer
, GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
4705 SetCachedSize(marginRect
.GetSize());
4708 // The maximum size is the length of the paragraph stretched out into a line.
4709 // So if there were a single word, or an image, or a fixed-size text box, the object could be shrunk around
4710 // this size. TODO: take into account line breaks.
4712 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
4713 contentRect
= wxRect(wxPoint(0, 0), wxSize(paraSize
.x
, currentPosition
.y
+ spaceAfterPara
));
4714 GetBoxRects(dc
, buffer
, GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
4715 SetMaxSize(marginRect
.GetSize());
4718 // Find the greatest minimum size. Currently we only look at non-text objects,
4719 // which isn't ideal but it would be slow to find the maximum word width to
4720 // use as the minimum.
4723 node
= m_children
.GetFirst();
4726 wxRichTextObject
* child
= node
->GetData();
4728 // If floating, ignore. We already laid out floats.
4729 // Also ignore if empty object, except if we haven't got any
4731 if (!child
->IsFloating() && child
->GetRange().GetLength() != 0 && !child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
4733 if (child
->GetCachedSize().x
> minWidth
)
4734 minWidth
= child
->GetMinSize().x
;
4736 node
= node
->GetNext();
4739 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
4740 contentRect
= wxRect(wxPoint(0, 0), wxSize(minWidth
, currentPosition
.y
+ spaceAfterPara
));
4741 GetBoxRects(dc
, buffer
, GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
4742 SetMinSize(marginRect
.GetSize());
4746 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4747 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4748 // Use the text extents to calculate the size of each fragment in each line
4749 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
4752 wxRichTextLine
* line
= lineNode
->GetData();
4753 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4755 // Loop through objects until we get to the one within range
4756 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
4760 wxRichTextObject
* child
= node2
->GetData();
4762 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
4764 wxRichTextRange rangeToUse
= lineRange
;
4765 rangeToUse
.LimitTo(child
->GetRange());
4767 // Find the size of the child from the text extents, and store in an array
4768 // for drawing later
4770 if (rangeToUse
.GetStart() > GetRange().GetStart())
4771 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
4772 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
4773 int sz
= right
- left
;
4774 line
->GetObjectSizes().Add(sz
);
4776 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
4777 // Can break out of inner loop now since we've passed this line's range
4780 node2
= node2
->GetNext();
4783 lineNode
= lineNode
->GetNext();
4791 /// Apply paragraph styles, such as centering, to wrapped lines
4792 /// TODO: take into account box attributes, possibly
4793 void wxRichTextParagraph::ApplyParagraphStyle(wxRichTextLine
* line
, const wxRichTextAttr
& attr
, const wxRect
& rect
, wxDC
& dc
)
4795 if (!attr
.HasAlignment())
4798 wxPoint pos
= line
->GetPosition();
4799 wxSize size
= line
->GetSize();
4801 // centering, right-justification
4802 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
4804 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
4805 pos
.x
= (rect
.GetWidth() - rightIndent
- size
.x
)/2 + pos
.x
;
4806 line
->SetPosition(pos
);
4808 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
4810 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
4811 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
- rightIndent
;
4812 line
->SetPosition(pos
);
4816 /// Insert text at the given position
4817 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
4819 wxRichTextObject
* childToUse
= NULL
;
4820 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
4822 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4825 wxRichTextObject
* child
= node
->GetData();
4826 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
4833 node
= node
->GetNext();
4838 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
4841 int posInString
= pos
- textObject
->GetRange().GetStart();
4843 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
4844 text
+ textObject
->GetText().Mid(posInString
);
4845 textObject
->SetText(newText
);
4847 int textLength
= text
.length();
4849 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
4850 textObject
->GetRange().GetEnd() + textLength
));
4852 // Increment the end range of subsequent fragments in this paragraph.
4853 // We'll set the paragraph range itself at a higher level.
4855 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
4858 wxRichTextObject
* child
= node
->GetData();
4859 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
4860 textObject
->GetRange().GetEnd() + textLength
));
4862 node
= node
->GetNext();
4869 // TODO: if not a text object, insert at closest position, e.g. in front of it
4875 // Don't pass parent initially to suppress auto-setting of parent range.
4876 // We'll do that at a higher level.
4877 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
4879 AppendChild(textObject
);
4886 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
4888 wxRichTextCompositeObject::Copy(obj
);
4891 /// Clear the cached lines
4892 void wxRichTextParagraph::ClearLines()
4894 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
4897 /// Get/set the object size for the given range. Returns false if the range
4898 /// is invalid for this object.
4899 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
4901 if (!range
.IsWithin(GetRange()))
4904 if (flags
& wxRICHTEXT_UNFORMATTED
)
4906 // Just use unformatted data, assume no line breaks
4907 // TODO: take into account line breaks
4911 wxArrayInt childExtents
;
4918 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4922 wxRichTextObject
* child
= node
->GetData();
4923 if (!child
->GetRange().IsOutside(range
))
4925 // Floating objects have a zero size within the paragraph.
4926 if (child
->IsFloating())
4931 if (partialExtents
->GetCount() > 0)
4932 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
4936 partialExtents
->Add(0 /* zero size */ + lastSize
);
4943 wxRichTextRange rangeToUse
= range
;
4944 rangeToUse
.LimitTo(child
->GetRange());
4946 if (child
->IsTopLevel())
4947 rangeToUse
= child
->GetOwnRange();
4949 int childDescent
= 0;
4951 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
4952 // but it's only going to be used after caching has taken place.
4953 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
4955 childDescent
= child
->GetDescent();
4956 childSize
= child
->GetCachedSize();
4958 sz
.y
= wxMax(sz
.y
, childSize
.y
);
4959 sz
.x
+= childSize
.x
;
4960 descent
= wxMax(descent
, childDescent
);
4962 else if (child
->IsTopLevel())
4964 childDescent
= child
->GetDescent();
4965 childSize
= child
->GetCachedSize();
4967 sz
.y
= wxMax(sz
.y
, childSize
.y
);
4968 sz
.x
+= childSize
.x
;
4969 descent
= wxMax(descent
, childDescent
);
4970 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
4972 child
->SetCachedSize(childSize
);
4973 child
->SetDescent(childDescent
);
4979 if (partialExtents
->GetCount() > 0)
4980 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
4984 partialExtents
->Add(childSize
.x
+ lastSize
);
4987 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
4989 sz
.y
= wxMax(sz
.y
, childSize
.y
);
4990 sz
.x
+= childSize
.x
;
4991 descent
= wxMax(descent
, childDescent
);
4993 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
4995 child
->SetCachedSize(childSize
);
4996 child
->SetDescent(childDescent
);
5002 if (partialExtents
->GetCount() > 0)
5003 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
5008 for (i
= 0; i
< childExtents
.GetCount(); i
++)
5010 partialExtents
->Add(childExtents
[i
] + lastSize
);
5020 node
= node
->GetNext();
5026 // Use formatted data, with line breaks
5029 // We're going to loop through each line, and then for each line,
5030 // call GetRangeSize for the fragment that comprises that line.
5031 // Only we have to do that multiple times within the line, because
5032 // the line may be broken into pieces. For now ignore line break commands
5033 // (so we can assume that getting the unformatted size for a fragment
5034 // within a line is the actual size)
5036 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
5039 wxRichTextLine
* line
= node
->GetData();
5040 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
5041 if (!lineRange
.IsOutside(range
))
5045 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
5048 wxRichTextObject
* child
= node2
->GetData();
5050 if (!child
->IsFloating() && !child
->GetRange().IsOutside(lineRange
))
5052 wxRichTextRange rangeToUse
= lineRange
;
5053 rangeToUse
.LimitTo(child
->GetRange());
5054 if (child
->IsTopLevel())
5055 rangeToUse
= child
->GetOwnRange();
5058 int childDescent
= 0;
5059 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
5061 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
5062 lineSize
.x
+= childSize
.x
;
5064 descent
= wxMax(descent
, childDescent
);
5067 node2
= node2
->GetNext();
5070 // Increase size by a line (TODO: paragraph spacing)
5072 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
5074 node
= node
->GetNext();
5081 /// Finds the absolute position and row height for the given character position
5082 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
5086 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
5088 *height
= line
->GetSize().y
;
5090 *height
= dc
.GetCharHeight();
5092 // -1 means 'the start of the buffer'.
5095 pt
= pt
+ line
->GetPosition();
5100 // The final position in a paragraph is taken to mean the position
5101 // at the start of the next paragraph.
5102 if (index
== GetRange().GetEnd())
5104 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
5105 wxASSERT( parent
!= NULL
);
5107 // Find the height at the next paragraph, if any
5108 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
5111 *height
= line
->GetSize().y
;
5112 pt
= line
->GetAbsolutePosition();
5116 *height
= dc
.GetCharHeight();
5117 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
5118 pt
= wxPoint(indent
, GetCachedSize().y
);
5124 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
5127 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
5130 wxRichTextLine
* line
= node
->GetData();
5131 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
5132 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
5134 // If this is the last point in the line, and we're forcing the
5135 // returned value to be the start of the next line, do the required
5137 if (index
== lineRange
.GetEnd() && forceLineStart
)
5139 if (node
->GetNext())
5141 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
5142 *height
= nextLine
->GetSize().y
;
5143 pt
= nextLine
->GetAbsolutePosition();
5148 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
5150 wxRichTextRange
r(lineRange
.GetStart(), index
);
5154 // We find the size of the line up to this point,
5155 // then we can add this size to the line start position and
5156 // paragraph start position to find the actual position.
5158 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
5160 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
5161 *height
= line
->GetSize().y
;
5168 node
= node
->GetNext();
5174 /// Hit-testing: returns a flag indicating hit test details, plus
5175 /// information about position
5176 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, wxRichTextObject
** contextObj
, int flags
)
5179 return wxRICHTEXT_HITTEST_NONE
;
5181 // If we're in the top-level container, then we can return
5182 // a suitable hit test code even if the point is outside the container area,
5183 // so that we can position the caret sensibly even if we don't
5184 // click on valid content. If we're not at the top-level, and the point
5185 // is not within this paragraph object, then we don't want to stop more
5186 // precise hit-testing from working prematurely, so return immediately.
5187 // NEW STRATEGY: use the parent boundary to test whether we're in the
5188 // right region, not the paragraph, since the paragraph may be positioned
5189 // some way in from where the user clicks.
5192 wxRichTextObject
* tempObj
, *tempContextObj
;
5193 if (GetParent() && GetParent()->wxRichTextObject::HitTest(dc
, pt
, tmpPos
, & tempObj
, & tempContextObj
, flags
) == wxRICHTEXT_HITTEST_NONE
)
5194 return wxRICHTEXT_HITTEST_NONE
;
5197 wxRichTextObjectList::compatibility_iterator objNode
= m_children
.GetFirst();
5200 wxRichTextObject
* child
= objNode
->GetData();
5201 if (child
->IsTopLevel() && ((flags
& wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
) == 0))
5204 int hitTest
= child
->HitTest(dc
, pt
, textPosition
, obj
, contextObj
);
5205 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
5210 objNode
= objNode
->GetNext();
5213 wxPoint paraPos
= GetPosition();
5215 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
5218 wxRichTextLine
* line
= node
->GetData();
5219 wxPoint linePos
= paraPos
+ line
->GetPosition();
5220 wxSize lineSize
= line
->GetSize();
5221 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
5223 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
5225 if (pt
.x
< linePos
.x
)
5227 textPosition
= lineRange
.GetStart();
5228 *obj
= FindObjectAtPosition(textPosition
);
5229 *contextObj
= GetContainer();
5230 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
5232 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
5234 textPosition
= lineRange
.GetEnd();
5235 *obj
= FindObjectAtPosition(textPosition
);
5236 *contextObj
= GetContainer();
5237 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
5241 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
5242 wxArrayInt partialExtents
;
5247 // This calculates the partial text extents
5248 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
, & partialExtents
);
5250 int lastX
= linePos
.x
;
5252 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
5254 int nextX
= partialExtents
[i
] + linePos
.x
;
5256 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
5258 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
5260 *obj
= FindObjectAtPosition(textPosition
);
5261 *contextObj
= GetContainer();
5263 // So now we know it's between i-1 and i.
5264 // Let's see if we can be more precise about
5265 // which side of the position it's on.
5267 int midPoint
= (nextX
+ lastX
)/2;
5268 if (pt
.x
>= midPoint
)
5269 return wxRICHTEXT_HITTEST_AFTER
;
5271 return wxRICHTEXT_HITTEST_BEFORE
;
5278 int lastX
= linePos
.x
;
5279 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
5284 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
5286 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
5288 int nextX
= childSize
.x
+ linePos
.x
;
5290 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
5294 *obj
= FindObjectAtPosition(textPosition
);
5295 *contextObj
= GetContainer();
5297 // So now we know it's between i-1 and i.
5298 // Let's see if we can be more precise about
5299 // which side of the position it's on.
5301 int midPoint
= (nextX
+ lastX
)/2;
5302 if (pt
.x
>= midPoint
)
5303 return wxRICHTEXT_HITTEST_AFTER
;
5305 return wxRICHTEXT_HITTEST_BEFORE
;
5316 node
= node
->GetNext();
5319 return wxRICHTEXT_HITTEST_NONE
;
5322 /// Split an object at this position if necessary, and return
5323 /// the previous object, or NULL if inserting at beginning.
5324 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
5326 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
5329 wxRichTextObject
* child
= node
->GetData();
5331 if (pos
== child
->GetRange().GetStart())
5335 if (node
->GetPrevious())
5336 *previousObject
= node
->GetPrevious()->GetData();
5338 *previousObject
= NULL
;
5344 if (child
->GetRange().Contains(pos
))
5346 // This should create a new object, transferring part of
5347 // the content to the old object and the rest to the new object.
5348 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
5350 // If we couldn't split this object, just insert in front of it.
5353 // Maybe this is an empty string, try the next one
5358 // Insert the new object after 'child'
5359 if (node
->GetNext())
5360 m_children
.Insert(node
->GetNext(), newObject
);
5362 m_children
.Append(newObject
);
5363 newObject
->SetParent(this);
5366 *previousObject
= child
;
5372 node
= node
->GetNext();
5375 *previousObject
= NULL
;
5379 /// Move content to a list from obj on
5380 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
5382 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
5385 wxRichTextObject
* child
= node
->GetData();
5388 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
5390 node
= node
->GetNext();
5392 m_children
.DeleteNode(oldNode
);
5396 /// Add content back from list
5397 void wxRichTextParagraph::MoveFromList(wxList
& list
)
5399 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
5401 AppendChild((wxRichTextObject
*) node
->GetData());
5406 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
5408 wxRichTextCompositeObject::CalculateRange(start
, end
);
5410 // Add one for end of paragraph
5413 m_range
.SetRange(start
, end
);
5416 /// Find the object at the given position
5417 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
5419 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
5422 wxRichTextObject
* obj
= node
->GetData();
5423 if (obj
->GetRange().Contains(position
) ||
5424 obj
->GetRange().GetStart() == position
||
5425 obj
->GetRange().GetEnd() == position
)
5428 node
= node
->GetNext();
5433 /// Get the plain text searching from the start or end of the range.
5434 /// The resulting string may be shorter than the range given.
5435 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
5437 text
= wxEmptyString
;
5441 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
5444 wxRichTextObject
* obj
= node
->GetData();
5445 if (!obj
->GetRange().IsOutside(range
))
5447 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
5450 text
+= textObj
->GetTextForRange(range
);
5458 node
= node
->GetNext();
5463 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
5466 wxRichTextObject
* obj
= node
->GetData();
5467 if (!obj
->GetRange().IsOutside(range
))
5469 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
5472 text
= textObj
->GetTextForRange(range
) + text
;
5476 text
= wxT(" ") + text
;
5480 node
= node
->GetPrevious();
5487 /// Find a suitable wrap position.
5488 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
5490 if (range
.GetLength() <= 0)
5493 // Find the first position where the line exceeds the available space.
5495 long breakPosition
= range
.GetEnd();
5497 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
5498 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
5502 if (range
.GetStart() > GetRange().GetStart())
5503 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
5508 for (i
= (size_t) range
.GetStart(); i
<= (size_t) range
.GetEnd(); i
++)
5510 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
5512 if (widthFromStartOfThisRange
> availableSpace
)
5514 breakPosition
= i
-1;
5522 // Binary chop for speed
5523 long minPos
= range
.GetStart();
5524 long maxPos
= range
.GetEnd();
5527 if (minPos
== maxPos
)
5530 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
5532 if (sz
.x
> availableSpace
)
5533 breakPosition
= minPos
- 1;
5536 else if ((maxPos
- minPos
) == 1)
5539 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
5541 if (sz
.x
> availableSpace
)
5542 breakPosition
= minPos
- 1;
5545 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
5546 if (sz
.x
> availableSpace
)
5547 breakPosition
= maxPos
-1;
5553 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
5556 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
5558 if (sz
.x
> availableSpace
)
5570 // Now we know the last position on the line.
5571 // Let's try to find a word break.
5574 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
5576 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
5577 if (newLinePos
!= wxNOT_FOUND
)
5579 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
5583 int spacePos
= plainText
.Find(wxT(' '), true);
5584 int tabPos
= plainText
.Find(wxT('\t'), true);
5585 int pos
= wxMax(spacePos
, tabPos
);
5586 if (pos
!= wxNOT_FOUND
)
5588 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
5589 breakPosition
= breakPosition
- positionsFromEndOfString
;
5594 wrapPosition
= breakPosition
;
5599 /// Get the bullet text for this paragraph.
5600 wxString
wxRichTextParagraph::GetBulletText()
5602 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
5603 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
5604 return wxEmptyString
;
5606 int number
= GetAttributes().GetBulletNumber();
5609 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
5611 text
.Printf(wxT("%d"), number
);
5613 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
5615 // TODO: Unicode, and also check if number > 26
5616 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
5618 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
5620 // TODO: Unicode, and also check if number > 26
5621 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
5623 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
5625 text
= wxRichTextDecimalToRoman(number
);
5627 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
5629 text
= wxRichTextDecimalToRoman(number
);
5632 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
5634 text
= GetAttributes().GetBulletText();
5637 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
5639 // The outline style relies on the text being computed statically,
5640 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
5641 // should be stored in the attributes; if not, just use the number for this
5642 // level, as previously computed.
5643 if (!GetAttributes().GetBulletText().IsEmpty())
5644 text
= GetAttributes().GetBulletText();
5647 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
5649 text
= wxT("(") + text
+ wxT(")");
5651 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
5653 text
= text
+ wxT(")");
5656 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
5664 /// Allocate or reuse a line object
5665 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
5667 if (pos
< (int) m_cachedLines
.GetCount())
5669 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
5675 wxRichTextLine
* line
= new wxRichTextLine(this);
5676 m_cachedLines
.Append(line
);
5681 /// Clear remaining unused line objects, if any
5682 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
5684 int cachedLineCount
= m_cachedLines
.GetCount();
5685 if ((int) cachedLineCount
> lineCount
)
5687 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
5689 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
5690 wxRichTextLine
* line
= node
->GetData();
5691 m_cachedLines
.Erase(node
);
5698 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
5699 /// retrieve the actual style.
5700 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxRichTextAttr
& contentStyle
, bool includingBoxAttr
) const
5702 wxRichTextAttr attr
;
5703 wxRichTextParagraphLayoutBox
* buf
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
5706 attr
= buf
->GetBasicStyle();
5707 if (!includingBoxAttr
)
5709 attr
.GetTextBoxAttr().Reset();
5710 // The background colour will be painted by the container, and we don't
5711 // want to unnecessarily overwrite the background when we're drawing text
5712 // because this may erase the guideline (which appears just under the text
5713 // if there's no padding).
5714 attr
.SetFlags(attr
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
5716 wxRichTextApplyStyle(attr
, GetAttributes());
5719 attr
= GetAttributes();
5721 wxRichTextApplyStyle(attr
, contentStyle
);
5725 /// Get combined attributes of the base style and paragraph style.
5726 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes(bool includingBoxAttr
) const
5728 wxRichTextAttr attr
;
5729 wxRichTextParagraphLayoutBox
* buf
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
5732 attr
= buf
->GetBasicStyle();
5733 if (!includingBoxAttr
)
5734 attr
.GetTextBoxAttr().Reset();
5735 wxRichTextApplyStyle(attr
, GetAttributes());
5738 attr
= GetAttributes();
5743 // Create default tabstop array
5744 void wxRichTextParagraph::InitDefaultTabs()
5746 // create a default tab list at 10 mm each.
5747 for (int i
= 0; i
< 20; ++i
)
5749 sm_defaultTabs
.Add(i
*100);
5753 // Clear default tabstop array
5754 void wxRichTextParagraph::ClearDefaultTabs()
5756 sm_defaultTabs
.Clear();
5759 void wxRichTextParagraph::LayoutFloat(wxDC
& dc
, const wxRect
& rect
, int style
, wxRichTextFloatCollector
* floatCollector
)
5761 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
5764 wxRichTextObject
* anchored
= node
->GetData();
5765 if (anchored
&& anchored
->IsFloating() && !floatCollector
->HasFloat(anchored
))
5769 anchored
->GetRangeSize(anchored
->GetRange(), size
, descent
, dc
, style
);
5772 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().IsValid())
5774 offsetY
= anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetValue();
5775 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
5777 offsetY
= ConvertTenthsMMToPixels(dc
, offsetY
);
5781 int pos
= floatCollector
->GetFitPosition(anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode(), rect
.y
+ offsetY
, size
.y
);
5783 /* Update the offset */
5784 int newOffsetY
= pos
- rect
.y
;
5785 if (newOffsetY
!= offsetY
)
5787 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
5788 newOffsetY
= ConvertPixelsToTenthsMM(dc
, newOffsetY
);
5789 anchored
->GetAttributes().GetTextBoxAttr().GetTop().SetValue(newOffsetY
);
5792 if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
5794 else if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
5795 x
= rect
.x
+ rect
.width
- size
.x
;
5797 anchored
->SetPosition(wxPoint(x
, pos
));
5798 anchored
->SetCachedSize(size
);
5799 floatCollector
->CollectFloat(this, anchored
);
5802 node
= node
->GetNext();
5806 // Get the first position from pos that has a line break character.
5807 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
5809 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
5812 wxRichTextObject
* obj
= node
->GetData();
5813 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
5815 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
5818 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
5823 node
= node
->GetNext();
5830 * This object represents a line in a paragraph, and stores
5831 * offsets from the start of the paragraph representing the
5832 * start and end positions of the line.
5835 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
5841 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
5844 m_range
.SetRange(-1, -1);
5845 m_pos
= wxPoint(0, 0);
5846 m_size
= wxSize(0, 0);
5848 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
5849 m_objectSizes
.Clear();
5854 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
5856 m_range
= obj
.m_range
;
5857 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
5858 m_objectSizes
= obj
.m_objectSizes
;
5862 /// Get the absolute object position
5863 wxPoint
wxRichTextLine::GetAbsolutePosition() const
5865 return m_parent
->GetPosition() + m_pos
;
5868 /// Get the absolute range
5869 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
5871 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
5872 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
5877 * wxRichTextPlainText
5878 * This object represents a single piece of text.
5881 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
5883 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* style
):
5884 wxRichTextObject(parent
)
5887 SetAttributes(*style
);
5892 #define USE_KERNING_FIX 1
5894 // If insufficient tabs are defined, this is the tab width used
5895 #define WIDTH_FOR_DEFAULT_TABS 50
5898 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
5900 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
5901 wxASSERT (para
!= NULL
);
5903 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes(), false /* no box attributes */) : GetAttributes());
5905 // Let's make the assumption for now that for content in a paragraph, including
5906 // text, we never have a discontinuous selection. So we only deal with a
5908 wxRichTextRange selectionRange
;
5909 if (selection
.IsValid())
5911 wxRichTextRangeArray selectionRanges
= selection
.GetSelectionForObject(this);
5912 if (selectionRanges
.GetCount() > 0)
5913 selectionRange
= selectionRanges
[0];
5915 selectionRange
= wxRICHTEXT_NO_SELECTION
;
5918 selectionRange
= wxRICHTEXT_NO_SELECTION
;
5920 int offset
= GetRange().GetStart();
5922 // Replace line break characters with spaces
5923 wxString str
= m_text
;
5924 wxString toRemove
= wxRichTextLineBreakChar
;
5925 str
.Replace(toRemove
, wxT(" "));
5926 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
5929 long len
= range
.GetLength();
5930 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
5932 // Test for the optimized situations where all is selected, or none
5935 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
5936 wxCheckSetFont(dc
, textFont
);
5937 int charHeight
= dc
.GetCharHeight();
5940 if ( textFont
.Ok() )
5942 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
5944 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
5945 textFont
.SetPointSize( static_cast<int>(size
) );
5948 wxCheckSetFont(dc
, textFont
);
5950 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
5952 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
5953 textFont
.SetPointSize( static_cast<int>(size
) );
5955 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
5956 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
5957 wxCheckSetFont(dc
, textFont
);
5962 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
5968 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
5971 // TODO: new selection code
5973 // (a) All selected.
5974 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
5976 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
5978 // (b) None selected.
5979 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
5981 // Draw all unselected
5982 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
5986 // (c) Part selected, part not
5987 // Let's draw unselected chunk, selected chunk, then unselected chunk.
5989 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
5991 // 1. Initial unselected chunk, if any, up until start of selection.
5992 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
5994 int r1
= range
.GetStart();
5995 int s1
= selectionRange
.GetStart()-1;
5996 int fragmentLen
= s1
- r1
+ 1;
5997 if (fragmentLen
< 0)
5999 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
6001 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
6003 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
6006 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
6008 // Compensate for kerning difference
6009 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
6010 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
6012 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
6013 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
6014 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
6015 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
6017 int kerningDiff
= (w1
+ w3
) - w2
;
6018 x
= x
- kerningDiff
;
6023 // 2. Selected chunk, if any.
6024 if (selectionRange
.GetEnd() >= range
.GetStart())
6026 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
6027 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
6029 int fragmentLen
= s2
- s1
+ 1;
6030 if (fragmentLen
< 0)
6032 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
6034 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
6036 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
6039 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
6041 // Compensate for kerning difference
6042 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
6043 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
6045 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
6046 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
6047 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
6048 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
6050 int kerningDiff
= (w1
+ w3
) - w2
;
6051 x
= x
- kerningDiff
;
6056 // 3. Remaining unselected chunk, if any
6057 if (selectionRange
.GetEnd() < range
.GetEnd())
6059 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
6060 int r2
= range
.GetEnd();
6062 int fragmentLen
= r2
- s2
+ 1;
6063 if (fragmentLen
< 0)
6065 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
6067 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
6069 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
6076 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
6078 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
6080 wxArrayInt tabArray
;
6084 if (attr
.GetTabs().IsEmpty())
6085 tabArray
= wxRichTextParagraph::GetDefaultTabs();
6087 tabArray
= attr
.GetTabs();
6088 tabCount
= tabArray
.GetCount();
6090 for (int i
= 0; i
< tabCount
; ++i
)
6092 int pos
= tabArray
[i
];
6093 pos
= ConvertTenthsMMToPixels(dc
, pos
);
6100 int nextTabPos
= -1;
6106 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
6107 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
6109 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
6110 wxCheckSetPen(dc
, wxPen(highlightColour
));
6111 dc
.SetTextForeground(highlightTextColour
);
6112 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6116 dc
.SetTextForeground(attr
.GetTextColour());
6118 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
6120 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
6121 dc
.SetTextBackground(attr
.GetBackgroundColour());
6124 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6127 wxCoord x_orig
= GetParent()->GetPosition().x
;
6130 // the string has a tab
6131 // break up the string at the Tab
6132 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
6133 str
= str
.AfterFirst(wxT('\t'));
6134 dc
.GetTextExtent(stringChunk
, & w
, & h
);
6136 bool not_found
= true;
6137 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
6139 nextTabPos
= tabArray
.Item(i
) + x_orig
;
6141 // Find the next tab position.
6142 // Even if we're at the end of the tab array, we must still draw the chunk.
6144 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
6146 if (nextTabPos
<= tabPos
)
6148 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
6149 nextTabPos
= tabPos
+ defaultTabWidth
;
6156 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
6157 dc
.DrawRectangle(selRect
);
6159 dc
.DrawText(stringChunk
, x
, y
);
6161 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
6163 wxPen oldPen
= dc
.GetPen();
6164 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
6165 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
6166 wxCheckSetPen(dc
, oldPen
);
6172 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
6177 dc
.GetTextExtent(str
, & w
, & h
);
6180 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
6181 dc
.DrawRectangle(selRect
);
6183 dc
.DrawText(str
, x
, y
);
6185 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
6187 wxPen oldPen
= dc
.GetPen();
6188 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
6189 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
6190 wxCheckSetPen(dc
, oldPen
);
6199 /// Lay the item out
6200 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
6202 // Only lay out if we haven't already cached the size
6204 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
6206 // Eventually we want to have a reasonable estimate of minimum size.
6207 m_minSize
= wxSize(0, 0);
6212 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
6214 wxRichTextObject::Copy(obj
);
6216 m_text
= obj
.m_text
;
6219 /// Get/set the object size for the given range. Returns false if the range
6220 /// is invalid for this object.
6221 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
6223 if (!range
.IsWithin(GetRange()))
6226 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
6227 wxASSERT (para
!= NULL
);
6229 int relativeX
= position
.x
- GetParent()->GetPosition().x
;
6231 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
6233 // Always assume unformatted text, since at this level we have no knowledge
6234 // of line breaks - and we don't need it, since we'll calculate size within
6235 // formatted text by doing it in chunks according to the line ranges
6237 bool bScript(false);
6238 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
6241 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
6242 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
6244 wxFont textFont
= font
;
6245 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
6246 textFont
.SetPointSize( static_cast<int>(size
) );
6247 wxCheckSetFont(dc
, textFont
);
6252 wxCheckSetFont(dc
, font
);
6256 bool haveDescent
= false;
6257 int startPos
= range
.GetStart() - GetRange().GetStart();
6258 long len
= range
.GetLength();
6260 wxString
str(m_text
);
6261 wxString toReplace
= wxRichTextLineBreakChar
;
6262 str
.Replace(toReplace
, wxT(" "));
6264 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
6266 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
6267 stringChunk
.MakeUpper();
6271 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
6273 // the string has a tab
6274 wxArrayInt tabArray
;
6275 if (textAttr
.GetTabs().IsEmpty())
6276 tabArray
= wxRichTextParagraph::GetDefaultTabs();
6278 tabArray
= textAttr
.GetTabs();
6280 int tabCount
= tabArray
.GetCount();
6282 for (int i
= 0; i
< tabCount
; ++i
)
6284 int pos
= tabArray
[i
];
6285 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
6289 int nextTabPos
= -1;
6291 while (stringChunk
.Find(wxT('\t')) >= 0)
6293 int absoluteWidth
= 0;
6295 // the string has a tab
6296 // break up the string at the Tab
6297 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
6298 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
6303 if (partialExtents
->GetCount() > 0)
6304 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
6308 // Add these partial extents
6310 dc
.GetPartialTextExtents(stringFragment
, p
);
6312 for (j
= 0; j
< p
.GetCount(); j
++)
6313 partialExtents
->Add(oldWidth
+ p
[j
]);
6315 if (partialExtents
->GetCount() > 0)
6316 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + relativeX
;
6318 absoluteWidth
= relativeX
;
6322 dc
.GetTextExtent(stringFragment
, & w
, & h
);
6324 absoluteWidth
= width
+ relativeX
;
6328 bool notFound
= true;
6329 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
6331 nextTabPos
= tabArray
.Item(i
);
6333 // Find the next tab position.
6334 // Even if we're at the end of the tab array, we must still process the chunk.
6336 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
6338 if (nextTabPos
<= absoluteWidth
)
6340 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
6341 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
6345 width
= nextTabPos
- relativeX
;
6348 partialExtents
->Add(width
);
6354 if (!stringChunk
.IsEmpty())
6359 if (partialExtents
->GetCount() > 0)
6360 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
6364 // Add these partial extents
6366 dc
.GetPartialTextExtents(stringChunk
, p
);
6368 for (j
= 0; j
< p
.GetCount(); j
++)
6369 partialExtents
->Add(oldWidth
+ p
[j
]);
6373 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
6381 int charHeight
= dc
.GetCharHeight();
6382 if ((*partialExtents
).GetCount() > 0)
6383 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
6386 size
= wxSize(w
, charHeight
);
6390 size
= wxSize(width
, dc
.GetCharHeight());
6394 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
6402 /// Do a split, returning an object containing the second part, and setting
6403 /// the first part in 'this'.
6404 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
6406 long index
= pos
- GetRange().GetStart();
6408 if (index
< 0 || index
>= (int) m_text
.length())
6411 wxString firstPart
= m_text
.Mid(0, index
);
6412 wxString secondPart
= m_text
.Mid(index
);
6416 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
6417 newObject
->SetAttributes(GetAttributes());
6419 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
6420 GetRange().SetEnd(pos
-1);
6426 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
6428 end
= start
+ m_text
.length() - 1;
6429 m_range
.SetRange(start
, end
);
6433 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
6435 wxRichTextRange r
= range
;
6437 r
.LimitTo(GetRange());
6439 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
6445 long startIndex
= r
.GetStart() - GetRange().GetStart();
6446 long len
= r
.GetLength();
6448 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
6452 /// Get text for the given range.
6453 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
6455 wxRichTextRange r
= range
;
6457 r
.LimitTo(GetRange());
6459 long startIndex
= r
.GetStart() - GetRange().GetStart();
6460 long len
= r
.GetLength();
6462 return m_text
.Mid(startIndex
, len
);
6465 /// Returns true if this object can merge itself with the given one.
6466 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
6468 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
6469 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
6472 /// Returns true if this object merged itself with the given one.
6473 /// The calling code will then delete the given object.
6474 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
6476 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
6477 wxASSERT( textObject
!= NULL
);
6481 m_text
+= textObject
->GetText();
6482 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
6489 /// Dump to output stream for debugging
6490 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
6492 wxRichTextObject::Dump(stream
);
6493 stream
<< m_text
<< wxT("\n");
6496 /// Get the first position from pos that has a line break character.
6497 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
6500 int len
= m_text
.length();
6501 int startPos
= pos
- m_range
.GetStart();
6502 for (i
= startPos
; i
< len
; i
++)
6504 wxChar ch
= m_text
[i
];
6505 if (ch
== wxRichTextLineBreakChar
)
6507 return i
+ m_range
.GetStart();
6515 * This is a kind of box, used to represent the whole buffer
6518 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
6520 wxList
wxRichTextBuffer::sm_handlers
;
6521 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
6522 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
6523 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
6526 void wxRichTextBuffer::Init()
6528 m_commandProcessor
= new wxCommandProcessor
;
6529 m_styleSheet
= NULL
;
6531 m_batchedCommandDepth
= 0;
6532 m_batchedCommand
= NULL
;
6539 wxRichTextBuffer::~wxRichTextBuffer()
6541 delete m_commandProcessor
;
6542 delete m_batchedCommand
;
6545 ClearEventHandlers();
6548 void wxRichTextBuffer::ResetAndClearCommands()
6552 GetCommandProcessor()->ClearCommands();
6555 Invalidate(wxRICHTEXT_ALL
);
6558 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
6560 wxRichTextParagraphLayoutBox::Copy(obj
);
6562 m_styleSheet
= obj
.m_styleSheet
;
6563 m_modified
= obj
.m_modified
;
6564 m_batchedCommandDepth
= 0;
6565 if (m_batchedCommand
)
6566 delete m_batchedCommand
;
6567 m_batchedCommand
= NULL
;
6568 m_suppressUndo
= obj
.m_suppressUndo
;
6569 m_invalidRange
= obj
.m_invalidRange
;
6572 /// Push style sheet to top of stack
6573 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
6576 styleSheet
->InsertSheet(m_styleSheet
);
6578 SetStyleSheet(styleSheet
);
6583 /// Pop style sheet from top of stack
6584 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
6588 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
6589 m_styleSheet
= oldSheet
->GetNextSheet();
6598 /// Submit command to insert paragraphs
6599 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
6601 return ctrl
->GetFocusObject()->InsertParagraphsWithUndo(pos
, paragraphs
, ctrl
, this, flags
);
6604 /// Submit command to insert paragraphs
6605 bool wxRichTextParagraphLayoutBox::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
, int WXUNUSED(flags
))
6607 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, buffer
, this, ctrl
, false);
6609 action
->GetNewParagraphs() = paragraphs
;
6611 action
->SetPosition(pos
);
6613 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetOwnRange().GetEnd() - 1);
6614 if (!paragraphs
.GetPartialParagraph())
6615 range
.SetEnd(range
.GetEnd()+1);
6617 // Set the range we'll need to delete in Undo
6618 action
->SetRange(range
);
6620 buffer
->SubmitAction(action
);
6625 /// Submit command to insert the given text
6626 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
6628 return ctrl
->GetFocusObject()->InsertTextWithUndo(pos
, text
, ctrl
, this, flags
);
6631 /// Submit command to insert the given text
6632 bool wxRichTextParagraphLayoutBox::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
, int flags
)
6634 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, buffer
, this, ctrl
, false);
6636 wxRichTextAttr
* p
= NULL
;
6637 wxRichTextAttr paraAttr
;
6638 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
6640 // Get appropriate paragraph style
6641 paraAttr
= GetStyleForNewParagraph(buffer
, pos
, false, false);
6642 if (!paraAttr
.IsDefault())
6646 action
->GetNewParagraphs().AddParagraphs(text
, p
);
6648 int length
= action
->GetNewParagraphs().GetOwnRange().GetLength();
6650 if (!text
.empty() && text
.Last() != wxT('\n'))
6652 // Don't count the newline when undoing
6654 action
->GetNewParagraphs().SetPartialParagraph(true);
6656 else if (!text
.empty() && text
.Last() == wxT('\n'))
6659 action
->SetPosition(pos
);
6661 // Set the range we'll need to delete in Undo
6662 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
6664 buffer
->SubmitAction(action
);
6669 /// Submit command to insert the given text
6670 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
6672 return ctrl
->GetFocusObject()->InsertNewlineWithUndo(pos
, ctrl
, this, flags
);
6675 /// Submit command to insert the given text
6676 bool wxRichTextParagraphLayoutBox::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
, int flags
)
6678 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, buffer
, this, ctrl
, false);
6680 wxRichTextAttr
* p
= NULL
;
6681 wxRichTextAttr paraAttr
;
6682 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
6684 paraAttr
= GetStyleForNewParagraph(buffer
, pos
, false, true /* look for next paragraph style */);
6685 if (!paraAttr
.IsDefault())
6689 wxRichTextAttr
attr(buffer
->GetDefaultStyle());
6691 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
6692 action
->GetNewParagraphs().AppendChild(newPara
);
6693 action
->GetNewParagraphs().UpdateRanges();
6694 action
->GetNewParagraphs().SetPartialParagraph(false);
6695 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
6699 newPara
->SetAttributes(*p
);
6701 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
6703 if (para
&& para
->GetRange().GetEnd() == pos
)
6706 // Now see if we need to number the paragraph.
6707 if (newPara
->GetAttributes().HasBulletNumber())
6709 wxRichTextAttr numberingAttr
;
6710 if (FindNextParagraphNumber(para
, numberingAttr
))
6711 wxRichTextApplyStyle(newPara
->GetAttributes(), (const wxRichTextAttr
&) numberingAttr
);
6715 action
->SetPosition(pos
);
6717 // Use the default character style
6718 // Use the default character style
6719 if (!buffer
->GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
6721 // Check whether the default style merely reflects the paragraph/basic style,
6722 // in which case don't apply it.
6723 wxRichTextAttr
defaultStyle(buffer
->GetDefaultStyle());
6724 wxRichTextAttr toApply
;
6727 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes(true /* include box attributes */);
6728 wxRichTextAttr newAttr
;
6729 // This filters out attributes that are accounted for by the current
6730 // paragraph/basic style
6731 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
6734 toApply
= defaultStyle
;
6736 if (!toApply
.IsDefault())
6737 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
6740 // Set the range we'll need to delete in Undo
6741 action
->SetRange(wxRichTextRange(pos1
, pos1
));
6743 buffer
->SubmitAction(action
);
6748 /// Submit command to insert the given image
6749 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
,
6750 const wxRichTextAttr
& textAttr
)
6752 return ctrl
->GetFocusObject()->InsertImageWithUndo(pos
, imageBlock
, ctrl
, this, flags
, textAttr
);
6755 /// Submit command to insert the given image
6756 bool wxRichTextParagraphLayoutBox::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
,
6757 wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
, int flags
,
6758 const wxRichTextAttr
& textAttr
)
6760 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, buffer
, this, ctrl
, false);
6762 wxRichTextAttr
* p
= NULL
;
6763 wxRichTextAttr paraAttr
;
6764 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
6766 paraAttr
= GetStyleForNewParagraph(buffer
, pos
);
6767 if (!paraAttr
.IsDefault())
6771 wxRichTextAttr
attr(buffer
->GetDefaultStyle());
6773 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
6775 newPara
->SetAttributes(*p
);
6777 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
6778 newPara
->AppendChild(imageObject
);
6779 imageObject
->SetAttributes(textAttr
);
6780 action
->GetNewParagraphs().AppendChild(newPara
);
6781 action
->GetNewParagraphs().UpdateRanges();
6783 action
->GetNewParagraphs().SetPartialParagraph(true);
6785 action
->SetPosition(pos
);
6787 // Set the range we'll need to delete in Undo
6788 action
->SetRange(wxRichTextRange(pos
, pos
));
6790 buffer
->SubmitAction(action
);
6795 // Insert an object with no change of it
6796 wxRichTextObject
* wxRichTextBuffer::InsertObjectWithUndo(long pos
, wxRichTextObject
*object
, wxRichTextCtrl
* ctrl
, int flags
)
6798 return ctrl
->GetFocusObject()->InsertObjectWithUndo(pos
, object
, ctrl
, this, flags
);
6801 // Insert an object with no change of it
6802 wxRichTextObject
* wxRichTextParagraphLayoutBox::InsertObjectWithUndo(long pos
, wxRichTextObject
*object
, wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
, int flags
)
6804 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Object"), wxRICHTEXT_INSERT
, buffer
, this, ctrl
, false);
6806 wxRichTextAttr
* p
= NULL
;
6807 wxRichTextAttr paraAttr
;
6808 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
6810 paraAttr
= GetStyleForNewParagraph(buffer
, pos
);
6811 if (!paraAttr
.IsDefault())
6815 wxRichTextAttr
attr(buffer
->GetDefaultStyle());
6817 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
6819 newPara
->SetAttributes(*p
);
6821 newPara
->AppendChild(object
);
6822 action
->GetNewParagraphs().AppendChild(newPara
);
6823 action
->GetNewParagraphs().UpdateRanges();
6825 action
->GetNewParagraphs().SetPartialParagraph(true);
6827 action
->SetPosition(pos
);
6829 // Set the range we'll need to delete in Undo
6830 action
->SetRange(wxRichTextRange(pos
, pos
));
6832 buffer
->SubmitAction(action
);
6834 wxRichTextObject
* obj
= GetLeafObjectAtPosition(pos
);
6838 /// Get the style that is appropriate for a new paragraph at this position.
6839 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
6841 wxRichTextAttr
wxRichTextParagraphLayoutBox::GetStyleForNewParagraph(wxRichTextBuffer
* buffer
, long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
6843 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
6846 wxRichTextAttr attr
;
6847 bool foundAttributes
= false;
6849 // Look for a matching paragraph style
6850 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && buffer
->GetStyleSheet())
6852 wxRichTextParagraphStyleDefinition
* paraDef
= buffer
->GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
6855 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
6856 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
6858 wxRichTextParagraphStyleDefinition
* nextParaDef
= buffer
->GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
6861 foundAttributes
= true;
6862 attr
= nextParaDef
->GetStyleMergedWithBase(buffer
->GetStyleSheet());
6866 // If we didn't find the 'next style', use this style instead.
6867 if (!foundAttributes
)
6869 foundAttributes
= true;
6870 attr
= paraDef
->GetStyleMergedWithBase(buffer
->GetStyleSheet());
6875 // Also apply list style if present
6876 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetListStyleName().IsEmpty() && buffer
->GetStyleSheet())
6878 wxRichTextListStyleDefinition
* listDef
= buffer
->GetStyleSheet()->FindListStyle(para
->GetAttributes().GetListStyleName());
6881 int thisIndent
= para
->GetAttributes().GetLeftIndent();
6882 int thisLevel
= para
->GetAttributes().HasOutlineLevel() ? para
->GetAttributes().GetOutlineLevel() : listDef
->FindLevelForIndent(thisIndent
);
6884 // Apply the overall list style, and item style for this level
6885 wxRichTextAttr
listStyle(listDef
->GetCombinedStyleForLevel(thisLevel
, buffer
->GetStyleSheet()));
6886 wxRichTextApplyStyle(attr
, listStyle
);
6887 attr
.SetOutlineLevel(thisLevel
);
6888 if (para
->GetAttributes().HasBulletNumber())
6889 attr
.SetBulletNumber(para
->GetAttributes().GetBulletNumber());
6893 if (!foundAttributes
)
6895 attr
= para
->GetAttributes();
6896 int flags
= attr
.GetFlags();
6898 // Eliminate character styles
6899 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
6900 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
6901 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
6902 attr
.SetFlags(flags
);
6908 return wxRichTextAttr();
6911 /// Submit command to delete this range
6912 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
6914 return ctrl
->GetFocusObject()->DeleteRangeWithUndo(range
, ctrl
, this);
6917 /// Submit command to delete this range
6918 bool wxRichTextParagraphLayoutBox::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
, wxRichTextBuffer
* buffer
)
6920 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, buffer
, this, ctrl
);
6922 action
->SetPosition(ctrl
->GetCaretPosition());
6924 // Set the range to delete
6925 action
->SetRange(range
);
6927 // Copy the fragment that we'll need to restore in Undo
6928 CopyFragment(range
, action
->GetOldParagraphs());
6930 // See if we're deleting a paragraph marker, in which case we need to
6931 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
6932 if (range
.GetStart() == range
.GetEnd())
6934 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
6935 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
6937 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
6938 if (nextPara
&& nextPara
!= para
)
6940 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
6941 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
6946 buffer
->SubmitAction(action
);
6951 /// Collapse undo/redo commands
6952 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
6954 if (m_batchedCommandDepth
== 0)
6956 wxASSERT(m_batchedCommand
== NULL
);
6957 if (m_batchedCommand
)
6959 GetCommandProcessor()->Store(m_batchedCommand
);
6961 m_batchedCommand
= new wxRichTextCommand(cmdName
);
6964 m_batchedCommandDepth
++;
6969 /// Collapse undo/redo commands
6970 bool wxRichTextBuffer::EndBatchUndo()
6972 m_batchedCommandDepth
--;
6974 wxASSERT(m_batchedCommandDepth
>= 0);
6975 wxASSERT(m_batchedCommand
!= NULL
);
6977 if (m_batchedCommandDepth
== 0)
6979 GetCommandProcessor()->Store(m_batchedCommand
);
6980 m_batchedCommand
= NULL
;
6986 /// Submit immediately, or delay according to whether collapsing is on
6987 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
6989 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
6991 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
6992 cmd
->AddAction(action
);
6994 cmd
->GetActions().Clear();
6997 m_batchedCommand
->AddAction(action
);
7001 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
7002 cmd
->AddAction(action
);
7004 // Only store it if we're not suppressing undo.
7005 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
7011 /// Begin suppressing undo/redo commands.
7012 bool wxRichTextBuffer::BeginSuppressUndo()
7019 /// End suppressing undo/redo commands.
7020 bool wxRichTextBuffer::EndSuppressUndo()
7027 /// Begin using a style
7028 bool wxRichTextBuffer::BeginStyle(const wxRichTextAttr
& style
)
7030 wxRichTextAttr
newStyle(GetDefaultStyle());
7032 // Save the old default style
7033 m_attributeStack
.Append((wxObject
*) new wxRichTextAttr(GetDefaultStyle()));
7035 wxRichTextApplyStyle(newStyle
, style
);
7036 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
7038 SetDefaultStyle(newStyle
);
7044 bool wxRichTextBuffer::EndStyle()
7046 if (!m_attributeStack
.GetFirst())
7048 wxLogDebug(_("Too many EndStyle calls!"));
7052 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
7053 wxRichTextAttr
* attr
= (wxRichTextAttr
*)node
->GetData();
7054 m_attributeStack
.Erase(node
);
7056 SetDefaultStyle(*attr
);
7063 bool wxRichTextBuffer::EndAllStyles()
7065 while (m_attributeStack
.GetCount() != 0)
7070 /// Clear the style stack
7071 void wxRichTextBuffer::ClearStyleStack()
7073 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
7074 delete (wxRichTextAttr
*) node
->GetData();
7075 m_attributeStack
.Clear();
7078 /// Begin using bold
7079 bool wxRichTextBuffer::BeginBold()
7081 wxRichTextAttr attr
;
7082 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
7084 return BeginStyle(attr
);
7087 /// Begin using italic
7088 bool wxRichTextBuffer::BeginItalic()
7090 wxRichTextAttr attr
;
7091 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
7093 return BeginStyle(attr
);
7096 /// Begin using underline
7097 bool wxRichTextBuffer::BeginUnderline()
7099 wxRichTextAttr attr
;
7100 attr
.SetFontUnderlined(true);
7102 return BeginStyle(attr
);
7105 /// Begin using point size
7106 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
7108 wxRichTextAttr attr
;
7109 attr
.SetFontSize(pointSize
);
7111 return BeginStyle(attr
);
7114 /// Begin using this font
7115 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
7117 wxRichTextAttr attr
;
7120 return BeginStyle(attr
);
7123 /// Begin using this colour
7124 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
7126 wxRichTextAttr attr
;
7127 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
7128 attr
.SetTextColour(colour
);
7130 return BeginStyle(attr
);
7133 /// Begin using alignment
7134 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
7136 wxRichTextAttr attr
;
7137 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
7138 attr
.SetAlignment(alignment
);
7140 return BeginStyle(attr
);
7143 /// Begin left indent
7144 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
7146 wxRichTextAttr attr
;
7147 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
7148 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
7150 return BeginStyle(attr
);
7153 /// Begin right indent
7154 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
7156 wxRichTextAttr attr
;
7157 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
7158 attr
.SetRightIndent(rightIndent
);
7160 return BeginStyle(attr
);
7163 /// Begin paragraph spacing
7164 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
7168 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
7170 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
7172 wxRichTextAttr attr
;
7173 attr
.SetFlags(flags
);
7174 attr
.SetParagraphSpacingBefore(before
);
7175 attr
.SetParagraphSpacingAfter(after
);
7177 return BeginStyle(attr
);
7180 /// Begin line spacing
7181 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
7183 wxRichTextAttr attr
;
7184 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
7185 attr
.SetLineSpacing(lineSpacing
);
7187 return BeginStyle(attr
);
7190 /// Begin numbered bullet
7191 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
7193 wxRichTextAttr attr
;
7194 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
7195 attr
.SetBulletStyle(bulletStyle
);
7196 attr
.SetBulletNumber(bulletNumber
);
7197 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
7199 return BeginStyle(attr
);
7202 /// Begin symbol bullet
7203 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
7205 wxRichTextAttr attr
;
7206 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
7207 attr
.SetBulletStyle(bulletStyle
);
7208 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
7209 attr
.SetBulletText(symbol
);
7211 return BeginStyle(attr
);
7214 /// Begin standard bullet
7215 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
7217 wxRichTextAttr attr
;
7218 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
7219 attr
.SetBulletStyle(bulletStyle
);
7220 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
7221 attr
.SetBulletName(bulletName
);
7223 return BeginStyle(attr
);
7226 /// Begin named character style
7227 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
7229 if (GetStyleSheet())
7231 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
7234 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
7235 return BeginStyle(attr
);
7241 /// Begin named paragraph style
7242 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
7244 if (GetStyleSheet())
7246 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
7249 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
7250 return BeginStyle(attr
);
7256 /// Begin named list style
7257 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
7259 if (GetStyleSheet())
7261 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
7264 wxRichTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
7266 attr
.SetBulletNumber(number
);
7268 return BeginStyle(attr
);
7275 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
7277 wxRichTextAttr attr
;
7279 if (!characterStyle
.IsEmpty() && GetStyleSheet())
7281 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
7284 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
7289 return BeginStyle(attr
);
7292 /// Adds a handler to the end
7293 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
7295 sm_handlers
.Append(handler
);
7298 /// Inserts a handler at the front
7299 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
7301 sm_handlers
.Insert( handler
);
7304 /// Removes a handler
7305 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
7307 wxRichTextFileHandler
*handler
= FindHandler(name
);
7310 sm_handlers
.DeleteObject(handler
);
7318 /// Finds a handler by filename or, if supplied, type
7319 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
7320 wxRichTextFileType imageType
)
7322 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
7323 return FindHandler(imageType
);
7324 else if (!filename
.IsEmpty())
7326 wxString path
, file
, ext
;
7327 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
7328 return FindHandler(ext
, imageType
);
7335 /// Finds a handler by name
7336 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
7338 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
7341 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
7342 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
7344 node
= node
->GetNext();
7349 /// Finds a handler by extension and type
7350 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
7352 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
7355 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
7356 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
7357 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
7359 node
= node
->GetNext();
7364 /// Finds a handler by type
7365 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
7367 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
7370 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
7371 if (handler
->GetType() == type
) return handler
;
7372 node
= node
->GetNext();
7377 void wxRichTextBuffer::InitStandardHandlers()
7379 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
7380 AddHandler(new wxRichTextPlainTextHandler
);
7383 void wxRichTextBuffer::CleanUpHandlers()
7385 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
7388 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
7389 wxList::compatibility_iterator next
= node
->GetNext();
7394 sm_handlers
.Clear();
7397 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
7404 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
7408 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
7409 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
7414 wildcard
+= wxT(";");
7415 wildcard
+= wxT("*.") + handler
->GetExtension();
7420 wildcard
+= wxT("|");
7421 wildcard
+= handler
->GetName();
7422 wildcard
+= wxT(" ");
7423 wildcard
+= _("files");
7424 wildcard
+= wxT(" (*.");
7425 wildcard
+= handler
->GetExtension();
7426 wildcard
+= wxT(")|*.");
7427 wildcard
+= handler
->GetExtension();
7429 types
->Add(handler
->GetType());
7434 node
= node
->GetNext();
7438 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
7443 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
7445 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
7448 SetDefaultStyle(wxRichTextAttr());
7449 handler
->SetFlags(GetHandlerFlags());
7450 bool success
= handler
->LoadFile(this, filename
);
7451 Invalidate(wxRICHTEXT_ALL
);
7459 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
7461 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
7464 handler
->SetFlags(GetHandlerFlags());
7465 return handler
->SaveFile(this, filename
);
7471 /// Load from a stream
7472 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
7474 wxRichTextFileHandler
* handler
= FindHandler(type
);
7477 SetDefaultStyle(wxRichTextAttr());
7478 handler
->SetFlags(GetHandlerFlags());
7479 bool success
= handler
->LoadFile(this, stream
);
7480 Invalidate(wxRICHTEXT_ALL
);
7487 /// Save to a stream
7488 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
7490 wxRichTextFileHandler
* handler
= FindHandler(type
);
7493 handler
->SetFlags(GetHandlerFlags());
7494 return handler
->SaveFile(this, stream
);
7500 /// Copy the range to the clipboard
7501 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
7503 bool success
= false;
7504 wxRichTextParagraphLayoutBox
* container
= this;
7505 if (GetRichTextCtrl())
7506 container
= GetRichTextCtrl()->GetFocusObject();
7508 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
7510 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
7512 wxTheClipboard
->Clear();
7514 // Add composite object
7516 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
7519 wxString text
= container
->GetTextForRange(range
);
7522 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
7525 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
7528 // Add rich text buffer data object. This needs the XML handler to be present.
7530 if (FindHandler(wxRICHTEXT_TYPE_XML
))
7532 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
7533 container
->CopyFragment(range
, *richTextBuf
);
7535 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
7538 if (wxTheClipboard
->SetData(compositeObject
))
7541 wxTheClipboard
->Close();
7550 /// Paste the clipboard content to the buffer
7551 bool wxRichTextBuffer::PasteFromClipboard(long position
)
7553 bool success
= false;
7554 wxRichTextParagraphLayoutBox
* container
= this;
7555 if (GetRichTextCtrl())
7556 container
= GetRichTextCtrl()->GetFocusObject();
7558 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
7559 if (CanPasteFromClipboard())
7561 if (wxTheClipboard
->Open())
7563 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
7565 wxRichTextBufferDataObject data
;
7566 wxTheClipboard
->GetData(data
);
7567 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
7570 container
->InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), this, 0);
7571 if (GetRichTextCtrl())
7572 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
7573 delete richTextBuffer
;
7576 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
)
7578 || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)
7582 wxTextDataObject data
;
7583 wxTheClipboard
->GetData(data
);
7584 wxString
text(data
.GetText());
7587 text2
.Alloc(text
.Length()+1);
7589 for (i
= 0; i
< text
.Length(); i
++)
7591 wxChar ch
= text
[i
];
7592 if (ch
!= wxT('\r'))
7596 wxString text2
= text
;
7598 container
->InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
7600 if (GetRichTextCtrl())
7601 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
7605 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
7607 wxBitmapDataObject data
;
7608 wxTheClipboard
->GetData(data
);
7609 wxBitmap
bitmap(data
.GetBitmap());
7610 wxImage
image(bitmap
.ConvertToImage());
7612 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, container
, GetRichTextCtrl(), false);
7614 action
->GetNewParagraphs().AddImage(image
);
7616 if (action
->GetNewParagraphs().GetChildCount() == 1)
7617 action
->GetNewParagraphs().SetPartialParagraph(true);
7619 action
->SetPosition(position
+1);
7621 // Set the range we'll need to delete in Undo
7622 action
->SetRange(wxRichTextRange(position
+1, position
+1));
7624 SubmitAction(action
);
7628 wxTheClipboard
->Close();
7632 wxUnusedVar(position
);
7637 /// Can we paste from the clipboard?
7638 bool wxRichTextBuffer::CanPasteFromClipboard() const
7640 bool canPaste
= false;
7641 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
7642 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
7644 if (wxTheClipboard
->IsSupported(wxDF_TEXT
)
7646 || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)
7648 || wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
7649 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
7653 wxTheClipboard
->Close();
7659 /// Dumps contents of buffer for debugging purposes
7660 void wxRichTextBuffer::Dump()
7664 wxStringOutputStream
stream(& text
);
7665 wxTextOutputStream
textStream(stream
);
7672 /// Add an event handler
7673 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
7675 m_eventHandlers
.Append(handler
);
7679 /// Remove an event handler
7680 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
7682 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
7685 m_eventHandlers
.Erase(node
);
7695 /// Clear event handlers
7696 void wxRichTextBuffer::ClearEventHandlers()
7698 m_eventHandlers
.Clear();
7701 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
7702 /// otherwise will stop at the first successful one.
7703 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
7705 bool success
= false;
7706 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
7708 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
7709 if (handler
->ProcessEvent(event
))
7719 /// Set style sheet and notify of the change
7720 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
7722 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
7724 wxWindowID id
= wxID_ANY
;
7725 if (GetRichTextCtrl())
7726 id
= GetRichTextCtrl()->GetId();
7728 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
7729 event
.SetEventObject(GetRichTextCtrl());
7730 event
.SetContainer(GetRichTextCtrl()->GetFocusObject());
7731 event
.SetOldStyleSheet(oldSheet
);
7732 event
.SetNewStyleSheet(sheet
);
7735 if (SendEvent(event
) && !event
.IsAllowed())
7737 if (sheet
!= oldSheet
)
7743 if (oldSheet
&& oldSheet
!= sheet
)
7746 SetStyleSheet(sheet
);
7748 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
7749 event
.SetOldStyleSheet(NULL
);
7752 return SendEvent(event
);
7755 /// Set renderer, deleting old one
7756 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
7760 sm_renderer
= renderer
;
7763 /// Hit-testing: returns a flag indicating hit test details, plus
7764 /// information about position
7765 int wxRichTextBuffer::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
, wxRichTextObject
** obj
, wxRichTextObject
** contextObj
, int flags
)
7767 int ret
= wxRichTextParagraphLayoutBox::HitTest(dc
, pt
, textPosition
, obj
, contextObj
, flags
);
7768 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
7774 textPosition
= m_ownRange
.GetEnd()-1;
7777 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
7781 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& bulletAttr
, const wxRect
& rect
)
7783 if (bulletAttr
.GetTextColour().Ok())
7785 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
7786 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
7790 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7791 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7795 if (bulletAttr
.HasFont())
7797 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
7800 font
= (*wxNORMAL_FONT
);
7802 wxCheckSetFont(dc
, font
);
7804 int charHeight
= dc
.GetCharHeight();
7806 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
7807 int bulletHeight
= bulletWidth
;
7811 // Calculate the top position of the character (as opposed to the whole line height)
7812 int y
= rect
.y
+ (rect
.height
- charHeight
);
7814 // Calculate where the bullet should be positioned
7815 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
7817 // The margin between a bullet and text.
7818 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
7820 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
7821 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
7822 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
7823 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
7825 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
7827 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
7829 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
7832 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
7833 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
7834 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
7835 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
7837 dc
.DrawPolygon(4, pts
);
7839 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
7842 pts
[0].x
= x
; pts
[0].y
= y
;
7843 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
7844 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
7846 dc
.DrawPolygon(3, pts
);
7848 else if (bulletAttr
.GetBulletName() == wxT("standard/circle-outline"))
7850 wxCheckSetBrush(dc
, *wxTRANSPARENT_BRUSH
);
7851 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
7853 else // "standard/circle", and catch-all
7855 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
7861 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
7866 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
7868 wxRichTextAttr fontAttr
;
7869 fontAttr
.SetFontSize(attr
.GetFontSize());
7870 fontAttr
.SetFontStyle(attr
.GetFontStyle());
7871 fontAttr
.SetFontWeight(attr
.GetFontWeight());
7872 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7873 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
7874 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
7876 else if (attr
.HasFont())
7877 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
7879 font
= (*wxNORMAL_FONT
);
7881 wxCheckSetFont(dc
, font
);
7883 if (attr
.GetTextColour().Ok())
7884 dc
.SetTextForeground(attr
.GetTextColour());
7886 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
7888 int charHeight
= dc
.GetCharHeight();
7890 dc
.GetTextExtent(text
, & tw
, & th
);
7894 // Calculate the top position of the character (as opposed to the whole line height)
7895 int y
= rect
.y
+ (rect
.height
- charHeight
);
7897 // The margin between a bullet and text.
7898 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
7900 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
7901 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
7902 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
7903 x
= x
+ (rect
.width
)/2 - tw
/2;
7905 dc
.DrawText(text
, x
, y
);
7913 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxRichTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
7915 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
7916 // with the buffer. The store will allow retrieval from memory, disk or other means.
7920 /// Enumerate the standard bullet names currently supported
7921 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
7923 bulletNames
.Add(wxTRANSLATE("standard/circle"));
7924 bulletNames
.Add(wxTRANSLATE("standard/circle-outline"));
7925 bulletNames
.Add(wxTRANSLATE("standard/square"));
7926 bulletNames
.Add(wxTRANSLATE("standard/diamond"));
7927 bulletNames
.Add(wxTRANSLATE("standard/triangle"));
7936 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextParagraphLayoutBox
)
7938 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
7939 wxRichTextParagraphLayoutBox(parent
)
7944 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
7949 // TODO: if the active object in the control, draw an indication.
7950 // We need to add the concept of active object, and not just focus object,
7951 // so we can apply commands (properties, delete, ...) to objects such as text boxes and images.
7952 // Ultimately we would like to be able to interactively resize an active object
7953 // using drag handles.
7954 return wxRichTextParagraphLayoutBox::Draw(dc
, range
, selection
, rect
, descent
, style
);
7958 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
7960 wxRichTextParagraphLayoutBox::Copy(obj
);
7963 // Edit properties via a GUI
7964 bool wxRichTextBox::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
7966 wxRichTextObjectPropertiesDialog
boxDlg(this, wxGetTopLevelParent(parent
), wxID_ANY
, _("Box Properties"));
7967 boxDlg
.SetAttributes(GetAttributes());
7969 if (boxDlg
.ShowModal() == wxID_OK
)
7971 // By passing wxRICHTEXT_SETSTYLE_RESET, indeterminate attributes set by the user will be set as
7972 // indeterminate in the object.
7973 boxDlg
.ApplyStyle(buffer
->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RESET
);
7980 IMPLEMENT_DYNAMIC_CLASS(wxRichTextCell
, wxRichTextBox
)
7982 wxRichTextCell::wxRichTextCell(wxRichTextObject
* parent
):
7983 wxRichTextBox(parent
)
7988 bool wxRichTextCell::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
7990 return wxRichTextBox::Draw(dc
, range
, selection
, rect
, descent
, style
);
7994 void wxRichTextCell::Copy(const wxRichTextCell
& obj
)
7996 wxRichTextBox::Copy(obj
);
7999 // Edit properties via a GUI
8000 bool wxRichTextCell::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
8002 // We need to gather common attributes for all selected cells.
8004 wxRichTextTable
* table
= wxDynamicCast(GetParent(), wxRichTextTable
);
8005 bool multipleCells
= false;
8006 wxRichTextAttr attr
;
8008 if (table
&& buffer
&& buffer
->GetRichTextCtrl() && buffer
->GetRichTextCtrl()->GetSelection().IsValid() &&
8009 buffer
->GetRichTextCtrl()->GetSelection().GetContainer() == GetParent())
8011 wxRichTextAttr clashingAttr
, absentAttr
;
8012 const wxRichTextSelection
& sel
= buffer
->GetRichTextCtrl()->GetSelection();
8014 int selectedCellCount
= 0;
8015 for (i
= 0; i
< sel
.GetCount(); i
++)
8017 const wxRichTextRange
& range
= sel
[i
];
8018 wxRichTextCell
* cell
= table
->GetCell(range
.GetStart());
8021 wxRichTextAttr cellStyle
= cell
->GetAttributes();
8023 CollectStyle(attr
, cellStyle
, clashingAttr
, absentAttr
);
8025 selectedCellCount
++;
8028 multipleCells
= selectedCellCount
> 1;
8032 attr
= GetAttributes();
8037 caption
= _("Multiple Cell Properties");
8039 caption
= _("Cell Properties");
8041 wxRichTextObjectPropertiesDialog
cellDlg(this, wxGetTopLevelParent(parent
), wxID_ANY
, caption
);
8042 cellDlg
.SetAttributes(attr
);
8044 wxRichTextSizePage
* sizePage
= wxDynamicCast(cellDlg
.FindPage(CLASSINFO(wxRichTextSizePage
)), wxRichTextSizePage
);
8047 // We don't want position and floating controls for a cell.
8048 sizePage
->ShowPositionControls(false);
8049 sizePage
->ShowFloatingControls(false);
8052 if (cellDlg
.ShowModal() == wxID_OK
)
8056 const wxRichTextSelection
& sel
= buffer
->GetRichTextCtrl()->GetSelection();
8057 // Apply the style; we interpret indeterminate attributes as 'don't touch this attribute'
8058 // since it may represent clashing attributes across multiple objects.
8059 table
->SetCellStyle(sel
, attr
);
8062 // For a single object, indeterminate attributes set by the user should be reflected in the
8063 // actual object style, so pass the wxRICHTEXT_SETSTYLE_RESET flag to assign
8064 // the style directly instead of applying (which ignores indeterminate attributes,
8065 // leaving them as they were).
8066 cellDlg
.ApplyStyle(buffer
->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RESET
);
8073 WX_DEFINE_OBJARRAY(wxRichTextObjectPtrArrayArray
)
8075 IMPLEMENT_DYNAMIC_CLASS(wxRichTextTable
, wxRichTextBox
)
8077 wxRichTextTable::wxRichTextTable(wxRichTextObject
* parent
): wxRichTextBox(parent
)
8083 // Draws the object.
8084 bool wxRichTextTable::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int descent
, int style
)
8086 return wxRichTextBox::Draw(dc
, range
, selection
, rect
, descent
, style
);
8089 WX_DECLARE_OBJARRAY(wxRect
, wxRichTextRectArray
);
8090 WX_DEFINE_OBJARRAY(wxRichTextRectArray
);
8092 // Lays the object out. rect is the space available for layout. Often it will
8093 // be the specified overall space for this object, if trying to constrain
8094 // layout to a particular size, or it could be the total space available in the
8095 // parent. rect is the overall size, so we must subtract margins and padding.
8096 // to get the actual available space.
8097 bool wxRichTextTable::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
8099 SetPosition(rect
.GetPosition());
8101 // TODO: the meaty bit. Calculate sizes of all cells and rows. Try to use
8102 // minimum size if within alloted size, then divide up remaining size
8103 // between rows/cols.
8106 wxRichTextBuffer
* buffer
= GetBuffer();
8107 if (buffer
) scale
= buffer
->GetScale();
8109 wxRect availableSpace
= GetAvailableContentArea(dc
, rect
);
8110 wxTextAttrDimensionConverter
converter(dc
, scale
, availableSpace
.GetSize());
8112 // If we have no fixed table size, and assuming we're not pushed for
8113 // space, then we don't have to try to stretch the table to fit the contents.
8114 bool stretchToFitTableWidth
= false;
8116 int tableWidth
= rect
.width
;
8117 if (GetAttributes().GetTextBoxAttr().GetWidth().IsValid())
8119 tableWidth
= converter
.GetPixels(GetAttributes().GetTextBoxAttr().GetWidth());
8121 // Fixed table width, so we do want to stretch columns out if necessary.
8122 stretchToFitTableWidth
= true;
8124 // Shouldn't be able to exceed the size passed to this function
8125 tableWidth
= wxMin(rect
.width
, tableWidth
);
8128 // Get internal padding
8129 int paddingLeft
= 0, paddingRight
= 0, paddingTop
= 0, paddingBottom
= 0;
8130 if (GetAttributes().GetTextBoxAttr().GetPadding().GetLeft().IsValid())
8131 paddingLeft
= converter
.GetPixels(GetAttributes().GetTextBoxAttr().GetPadding().GetLeft());
8132 if (GetAttributes().GetTextBoxAttr().GetPadding().GetRight().IsValid())
8133 paddingRight
= converter
.GetPixels(GetAttributes().GetTextBoxAttr().GetPadding().GetRight());
8134 if (GetAttributes().GetTextBoxAttr().GetPadding().GetTop().IsValid())
8135 paddingTop
= converter
.GetPixels(GetAttributes().GetTextBoxAttr().GetPadding().GetTop());
8136 if (GetAttributes().GetTextBoxAttr().GetPadding().GetLeft().IsValid())
8137 paddingBottom
= converter
.GetPixels(GetAttributes().GetTextBoxAttr().GetPadding().GetBottom());
8139 // Assume that left and top padding are also used for inter-cell padding.
8140 int paddingX
= paddingLeft
;
8141 int paddingY
= paddingTop
;
8143 int totalLeftMargin
= 0, totalRightMargin
= 0, totalTopMargin
= 0, totalBottomMargin
= 0;
8144 GetTotalMargin(dc
, buffer
, GetAttributes(), totalLeftMargin
, totalRightMargin
, totalTopMargin
, totalBottomMargin
);
8146 // Internal table width - the area for content
8147 int internalTableWidth
= tableWidth
- totalLeftMargin
- totalRightMargin
;
8149 int rowCount
= m_cells
.GetCount();
8150 if (m_colCount
== 0 || rowCount
== 0)
8152 wxRect
overallRect(rect
.x
, rect
.y
, totalLeftMargin
+ totalRightMargin
, totalTopMargin
+ totalBottomMargin
);
8153 SetCachedSize(overallRect
.GetSize());
8155 // Zero content size
8156 SetMinSize(overallRect
.GetSize());
8157 SetMaxSize(GetMinSize());
8161 // The final calculated widths
8162 wxArrayInt
colWidths(m_colCount
);
8164 wxArrayInt
absoluteColWidths(m_colCount
);
8165 // wxArrayInt absoluteColWidthsSpanning(m_colCount);
8166 wxArrayInt
percentageColWidths(m_colCount
);
8167 // wxArrayInt percentageColWidthsSpanning(m_colCount);
8168 // These are only relevant when the first column contains spanning information.
8169 // wxArrayInt columnSpans(m_colCount); // Each contains 1 for non-spanning cell, > 1 for spanning cell.
8170 wxArrayInt
maxColWidths(m_colCount
);
8171 wxArrayInt
minColWidths(m_colCount
);
8173 wxSize
tableSize(tableWidth
, 0);
8177 for (i
= 0; i
< m_colCount
; i
++)
8179 absoluteColWidths
[i
] = 0;
8180 // absoluteColWidthsSpanning[i] = 0;
8181 percentageColWidths
[i
] = -1;
8182 // percentageColWidthsSpanning[i] = -1;
8184 maxColWidths
[i
] = 0;
8185 minColWidths
[i
] = 0;
8186 // columnSpans[i] = 1;
8189 // (0) Determine which cells are visible according to spans
8191 // __________________
8196 // |------------------|
8197 // |__________________| 4
8199 // To calculate cell visibility:
8200 // First find all spanning cells. Build an array of span records with start x, y and end x, y.
8201 // Then for each cell, test whether we're within one of those cells, and unless we're at the start of
8202 // that cell, hide the cell.
8204 // We can also use this array to match the size of spanning cells to the grid. Or just do
8205 // this when we iterate through all cells.
8207 // 0.1: add spanning cells to an array
8208 wxRichTextRectArray rectArray
;
8209 for (j
= 0; j
< m_rowCount
; j
++)
8211 for (i
= 0; i
< m_colCount
; i
++)
8213 wxRichTextBox
* cell
= GetCell(j
, i
);
8214 int colSpan
= 1, rowSpan
= 1;
8215 if (cell
->GetProperties().HasProperty(wxT("colspan")))
8216 colSpan
= cell
->GetProperties().GetPropertyLong(wxT("colspan"));
8217 if (cell
->GetProperties().HasProperty(wxT("rowspan")))
8218 rowSpan
= cell
->GetProperties().GetPropertyLong(wxT("rowspan"));
8219 if (colSpan
> 1 || rowSpan
> 1)
8221 rectArray
.Add(wxRect(i
, j
, colSpan
, rowSpan
));
8225 // 0.2: find which cells are subsumed by a spanning cell
8226 for (j
= 0; j
< m_rowCount
; j
++)
8228 for (i
= 0; i
< m_colCount
; i
++)
8230 wxRichTextBox
* cell
= GetCell(j
, i
);
8231 if (rectArray
.GetCount() == 0)
8237 int colSpan
= 1, rowSpan
= 1;
8238 if (cell
->GetProperties().HasProperty(wxT("colspan")))
8239 colSpan
= cell
->GetProperties().GetPropertyLong(wxT("colspan"));
8240 if (cell
->GetProperties().HasProperty(wxT("rowspan")))
8241 rowSpan
= cell
->GetProperties().GetPropertyLong(wxT("rowspan"));
8242 if (colSpan
> 1 || rowSpan
> 1)
8244 // Assume all spanning cells are shown
8250 for (k
= 0; k
< (int) rectArray
.GetCount(); k
++)
8252 if (rectArray
[k
].Contains(wxPoint(i
, j
)))
8264 // TODO: find the first spanned cell in each row that spans the most columns and doesn't
8265 // overlap with a spanned cell starting at a previous column position.
8266 // This means we need to keep an array of rects so we can check. However
8267 // it does also mean that some spans simply may not be taken into account
8268 // where there are different spans happening on different rows. In these cases,
8269 // they will simply be as wide as their constituent columns.
8271 // (1) Do an initial layout for all cells to get minimum and maximum size, and get
8272 // the absolute or percentage width of each column.
8274 for (j
= 0; j
< m_rowCount
; j
++)
8276 // First get the overall margins so we can calculate percentage widths based on
8277 // the available content space for all cells on the row
8279 int overallRowContentMargin
= 0;
8280 int visibleCellCount
= 0;
8282 for (i
= 0; i
< m_colCount
; i
++)
8284 wxRichTextBox
* cell
= GetCell(j
, i
);
8285 if (cell
->IsShown())
8287 int cellTotalLeftMargin
= 0, cellTotalRightMargin
= 0, cellTotalTopMargin
= 0, cellTotalBottomMargin
= 0;
8288 GetTotalMargin(dc
, buffer
, cell
->GetAttributes(), cellTotalLeftMargin
, cellTotalRightMargin
, cellTotalTopMargin
, cellTotalBottomMargin
);
8290 overallRowContentMargin
+= (cellTotalLeftMargin
+ cellTotalRightMargin
);
8291 visibleCellCount
++;
8295 // Add in inter-cell padding
8296 overallRowContentMargin
+= ((visibleCellCount
-1) * paddingX
);
8298 int rowContentWidth
= internalTableWidth
- overallRowContentMargin
;
8299 wxSize
rowTableSize(rowContentWidth
, 0);
8300 wxTextAttrDimensionConverter
converter(dc
, scale
, rowTableSize
);
8302 for (i
= 0; i
< m_colCount
; i
++)
8304 wxRichTextBox
* cell
= GetCell(j
, i
);
8305 if (cell
->IsShown())
8308 if (cell
->GetProperties().HasProperty(wxT("colspan")))
8309 colSpan
= cell
->GetProperties().GetPropertyLong(wxT("colspan"));
8311 // Lay out cell to find min/max widths
8312 cell
->Invalidate(wxRICHTEXT_ALL
);
8313 cell
->Layout(dc
, availableSpace
, style
);
8317 int absoluteCellWidth
= -1;
8318 int percentageCellWidth
= -1;
8320 // I think we need to calculate percentages from the internal table size,
8321 // minus the padding between cells which we'll need to calculate from the
8322 // (number of VISIBLE cells - 1)*paddingX. Then percentages that add up to 100%
8323 // will add up to 100%. In CSS, the width specifies the cell's content rect width,
8324 // so if we want to conform to that we'll need to add in the overall cell margins.
8325 // However, this will make it difficult to specify percentages that add up to
8326 // 100% and still fit within the table width.
8327 // Let's say two cells have 50% width. They have 10 pixels of overall margin each.
8328 // The table content rect is 500 pixels and the inter-cell padding is 20 pixels.
8329 // If we're using internal content size for the width, we would calculate the
8330 // the overall cell width for n cells as:
8331 // (500 - 20*(n-1) - overallCellMargin1 - overallCellMargin2 - ...) * percentage / 100
8332 // + thisOverallCellMargin
8333 // = 500 - 20 - 10 - 10) * 0.5 + 10 = 240 pixels overall cell width.
8334 // Adding this back, we get 240 + 240 + 20 = 500 pixels.
8336 if (cell
->GetAttributes().GetTextBoxAttr().GetWidth().IsValid())
8338 int w
= converter
.GetPixels(cell
->GetAttributes().GetTextBoxAttr().GetWidth());
8339 if (cell
->GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE
)
8341 percentageCellWidth
= w
;
8345 absoluteCellWidth
= w
;
8347 // Override absolute width with minimum width if necessary
8348 if (cell
->GetMinSize().x
> 0 && absoluteCellWidth
!=1 && cell
->GetMinSize().x
> absoluteCellWidth
)
8349 absoluteCellWidth
= cell
->GetMinSize().x
;
8352 if (absoluteCellWidth
!= -1)
8354 if (absoluteCellWidth
> absoluteColWidths
[i
])
8355 absoluteColWidths
[i
] = absoluteCellWidth
;
8358 if (percentageCellWidth
!= -1)
8360 if (percentageCellWidth
> percentageColWidths
[i
])
8361 percentageColWidths
[i
] = percentageCellWidth
;
8364 if (colSpan
== 1 && cell
->GetMinSize().x
&& cell
->GetMinSize().x
> minColWidths
[i
])
8365 minColWidths
[i
] = cell
->GetMinSize().x
;
8366 if (colSpan
== 1 && cell
->GetMaxSize().x
&& cell
->GetMaxSize().x
> maxColWidths
[i
])
8367 maxColWidths
[i
] = cell
->GetMaxSize().x
;
8373 // (2) Allocate initial column widths from minimum widths, absolute values and proportions
8374 // TODO: simply merge this into (1).
8375 for (i
= 0; i
< m_colCount
; i
++)
8377 if (absoluteColWidths
[i
] > 0)
8379 colWidths
[i
] = absoluteColWidths
[i
];
8381 else if (percentageColWidths
[i
] > 0)
8383 colWidths
[i
] = percentageColWidths
[i
];
8385 // This is rubbish - we calculated the absolute widths from percentages, so
8386 // we can't do it again here.
8387 //colWidths[i] = (int) (double(percentageColWidths[i]) * double(tableWidth) / 100.0 + 0.5);
8391 // (3) Process absolute or proportional widths of spanning columns,
8392 // now that we know what our fixed column widths are going to be.
8393 // Spanned cells will try to adjust columns so the span will fit.
8394 // Even existing fixed column widths can be expanded if necessary.
8395 // Actually, currently fixed columns widths aren't adjusted; instead,
8396 // the algorithm favours earlier rows and adjusts unspecified column widths
8397 // the first time only. After that, we can't know whether the column has been
8398 // specified explicitly or not. (We could make a note if necessary.)
8399 for (j
= 0; j
< m_rowCount
; j
++)
8401 // First get the overall margins so we can calculate percentage widths based on
8402 // the available content space for all cells on the row
8404 int overallRowContentMargin
= 0;
8405 int visibleCellCount
= 0;
8407 for (i
= 0; i
< m_colCount
; i
++)
8409 wxRichTextBox
* cell
= GetCell(j
, i
);
8410 if (cell
->IsShown())
8412 int cellTotalLeftMargin
= 0, cellTotalRightMargin
= 0, cellTotalTopMargin
= 0, cellTotalBottomMargin
= 0;
8413 GetTotalMargin(dc
, buffer
, cell
->GetAttributes(), cellTotalLeftMargin
, cellTotalRightMargin
, cellTotalTopMargin
, cellTotalBottomMargin
);
8415 overallRowContentMargin
+= (cellTotalLeftMargin
+ cellTotalRightMargin
);
8416 visibleCellCount
++;
8420 // Add in inter-cell padding
8421 overallRowContentMargin
+= ((visibleCellCount
-1) * paddingX
);
8423 int rowContentWidth
= internalTableWidth
- overallRowContentMargin
;
8424 wxSize
rowTableSize(rowContentWidth
, 0);
8425 wxTextAttrDimensionConverter
converter(dc
, scale
, rowTableSize
);
8427 for (i
= 0; i
< m_colCount
; i
++)
8429 wxRichTextBox
* cell
= GetCell(j
, i
);
8430 if (cell
->IsShown())
8433 if (cell
->GetProperties().HasProperty(wxT("colspan")))
8434 colSpan
= cell
->GetProperties().GetPropertyLong(wxT("colspan"));
8438 int spans
= wxMin(colSpan
, m_colCount
- i
);
8442 if (cell
->GetAttributes().GetTextBoxAttr().GetWidth().IsValid())
8444 cellWidth
= converter
.GetPixels(cell
->GetAttributes().GetTextBoxAttr().GetWidth());
8445 // Override absolute width with minimum width if necessary
8446 if (cell
->GetMinSize().x
> 0 && cellWidth
!=1 && cell
->GetMinSize().x
> cellWidth
)
8447 cellWidth
= cell
->GetMinSize().x
;
8451 // Do we want to do this? It's the only chance we get to
8452 // use the cell's min/max sizes, so we need to work out
8453 // how we're going to balance the unspecified spanning cell
8454 // width with the possibility more-constrained constituent cell widths.
8455 // Say there's a tiny bitmap giving it a max width of 10 pixels. We
8456 // don't want to constraint all the spanned columns to fit into this cell.
8457 // OK, let's say that if any of the constituent columns don't fit,
8458 // then we simply stop constraining the columns; instead, we'll just fit the spanning
8459 // cells to the columns later.
8460 cellWidth
= cell
->GetMinSize().x
;
8461 if (cell
->GetMaxSize().x
> cellWidth
)
8462 cellWidth
= cell
->GetMaxSize().x
;
8465 // Subtract the padding between cells
8466 int spanningWidth
= cellWidth
;
8467 spanningWidth
-= paddingX
* (spans
-1);
8469 if (spanningWidth
> 0)
8471 // Now share the spanning width between columns within that span
8472 // TODO: take into account min widths of columns within the span
8473 int spanningWidthLeft
= spanningWidth
;
8474 int stretchColCount
= 0;
8475 for (k
= i
; k
< (i
+spans
); k
++)
8477 if (colWidths
[k
] > 0) // absolute or proportional width has been specified
8478 spanningWidthLeft
-= colWidths
[k
];
8482 // Now divide what's left between the remaining columns
8484 if (stretchColCount
> 0)
8485 colShare
= spanningWidthLeft
/ stretchColCount
;
8486 int colShareRemainder
= spanningWidthLeft
- (colShare
* stretchColCount
);
8488 // If fixed-width columns are currently too big, then we'll later
8489 // stretch the spanned cell to fit.
8491 if (spanningWidthLeft
> 0)
8493 for (k
= i
; k
< (i
+spans
); k
++)
8495 if (colWidths
[k
] <= 0) // absolute or proportional width has not been specified
8497 int newWidth
= colShare
;
8498 if (k
== (i
+spans
-1))
8499 newWidth
+= colShareRemainder
; // ensure all pixels are filled
8500 colWidths
[k
] = newWidth
;
8511 // (4) Next, share any remaining space out between columns that have not yet been calculated.
8512 // TODO: take into account min widths of columns within the span
8513 int tableWidthMinusPadding
= internalTableWidth
- (m_colCount
-1)*paddingX
;
8514 int widthLeft
= tableWidthMinusPadding
;
8515 int stretchColCount
= 0;
8516 for (i
= 0; i
< m_colCount
; i
++)
8518 // TODO: we need to take into account min widths.
8519 // Subtract min width from width left, then
8520 // add the colShare to the min width
8521 if (colWidths
[i
] > 0) // absolute or proportional width has been specified
8522 widthLeft
-= colWidths
[i
];
8525 if (minColWidths
[i
] > 0)
8526 widthLeft
-= minColWidths
[i
];
8532 // Now divide what's left between the remaining columns
8534 if (stretchColCount
> 0)
8535 colShare
= widthLeft
/ stretchColCount
;
8536 int colShareRemainder
= widthLeft
- (colShare
* stretchColCount
);
8538 // Check we don't have enough space, in which case shrink all columns, overriding
8539 // any absolute/proportional widths
8540 // TODO: actually we would like to divide up the shrinkage according to size.
8541 // How do we calculate the proportions that will achieve this?
8542 // Could first choose an arbitrary value for stretching cells, and then calculate
8543 // factors to multiply each width by.
8544 // TODO: want to record this fact and pass to an iteration that tries e.g. min widths
8545 if (widthLeft
< 0 || (stretchToFitTableWidth
&& (stretchColCount
== 0)))
8547 colShare
= tableWidthMinusPadding
/ m_colCount
;
8548 colShareRemainder
= tableWidthMinusPadding
- (colShare
* m_colCount
);
8549 for (i
= 0; i
< m_colCount
; i
++)
8552 minColWidths
[i
] = 0;
8556 // We have to adjust the columns if either we need to shrink the
8557 // table to fit the parent/table width, or we explicitly set the
8558 // table width and need to stretch out the table.
8559 if (widthLeft
< 0 || stretchToFitTableWidth
)
8561 for (i
= 0; i
< m_colCount
; i
++)
8563 if (colWidths
[i
] <= 0) // absolute or proportional width has not been specified
8565 if (minColWidths
[i
] > 0)
8566 colWidths
[i
] = minColWidths
[i
] + colShare
;
8568 colWidths
[i
] = colShare
;
8569 if (i
== (m_colCount
-1))
8570 colWidths
[i
] += colShareRemainder
; // ensure all pixels are filled
8575 // TODO: if spanned cells have no specified or max width, make them the
8576 // as big as the columns they span. Do this for all spanned cells in all
8577 // rows, of course. Size any spanned cells left over at the end - even if they
8578 // have width > 0, make sure they're limited to the appropriate column edge.
8582 Sort out confusion between content width
8583 and overall width later. For now, assume we specify overall width.
8585 So, now we've laid out the table to fit into the given space
8586 and have used specified widths and minimum widths.
8588 Now we need to consider how we will try to take maximum width into account.
8592 // (??) TODO: take max width into account
8594 // (6) Lay out all cells again with the current values
8597 int y
= availableSpace
.y
;
8598 for (j
= 0; j
< m_rowCount
; j
++)
8600 int x
= availableSpace
.x
; // TODO: take into account centering etc.
8601 int maxCellHeight
= 0;
8602 int maxSpecifiedCellHeight
= 0;
8604 wxArrayInt
actualWidths(m_colCount
);
8606 wxTextAttrDimensionConverter
converter(dc
, scale
);
8607 for (i
= 0; i
< m_colCount
; i
++)
8609 wxRichTextCell
* cell
= GetCell(j
, i
);
8610 if (cell
->IsShown())
8612 // Get max specified cell height
8613 // Don't handle percentages for height
8614 if (cell
->GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && cell
->GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() != wxTEXT_ATTR_UNITS_PERCENTAGE
)
8616 int h
= converter
.GetPixels(cell
->GetAttributes().GetTextBoxAttr().GetHeight());
8617 if (h
> maxSpecifiedCellHeight
)
8618 maxSpecifiedCellHeight
= h
;
8621 if (colWidths
[i
] > 0) // absolute or proportional width has been specified
8624 if (cell
->GetProperties().HasProperty(wxT("colspan")))
8625 colSpan
= cell
->GetProperties().GetPropertyLong(wxT("colspan"));
8627 wxRect availableCellSpace
;
8629 // TODO: take into acount spans
8632 // Calculate the size of this spanning cell from its constituent columns
8634 int spans
= wxMin(colSpan
, m_colCount
- i
);
8635 for (k
= i
; k
< spans
; k
++)
8641 availableCellSpace
= wxRect(x
, y
, xx
, -1);
8644 availableCellSpace
= wxRect(x
, y
, colWidths
[i
], -1);
8646 // Store actual width so we can force cell to be the appropriate width on the final loop
8647 actualWidths
[i
] = availableCellSpace
.GetWidth();
8650 cell
->Invalidate(wxRICHTEXT_ALL
);
8651 cell
->Layout(dc
, availableCellSpace
, style
);
8653 // TODO: use GetCachedSize().x to compute 'natural' size
8655 x
+= (availableCellSpace
.GetWidth() + paddingX
);
8656 if (cell
->GetCachedSize().y
> maxCellHeight
)
8657 maxCellHeight
= cell
->GetCachedSize().y
;
8662 maxCellHeight
= wxMax(maxCellHeight
, maxSpecifiedCellHeight
);
8664 for (i
= 0; i
< m_colCount
; i
++)
8666 wxRichTextCell
* cell
= GetCell(j
, i
);
8667 if (cell
->IsShown())
8669 wxRect availableCellSpace
= wxRect(cell
->GetPosition(), wxSize(actualWidths
[i
], maxCellHeight
));
8670 // Lay out cell with new height
8671 cell
->Invalidate(wxRICHTEXT_ALL
);
8672 cell
->Layout(dc
, availableCellSpace
, style
);
8674 // Make sure the cell size really is the appropriate size,
8675 // not the calculated box size
8676 cell
->SetCachedSize(wxSize(actualWidths
[i
], maxCellHeight
));
8678 maxRight
= wxMax(maxRight
, cell
->GetPosition().x
+ cell
->GetCachedSize().x
);
8683 if (j
< (m_rowCount
-1))
8687 // We need to add back the margins etc.
8689 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
8690 contentRect
= wxRect(wxPoint(0, 0), wxSize(maxRight
- availableSpace
.x
, y
- availableSpace
.y
));
8691 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
8692 SetCachedSize(marginRect
.GetSize());
8695 // TODO: calculate max size
8697 SetMaxSize(GetCachedSize());
8700 // TODO: calculate min size
8702 SetMinSize(GetCachedSize());
8705 // TODO: currently we use either a fixed table width or the parent's size.
8706 // We also want to be able to calculate the table width from its content,
8707 // whether using fixed column widths or cell content min/max width.
8708 // Probably need a boolean flag to say whether we need to stretch cells
8709 // to fit the table width, or to simply use min/max cell widths. The
8710 // trouble with this is that if cell widths are not specified, they
8711 // will be tiny; we could use arbitrary defaults but this seems unsatisfactory.
8712 // Anyway, ignoring that problem, we probably need to factor layout into a function
8713 // that can can calculate the maximum unconstrained layout in case table size is
8714 // not specified. Then LayoutToBestSize() can choose to use either parent size to
8715 // constrain Layout(), or the previously-calculated max size to constraint layout.
8720 // Finds the absolute position and row height for the given character position
8721 bool wxRichTextTable::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
8723 wxRichTextCell
* child
= GetCell(index
+1);
8726 // Find the position at the start of the child cell, since the table doesn't
8727 // have any caret position of its own.
8728 return child
->FindPosition(dc
, -1, pt
, height
, forceLineStart
);
8734 // Get the cell at the given character position (in the range of the table).
8735 wxRichTextCell
* wxRichTextTable::GetCell(long pos
) const
8737 int row
= 0, col
= 0;
8738 if (GetCellRowColumnPosition(pos
, row
, col
))
8740 return GetCell(row
, col
);
8746 // Get the row/column for a given character position
8747 bool wxRichTextTable::GetCellRowColumnPosition(long pos
, int& row
, int& col
) const
8749 if (m_colCount
== 0 || m_rowCount
== 0)
8752 row
= (int) (pos
/ m_colCount
);
8753 col
= pos
- (row
* m_colCount
);
8755 wxASSERT(row
< m_rowCount
&& col
< m_colCount
);
8757 if (row
< m_rowCount
&& col
< m_colCount
)
8763 // Calculate range, taking row/cell ordering into account instead of relying
8764 // on list ordering.
8765 void wxRichTextTable::CalculateRange(long start
, long& end
)
8767 long current
= start
;
8768 long lastEnd
= current
;
8777 for (i
= 0; i
< m_rowCount
; i
++)
8779 for (j
= 0; j
< m_colCount
; j
++)
8781 wxRichTextCell
* child
= GetCell(i
, j
);
8786 child
->CalculateRange(current
, childEnd
);
8789 current
= childEnd
+ 1;
8794 // A top-level object always has a range of size 1,
8795 // because its children don't count at this level.
8797 m_range
.SetRange(start
, start
);
8799 // An object with no children has zero length
8800 if (m_children
.GetCount() == 0)
8802 m_ownRange
.SetRange(0, lastEnd
);
8805 // Gets the range size.
8806 bool wxRichTextTable::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
8808 return wxRichTextBox::GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
8811 // Deletes content in the given range.
8812 bool wxRichTextTable::DeleteRange(const wxRichTextRange
& WXUNUSED(range
))
8814 // TODO: implement deletion of cells
8818 // Gets any text in this object for the given range.
8819 wxString
wxRichTextTable::GetTextForRange(const wxRichTextRange
& range
) const
8821 return wxRichTextBox::GetTextForRange(range
);
8824 // Copies this object.
8825 void wxRichTextTable::Copy(const wxRichTextTable
& obj
)
8827 wxRichTextBox::Copy(obj
);
8831 m_rowCount
= obj
.m_rowCount
;
8832 m_colCount
= obj
.m_colCount
;
8834 m_cells
.Add(wxRichTextObjectPtrArray(), m_rowCount
);
8837 for (i
= 0; i
< m_rowCount
; i
++)
8839 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
8840 for (j
= 0; j
< m_colCount
; j
++)
8842 wxRichTextCell
* cell
= wxDynamicCast(obj
.GetCell(i
, j
)->Clone(), wxRichTextCell
);
8850 void wxRichTextTable::ClearTable()
8856 bool wxRichTextTable::CreateTable(int rows
, int cols
)
8863 m_cells
.Add(wxRichTextObjectPtrArray(), rows
);
8866 for (i
= 0; i
< rows
; i
++)
8868 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
8869 for (j
= 0; j
< cols
; j
++)
8871 wxRichTextCell
* cell
= new wxRichTextCell
;
8873 cell
->AddParagraph(wxEmptyString
);
8882 wxRichTextCell
* wxRichTextTable::GetCell(int row
, int col
) const
8884 wxASSERT(row
< m_rowCount
);
8885 wxASSERT(col
< m_colCount
);
8887 if (row
< m_rowCount
&& col
< m_colCount
)
8889 wxRichTextObjectPtrArray
& colArray
= m_cells
[row
];
8890 wxRichTextObject
* obj
= colArray
[col
];
8891 return wxDynamicCast(obj
, wxRichTextCell
);
8897 // Returns a selection object specifying the selections between start and end character positions.
8898 // For example, a table would deduce what cells (of range length 1) are selected when dragging across the table.
8899 wxRichTextSelection
wxRichTextTable::GetSelection(long start
, long end
) const
8901 wxRichTextSelection selection
;
8902 selection
.SetContainer((wxRichTextTable
*) this);
8911 wxASSERT( start
>= 0 && end
< (m_colCount
* m_rowCount
));
8913 if (end
>= (m_colCount
* m_rowCount
))
8916 // We need to find the rectangle of cells that is described by the rectangle
8917 // with start, end as the diagonal. Make sure we don't add cells that are
8918 // not currenty visible because they are overlapped by spanning cells.
8920 --------------------------
8921 | 0 | 1 | 2 | 3 | 4 |
8922 --------------------------
8923 | 5 | 6 | 7 | 8 | 9 |
8924 --------------------------
8925 | 10 | 11 | 12 | 13 | 14 |
8926 --------------------------
8927 | 15 | 16 | 17 | 18 | 19 |
8928 --------------------------
8930 Let's say we select 6 -> 18.
8932 Left and right edge cols of rectangle are 1 and 3 inclusive. Find least/greatest to find
8933 which is left and which is right.
8935 Top and bottom edge rows are 1 and 3 inclusive. Again, find least/greatest to find top and bottom.
8937 Now go through rows from 1 to 3 and only add cells that are (a) within above column range
8943 int leftCol
= start
- m_colCount
* int(start
/m_colCount
);
8944 int rightCol
= end
- m_colCount
* int(end
/m_colCount
);
8946 int topRow
= int(start
/m_colCount
);
8947 int bottomRow
= int(end
/m_colCount
);
8949 if (leftCol
> rightCol
)
8956 if (topRow
> bottomRow
)
8958 int tmp
= bottomRow
;
8964 for (i
= topRow
; i
<= bottomRow
; i
++)
8966 for (j
= leftCol
; j
<= rightCol
; j
++)
8968 wxRichTextCell
* cell
= GetCell(i
, j
);
8969 if (cell
&& cell
->IsShown())
8970 selection
.Add(cell
->GetRange());
8977 // Sets the attributes for the cells specified by the selection.
8978 bool wxRichTextTable::SetCellStyle(const wxRichTextSelection
& selection
, const wxRichTextAttr
& style
, int flags
)
8980 if (selection
.GetContainer() != this)
8983 wxRichTextBuffer
* buffer
= GetBuffer();
8984 bool haveControl
= (buffer
&& buffer
->GetRichTextCtrl() != NULL
);
8985 bool withUndo
= haveControl
&& ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
8988 buffer
->BeginBatchUndo(_("Set Cell Style"));
8990 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
8993 wxRichTextCell
* cell
= wxDynamicCast(node
->GetData(), wxRichTextCell
);
8994 if (cell
&& selection
.WithinSelection(cell
->GetRange().GetStart()))
8995 SetStyle(cell
, style
, flags
);
8996 node
= node
->GetNext();
8999 // Do action, or delay it until end of batch.
9001 buffer
->EndBatchUndo();
9006 bool wxRichTextTable::DeleteRows(int startRow
, int noRows
)
9008 wxASSERT((startRow
+ noRows
) < m_rowCount
);
9009 if ((startRow
+ noRows
) >= m_rowCount
)
9013 for (i
= startRow
; i
< (startRow
+noRows
); i
++)
9015 wxRichTextObjectPtrArray
& colArray
= m_cells
[startRow
];
9016 for (j
= 0; j
< (int) colArray
.GetCount(); j
++)
9018 wxRichTextObject
* cell
= colArray
[j
];
9019 RemoveChild(cell
, true);
9022 // Keep deleting at the same position, since we move all
9024 m_cells
.RemoveAt(startRow
);
9027 m_rowCount
= m_rowCount
- noRows
;
9032 bool wxRichTextTable::DeleteColumns(int startCol
, int noCols
)
9034 wxASSERT((startCol
+ noCols
) < m_colCount
);
9035 if ((startCol
+ noCols
) >= m_colCount
)
9038 bool deleteRows
= (noCols
== m_colCount
);
9041 for (i
= 0; i
< m_rowCount
; i
++)
9043 wxRichTextObjectPtrArray
& colArray
= m_cells
[deleteRows
? 0 : i
];
9044 for (j
= startCol
; j
< (startCol
+noCols
); j
++)
9046 wxRichTextObject
* cell
= colArray
[j
];
9047 RemoveChild(cell
, true);
9051 m_cells
.RemoveAt(0);
9056 m_colCount
= m_colCount
- noCols
;
9061 bool wxRichTextTable::AddRows(int startRow
, int noRows
, const wxRichTextAttr
& attr
)
9063 wxASSERT(startRow
<= m_rowCount
);
9064 if (startRow
> m_rowCount
)
9068 for (i
= 0; i
< noRows
; i
++)
9071 if (startRow
== m_rowCount
)
9073 m_cells
.Add(wxRichTextObjectPtrArray());
9074 idx
= m_cells
.GetCount() - 1;
9078 m_cells
.Insert(wxRichTextObjectPtrArray(), startRow
+i
);
9082 wxRichTextObjectPtrArray
& colArray
= m_cells
[idx
];
9083 for (j
= 0; j
< m_colCount
; j
++)
9085 wxRichTextCell
* cell
= new wxRichTextCell
;
9086 cell
->GetAttributes() = attr
;
9093 m_rowCount
= m_rowCount
+ noRows
;
9097 bool wxRichTextTable::AddColumns(int startCol
, int noCols
, const wxRichTextAttr
& attr
)
9099 wxASSERT(startCol
<= m_colCount
);
9100 if (startCol
> m_colCount
)
9104 for (i
= 0; i
< m_rowCount
; i
++)
9106 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
9107 for (j
= 0; j
< noCols
; j
++)
9109 wxRichTextCell
* cell
= new wxRichTextCell
;
9110 cell
->GetAttributes() = attr
;
9114 if (startCol
== m_colCount
)
9117 colArray
.Insert(cell
, startCol
+j
);
9121 m_colCount
= m_colCount
+ noCols
;
9126 // Edit properties via a GUI
9127 bool wxRichTextTable::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
9129 wxRichTextObjectPropertiesDialog
boxDlg(this, wxGetTopLevelParent(parent
), wxID_ANY
, _("Table Properties"));
9130 boxDlg
.SetAttributes(GetAttributes());
9132 if (boxDlg
.ShowModal() == wxID_OK
)
9134 boxDlg
.ApplyStyle(buffer
->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RESET
);
9142 * Module to initialise and clean up handlers
9145 class wxRichTextModule
: public wxModule
9147 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
9149 wxRichTextModule() {}
9152 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
9153 wxRichTextBuffer::InitStandardHandlers();
9154 wxRichTextParagraph::InitDefaultTabs();
9159 wxRichTextBuffer::CleanUpHandlers();
9160 wxRichTextDecimalToRoman(-1);
9161 wxRichTextParagraph::ClearDefaultTabs();
9162 wxRichTextCtrl::ClearAvailableFontNames();
9163 wxRichTextBuffer::SetRenderer(NULL
);
9167 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
9170 // If the richtext lib is dynamically loaded after the app has already started
9171 // (such as from wxPython) then the built-in module system will not init this
9172 // module. Provide this function to do it manually.
9173 void wxRichTextModuleInit()
9175 wxModule
* module = new wxRichTextModule
;
9177 wxModule::RegisterModule(module);
9182 * Commands for undo/redo
9186 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
9187 wxRichTextParagraphLayoutBox
* container
, wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
9189 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, container
, ctrl
, ignoreFirstTime
);
9192 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
9196 wxRichTextCommand::~wxRichTextCommand()
9201 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
9203 if (!m_actions
.Member(action
))
9204 m_actions
.Append(action
);
9207 bool wxRichTextCommand::Do()
9209 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
9211 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
9218 bool wxRichTextCommand::Undo()
9220 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
9222 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
9229 void wxRichTextCommand::ClearActions()
9231 WX_CLEAR_LIST(wxList
, m_actions
);
9239 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
,
9240 wxRichTextBuffer
* buffer
, wxRichTextParagraphLayoutBox
* container
,
9241 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
9245 m_containerAddress
.Create(buffer
, container
);
9246 m_ignoreThis
= ignoreFirstTime
;
9251 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
9252 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
9254 cmd
->AddAction(this);
9257 wxRichTextAction::~wxRichTextAction()
9263 // Returns the container that this action refers to, using the container address and top-level buffer.
9264 wxRichTextParagraphLayoutBox
* wxRichTextAction::GetContainer() const
9266 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(GetContainerAddress().GetObject(m_buffer
), wxRichTextParagraphLayoutBox
);
9271 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
9273 // Store a list of line start character and y positions so we can figure out which area
9274 // we need to refresh
9276 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9277 wxRichTextParagraphLayoutBox
* container
= GetContainer();
9278 wxASSERT(container
!= NULL
);
9282 // NOTE: we're assuming that the buffer is laid out correctly at this point.
9283 // If we had several actions, which only invalidate and leave layout until the
9284 // paint handler is called, then this might not be true. So we may need to switch
9285 // optimisation on only when we're simply adding text and not simultaneously
9286 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
9287 // first, but of course this means we'll be doing it twice.
9288 if (!m_buffer
->IsDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
9290 wxSize clientSize
= m_ctrl
->GetClientSize();
9291 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
9292 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
9294 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(GetRange().GetStart());
9295 wxRichTextObjectList::compatibility_iterator node
= container
->GetChildren().Find(para
);
9298 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
9299 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
9302 wxRichTextLine
* line
= node2
->GetData();
9303 wxPoint pt
= line
->GetAbsolutePosition();
9304 wxRichTextRange range
= line
->GetAbsoluteRange();
9308 node2
= wxRichTextLineList::compatibility_iterator();
9309 node
= wxRichTextObjectList::compatibility_iterator();
9311 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
9313 optimizationLineCharPositions
.Add(range
.GetStart());
9314 optimizationLineYPositions
.Add(pt
.y
);
9318 node2
= node2
->GetNext();
9322 node
= node
->GetNext();
9328 bool wxRichTextAction::Do()
9330 m_buffer
->Modify(true);
9332 wxRichTextParagraphLayoutBox
* container
= GetContainer();
9333 wxASSERT(container
!= NULL
);
9339 case wxRICHTEXT_INSERT
:
9341 // Store a list of line start character and y positions so we can figure out which area
9342 // we need to refresh
9343 wxArrayInt optimizationLineCharPositions
;
9344 wxArrayInt optimizationLineYPositions
;
9346 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9347 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
9350 container
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
9351 container
->UpdateRanges();
9353 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9354 // Layout() would stop prematurely at the top level.
9355 container
->InvalidateHierarchy(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
9357 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetOwnRange().GetLength();
9359 // Character position to caret position
9360 newCaretPosition
--;
9362 // Don't take into account the last newline
9363 if (m_newParagraphs
.GetPartialParagraph())
9364 newCaretPosition
--;
9366 if (m_newParagraphs
.GetChildren().GetCount() > 1)
9368 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
9369 if (p
->GetRange().GetLength() == 1)
9370 newCaretPosition
--;
9373 newCaretPosition
= wxMin(newCaretPosition
, (container
->GetOwnRange().GetEnd()-1));
9375 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
9377 wxRichTextEvent
cmdEvent(
9378 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
9379 m_ctrl
? m_ctrl
->GetId() : -1);
9380 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9381 cmdEvent
.SetRange(GetRange());
9382 cmdEvent
.SetPosition(GetRange().GetStart());
9383 cmdEvent
.SetContainer(container
);
9385 m_buffer
->SendEvent(cmdEvent
);
9389 case wxRICHTEXT_DELETE
:
9391 wxArrayInt optimizationLineCharPositions
;
9392 wxArrayInt optimizationLineYPositions
;
9394 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9395 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
9398 container
->DeleteRange(GetRange());
9399 container
->UpdateRanges();
9400 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9401 // Layout() would stop prematurely at the top level.
9402 container
->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
9404 long caretPos
= GetRange().GetStart()-1;
9405 if (caretPos
>= container
->GetOwnRange().GetEnd())
9408 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
9410 wxRichTextEvent
cmdEvent(
9411 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
9412 m_ctrl
? m_ctrl
->GetId() : -1);
9413 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9414 cmdEvent
.SetRange(GetRange());
9415 cmdEvent
.SetPosition(GetRange().GetStart());
9416 cmdEvent
.SetContainer(container
);
9418 m_buffer
->SendEvent(cmdEvent
);
9422 case wxRICHTEXT_CHANGE_STYLE
:
9424 ApplyParagraphs(GetNewParagraphs());
9426 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9427 // Layout() would stop prematurely at the top level.
9428 container
->InvalidateHierarchy(GetRange());
9430 UpdateAppearance(GetPosition());
9432 wxRichTextEvent
cmdEvent(
9433 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
9434 m_ctrl
? m_ctrl
->GetId() : -1);
9435 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9436 cmdEvent
.SetRange(GetRange());
9437 cmdEvent
.SetPosition(GetRange().GetStart());
9438 cmdEvent
.SetContainer(container
);
9440 m_buffer
->SendEvent(cmdEvent
);
9444 case wxRICHTEXT_CHANGE_ATTRIBUTES
:
9446 wxRichTextObject
* obj
= m_objectAddress
.GetObject(m_buffer
); // container->GetChildAtPosition(GetRange().GetStart());
9449 wxRichTextAttr oldAttr
= obj
->GetAttributes();
9450 obj
->GetAttributes() = m_attributes
;
9451 m_attributes
= oldAttr
;
9454 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9455 // Layout() would stop prematurely at the top level.
9456 container
->InvalidateHierarchy(GetRange());
9458 UpdateAppearance(GetPosition());
9460 wxRichTextEvent
cmdEvent(
9461 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
9462 m_ctrl
? m_ctrl
->GetId() : -1);
9463 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9464 cmdEvent
.SetRange(GetRange());
9465 cmdEvent
.SetPosition(GetRange().GetStart());
9466 cmdEvent
.SetContainer(container
);
9468 m_buffer
->SendEvent(cmdEvent
);
9472 case wxRICHTEXT_CHANGE_OBJECT
:
9474 wxRichTextObject
* obj
= m_objectAddress
.GetObject(m_buffer
);
9475 // wxRichTextObject* obj = container->GetChildAtPosition(GetRange().GetStart());
9476 if (obj
&& m_object
)
9478 wxRichTextObjectList::compatibility_iterator node
= container
->GetChildren().Find(obj
);
9481 wxRichTextObject
* obj
= node
->GetData();
9482 node
->SetData(m_object
);
9487 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9488 // Layout() would stop prematurely at the top level.
9489 container
->InvalidateHierarchy(GetRange());
9491 UpdateAppearance(GetPosition());
9493 // TODO: send new kind of modification event
9504 bool wxRichTextAction::Undo()
9506 m_buffer
->Modify(true);
9508 wxRichTextParagraphLayoutBox
* container
= GetContainer();
9509 wxASSERT(container
!= NULL
);
9515 case wxRICHTEXT_INSERT
:
9517 wxArrayInt optimizationLineCharPositions
;
9518 wxArrayInt optimizationLineYPositions
;
9520 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9521 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
9524 container
->DeleteRange(GetRange());
9525 container
->UpdateRanges();
9526 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9527 // Layout() would stop prematurely at the top level.
9528 container
->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
9530 long newCaretPosition
= GetPosition() - 1;
9532 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
9534 wxRichTextEvent
cmdEvent(
9535 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
9536 m_ctrl
? m_ctrl
->GetId() : -1);
9537 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9538 cmdEvent
.SetRange(GetRange());
9539 cmdEvent
.SetPosition(GetRange().GetStart());
9540 cmdEvent
.SetContainer(container
);
9542 m_buffer
->SendEvent(cmdEvent
);
9546 case wxRICHTEXT_DELETE
:
9548 wxArrayInt optimizationLineCharPositions
;
9549 wxArrayInt optimizationLineYPositions
;
9551 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9552 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
9555 container
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
9556 container
->UpdateRanges();
9557 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9558 // Layout() would stop prematurely at the top level.
9559 container
->InvalidateHierarchy(GetRange());
9561 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
9563 wxRichTextEvent
cmdEvent(
9564 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
9565 m_ctrl
? m_ctrl
->GetId() : -1);
9566 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9567 cmdEvent
.SetRange(GetRange());
9568 cmdEvent
.SetPosition(GetRange().GetStart());
9569 cmdEvent
.SetContainer(container
);
9571 m_buffer
->SendEvent(cmdEvent
);
9575 case wxRICHTEXT_CHANGE_STYLE
:
9577 ApplyParagraphs(GetOldParagraphs());
9578 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
9579 // Layout() would stop prematurely at the top level.
9580 container
->InvalidateHierarchy(GetRange());
9582 UpdateAppearance(GetPosition());
9584 wxRichTextEvent
cmdEvent(
9585 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
9586 m_ctrl
? m_ctrl
->GetId() : -1);
9587 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
9588 cmdEvent
.SetRange(GetRange());
9589 cmdEvent
.SetPosition(GetRange().GetStart());
9590 cmdEvent
.SetContainer(container
);
9592 m_buffer
->SendEvent(cmdEvent
);
9596 case wxRICHTEXT_CHANGE_ATTRIBUTES
:
9597 case wxRICHTEXT_CHANGE_OBJECT
:
9608 /// Update the control appearance
9609 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
, bool isDoCmd
)
9611 wxRichTextParagraphLayoutBox
* container
= GetContainer();
9612 wxASSERT(container
!= NULL
);
9618 m_ctrl
->SetFocusObject(container
);
9619 m_ctrl
->SetCaretPosition(caretPosition
);
9621 if (!m_ctrl
->IsFrozen())
9623 wxRect containerRect
= container
->GetRect();
9625 m_ctrl
->LayoutContent();
9627 // Refresh everything if there were floating objects or the container changed size
9628 // (we can't yet optimize in these cases, since more complex interaction with other content occurs)
9629 if (container
->GetFloatingObjectCount() > 0 || (container
->GetParent() && containerRect
!= container
->GetRect()))
9631 m_ctrl
->Refresh(false);
9635 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
9636 // Find refresh rectangle if we are in a position to optimise refresh
9637 if ((m_cmdId
== wxRICHTEXT_INSERT
|| m_cmdId
== wxRICHTEXT_DELETE
) && optimizationLineCharPositions
)
9641 wxSize clientSize
= m_ctrl
->GetClientSize();
9642 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
9644 // Start/end positions
9646 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
9648 bool foundEnd
= false;
9650 // position offset - how many characters were inserted
9651 int positionOffset
= GetRange().GetLength();
9653 // Determine whether this is Do or Undo, and adjust positionOffset accordingly
9654 if ((m_cmdId
== wxRICHTEXT_DELETE
&& isDoCmd
) || (m_cmdId
== wxRICHTEXT_INSERT
&& !isDoCmd
))
9655 positionOffset
= - positionOffset
;
9657 // find the first line which is being drawn at the same position as it was
9658 // before. Since we're talking about a simple insertion, we can assume
9659 // that the rest of the window does not need to be redrawn.
9661 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(GetPosition());
9662 // Since we support floating layout, we should redraw the whole para instead of just
9663 // the first line touching the invalid range.
9666 firstY
= para
->GetPosition().y
;
9669 wxRichTextObjectList::compatibility_iterator node
= container
->GetChildren().Find(para
);
9672 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
9673 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
9676 wxRichTextLine
* line
= node2
->GetData();
9677 wxPoint pt
= line
->GetAbsolutePosition();
9678 wxRichTextRange range
= line
->GetAbsoluteRange();
9680 // we want to find the first line that is in the same position
9681 // as before. This will mean we're at the end of the changed text.
9683 if (pt
.y
> lastY
) // going past the end of the window, no more info
9685 node2
= wxRichTextLineList::compatibility_iterator();
9686 node
= wxRichTextObjectList::compatibility_iterator();
9688 // Detect last line in the buffer
9689 else if (!node2
->GetNext() && para
->GetRange().Contains(container
->GetOwnRange().GetEnd()))
9691 // If deleting text, make sure we refresh below as well as above
9692 if (positionOffset
>= 0)
9695 lastY
= pt
.y
+ line
->GetSize().y
;
9698 node2
= wxRichTextLineList::compatibility_iterator();
9699 node
= wxRichTextObjectList::compatibility_iterator();
9705 // search for this line being at the same position as before
9706 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
9708 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
9709 ((*optimizationLineYPositions
)[i
] == pt
.y
))
9711 // Stop, we're now the same as we were
9716 node2
= wxRichTextLineList::compatibility_iterator();
9717 node
= wxRichTextObjectList::compatibility_iterator();
9725 node2
= node2
->GetNext();
9729 node
= node
->GetNext();
9732 firstY
= wxMax(firstVisiblePt
.y
, firstY
);
9734 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
9736 // Convert to device coordinates
9737 wxRect
rect(m_ctrl
->GetPhysicalPoint(wxPoint(firstVisiblePt
.x
, firstY
)), wxSize(clientSize
.x
, lastY
- firstY
));
9738 m_ctrl
->RefreshRect(rect
);
9742 m_ctrl
->Refresh(false);
9744 m_ctrl
->PositionCaret();
9746 // This causes styles to persist when doing programmatic
9747 // content creation except when Freeze/Thaw is used, so
9748 // disable this and check for the consequences.
9749 // m_ctrl->SetDefaultStyleToCursorStyle();
9751 if (sendUpdateEvent
)
9752 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
9757 /// Replace the buffer paragraphs with the new ones.
9758 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
9760 wxRichTextParagraphLayoutBox
* container
= GetContainer();
9761 wxASSERT(container
!= NULL
);
9765 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
9768 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
9769 wxASSERT (para
!= NULL
);
9771 // We'll replace the existing paragraph by finding the paragraph at this position,
9772 // delete its node data, and setting a copy as the new node data.
9773 // TODO: make more efficient by simply swapping old and new paragraph objects.
9775 wxRichTextParagraph
* existingPara
= container
->GetParagraphAtPosition(para
->GetRange().GetStart());
9778 wxRichTextObjectList::compatibility_iterator bufferParaNode
= container
->GetChildren().Find(existingPara
);
9781 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
9782 newPara
->SetParent(container
);
9784 bufferParaNode
->SetData(newPara
);
9786 delete existingPara
;
9790 node
= node
->GetNext();
9797 * This stores beginning and end positions for a range of data.
9800 WX_DEFINE_OBJARRAY(wxRichTextRangeArray
);
9802 /// Limit this range to be within 'range'
9803 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
9805 if (m_start
< range
.m_start
)
9806 m_start
= range
.m_start
;
9808 if (m_end
> range
.m_end
)
9809 m_end
= range
.m_end
;
9815 * wxRichTextImage implementation
9816 * This object represents an image.
9819 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
9821 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
9822 wxRichTextObject(parent
)
9824 m_imageBlock
.MakeImageBlockDefaultQuality(image
, wxBITMAP_TYPE_PNG
);
9826 SetAttributes(*charStyle
);
9829 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
9830 wxRichTextObject(parent
)
9832 m_imageBlock
= imageBlock
;
9834 SetAttributes(*charStyle
);
9837 /// Create a cached image at the required size
9838 bool wxRichTextImage::LoadImageCache(wxDC
& dc
, bool resetCache
)
9840 if (resetCache
|| !m_imageCache
.IsOk() /* || m_imageCache.GetWidth() != size.x || m_imageCache.GetHeight() != size.y */)
9842 if (!m_imageBlock
.IsOk())
9846 m_imageBlock
.Load(image
);
9850 int width
= image
.GetWidth();
9851 int height
= image
.GetHeight();
9853 if (GetAttributes().GetTextBoxAttr().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
9855 if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
9856 width
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
9858 width
= GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
9860 if (GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
9862 if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
9863 height
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
9865 height
= GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
9868 if (image
.GetWidth() == width
&& image
.GetHeight() == height
)
9869 m_imageCache
= wxBitmap(image
);
9872 // If the original width and height is small, e.g. 400 or below,
9873 // scale up and then down to improve image quality. This can make
9874 // a big difference, with not much performance hit.
9875 int upscaleThreshold
= 400;
9877 if (image
.GetWidth() <= upscaleThreshold
|| image
.GetHeight() <= upscaleThreshold
)
9879 img
= image
.Scale(image
.GetWidth()*2, image
.GetHeight()*2);
9880 img
.Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
9883 img
= image
.Scale(width
, height
, wxIMAGE_QUALITY_HIGH
);
9884 m_imageCache
= wxBitmap(img
);
9888 return m_imageCache
.IsOk();
9892 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextSelection
& selection
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
9897 // Don't need cached size AFAIK
9898 // wxSize size = GetCachedSize();
9899 if (!LoadImageCache(dc
))
9902 DrawBoxAttributes(dc
, GetBuffer(), GetAttributes(), wxRect(GetPosition(), GetCachedSize()));
9905 int y
= rect
.y
+ (rect
.height
- m_imageCache
.GetHeight());
9907 dc
.DrawBitmap(m_imageCache
, rect
.x
, y
, true);
9910 wxSize
imageSize(m_imageCache
.GetWidth(), m_imageCache
.GetHeight());
9911 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
9912 marginRect
= rect
; // outer rectangle, will calculate contentRect
9913 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
9915 dc
.DrawBitmap(m_imageCache
, contentRect
.x
, contentRect
.y
, true);
9917 if (selection
.WithinSelection(range
.GetStart(), this))
9919 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
9920 wxCheckSetPen(dc
, *wxBLACK_PEN
);
9921 dc
.SetLogicalFunction(wxINVERT
);
9922 dc
.DrawRectangle(contentRect
);
9923 dc
.SetLogicalFunction(wxCOPY
);
9929 /// Lay the item out
9930 bool wxRichTextImage::Layout(wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(style
))
9932 if (!LoadImageCache(dc
))
9935 wxSize
imageSize(m_imageCache
.GetWidth(), m_imageCache
.GetHeight());
9936 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
9937 contentRect
= wxRect(wxPoint(0,0), imageSize
);
9938 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
9940 wxSize overallSize
= marginRect
.GetSize();
9942 SetCachedSize(overallSize
);
9943 SetMaxSize(overallSize
);
9944 SetMinSize(overallSize
);
9945 SetPosition(rect
.GetPosition());
9950 /// Get/set the object size for the given range. Returns false if the range
9951 /// is invalid for this object.
9952 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& dc
, int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
9954 if (!range
.IsWithin(GetRange()))
9957 if (!((wxRichTextImage
*)this)->LoadImageCache(dc
))
9959 size
.x
= 0; size
.y
= 0;
9961 partialExtents
->Add(0);
9965 wxSize
imageSize(m_imageCache
.GetWidth(), m_imageCache
.GetHeight());
9966 wxRect marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
;
9967 contentRect
= wxRect(wxPoint(0,0), imageSize
);
9968 GetBoxRects(dc
, GetBuffer(), GetAttributes(), marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
9970 wxSize overallSize
= marginRect
.GetSize();
9973 partialExtents
->Add(overallSize
.x
);
9980 // Get the 'natural' size for an object. For an image, it would be the
9982 wxTextAttrSize
wxRichTextImage::GetNaturalSize() const
9984 wxTextAttrSize size
;
9985 if (GetImageCache().IsOk())
9987 size
.SetWidth(GetImageCache().GetWidth(), wxTEXT_ATTR_UNITS_PIXELS
);
9988 size
.SetHeight(GetImageCache().GetHeight(), wxTEXT_ATTR_UNITS_PIXELS
);
9995 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
9997 wxRichTextObject::Copy(obj
);
9999 m_imageBlock
= obj
.m_imageBlock
;
10002 /// Edit properties via a GUI
10003 bool wxRichTextImage::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
10005 wxRichTextObjectPropertiesDialog
imageDlg(this, wxGetTopLevelParent(parent
), wxID_ANY
, _("Picture Properties"));
10006 imageDlg
.SetAttributes(GetAttributes());
10008 if (imageDlg
.ShowModal() == wxID_OK
)
10010 // By passing wxRICHTEXT_SETSTYLE_RESET, indeterminate attributes set by the user will be set as
10011 // indeterminate in the object.
10012 imageDlg
.ApplyStyle(buffer
->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RESET
);
10024 /// Compare two attribute objects
10025 bool wxTextAttrEq(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
10027 return (attr1
== attr2
);
10030 // Partial equality test taking flags into account
10031 bool wxTextAttrEqPartial(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
10033 return attr1
.EqPartial(attr2
);
10037 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
10039 if (tabs1
.GetCount() != tabs2
.GetCount())
10043 for (i
= 0; i
< tabs1
.GetCount(); i
++)
10045 if (tabs1
[i
] != tabs2
[i
])
10051 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
10053 return destStyle
.Apply(style
, compareWith
);
10056 // Remove attributes
10057 bool wxRichTextRemoveStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
)
10059 return destStyle
.RemoveStyle(style
);
10062 /// Combine two bitlists, specifying the bits of interest with separate flags.
10063 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
10065 return wxRichTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
10068 /// Compare two bitlists
10069 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
10071 return wxRichTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
10074 /// Split into paragraph and character styles
10075 bool wxRichTextSplitParaCharStyles(const wxRichTextAttr
& style
, wxRichTextAttr
& parStyle
, wxRichTextAttr
& charStyle
)
10077 return wxRichTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
10080 /// Convert a decimal to Roman numerals
10081 wxString
wxRichTextDecimalToRoman(long n
)
10083 static wxArrayInt decimalNumbers
;
10084 static wxArrayString romanNumbers
;
10089 decimalNumbers
.Clear();
10090 romanNumbers
.Clear();
10091 return wxEmptyString
;
10094 if (decimalNumbers
.GetCount() == 0)
10096 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
10098 wxRichTextAddDecRom(1000, wxT("M"));
10099 wxRichTextAddDecRom(900, wxT("CM"));
10100 wxRichTextAddDecRom(500, wxT("D"));
10101 wxRichTextAddDecRom(400, wxT("CD"));
10102 wxRichTextAddDecRom(100, wxT("C"));
10103 wxRichTextAddDecRom(90, wxT("XC"));
10104 wxRichTextAddDecRom(50, wxT("L"));
10105 wxRichTextAddDecRom(40, wxT("XL"));
10106 wxRichTextAddDecRom(10, wxT("X"));
10107 wxRichTextAddDecRom(9, wxT("IX"));
10108 wxRichTextAddDecRom(5, wxT("V"));
10109 wxRichTextAddDecRom(4, wxT("IV"));
10110 wxRichTextAddDecRom(1, wxT("I"));
10116 while (n
> 0 && i
< 13)
10118 if (n
>= decimalNumbers
[i
])
10120 n
-= decimalNumbers
[i
];
10121 roman
+= romanNumbers
[i
];
10128 if (roman
.IsEmpty())
10134 * wxRichTextFileHandler
10135 * Base class for file handlers
10138 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
10140 #if wxUSE_FFILE && wxUSE_STREAMS
10141 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
10143 wxFFileInputStream
stream(filename
);
10145 return LoadFile(buffer
, stream
);
10150 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
10152 wxFFileOutputStream
stream(filename
);
10154 return SaveFile(buffer
, stream
);
10158 #endif // wxUSE_FFILE && wxUSE_STREAMS
10160 /// Can we handle this filename (if using files)? By default, checks the extension.
10161 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
10163 wxString path
, file
, ext
;
10164 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
10166 return (ext
.Lower() == GetExtension());
10170 * wxRichTextTextHandler
10171 * Plain text handler
10174 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
10177 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
10179 if (!stream
.IsOk())
10185 while (!stream
.Eof())
10187 int ch
= stream
.GetC();
10191 if (ch
== 10 && lastCh
!= 13)
10194 if (ch
> 0 && ch
!= 10)
10201 buffer
->ResetAndClearCommands();
10203 buffer
->AddParagraphs(str
);
10204 buffer
->UpdateRanges();
10209 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
10211 if (!stream
.IsOk())
10214 wxString text
= buffer
->GetText();
10216 wxString newLine
= wxRichTextLineBreakChar
;
10217 text
.Replace(newLine
, wxT("\n"));
10219 wxCharBuffer buf
= text
.ToAscii();
10221 stream
.Write((const char*) buf
, text
.length());
10224 #endif // wxUSE_STREAMS
10227 * Stores information about an image, in binary in-memory form
10230 wxRichTextImageBlock::wxRichTextImageBlock()
10235 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
10241 wxRichTextImageBlock::~wxRichTextImageBlock()
10246 void wxRichTextImageBlock::Init()
10250 m_imageType
= wxBITMAP_TYPE_INVALID
;
10253 void wxRichTextImageBlock::Clear()
10257 m_imageType
= wxBITMAP_TYPE_INVALID
;
10261 // Load the original image into a memory block.
10262 // If the image is not a JPEG, we must convert it into a JPEG
10263 // to conserve space.
10264 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
10265 // load the image a 2nd time.
10267 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
10268 wxImage
& image
, bool convertToJPEG
)
10270 m_imageType
= imageType
;
10272 wxString
filenameToRead(filename
);
10273 bool removeFile
= false;
10275 if (imageType
== wxBITMAP_TYPE_INVALID
)
10276 return false; // Could not determine image type
10278 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
10280 wxString tempFile
=
10281 wxFileName::CreateTempFileName(_("image"));
10283 wxASSERT(!tempFile
.IsEmpty());
10285 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
10286 filenameToRead
= tempFile
;
10289 m_imageType
= wxBITMAP_TYPE_JPEG
;
10292 if (!file
.Open(filenameToRead
))
10295 m_dataSize
= (size_t) file
.Length();
10300 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
10303 wxRemoveFile(filenameToRead
);
10305 return (m_data
!= NULL
);
10308 // Make an image block from the wxImage in the given
10310 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
10312 image
.SetOption(wxT("quality"), quality
);
10314 if (imageType
== wxBITMAP_TYPE_INVALID
)
10315 return false; // Could not determine image type
10317 return DoMakeImageBlock(image
, imageType
);
10320 // Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
10321 bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage
& image
, wxBitmapType imageType
)
10323 if (imageType
== wxBITMAP_TYPE_INVALID
)
10324 return false; // Could not determine image type
10326 return DoMakeImageBlock(image
, imageType
);
10329 // Makes the image block
10330 bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage
& image
, wxBitmapType imageType
)
10332 wxMemoryOutputStream memStream
;
10333 if (!image
.SaveFile(memStream
, imageType
))
10338 unsigned char* block
= new unsigned char[memStream
.GetSize()];
10346 m_imageType
= imageType
;
10347 m_dataSize
= memStream
.GetSize();
10349 memStream
.CopyTo(m_data
, m_dataSize
);
10351 return (m_data
!= NULL
);
10355 bool wxRichTextImageBlock::Write(const wxString
& filename
)
10357 return WriteBlock(filename
, m_data
, m_dataSize
);
10360 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
10362 m_imageType
= block
.m_imageType
;
10364 m_dataSize
= block
.m_dataSize
;
10365 if (m_dataSize
== 0)
10368 m_data
= new unsigned char[m_dataSize
];
10370 for (i
= 0; i
< m_dataSize
; i
++)
10371 m_data
[i
] = block
.m_data
[i
];
10375 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
10380 // Load a wxImage from the block
10381 bool wxRichTextImageBlock::Load(wxImage
& image
)
10386 // Read in the image.
10388 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
10389 bool success
= image
.LoadFile(mstream
, GetImageType());
10391 wxString tempFile
= wxFileName::CreateTempFileName(_("image"));
10392 wxASSERT(!tempFile
.IsEmpty());
10394 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
10398 success
= image
.LoadFile(tempFile
, GetImageType());
10399 wxRemoveFile(tempFile
);
10405 // Write data in hex to a stream
10406 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
10408 const int bufSize
= 512;
10409 char buf
[bufSize
+1];
10411 int left
= m_dataSize
;
10416 if (left
*2 > bufSize
)
10418 n
= bufSize
; left
-= (bufSize
/2);
10422 n
= left
*2; left
= 0;
10426 for (i
= 0; i
< (n
/2); i
++)
10428 wxDecToHex(m_data
[j
], b
, b
+1);
10433 stream
.Write((const char*) buf
, n
);
10438 // Read data in hex from a stream
10439 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
10441 int dataSize
= length
/2;
10446 // create a null terminated temporary string:
10450 m_data
= new unsigned char[dataSize
];
10452 for (i
= 0; i
< dataSize
; i
++)
10454 str
[0] = (char)stream
.GetC();
10455 str
[1] = (char)stream
.GetC();
10457 m_data
[i
] = (unsigned char)wxHexToDec(str
);
10460 m_dataSize
= dataSize
;
10461 m_imageType
= imageType
;
10466 // Allocate and read from stream as a block of memory
10467 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
10469 unsigned char* block
= new unsigned char[size
];
10473 stream
.Read(block
, size
);
10478 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
10480 wxFileInputStream
stream(filename
);
10484 return ReadBlock(stream
, size
);
10487 // Write memory block to stream
10488 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
10490 stream
.Write((void*) block
, size
);
10491 return stream
.IsOk();
10495 // Write memory block to file
10496 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
10498 wxFileOutputStream
outStream(filename
);
10499 if (!outStream
.Ok())
10502 return WriteBlock(outStream
, block
, size
);
10505 // Gets the extension for the block's type
10506 wxString
wxRichTextImageBlock::GetExtension() const
10508 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
10510 return handler
->GetExtension();
10512 return wxEmptyString
;
10518 * The data object for a wxRichTextBuffer
10521 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
10523 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
10525 m_richTextBuffer
= richTextBuffer
;
10527 // this string should uniquely identify our format, but is otherwise
10529 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
10531 SetFormat(m_formatRichTextBuffer
);
10534 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
10536 delete m_richTextBuffer
;
10539 // after a call to this function, the richTextBuffer is owned by the caller and it
10540 // is responsible for deleting it!
10541 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
10543 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
10544 m_richTextBuffer
= NULL
;
10546 return richTextBuffer
;
10549 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
10551 return m_formatRichTextBuffer
;
10554 size_t wxRichTextBufferDataObject::GetDataSize() const
10556 if (!m_richTextBuffer
)
10562 wxStringOutputStream
stream(& bufXML
);
10563 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
10565 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
10571 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
10572 return strlen(buffer
) + 1;
10574 return bufXML
.Length()+1;
10578 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
10580 if (!pBuf
|| !m_richTextBuffer
)
10586 wxStringOutputStream
stream(& bufXML
);
10587 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
10589 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
10595 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
10596 size_t len
= strlen(buffer
);
10597 memcpy((char*) pBuf
, (const char*) buffer
, len
);
10598 ((char*) pBuf
)[len
] = 0;
10600 size_t len
= bufXML
.Length();
10601 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
10602 ((char*) pBuf
)[len
] = 0;
10608 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
10610 wxDELETE(m_richTextBuffer
);
10612 wxString
bufXML((const char*) buf
, wxConvUTF8
);
10614 m_richTextBuffer
= new wxRichTextBuffer
;
10616 wxStringInputStream
stream(bufXML
);
10617 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
10619 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
10621 wxDELETE(m_richTextBuffer
);
10633 * wxRichTextFontTable
10634 * Manages quick access to a pool of fonts for rendering rich text
10637 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
10639 class wxRichTextFontTableData
: public wxObjectRefData
10642 wxRichTextFontTableData() {}
10644 wxFont
FindFont(const wxRichTextAttr
& fontSpec
);
10646 wxRichTextFontTableHashMap m_hashMap
;
10649 wxFont
wxRichTextFontTableData::FindFont(const wxRichTextAttr
& fontSpec
)
10651 wxString
facename(fontSpec
.GetFontFaceName());
10652 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()));
10653 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
10655 if ( entry
== m_hashMap
.end() )
10657 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
10658 m_hashMap
[spec
] = font
;
10663 return entry
->second
;
10667 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
10669 wxRichTextFontTable::wxRichTextFontTable()
10671 m_refData
= new wxRichTextFontTableData
;
10674 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
10680 wxRichTextFontTable::~wxRichTextFontTable()
10685 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
10687 return (m_refData
== table
.m_refData
);
10690 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
10695 wxFont
wxRichTextFontTable::FindFont(const wxRichTextAttr
& fontSpec
)
10697 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
10699 return data
->FindFont(fontSpec
);
10704 void wxRichTextFontTable::Clear()
10706 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
10708 data
->m_hashMap
.clear();
10714 void wxTextBoxAttr::Reset()
10717 m_floatMode
= wxTEXT_BOX_ATTR_FLOAT_NONE
;
10718 m_clearMode
= wxTEXT_BOX_ATTR_CLEAR_NONE
;
10719 m_collapseMode
= wxTEXT_BOX_ATTR_COLLAPSE_NONE
;
10720 m_verticalAlignment
= wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_NONE
;
10724 m_position
.Reset();
10733 bool wxTextBoxAttr::operator== (const wxTextBoxAttr
& attr
) const
10736 m_flags
== attr
.m_flags
&&
10737 m_floatMode
== attr
.m_floatMode
&&
10738 m_clearMode
== attr
.m_clearMode
&&
10739 m_collapseMode
== attr
.m_collapseMode
&&
10740 m_verticalAlignment
== attr
.m_verticalAlignment
&&
10742 m_margins
== attr
.m_margins
&&
10743 m_padding
== attr
.m_padding
&&
10744 m_position
== attr
.m_position
&&
10746 m_size
== attr
.m_size
&&
10748 m_border
== attr
.m_border
&&
10749 m_outline
== attr
.m_outline
10753 // Partial equality test
10754 bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr
& attr
) const
10756 if (attr
.HasFloatMode() && HasFloatMode() && (GetFloatMode() != attr
.GetFloatMode()))
10759 if (attr
.HasClearMode() && HasClearMode() && (GetClearMode() != attr
.GetClearMode()))
10762 if (attr
.HasCollapseBorders() && HasCollapseBorders() && (attr
.GetCollapseBorders() != GetCollapseBorders()))
10765 if (attr
.HasVerticalAlignment() && HasVerticalAlignment() && (attr
.GetVerticalAlignment() != GetVerticalAlignment()))
10770 if (!m_position
.EqPartial(attr
.m_position
))
10775 if (!m_margins
.EqPartial(attr
.m_margins
))
10780 if (!m_padding
.EqPartial(attr
.m_padding
))
10785 if (!GetBorder().EqPartial(attr
.GetBorder()))
10790 if (!GetOutline().EqPartial(attr
.GetOutline()))
10796 // Merges the given attributes. If compareWith
10797 // is non-NULL, then it will be used to mask out those attributes that are the same in style
10798 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
10799 bool wxTextBoxAttr::Apply(const wxTextBoxAttr
& attr
, const wxTextBoxAttr
* compareWith
)
10801 if (attr
.HasFloatMode())
10803 if (!(compareWith
&& compareWith
->HasFloatMode() && compareWith
->GetFloatMode() == attr
.GetFloatMode()))
10804 SetFloatMode(attr
.GetFloatMode());
10807 if (attr
.HasClearMode())
10809 if (!(compareWith
&& compareWith
->HasClearMode() && compareWith
->GetClearMode() == attr
.GetClearMode()))
10810 SetClearMode(attr
.GetClearMode());
10813 if (attr
.HasCollapseBorders())
10815 if (!(compareWith
&& compareWith
->HasCollapseBorders() && compareWith
->GetCollapseBorders() == attr
.GetCollapseBorders()))
10816 SetCollapseBorders(attr
.GetCollapseBorders());
10819 if (attr
.HasVerticalAlignment())
10821 if (!(compareWith
&& compareWith
->HasVerticalAlignment() && compareWith
->GetVerticalAlignment() == attr
.GetVerticalAlignment()))
10822 SetVerticalAlignment(attr
.GetVerticalAlignment());
10825 m_margins
.Apply(attr
.m_margins
, compareWith
? (& attr
.m_margins
) : (const wxTextAttrDimensions
*) NULL
);
10826 m_padding
.Apply(attr
.m_padding
, compareWith
? (& attr
.m_padding
) : (const wxTextAttrDimensions
*) NULL
);
10827 m_position
.Apply(attr
.m_position
, compareWith
? (& attr
.m_position
) : (const wxTextAttrDimensions
*) NULL
);
10829 m_size
.Apply(attr
.m_size
, compareWith
? (& attr
.m_size
) : (const wxTextAttrSize
*) NULL
);
10831 m_border
.Apply(attr
.m_border
, compareWith
? (& attr
.m_border
) : (const wxTextAttrBorders
*) NULL
);
10832 m_outline
.Apply(attr
.m_outline
, compareWith
? (& attr
.m_outline
) : (const wxTextAttrBorders
*) NULL
);
10837 // Remove specified attributes from this object
10838 bool wxTextBoxAttr::RemoveStyle(const wxTextBoxAttr
& attr
)
10840 if (attr
.HasFloatMode())
10841 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
10843 if (attr
.HasClearMode())
10844 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
10846 if (attr
.HasCollapseBorders())
10847 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
10849 if (attr
.HasVerticalAlignment())
10850 RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT
);
10852 m_margins
.RemoveStyle(attr
.m_margins
);
10853 m_padding
.RemoveStyle(attr
.m_padding
);
10854 m_position
.RemoveStyle(attr
.m_position
);
10856 m_size
.RemoveStyle(attr
.m_size
);
10858 m_border
.RemoveStyle(attr
.m_border
);
10859 m_outline
.RemoveStyle(attr
.m_outline
);
10864 // Collects the attributes that are common to a range of content, building up a note of
10865 // which attributes are absent in some objects and which clash in some objects.
10866 void wxTextBoxAttr::CollectCommonAttributes(const wxTextBoxAttr
& attr
, wxTextBoxAttr
& clashingAttr
, wxTextBoxAttr
& absentAttr
)
10868 if (attr
.HasFloatMode())
10870 if (!clashingAttr
.HasFloatMode() && !absentAttr
.HasFloatMode())
10872 if (HasFloatMode())
10874 if (GetFloatMode() != attr
.GetFloatMode())
10876 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
10877 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
10881 SetFloatMode(attr
.GetFloatMode());
10885 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
10887 if (attr
.HasClearMode())
10889 if (!clashingAttr
.HasClearMode() && !absentAttr
.HasClearMode())
10891 if (HasClearMode())
10893 if (GetClearMode() != attr
.GetClearMode())
10895 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
10896 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
10900 SetClearMode(attr
.GetClearMode());
10904 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
10906 if (attr
.HasCollapseBorders())
10908 if (!clashingAttr
.HasCollapseBorders() && !absentAttr
.HasCollapseBorders())
10910 if (HasCollapseBorders())
10912 if (GetCollapseBorders() != attr
.GetCollapseBorders())
10914 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
10915 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
10919 SetCollapseBorders(attr
.GetCollapseBorders());
10923 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
10925 if (attr
.HasVerticalAlignment())
10927 if (!clashingAttr
.HasVerticalAlignment() && !absentAttr
.HasVerticalAlignment())
10929 if (HasVerticalAlignment())
10931 if (GetVerticalAlignment() != attr
.GetVerticalAlignment())
10933 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT
);
10934 RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT
);
10938 SetVerticalAlignment(attr
.GetVerticalAlignment());
10942 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT
);
10944 m_margins
.CollectCommonAttributes(attr
.m_margins
, clashingAttr
.m_margins
, absentAttr
.m_margins
);
10945 m_padding
.CollectCommonAttributes(attr
.m_padding
, clashingAttr
.m_padding
, absentAttr
.m_padding
);
10946 m_position
.CollectCommonAttributes(attr
.m_position
, clashingAttr
.m_position
, absentAttr
.m_position
);
10948 m_size
.CollectCommonAttributes(attr
.m_size
, clashingAttr
.m_size
, absentAttr
.m_size
);
10950 m_border
.CollectCommonAttributes(attr
.m_border
, clashingAttr
.m_border
, absentAttr
.m_border
);
10951 m_outline
.CollectCommonAttributes(attr
.m_outline
, clashingAttr
.m_outline
, absentAttr
.m_outline
);
10956 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
10958 wxTextAttr::Copy(attr
);
10960 m_textBoxAttr
= attr
.m_textBoxAttr
;
10963 bool wxRichTextAttr::operator==(const wxRichTextAttr
& attr
) const
10965 if (!(wxTextAttr::operator==(attr
)))
10968 return (m_textBoxAttr
== attr
.m_textBoxAttr
);
10971 // Partial equality test taking comparison object into account
10972 bool wxRichTextAttr::EqPartial(const wxRichTextAttr
& attr
) const
10974 if (!(wxTextAttr::EqPartial(attr
)))
10977 return m_textBoxAttr
.EqPartial(attr
.m_textBoxAttr
);
10980 // Merges the given attributes. If compareWith
10981 // is non-NULL, then it will be used to mask out those attributes that are the same in style
10982 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
10983 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
10985 wxTextAttr::Apply(style
, compareWith
);
10987 return m_textBoxAttr
.Apply(style
.m_textBoxAttr
, compareWith
? (& compareWith
->m_textBoxAttr
) : (const wxTextBoxAttr
*) NULL
);
10990 // Remove specified attributes from this object
10991 bool wxRichTextAttr::RemoveStyle(const wxRichTextAttr
& attr
)
10993 wxTextAttr::RemoveStyle(*this, attr
);
10995 return m_textBoxAttr
.RemoveStyle(attr
.m_textBoxAttr
);
10998 // Collects the attributes that are common to a range of content, building up a note of
10999 // which attributes are absent in some objects and which clash in some objects.
11000 void wxRichTextAttr::CollectCommonAttributes(const wxRichTextAttr
& attr
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
11002 wxTextAttrCollectCommonAttributes(*this, attr
, clashingAttr
, absentAttr
);
11004 m_textBoxAttr
.CollectCommonAttributes(attr
.m_textBoxAttr
, clashingAttr
.m_textBoxAttr
, absentAttr
.m_textBoxAttr
);
11007 // Partial equality test
11008 bool wxTextAttrBorder::EqPartial(const wxTextAttrBorder
& border
) const
11010 if (border
.HasStyle() && !HasStyle() && (border
.GetStyle() != GetStyle()))
11013 if (border
.HasColour() && !HasColour() && (border
.GetColourLong() != GetColourLong()))
11016 if (border
.HasWidth() && !HasWidth() && !(border
.GetWidth() == GetWidth()))
11022 // Apply border to 'this', but not if the same as compareWith
11023 bool wxTextAttrBorder::Apply(const wxTextAttrBorder
& border
, const wxTextAttrBorder
* compareWith
)
11025 if (border
.HasStyle())
11027 if (!(compareWith
&& (border
.GetStyle() == compareWith
->GetStyle())))
11028 SetStyle(border
.GetStyle());
11030 if (border
.HasColour())
11032 if (!(compareWith
&& (border
.GetColourLong() == compareWith
->GetColourLong())))
11033 SetColour(border
.GetColourLong());
11035 if (border
.HasWidth())
11037 if (!(compareWith
&& (border
.GetWidth() == compareWith
->GetWidth())))
11038 SetWidth(border
.GetWidth());
11044 // Remove specified attributes from this object
11045 bool wxTextAttrBorder::RemoveStyle(const wxTextAttrBorder
& attr
)
11047 if (attr
.HasStyle() && HasStyle())
11048 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_STYLE
);
11049 if (attr
.HasColour() && HasColour())
11050 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_COLOUR
);
11051 if (attr
.HasWidth() && HasWidth())
11052 m_borderWidth
.Reset();
11057 // Collects the attributes that are common to a range of content, building up a note of
11058 // which attributes are absent in some objects and which clash in some objects.
11059 void wxTextAttrBorder::CollectCommonAttributes(const wxTextAttrBorder
& attr
, wxTextAttrBorder
& clashingAttr
, wxTextAttrBorder
& absentAttr
)
11061 if (attr
.HasStyle())
11063 if (!clashingAttr
.HasStyle() && !absentAttr
.HasStyle())
11067 if (GetStyle() != attr
.GetStyle())
11069 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
11070 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
11074 SetStyle(attr
.GetStyle());
11078 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
11080 if (attr
.HasColour())
11082 if (!clashingAttr
.HasColour() && !absentAttr
.HasColour())
11086 if (GetColour() != attr
.GetColour())
11088 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
11089 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
11093 SetColour(attr
.GetColourLong());
11097 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
11099 m_borderWidth
.CollectCommonAttributes(attr
.m_borderWidth
, clashingAttr
.m_borderWidth
, absentAttr
.m_borderWidth
);
11102 // Partial equality test
11103 bool wxTextAttrBorders::EqPartial(const wxTextAttrBorders
& borders
) const
11105 return m_left
.EqPartial(borders
.m_left
) && m_right
.EqPartial(borders
.m_right
) &&
11106 m_top
.EqPartial(borders
.m_top
) && m_bottom
.EqPartial(borders
.m_bottom
);
11109 // Apply border to 'this', but not if the same as compareWith
11110 bool wxTextAttrBorders::Apply(const wxTextAttrBorders
& borders
, const wxTextAttrBorders
* compareWith
)
11112 m_left
.Apply(borders
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextAttrBorder
*) NULL
);
11113 m_right
.Apply(borders
.m_right
, compareWith
? (& compareWith
->m_right
) : (const wxTextAttrBorder
*) NULL
);
11114 m_top
.Apply(borders
.m_top
, compareWith
? (& compareWith
->m_top
) : (const wxTextAttrBorder
*) NULL
);
11115 m_bottom
.Apply(borders
.m_bottom
, compareWith
? (& compareWith
->m_bottom
) : (const wxTextAttrBorder
*) NULL
);
11119 // Remove specified attributes from this object
11120 bool wxTextAttrBorders::RemoveStyle(const wxTextAttrBorders
& attr
)
11122 m_left
.RemoveStyle(attr
.m_left
);
11123 m_right
.RemoveStyle(attr
.m_right
);
11124 m_top
.RemoveStyle(attr
.m_top
);
11125 m_bottom
.RemoveStyle(attr
.m_bottom
);
11129 // Collects the attributes that are common to a range of content, building up a note of
11130 // which attributes are absent in some objects and which clash in some objects.
11131 void wxTextAttrBorders::CollectCommonAttributes(const wxTextAttrBorders
& attr
, wxTextAttrBorders
& clashingAttr
, wxTextAttrBorders
& absentAttr
)
11133 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
11134 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
11135 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
11136 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
11139 // Set style of all borders
11140 void wxTextAttrBorders::SetStyle(int style
)
11142 m_left
.SetStyle(style
);
11143 m_right
.SetStyle(style
);
11144 m_top
.SetStyle(style
);
11145 m_bottom
.SetStyle(style
);
11148 // Set colour of all borders
11149 void wxTextAttrBorders::SetColour(unsigned long colour
)
11151 m_left
.SetColour(colour
);
11152 m_right
.SetColour(colour
);
11153 m_top
.SetColour(colour
);
11154 m_bottom
.SetColour(colour
);
11157 void wxTextAttrBorders::SetColour(const wxColour
& colour
)
11159 m_left
.SetColour(colour
);
11160 m_right
.SetColour(colour
);
11161 m_top
.SetColour(colour
);
11162 m_bottom
.SetColour(colour
);
11165 // Set width of all borders
11166 void wxTextAttrBorders::SetWidth(const wxTextAttrDimension
& width
)
11168 m_left
.SetWidth(width
);
11169 m_right
.SetWidth(width
);
11170 m_top
.SetWidth(width
);
11171 m_bottom
.SetWidth(width
);
11174 // Partial equality test
11175 bool wxTextAttrDimension::EqPartial(const wxTextAttrDimension
& dim
) const
11177 if (dim
.IsValid() && IsValid() && !((*this) == dim
))
11183 bool wxTextAttrDimension::Apply(const wxTextAttrDimension
& dim
, const wxTextAttrDimension
* compareWith
)
11187 if (!(compareWith
&& dim
== (*compareWith
)))
11194 // Collects the attributes that are common to a range of content, building up a note of
11195 // which attributes are absent in some objects and which clash in some objects.
11196 void wxTextAttrDimension::CollectCommonAttributes(const wxTextAttrDimension
& attr
, wxTextAttrDimension
& clashingAttr
, wxTextAttrDimension
& absentAttr
)
11198 if (attr
.IsValid())
11200 if (!clashingAttr
.IsValid() && !absentAttr
.IsValid())
11204 if (!((*this) == attr
))
11206 clashingAttr
.SetValid(true);
11215 absentAttr
.SetValid(true);
11218 wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(wxDC
& dc
, double scale
, const wxSize
& parentSize
)
11220 m_ppi
= dc
.GetPPI().x
; m_scale
= scale
; m_parentSize
= parentSize
;
11223 wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(int ppi
, double scale
, const wxSize
& parentSize
)
11225 m_ppi
= ppi
; m_scale
= scale
; m_parentSize
= parentSize
;
11228 int wxTextAttrDimensionConverter::ConvertTenthsMMToPixels(int units
) const
11230 return wxRichTextObject::ConvertTenthsMMToPixels(m_ppi
, units
, m_scale
);
11233 int wxTextAttrDimensionConverter::ConvertPixelsToTenthsMM(int pixels
) const
11235 return wxRichTextObject::ConvertPixelsToTenthsMM(m_ppi
, pixels
, m_scale
);
11238 int wxTextAttrDimensionConverter::GetPixels(const wxTextAttrDimension
& dim
, int direction
) const
11240 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
11241 return ConvertTenthsMMToPixels(dim
.GetValue());
11242 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS
)
11243 return dim
.GetValue();
11244 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE
)
11246 wxASSERT(m_parentSize
!= wxDefaultSize
);
11247 if (direction
== wxHORIZONTAL
)
11248 return (int) (double(m_parentSize
.x
) * double(dim
.GetValue()) / 100.0);
11250 return (int) (double(m_parentSize
.y
) * double(dim
.GetValue()) / 100.0);
11259 int wxTextAttrDimensionConverter::GetTenthsMM(const wxTextAttrDimension
& dim
) const
11261 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
11262 return dim
.GetValue();
11263 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS
)
11264 return ConvertPixelsToTenthsMM(dim
.GetValue());
11272 // Partial equality test
11273 bool wxTextAttrDimensions::EqPartial(const wxTextAttrDimensions
& dims
) const
11275 if (!m_left
.EqPartial(dims
.m_left
))
11278 if (!m_right
.EqPartial(dims
.m_right
))
11281 if (!m_top
.EqPartial(dims
.m_top
))
11284 if (!m_bottom
.EqPartial(dims
.m_bottom
))
11290 // Apply border to 'this', but not if the same as compareWith
11291 bool wxTextAttrDimensions::Apply(const wxTextAttrDimensions
& dims
, const wxTextAttrDimensions
* compareWith
)
11293 m_left
.Apply(dims
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextAttrDimension
*) NULL
);
11294 m_right
.Apply(dims
.m_right
, compareWith
? (& compareWith
->m_right
): (const wxTextAttrDimension
*) NULL
);
11295 m_top
.Apply(dims
.m_top
, compareWith
? (& compareWith
->m_top
): (const wxTextAttrDimension
*) NULL
);
11296 m_bottom
.Apply(dims
.m_bottom
, compareWith
? (& compareWith
->m_bottom
): (const wxTextAttrDimension
*) NULL
);
11301 // Remove specified attributes from this object
11302 bool wxTextAttrDimensions::RemoveStyle(const wxTextAttrDimensions
& attr
)
11304 if (attr
.m_left
.IsValid())
11306 if (attr
.m_right
.IsValid())
11308 if (attr
.m_top
.IsValid())
11310 if (attr
.m_bottom
.IsValid())
11316 // Collects the attributes that are common to a range of content, building up a note of
11317 // which attributes are absent in some objects and which clash in some objects.
11318 void wxTextAttrDimensions::CollectCommonAttributes(const wxTextAttrDimensions
& attr
, wxTextAttrDimensions
& clashingAttr
, wxTextAttrDimensions
& absentAttr
)
11320 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
11321 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
11322 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
11323 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
11326 // Partial equality test
11327 bool wxTextAttrSize::EqPartial(const wxTextAttrSize
& size
) const
11329 if (!m_width
.EqPartial(size
.m_width
))
11332 if (!m_height
.EqPartial(size
.m_height
))
11338 // Apply border to 'this', but not if the same as compareWith
11339 bool wxTextAttrSize::Apply(const wxTextAttrSize
& size
, const wxTextAttrSize
* compareWith
)
11341 m_width
.Apply(size
.m_width
, compareWith
? (& compareWith
->m_width
) : (const wxTextAttrDimension
*) NULL
);
11342 m_height
.Apply(size
.m_height
, compareWith
? (& compareWith
->m_height
): (const wxTextAttrDimension
*) NULL
);
11347 // Remove specified attributes from this object
11348 bool wxTextAttrSize::RemoveStyle(const wxTextAttrSize
& attr
)
11350 if (attr
.m_width
.IsValid())
11352 if (attr
.m_height
.IsValid())
11358 // Collects the attributes that are common to a range of content, building up a note of
11359 // which attributes are absent in some objects and which clash in some objects.
11360 void wxTextAttrSize::CollectCommonAttributes(const wxTextAttrSize
& attr
, wxTextAttrSize
& clashingAttr
, wxTextAttrSize
& absentAttr
)
11362 m_width
.CollectCommonAttributes(attr
.m_width
, clashingAttr
.m_width
, absentAttr
.m_width
);
11363 m_height
.CollectCommonAttributes(attr
.m_height
, clashingAttr
.m_height
, absentAttr
.m_height
);
11366 // Collects the attributes that are common to a range of content, building up a note of
11367 // which attributes are absent in some objects and which clash in some objects.
11368 void wxTextAttrCollectCommonAttributes(wxTextAttr
& currentStyle
, const wxTextAttr
& attr
, wxTextAttr
& clashingAttr
, wxTextAttr
& absentAttr
)
11370 absentAttr
.SetFlags(absentAttr
.GetFlags() | (~attr
.GetFlags() & wxTEXT_ATTR_ALL
));
11371 absentAttr
.SetTextEffectFlags(absentAttr
.GetTextEffectFlags() | (~attr
.GetTextEffectFlags() & 0xFFFF));
11373 long forbiddenFlags
= clashingAttr
.GetFlags()|absentAttr
.GetFlags();
11375 if (attr
.HasFont())
11377 if (attr
.HasFontSize() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_SIZE
))
11379 if (currentStyle
.HasFontSize())
11381 if (currentStyle
.GetFontSize() != attr
.GetFontSize())
11383 // Clash of attr - mark as such
11384 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_SIZE
);
11385 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_SIZE
);
11389 currentStyle
.SetFontSize(attr
.GetFontSize());
11392 if (attr
.HasFontItalic() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_ITALIC
))
11394 if (currentStyle
.HasFontItalic())
11396 if (currentStyle
.GetFontStyle() != attr
.GetFontStyle())
11398 // Clash of attr - mark as such
11399 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_ITALIC
);
11400 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC
);
11404 currentStyle
.SetFontStyle(attr
.GetFontStyle());
11407 if (attr
.HasFontFamily() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FAMILY
))
11409 if (currentStyle
.HasFontFamily())
11411 if (currentStyle
.GetFontFamily() != attr
.GetFontFamily())
11413 // Clash of attr - mark as such
11414 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FAMILY
);
11415 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY
);
11419 currentStyle
.SetFontFamily(attr
.GetFontFamily());
11422 if (attr
.HasFontWeight() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_WEIGHT
))
11424 if (currentStyle
.HasFontWeight())
11426 if (currentStyle
.GetFontWeight() != attr
.GetFontWeight())
11428 // Clash of attr - mark as such
11429 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_WEIGHT
);
11430 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT
);
11434 currentStyle
.SetFontWeight(attr
.GetFontWeight());
11437 if (attr
.HasFontFaceName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FACE
))
11439 if (currentStyle
.HasFontFaceName())
11441 wxString
faceName1(currentStyle
.GetFontFaceName());
11442 wxString
faceName2(attr
.GetFontFaceName());
11444 if (faceName1
!= faceName2
)
11446 // Clash of attr - mark as such
11447 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FACE
);
11448 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FACE
);
11452 currentStyle
.SetFontFaceName(attr
.GetFontFaceName());
11455 if (attr
.HasFontUnderlined() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_UNDERLINE
))
11457 if (currentStyle
.HasFontUnderlined())
11459 if (currentStyle
.GetFontUnderlined() != attr
.GetFontUnderlined())
11461 // Clash of attr - mark as such
11462 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
11463 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
11467 currentStyle
.SetFontUnderlined(attr
.GetFontUnderlined());
11471 if (attr
.HasTextColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TEXT_COLOUR
))
11473 if (currentStyle
.HasTextColour())
11475 if (currentStyle
.GetTextColour() != attr
.GetTextColour())
11477 // Clash of attr - mark as such
11478 clashingAttr
.AddFlag(wxTEXT_ATTR_TEXT_COLOUR
);
11479 currentStyle
.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR
);
11483 currentStyle
.SetTextColour(attr
.GetTextColour());
11486 if (attr
.HasBackgroundColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
11488 if (currentStyle
.HasBackgroundColour())
11490 if (currentStyle
.GetBackgroundColour() != attr
.GetBackgroundColour())
11492 // Clash of attr - mark as such
11493 clashingAttr
.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
11494 currentStyle
.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
11498 currentStyle
.SetBackgroundColour(attr
.GetBackgroundColour());
11501 if (attr
.HasAlignment() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_ALIGNMENT
))
11503 if (currentStyle
.HasAlignment())
11505 if (currentStyle
.GetAlignment() != attr
.GetAlignment())
11507 // Clash of attr - mark as such
11508 clashingAttr
.AddFlag(wxTEXT_ATTR_ALIGNMENT
);
11509 currentStyle
.RemoveFlag(wxTEXT_ATTR_ALIGNMENT
);
11513 currentStyle
.SetAlignment(attr
.GetAlignment());
11516 if (attr
.HasTabs() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TABS
))
11518 if (currentStyle
.HasTabs())
11520 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), attr
.GetTabs()))
11522 // Clash of attr - mark as such
11523 clashingAttr
.AddFlag(wxTEXT_ATTR_TABS
);
11524 currentStyle
.RemoveFlag(wxTEXT_ATTR_TABS
);
11528 currentStyle
.SetTabs(attr
.GetTabs());
11531 if (attr
.HasLeftIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LEFT_INDENT
))
11533 if (currentStyle
.HasLeftIndent())
11535 if (currentStyle
.GetLeftIndent() != attr
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != attr
.GetLeftSubIndent())
11537 // Clash of attr - mark as such
11538 clashingAttr
.AddFlag(wxTEXT_ATTR_LEFT_INDENT
);
11539 currentStyle
.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT
);
11543 currentStyle
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
11546 if (attr
.HasRightIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_RIGHT_INDENT
))
11548 if (currentStyle
.HasRightIndent())
11550 if (currentStyle
.GetRightIndent() != attr
.GetRightIndent())
11552 // Clash of attr - mark as such
11553 clashingAttr
.AddFlag(wxTEXT_ATTR_RIGHT_INDENT
);
11554 currentStyle
.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT
);
11558 currentStyle
.SetRightIndent(attr
.GetRightIndent());
11561 if (attr
.HasParagraphSpacingAfter() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
11563 if (currentStyle
.HasParagraphSpacingAfter())
11565 if (currentStyle
.GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter())
11567 // Clash of attr - mark as such
11568 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
11569 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
11573 currentStyle
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
11576 if (attr
.HasParagraphSpacingBefore() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
11578 if (currentStyle
.HasParagraphSpacingBefore())
11580 if (currentStyle
.GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore())
11582 // Clash of attr - mark as such
11583 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
11584 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
11588 currentStyle
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
11591 if (attr
.HasLineSpacing() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LINE_SPACING
))
11593 if (currentStyle
.HasLineSpacing())
11595 if (currentStyle
.GetLineSpacing() != attr
.GetLineSpacing())
11597 // Clash of attr - mark as such
11598 clashingAttr
.AddFlag(wxTEXT_ATTR_LINE_SPACING
);
11599 currentStyle
.RemoveFlag(wxTEXT_ATTR_LINE_SPACING
);
11603 currentStyle
.SetLineSpacing(attr
.GetLineSpacing());
11606 if (attr
.HasCharacterStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
11608 if (currentStyle
.HasCharacterStyleName())
11610 if (currentStyle
.GetCharacterStyleName() != attr
.GetCharacterStyleName())
11612 // Clash of attr - mark as such
11613 clashingAttr
.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
11614 currentStyle
.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
11618 currentStyle
.SetCharacterStyleName(attr
.GetCharacterStyleName());
11621 if (attr
.HasParagraphStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
11623 if (currentStyle
.HasParagraphStyleName())
11625 if (currentStyle
.GetParagraphStyleName() != attr
.GetParagraphStyleName())
11627 // Clash of attr - mark as such
11628 clashingAttr
.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
11629 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
11633 currentStyle
.SetParagraphStyleName(attr
.GetParagraphStyleName());
11636 if (attr
.HasListStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LIST_STYLE_NAME
))
11638 if (currentStyle
.HasListStyleName())
11640 if (currentStyle
.GetListStyleName() != attr
.GetListStyleName())
11642 // Clash of attr - mark as such
11643 clashingAttr
.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
11644 currentStyle
.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
11648 currentStyle
.SetListStyleName(attr
.GetListStyleName());
11651 if (attr
.HasBulletStyle() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_STYLE
))
11653 if (currentStyle
.HasBulletStyle())
11655 if (currentStyle
.GetBulletStyle() != attr
.GetBulletStyle())
11657 // Clash of attr - mark as such
11658 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_STYLE
);
11659 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE
);
11663 currentStyle
.SetBulletStyle(attr
.GetBulletStyle());
11666 if (attr
.HasBulletNumber() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NUMBER
))
11668 if (currentStyle
.HasBulletNumber())
11670 if (currentStyle
.GetBulletNumber() != attr
.GetBulletNumber())
11672 // Clash of attr - mark as such
11673 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NUMBER
);
11674 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER
);
11678 currentStyle
.SetBulletNumber(attr
.GetBulletNumber());
11681 if (attr
.HasBulletText() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_TEXT
))
11683 if (currentStyle
.HasBulletText())
11685 if (currentStyle
.GetBulletText() != attr
.GetBulletText())
11687 // Clash of attr - mark as such
11688 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_TEXT
);
11689 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT
);
11694 currentStyle
.SetBulletText(attr
.GetBulletText());
11695 currentStyle
.SetBulletFont(attr
.GetBulletFont());
11699 if (attr
.HasBulletName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NAME
))
11701 if (currentStyle
.HasBulletName())
11703 if (currentStyle
.GetBulletName() != attr
.GetBulletName())
11705 // Clash of attr - mark as such
11706 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NAME
);
11707 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NAME
);
11712 currentStyle
.SetBulletName(attr
.GetBulletName());
11716 if (attr
.HasURL() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_URL
))
11718 if (currentStyle
.HasURL())
11720 if (currentStyle
.GetURL() != attr
.GetURL())
11722 // Clash of attr - mark as such
11723 clashingAttr
.AddFlag(wxTEXT_ATTR_URL
);
11724 currentStyle
.RemoveFlag(wxTEXT_ATTR_URL
);
11729 currentStyle
.SetURL(attr
.GetURL());
11733 if (attr
.HasTextEffects() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_EFFECTS
))
11735 if (currentStyle
.HasTextEffects())
11737 // We need to find the bits in the new attr that are different:
11738 // just look at those bits that are specified by the new attr.
11740 // We need to remove the bits and flags that are not common between current attr
11741 // and new attr. In so doing we need to take account of the styles absent from one or more of the
11742 // previous styles.
11744 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & attr
.GetTextEffectFlags();
11745 int newRelevantTextEffects
= attr
.GetTextEffects() & attr
.GetTextEffectFlags();
11747 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
11749 // Find the text effects that were different, using XOR
11750 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
11752 // Clash of attr - mark as such
11753 clashingAttr
.SetTextEffectFlags(clashingAttr
.GetTextEffectFlags() | differentEffects
);
11754 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
11759 currentStyle
.SetTextEffects(attr
.GetTextEffects());
11760 currentStyle
.SetTextEffectFlags(attr
.GetTextEffectFlags());
11763 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
11764 // that we've looked at so far
11765 currentStyle
.SetTextEffects(currentStyle
.GetTextEffects() & ~absentAttr
.GetTextEffectFlags());
11766 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~absentAttr
.GetTextEffectFlags());
11768 if (currentStyle
.GetTextEffectFlags() == 0)
11769 currentStyle
.RemoveFlag(wxTEXT_ATTR_EFFECTS
);
11772 if (attr
.HasOutlineLevel() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_OUTLINE_LEVEL
))
11774 if (currentStyle
.HasOutlineLevel())
11776 if (currentStyle
.GetOutlineLevel() != attr
.GetOutlineLevel())
11778 // Clash of attr - mark as such
11779 clashingAttr
.AddFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
11780 currentStyle
.RemoveFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
11784 currentStyle
.SetOutlineLevel(attr
.GetOutlineLevel());
11788 WX_DEFINE_OBJARRAY(wxRichTextVariantArray
);
11790 IMPLEMENT_DYNAMIC_CLASS(wxRichTextProperties
, wxObject
)
11792 bool wxRichTextProperties::operator==(const wxRichTextProperties
& props
) const
11794 if (m_properties
.GetCount() != props
.GetCount())
11798 for (i
= 0; i
< m_properties
.GetCount(); i
++)
11800 const wxVariant
& var1
= m_properties
[i
];
11801 int idx
= props
.Find(var1
.GetName());
11804 const wxVariant
& var2
= props
.m_properties
[idx
];
11805 if (!(var1
== var2
))
11812 wxArrayString
wxRichTextProperties::GetPropertyNames() const
11816 for (i
= 0; i
< m_properties
.GetCount(); i
++)
11818 arr
.Add(m_properties
[i
].GetName());
11823 int wxRichTextProperties::Find(const wxString
& name
) const
11826 for (i
= 0; i
< m_properties
.GetCount(); i
++)
11828 if (m_properties
[i
].GetName() == name
)
11834 wxVariant
* wxRichTextProperties::FindOrCreateProperty(const wxString
& name
)
11836 int idx
= Find(name
);
11837 if (idx
== wxNOT_FOUND
)
11838 SetProperty(name
, wxString());
11840 if (idx
!= wxNOT_FOUND
)
11842 return & (*this)[idx
];
11848 const wxVariant
& wxRichTextProperties::GetProperty(const wxString
& name
) const
11850 static const wxVariant nullVariant
;
11851 int idx
= Find(name
);
11853 return m_properties
[idx
];
11855 return nullVariant
;
11858 wxString
wxRichTextProperties::GetPropertyString(const wxString
& name
) const
11860 return GetProperty(name
).GetString();
11863 long wxRichTextProperties::GetPropertyLong(const wxString
& name
) const
11865 return GetProperty(name
).GetLong();
11868 bool wxRichTextProperties::GetPropertyBool(const wxString
& name
) const
11870 return GetProperty(name
).GetBool();
11873 double wxRichTextProperties::GetPropertyDouble(const wxString
& name
) const
11875 return GetProperty(name
).GetDouble();
11878 void wxRichTextProperties::SetProperty(const wxVariant
& variant
)
11880 wxASSERT(!variant
.GetName().IsEmpty());
11882 int idx
= Find(variant
.GetName());
11885 m_properties
.Add(variant
);
11887 m_properties
[idx
] = variant
;
11890 void wxRichTextProperties::SetProperty(const wxString
& name
, const wxVariant
& variant
)
11892 int idx
= Find(name
);
11893 wxVariant
var(variant
);
11897 m_properties
.Add(var
);
11899 m_properties
[idx
] = var
;
11902 void wxRichTextProperties::SetProperty(const wxString
& name
, const wxString
& value
)
11904 SetProperty(name
, wxVariant(value
, name
));
11907 void wxRichTextProperties::SetProperty(const wxString
& name
, long value
)
11909 SetProperty(name
, wxVariant(value
, name
));
11912 void wxRichTextProperties::SetProperty(const wxString
& name
, double value
)
11914 SetProperty(name
, wxVariant(value
, name
));
11917 void wxRichTextProperties::SetProperty(const wxString
& name
, bool value
)
11919 SetProperty(name
, wxVariant(value
, name
));
11922 wxRichTextObject
* wxRichTextObjectAddress::GetObject(wxRichTextParagraphLayoutBox
* topLevelContainer
) const
11924 if (m_address
.GetCount() == 0)
11925 return topLevelContainer
;
11927 wxRichTextCompositeObject
* p
= topLevelContainer
;
11929 while (p
&& i
< m_address
.GetCount())
11931 int pos
= m_address
[i
];
11932 wxASSERT(pos
>= 0 && pos
< (int) p
->GetChildren().GetCount());
11933 if (pos
< 0 || pos
>= (int) p
->GetChildren().GetCount())
11936 wxRichTextObject
* p1
= p
->GetChild(pos
);
11937 if (i
== (m_address
.GetCount()-1))
11940 p
= wxDynamicCast(p1
, wxRichTextCompositeObject
);
11946 bool wxRichTextObjectAddress::Create(wxRichTextParagraphLayoutBox
* topLevelContainer
, wxRichTextObject
* obj
)
11950 if (topLevelContainer
== obj
)
11953 wxRichTextObject
* o
= obj
;
11956 wxRichTextCompositeObject
* p
= wxDynamicCast(o
->GetParent(), wxRichTextCompositeObject
);
11960 int pos
= p
->GetChildren().IndexOf(o
);
11964 m_address
.Insert(pos
, 0);
11966 if (p
== topLevelContainer
)
11975 bool wxRichTextSelection::operator==(const wxRichTextSelection
& sel
) const
11977 if (m_container
!= sel
.m_container
)
11979 if (m_ranges
.GetCount() != sel
.m_ranges
.GetCount())
11982 for (i
= 0; i
< m_ranges
.GetCount(); i
++)
11983 if (!(m_ranges
[i
] == sel
.m_ranges
[i
]))
11988 // Get the selections appropriate to the specified object, if any; returns wxRICHTEXT_NO_SELECTION if none
11989 // or none at the level of the object's container.
11990 wxRichTextRangeArray
wxRichTextSelection::GetSelectionForObject(wxRichTextObject
* obj
) const
11994 wxRichTextParagraphLayoutBox
* container
= obj
->GetParentContainer();
11996 if (container
== m_container
)
11999 container
= obj
->GetContainer();
12002 if (container
->GetParent())
12004 // If we found that our object's container is within the range of
12005 // a selection higher up, then assume the whole original object
12006 // is also selected.
12007 wxRichTextParagraphLayoutBox
* parentContainer
= container
->GetParentContainer();
12008 if (parentContainer
== m_container
)
12010 if (WithinSelection(container
->GetRange().GetStart(), m_ranges
))
12012 wxRichTextRangeArray ranges
;
12013 ranges
.Add(obj
->GetRange());
12018 container
= parentContainer
;
12027 return wxRichTextRangeArray();
12030 // Is the given position within the selection?
12031 bool wxRichTextSelection::WithinSelection(long pos
, wxRichTextObject
* obj
) const
12037 wxRichTextRangeArray selectionRanges
= GetSelectionForObject(obj
);
12038 return WithinSelection(pos
, selectionRanges
);
12042 // Is the given position within the selection range?
12043 bool wxRichTextSelection::WithinSelection(long pos
, const wxRichTextRangeArray
& ranges
)
12046 for (i
= 0; i
< ranges
.GetCount(); i
++)
12048 const wxRichTextRange
& range
= ranges
[i
];
12049 if (pos
>= range
.GetStart() && pos
<= range
.GetEnd())
12055 // Is the given range completely within the selection range?
12056 bool wxRichTextSelection::WithinSelection(const wxRichTextRange
& range
, const wxRichTextRangeArray
& ranges
)
12059 for (i
= 0; i
< ranges
.GetCount(); i
++)
12061 const wxRichTextRange
& eachRange
= ranges
[i
];
12062 if (range
.IsWithin(eachRange
))