+// Switch off if the platform doesn't like it for some reason
+#define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
+
+// Use GetPartialTextExtents for platforms that support it natively
+#define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
+
+const wxChar wxRichTextLineBreakChar = (wxChar) 29;
+
+// Helper classes for floating layout
+struct wxRichTextFloatRectMap
+{
+ wxRichTextFloatRectMap(int sY, int eY, int w, wxRichTextObject* obj)
+ {
+ startY = sY;
+ endY = eY;
+ width = w;
+ anchor = obj;
+ }
+
+ int startY, endY;
+ int width;
+ wxRichTextObject* anchor;
+};
+
+WX_DEFINE_SORTED_ARRAY(wxRichTextFloatRectMap*, wxRichTextFloatRectMapArray);
+
+int wxRichTextFloatRectMapCmp(wxRichTextFloatRectMap* r1, wxRichTextFloatRectMap* r2)
+{
+ return r1->startY - r2->startY;
+}
+
+class wxRichTextFloatCollector
+{
+public:
+ wxRichTextFloatCollector(int width);
+ ~wxRichTextFloatCollector();
+
+ // Collect the floating objects info in the given paragraph
+ void CollectFloat(wxRichTextParagraph* para);
+ void CollectFloat(wxRichTextParagraph* para, wxRichTextObject* floating);
+
+ // Return the last paragraph we collected
+ wxRichTextParagraph* LastParagraph();
+
+ // Given the start y position and the height of the line,
+ // find out how wide the line can be
+ wxRect GetAvailableRect(int startY, int endY);
+
+ // Given a floating box, find its fit position
+ int GetFitPosition(int direction, int start, int height) const;
+ int GetFitPosition(const wxRichTextFloatRectMapArray& array, int start, int height) const;
+
+ // Find the last y position
+ int GetLastRectBottom();
+
+ // Draw the floats inside a rect
+ void Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
+
+ // HitTest the floats
+ int HitTest(wxDC& dc, const wxPoint& pt, long& textPosition);
+
+ static int SearchAdjacentRect(const wxRichTextFloatRectMapArray& array, int point);
+
+ static int GetWidthFromFloatRect(const wxRichTextFloatRectMapArray& array, int index, int startY, int endY);
+
+ static void FreeFloatRectMapArray(wxRichTextFloatRectMapArray& array);
+
+ static void DrawFloat(const wxRichTextFloatRectMapArray& array, wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
+
+ static int HitTestFloat(const wxRichTextFloatRectMapArray& array, wxDC& WXUNUSED(dc), const wxPoint& pt, long& textPosition);
+
+private:
+ wxRichTextFloatRectMapArray m_left;
+ wxRichTextFloatRectMapArray m_right;
+ int m_width;
+ wxRichTextParagraph* m_para;
+};
+
+/*
+ * Binary search helper function
+ * The argument point is the Y coordinate, and this fuction
+ * always return the floating rect that contain this coordinate
+ * or under this coordinate.
+ */
+int wxRichTextFloatCollector::SearchAdjacentRect(const wxRichTextFloatRectMapArray& array, int point)
+{
+ int end = array.GetCount() - 1;
+ int start = 0;
+ int ret = 0;
+
+ wxASSERT(end >= 0);
+
+ while (true)
+ {
+ if (start > end)
+ {
+ break;
+ }
+
+ int mid = (start + end) / 2;
+ if (array[mid]->startY <= point && array[mid]->endY >= point)
+ return mid;
+ else if (array[mid]->startY > point)
+ {
+ end = mid - 1;
+ ret = mid;
+ }
+ else if (array[mid]->endY < point)
+ {
+ start = mid + 1;
+ ret = start;
+ }
+ }
+
+ return ret;
+}
+
+int wxRichTextFloatCollector::GetWidthFromFloatRect(const wxRichTextFloatRectMapArray& array, int index, int startY, int endY)
+{
+ int ret = 0;
+ int len = array.GetCount();
+
+ wxASSERT(index >= 0 && index < len);
+
+ if (array[index]->startY < startY && array[index]->endY > startY)
+ ret = ret < array[index]->width ? array[index]->width : ret;
+ while (index < len && array[index]->startY <= endY)
+ {
+ ret = ret < array[index]->width ? array[index]->width : ret;
+ index++;
+ }
+
+ return ret;
+}
+
+wxRichTextFloatCollector::wxRichTextFloatCollector(int width) : m_left(wxRichTextFloatRectMapCmp), m_right(wxRichTextFloatRectMapCmp)
+{
+ m_width = width;
+ m_para = NULL;
+}
+
+void wxRichTextFloatCollector::FreeFloatRectMapArray(wxRichTextFloatRectMapArray& array)
+{
+ int len = array.GetCount();
+ for (int i = 0; i < len; i++)
+ delete array[i];
+}
+
+wxRichTextFloatCollector::~wxRichTextFloatCollector()
+{
+ FreeFloatRectMapArray(m_left);
+ FreeFloatRectMapArray(m_right);
+}
+
+int wxRichTextFloatCollector::GetFitPosition(const wxRichTextFloatRectMapArray& array, int start, int height) const
+{
+ if (array.GetCount() == 0)
+ return start;
+
+ unsigned int i = SearchAdjacentRect(array, start);
+ int last = start;
+ while (i < array.GetCount())
+ {
+ if (array[i]->startY - last >= height)
+ return last + 1;
+ last = array[i]->endY;
+ i++;
+ }
+
+ return last + 1;
+}
+
+int wxRichTextFloatCollector::GetFitPosition(int direction, int start, int height) const
+{
+ if (direction == wxTEXT_BOX_ATTR_FLOAT_LEFT)
+ return GetFitPosition(m_left, start, height);
+ else if (direction == wxTEXT_BOX_ATTR_FLOAT_RIGHT)
+ return GetFitPosition(m_right, start, height);
+ else
+ {
+ wxASSERT("Never should be here");
+ return start;
+ }
+}
+
+void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph* para, wxRichTextObject* floating)
+{
+ int direction = floating->GetFloatDirection();
+
+ wxPoint pos = floating->GetPosition();
+ wxSize size = floating->GetCachedSize();
+ wxRichTextFloatRectMap *map = new wxRichTextFloatRectMap(pos.y, pos.y + size.y, size.x, floating);
+ switch (direction)
+ {
+ case wxTEXT_BOX_ATTR_FLOAT_NONE:
+ delete map;
+ break;
+ case wxTEXT_BOX_ATTR_FLOAT_LEFT:
+ // Just a not-enough simple assertion
+ wxASSERT (m_left.Index(map) == wxNOT_FOUND);
+ m_left.Add(map);
+ break;
+ case wxTEXT_BOX_ATTR_FLOAT_RIGHT:
+ wxASSERT (m_right.Index(map) == wxNOT_FOUND);
+ m_right.Add(map);
+ break;
+ default:
+ delete map;
+ wxASSERT("Must some error occurs");
+ }
+
+ m_para = para;
+}
+
+void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph* para)
+{
+ wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst();
+ while (node)
+ {
+ wxRichTextObject* floating = node->GetData();
+
+ if (floating->IsFloating())
+ {
+ CollectFloat(para, floating);
+ }
+
+ node = node->GetNext();
+ }
+
+ m_para = para;
+}
+
+wxRichTextParagraph* wxRichTextFloatCollector::LastParagraph()
+{
+ return m_para;
+}
+
+wxRect wxRichTextFloatCollector::GetAvailableRect(int startY, int endY)
+{
+ int widthLeft = 0, widthRight = 0;
+ if (m_left.GetCount() != 0)
+ {
+ unsigned int i = SearchAdjacentRect(m_left, startY);
+ if (i >= 0 && i < m_left.GetCount())
+ widthLeft = GetWidthFromFloatRect(m_left, i, startY, endY);
+ }
+ if (m_right.GetCount() != 0)
+ {
+ unsigned int j = SearchAdjacentRect(m_right, startY);
+ if (j >= 0 && j < m_right.GetCount())
+ widthRight = GetWidthFromFloatRect(m_right, j, startY, endY);
+ }
+
+ return wxRect(widthLeft, 0, m_width - widthLeft - widthRight, 0);
+}
+
+int wxRichTextFloatCollector::GetLastRectBottom()
+{
+ int ret = 0;
+ int len = m_left.GetCount();
+ if (len) {
+ ret = ret > m_left[len-1]->endY ? ret : m_left[len-1]->endY;
+ }
+ len = m_right.GetCount();
+ if (len) {
+ ret = ret > m_right[len-1]->endY ? ret : m_right[len-1]->endY;
+ }
+
+ return ret;
+}
+
+void wxRichTextFloatCollector::DrawFloat(const wxRichTextFloatRectMapArray& array, wxDC& dc, const wxRichTextRange& WXUNUSED(range), const wxRichTextRange& WXUNUSED(selectionRange), const wxRect& rect, int descent, int style)
+{
+ int start = rect.y;
+ int end = rect.y + rect.height;
+ unsigned int i, j;
+ i = SearchAdjacentRect(array, start);
+ if (i < 0 || i >= array.GetCount())
+ return;
+ j = SearchAdjacentRect(array, end);
+ if (j < 0 || j >= array.GetCount())
+ j = array.GetCount() - 1;
+ while (i <= j)
+ {
+ wxRichTextObject* obj = array[i]->anchor;
+ wxRichTextRange r = obj->GetRange();
+ obj->Draw(dc, r, wxRichTextRange(0, -1), wxRect(obj->GetPosition(), obj->GetCachedSize()), descent, style);
+ i++;
+ }
+}
+
+void wxRichTextFloatCollector::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style)
+{
+ if (m_left.GetCount() > 0)
+ DrawFloat(m_left, dc, range, selectionRange, rect, descent, style);
+ if (m_right.GetCount() > 0)
+ DrawFloat(m_right, dc, range, selectionRange, rect, descent, style);
+}
+
+int wxRichTextFloatCollector::HitTestFloat(const wxRichTextFloatRectMapArray& array, wxDC& WXUNUSED(dc), const wxPoint& pt, long& textPosition)
+{
+ unsigned int i;
+ if (array.GetCount() == 0)
+ return wxRICHTEXT_HITTEST_NONE;
+ i = SearchAdjacentRect(array, pt.y);
+ if (i < 0 || i >= array.GetCount())
+ return wxRICHTEXT_HITTEST_NONE;
+ wxPoint point = array[i]->anchor->GetPosition();
+ wxSize size = array[i]->anchor->GetCachedSize();
+ if (point.x <= pt.x && point.x + size.x >= pt.x
+ && point.y <= pt.y && point.y + size.y >= pt.y)
+ {
+ textPosition = array[i]->anchor->GetRange().GetStart();
+ if (pt.x > (pt.x + pt.x + size.x) / 2)
+ return wxRICHTEXT_HITTEST_BEFORE;
+ else
+ return wxRICHTEXT_HITTEST_AFTER;
+ }
+
+ return wxRICHTEXT_HITTEST_NONE;
+}
+
+int wxRichTextFloatCollector::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
+{
+ int ret = HitTestFloat(m_left, dc, pt, textPosition);
+ if (ret == wxRICHTEXT_HITTEST_NONE)
+ {
+ ret = HitTestFloat(m_right, dc, pt, textPosition);
+ }
+ return ret;
+}
+
+// Helpers for efficiency
+inline void wxCheckSetFont(wxDC& dc, const wxFont& font)
+{
+ // JACS: did I do this some time ago when testing? Should we re-enable it?
+#if 0
+ const wxFont& font1 = dc.GetFont();
+ if (font1.IsOk() && font.IsOk())
+ {
+ if (font1.GetPointSize() == font.GetPointSize() &&
+ font1.GetFamily() == font.GetFamily() &&
+ font1.GetStyle() == font.GetStyle() &&
+ font1.GetWeight() == font.GetWeight() &&
+ font1.GetUnderlined() == font.GetUnderlined() &&
+ font1.GetFamily() == font.GetFamily() &&
+ font1.GetFaceName() == font.GetFaceName())
+ return;
+ }
+#endif
+ dc.SetFont(font);
+}
+
+inline void wxCheckSetPen(wxDC& dc, const wxPen& pen)
+{
+ const wxPen& pen1 = dc.GetPen();
+ if (pen1.IsOk() && pen.IsOk())
+ {
+ if (pen1.GetWidth() == pen.GetWidth() &&
+ pen1.GetStyle() == pen.GetStyle() &&
+ pen1.GetColour() == pen.GetColour())
+ return;
+ }
+ dc.SetPen(pen);
+}
+
+inline void wxCheckSetBrush(wxDC& dc, const wxBrush& brush)
+{
+ const wxBrush& brush1 = dc.GetBrush();
+ if (brush1.IsOk() && brush.IsOk())
+ {
+ if (brush1.GetStyle() == brush.GetStyle() &&
+ brush1.GetColour() == brush.GetColour())
+ return;
+ }
+ dc.SetBrush(brush);
+}
+