+ return true;
+ }
+ else
+ {
+ // TODO: if not a text object, insert at closest position, e.g. in front of it
+ }
+ }
+ else
+ {
+ // Add at end.
+ // Don't pass parent initially to suppress auto-setting of parent range.
+ // We'll do that at a higher level.
+ wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this);
+
+ AppendChild(textObject);
+ return true;
+ }
+
+ return false;
+}
+
+void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj)
+{
+ wxRichTextCompositeObject::Copy(obj);
+}
+
+/// Clear the cached lines
+void wxRichTextParagraph::ClearLines()
+{
+ WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines);
+}
+
+/// Get/set the object size for the given range. Returns false if the range
+/// is invalid for this object.
+bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position, wxArrayInt* partialExtents) const
+{
+ if (!range.IsWithin(GetRange()))
+ return false;
+
+ if (flags & wxRICHTEXT_UNFORMATTED)
+ {
+ // Just use unformatted data, assume no line breaks
+ // TODO: take into account line breaks
+
+ wxSize sz;
+
+ wxArrayInt childExtents;
+ wxArrayInt* p;
+ if (partialExtents)
+ p = & childExtents;
+ else
+ p = NULL;
+
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+
+ wxRichTextObject* child = node->GetData();
+ if (!child->GetRange().IsOutside(range))
+ {
+ // Floating objects have a zero size within the paragraph.
+ if (child->IsFloating())
+ {
+ if (partialExtents)
+ {
+ int lastSize;
+ if (partialExtents->GetCount() > 0)
+ lastSize = (*partialExtents)[partialExtents->GetCount()-1];
+ else
+ lastSize = 0;
+
+ partialExtents->Add(0 /* zero size */ + lastSize);
+ }
+ }
+ else
+ {
+ wxSize childSize;
+
+ wxRichTextRange rangeToUse = range;
+ rangeToUse.LimitTo(child->GetRange());
+ int childDescent = 0;
+
+ // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
+ // but it's only going to be used after caching has taken place.
+ if ((flags & wxRICHTEXT_HEIGHT_ONLY) && child->GetCachedSize().y != 0)
+ {
+ childDescent = child->GetDescent();
+ childSize = child->GetCachedSize();
+
+ sz.y = wxMax(sz.y, childSize.y);
+ sz.x += childSize.x;
+ descent = wxMax(descent, childDescent);
+ }
+ else if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y), p))
+ {
+ sz.y = wxMax(sz.y, childSize.y);
+ sz.x += childSize.x;
+ descent = wxMax(descent, childDescent);
+
+ if ((flags & wxRICHTEXT_CACHE_SIZE) && (rangeToUse == child->GetRange()))
+ {
+ child->SetCachedSize(childSize);
+ child->SetDescent(childDescent);
+ }
+
+ if (partialExtents)
+ {
+ int lastSize;
+ if (partialExtents->GetCount() > 0)
+ lastSize = (*partialExtents)[partialExtents->GetCount()-1];
+ else
+ lastSize = 0;
+
+ size_t i;
+ for (i = 0; i < childExtents.GetCount(); i++)
+ {
+ partialExtents->Add(childExtents[i] + lastSize);
+ }
+ }
+ }
+ }
+
+ if (p)
+ p->Clear();
+ }
+
+ node = node->GetNext();
+ }
+ size = sz;
+ }
+ else
+ {
+ // Use formatted data, with line breaks
+ wxSize sz;
+
+ // We're going to loop through each line, and then for each line,
+ // call GetRangeSize for the fragment that comprises that line.
+ // Only we have to do that multiple times within the line, because
+ // the line may be broken into pieces. For now ignore line break commands
+ // (so we can assume that getting the unformatted size for a fragment
+ // within a line is the actual size)
+
+ wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
+ while (node)
+ {
+ wxRichTextLine* line = node->GetData();
+ wxRichTextRange lineRange = line->GetAbsoluteRange();
+ if (!lineRange.IsOutside(range))
+ {
+ wxSize lineSize;
+
+ wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
+ while (node2)
+ {
+ wxRichTextObject* child = node2->GetData();
+
+ if (!child->IsFloating() && !child->GetRange().IsOutside(lineRange))
+ {
+ wxRichTextRange rangeToUse = lineRange;
+ rangeToUse.LimitTo(child->GetRange());
+
+ wxSize childSize;
+ int childDescent = 0;
+ if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y)))
+ {
+ lineSize.y = wxMax(lineSize.y, childSize.y);
+ lineSize.x += childSize.x;
+ }
+ descent = wxMax(descent, childDescent);
+ }
+
+ node2 = node2->GetNext();
+ }
+
+ // Increase size by a line (TODO: paragraph spacing)
+ sz.y += lineSize.y;
+ sz.x = wxMax(sz.x, lineSize.x);
+ }
+ node = node->GetNext();
+ }
+ size = sz;
+ }
+ return true;
+}
+
+/// Finds the absolute position and row height for the given character position
+bool wxRichTextParagraph::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
+{
+ if (index == -1)
+ {
+ wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0);
+ if (line)
+ *height = line->GetSize().y;
+ else
+ *height = dc.GetCharHeight();
+
+ // -1 means 'the start of the buffer'.
+ pt = GetPosition();
+ if (line)
+ pt = pt + line->GetPosition();
+
+ return true;
+ }
+
+ // The final position in a paragraph is taken to mean the position
+ // at the start of the next paragraph.
+ if (index == GetRange().GetEnd())
+ {
+ wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
+ wxASSERT( parent != NULL );
+
+ // Find the height at the next paragraph, if any
+ wxRichTextLine* line = parent->GetLineAtPosition(index + 1);
+ if (line)
+ {
+ *height = line->GetSize().y;
+ pt = line->GetAbsolutePosition();
+ }
+ else
+ {
+ *height = dc.GetCharHeight();
+ int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent());
+ pt = wxPoint(indent, GetCachedSize().y);
+ }
+
+ return true;
+ }
+
+ if (index < GetRange().GetStart() || index > GetRange().GetEnd())
+ return false;
+
+ wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
+ while (node)
+ {
+ wxRichTextLine* line = node->GetData();
+ wxRichTextRange lineRange = line->GetAbsoluteRange();
+ if (index >= lineRange.GetStart() && index <= lineRange.GetEnd())
+ {
+ // If this is the last point in the line, and we're forcing the
+ // returned value to be the start of the next line, do the required
+ // thing.
+ if (index == lineRange.GetEnd() && forceLineStart)
+ {
+ if (node->GetNext())
+ {
+ wxRichTextLine* nextLine = node->GetNext()->GetData();
+ *height = nextLine->GetSize().y;
+ pt = nextLine->GetAbsolutePosition();
+ return true;
+ }
+ }
+
+ pt.y = line->GetPosition().y + GetPosition().y;
+
+ wxRichTextRange r(lineRange.GetStart(), index);
+ wxSize rangeSize;
+ int descent = 0;
+
+ // We find the size of the line up to this point,
+ // then we can add this size to the line start position and
+ // paragraph start position to find the actual position.
+
+ if (GetRangeSize(r, rangeSize, descent, dc, wxRICHTEXT_UNFORMATTED, line->GetPosition()+ GetPosition()))
+ {
+ pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x;
+ *height = line->GetSize().y;
+
+ return true;
+ }
+
+ }
+
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+/// Hit-testing: returns a flag indicating hit test details, plus
+/// information about position
+int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
+{
+ wxPoint paraPos = GetPosition();
+
+ wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
+ while (node)
+ {
+ wxRichTextLine* line = node->GetData();
+ wxPoint linePos = paraPos + line->GetPosition();
+ wxSize lineSize = line->GetSize();
+ wxRichTextRange lineRange = line->GetAbsoluteRange();
+
+ if (pt.y <= linePos.y + lineSize.y)
+ {
+ if (pt.x < linePos.x)
+ {
+ textPosition = lineRange.GetStart();
+ return wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_OUTSIDE;
+ }
+ else if (pt.x >= (linePos.x + lineSize.x))
+ {
+ textPosition = lineRange.GetEnd();
+ return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE;
+ }
+ else
+ {
+#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
+ wxArrayInt partialExtents;
+
+ wxSize paraSize;
+ int paraDescent;
+
+ // This calculates the partial text extents
+ GetRangeSize(lineRange, paraSize, paraDescent, dc, wxRICHTEXT_UNFORMATTED, wxPoint(0,0), & partialExtents);
+
+ int lastX = linePos.x;
+ size_t i;
+ for (i = 0; i < partialExtents.GetCount(); i++)
+ {
+ int nextX = partialExtents[i] + linePos.x;
+
+ if (pt.x >= lastX && pt.x <= nextX)
+ {
+ textPosition = i + lineRange.GetStart(); // minus 1?
+
+ // So now we know it's between i-1 and i.
+ // Let's see if we can be more precise about
+ // which side of the position it's on.
+
+ int midPoint = (nextX + lastX)/2;
+ if (pt.x >= midPoint)
+ return wxRICHTEXT_HITTEST_AFTER;
+ else
+ return wxRICHTEXT_HITTEST_BEFORE;
+ }
+
+ lastX = nextX;
+ }
+#else
+ long i;
+ int lastX = linePos.x;
+ for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++)
+ {
+ wxSize childSize;
+ int descent = 0;
+
+ wxRichTextRange rangeToUse(lineRange.GetStart(), i);
+
+ GetRangeSize(rangeToUse, childSize, descent, dc, wxRICHTEXT_UNFORMATTED, linePos);
+
+ int nextX = childSize.x + linePos.x;
+
+ if (pt.x >= lastX && pt.x <= nextX)
+ {
+ textPosition = i;
+
+ // So now we know it's between i-1 and i.
+ // Let's see if we can be more precise about
+ // which side of the position it's on.
+
+ int midPoint = (nextX + lastX)/2;
+ if (pt.x >= midPoint)
+ return wxRICHTEXT_HITTEST_AFTER;
+ else
+ return wxRICHTEXT_HITTEST_BEFORE;
+ }
+ else
+ {
+ lastX = nextX;
+ }
+ }
+#endif
+ }
+ }
+
+ node = node->GetNext();
+ }
+
+ return wxRICHTEXT_HITTEST_NONE;
+}
+
+/// Split an object at this position if necessary, and return
+/// the previous object, or NULL if inserting at beginning.
+wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject)
+{
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxRichTextObject* child = node->GetData();
+
+ if (pos == child->GetRange().GetStart())
+ {
+ if (previousObject)
+ {
+ if (node->GetPrevious())
+ *previousObject = node->GetPrevious()->GetData();
+ else
+ *previousObject = NULL;
+ }
+
+ return child;
+ }
+
+ if (child->GetRange().Contains(pos))
+ {
+ // This should create a new object, transferring part of
+ // the content to the old object and the rest to the new object.
+ wxRichTextObject* newObject = child->DoSplit(pos);
+
+ // If we couldn't split this object, just insert in front of it.
+ if (!newObject)
+ {
+ // Maybe this is an empty string, try the next one
+ // return child;
+ }
+ else
+ {
+ // Insert the new object after 'child'
+ if (node->GetNext())
+ m_children.Insert(node->GetNext(), newObject);
+ else
+ m_children.Append(newObject);
+ newObject->SetParent(this);
+
+ if (previousObject)
+ *previousObject = child;
+
+ return newObject;
+ }
+ }
+
+ node = node->GetNext();
+ }
+ if (previousObject)
+ *previousObject = NULL;
+ return NULL;
+}
+
+/// Move content to a list from obj on
+void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list)
+{
+ wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj);
+ while (node)
+ {
+ wxRichTextObject* child = node->GetData();
+ list.Append(child);
+
+ wxRichTextObjectList::compatibility_iterator oldNode = node;
+
+ node = node->GetNext();
+
+ m_children.DeleteNode(oldNode);
+ }
+}
+
+/// Add content back from list
+void wxRichTextParagraph::MoveFromList(wxList& list)
+{
+ for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
+ {
+ AppendChild((wxRichTextObject*) node->GetData());
+ }
+}
+
+/// Calculate range
+void wxRichTextParagraph::CalculateRange(long start, long& end)
+{
+ wxRichTextCompositeObject::CalculateRange(start, end);
+
+ // Add one for end of paragraph
+ end ++;
+
+ m_range.SetRange(start, end);
+}
+
+/// Find the object at the given position
+wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position)
+{
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ if (obj->GetRange().Contains(position))
+ return obj;
+
+ node = node->GetNext();
+ }
+ return NULL;
+}
+
+/// Get the plain text searching from the start or end of the range.
+/// The resulting string may be shorter than the range given.
+bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart)
+{
+ text = wxEmptyString;
+
+ if (fromStart)
+ {
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ if (!obj->GetRange().IsOutside(range))
+ {
+ wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
+ if (textObj)
+ {
+ text += textObj->GetTextForRange(range);
+ }
+ else
+ {
+ text += wxT(" ");
+ }
+ }
+
+ node = node->GetNext();
+ }
+ }
+ else
+ {
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
+ while (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ if (!obj->GetRange().IsOutside(range))
+ {
+ wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
+ if (textObj)
+ {
+ text = textObj->GetTextForRange(range) + text;
+ }
+ else
+ {
+ text = wxT(" ") + text;
+ }
+ }
+
+ node = node->GetPrevious();
+ }
+ }
+
+ return true;
+}
+
+/// Find a suitable wrap position.
+bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition, wxArrayInt* partialExtents)
+{
+ if (range.GetLength() <= 0)
+ return false;
+
+ // Find the first position where the line exceeds the available space.
+ wxSize sz;
+ long breakPosition = range.GetEnd();
+
+#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
+ if (partialExtents && partialExtents->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
+ {
+ int widthBefore;
+
+ if (range.GetStart() > GetRange().GetStart())
+ widthBefore = (*partialExtents)[range.GetStart() - GetRange().GetStart() - 1];
+ else
+ widthBefore = 0;
+
+ size_t i;
+ for (i = (size_t) range.GetStart(); i <= (size_t) range.GetEnd(); i++)
+ {
+ int widthFromStartOfThisRange = (*partialExtents)[i - GetRange().GetStart()] - widthBefore;
+
+ if (widthFromStartOfThisRange > availableSpace)
+ {
+ breakPosition = i-1;
+ break;
+ }
+ }
+ }
+ else
+#endif
+ {
+ // Binary chop for speed
+ long minPos = range.GetStart();
+ long maxPos = range.GetEnd();
+ while (true)
+ {
+ if (minPos == maxPos)
+ {
+ int descent = 0;
+ GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
+
+ if (sz.x > availableSpace)
+ breakPosition = minPos - 1;
+ break;
+ }
+ else if ((maxPos - minPos) == 1)
+ {
+ int descent = 0;
+ GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
+
+ if (sz.x > availableSpace)
+ breakPosition = minPos - 1;
+ else
+ {
+ GetRangeSize(wxRichTextRange(range.GetStart(), maxPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
+ if (sz.x > availableSpace)
+ breakPosition = maxPos-1;
+ }
+ break;
+ }
+ else
+ {
+ long nextPos = minPos + ((maxPos - minPos) / 2);
+
+ int descent = 0;
+ GetRangeSize(wxRichTextRange(range.GetStart(), nextPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
+
+ if (sz.x > availableSpace)
+ {
+ maxPos = nextPos;
+ }
+ else
+ {
+ minPos = nextPos;
+ }
+ }
+ }
+ }
+
+ // Now we know the last position on the line.
+ // Let's try to find a word break.
+
+ wxString plainText;
+ if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
+ {
+ int newLinePos = plainText.Find(wxRichTextLineBreakChar);
+ if (newLinePos != wxNOT_FOUND)
+ {
+ breakPosition = wxMax(0, range.GetStart() + newLinePos);
+ }
+ else
+ {
+ int spacePos = plainText.Find(wxT(' '), true);
+ int tabPos = plainText.Find(wxT('\t'), true);
+ int pos = wxMax(spacePos, tabPos);
+ if (pos != wxNOT_FOUND)
+ {
+ int positionsFromEndOfString = plainText.length() - pos - 1;
+ breakPosition = breakPosition - positionsFromEndOfString;
+ }
+ }
+ }
+
+ wrapPosition = breakPosition;
+
+ return true;
+}
+
+/// Get the bullet text for this paragraph.
+wxString wxRichTextParagraph::GetBulletText()
+{
+ if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ||
+ (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP))
+ return wxEmptyString;
+
+ int number = GetAttributes().GetBulletNumber();
+
+ wxString text;
+ if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE))
+ {
+ text.Printf(wxT("%d"), number);
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
+ {
+ // TODO: Unicode, and also check if number > 26
+ text.Printf(wxT("%c"), (wxChar) (number+64));
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
+ {
+ // TODO: Unicode, and also check if number > 26
+ text.Printf(wxT("%c"), (wxChar) (number+96));
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
+ {
+ text = wxRichTextDecimalToRoman(number);
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
+ {
+ text = wxRichTextDecimalToRoman(number);
+ text.MakeLower();
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
+ {
+ text = GetAttributes().GetBulletText();
+ }
+
+ if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
+ {
+ // The outline style relies on the text being computed statically,
+ // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
+ // should be stored in the attributes; if not, just use the number for this
+ // level, as previously computed.
+ if (!GetAttributes().GetBulletText().IsEmpty())
+ text = GetAttributes().GetBulletText();
+ }
+
+ if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES)
+ {
+ text = wxT("(") + text + wxT(")");
+ }
+ else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS)
+ {
+ text = text + wxT(")");
+ }
+
+ if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD)
+ {
+ text += wxT(".");
+ }
+
+ return text;
+}
+
+/// Allocate or reuse a line object
+wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos)
+{
+ if (pos < (int) m_cachedLines.GetCount())
+ {
+ wxRichTextLine* line = m_cachedLines.Item(pos)->GetData();
+ line->Init(this);
+ return line;
+ }
+ else
+ {
+ wxRichTextLine* line = new wxRichTextLine(this);
+ m_cachedLines.Append(line);
+ return line;
+ }
+}
+
+/// Clear remaining unused line objects, if any
+bool wxRichTextParagraph::ClearUnusedLines(int lineCount)
+{
+ int cachedLineCount = m_cachedLines.GetCount();
+ if ((int) cachedLineCount > lineCount)
+ {
+ for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++)
+ {
+ wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast();
+ wxRichTextLine* line = node->GetData();
+ m_cachedLines.Erase(node);
+ delete line;
+ }
+ }
+ return true;
+}
+
+/// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
+/// retrieve the actual style.
+wxRichTextAttr wxRichTextParagraph::GetCombinedAttributes(const wxRichTextAttr& contentStyle) const
+{
+ wxRichTextAttr attr;
+ wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
+ if (buf)
+ {
+ attr = buf->GetBasicStyle();
+ wxRichTextApplyStyle(attr, GetAttributes());
+ }
+ else
+ attr = GetAttributes();
+
+ wxRichTextApplyStyle(attr, contentStyle);
+ return attr;
+}
+
+/// Get combined attributes of the base style and paragraph style.
+wxRichTextAttr wxRichTextParagraph::GetCombinedAttributes() const
+{
+ wxRichTextAttr attr;
+ wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
+ if (buf)
+ {
+ attr = buf->GetBasicStyle();
+ wxRichTextApplyStyle(attr, GetAttributes());
+ }
+ else
+ attr = GetAttributes();
+
+ return attr;
+}
+
+/// Create default tabstop array
+void wxRichTextParagraph::InitDefaultTabs()
+{
+ // create a default tab list at 10 mm each.
+ for (int i = 0; i < 20; ++i)
+ {
+ sm_defaultTabs.Add(i*100);
+ }
+}
+
+/// Clear default tabstop array
+void wxRichTextParagraph::ClearDefaultTabs()
+{
+ sm_defaultTabs.Clear();
+}
+
+void wxRichTextParagraph::LayoutFloat(wxDC& dc, const wxRect& rect, int style, wxRichTextFloatCollector* floatCollector)
+{
+ wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst();
+ while (node)
+ {
+ wxRichTextObject* anchored = node->GetData();
+ if (anchored && anchored->IsFloating())
+ {
+ wxSize size;
+ int descent, x = 0;
+ anchored->GetRangeSize(anchored->GetRange(), size, descent, dc, style);
+
+ int offsetY = 0;
+ if (anchored->GetAttributes().GetTextBoxAttr().GetTop().IsPresent())
+ {
+ offsetY = anchored->GetAttributes().GetTextBoxAttr().GetTop().GetValue();
+ if (anchored->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ {
+ offsetY = ConvertTenthsMMToPixels(dc, offsetY);
+ }
+ }
+
+ int pos = floatCollector->GetFitPosition(anchored->GetAttributes().GetTextBoxAttr().GetFloatMode(), rect.y + offsetY, size.y);
+
+ /* Update the offset */
+ int newOffsetY = pos - rect.y;
+ if (newOffsetY != offsetY)
+ {
+ if (anchored->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ newOffsetY = ConvertPixelsToTenthsMM(dc, newOffsetY);
+ anchored->GetAttributes().GetTextBoxAttr().GetTop().SetValue(newOffsetY);
+ }
+
+ if (anchored->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT)
+ x = 0;
+ else if (anchored->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT)
+ x = rect.width - size.x;
+
+ anchored->SetPosition(wxPoint(x, pos));
+ anchored->SetCachedSize(size);
+ floatCollector->CollectFloat(this, anchored);
+ }
+
+ node = node->GetNext();
+ }
+}
+
+/// Get the first position from pos that has a line break character.
+long wxRichTextParagraph::GetFirstLineBreakPosition(long pos)
+{
+ wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ if (pos >= obj->GetRange().GetStart() && pos <= obj->GetRange().GetEnd())
+ {
+ wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
+ if (textObj)
+ {
+ long breakPos = textObj->GetFirstLineBreakPosition(pos);
+ if (breakPos > -1)
+ return breakPos;
+ }
+ }
+ node = node->GetNext();
+ }
+ return -1;
+}
+
+/*!
+ * wxRichTextLine
+ * This object represents a line in a paragraph, and stores
+ * offsets from the start of the paragraph representing the
+ * start and end positions of the line.
+ */
+
+wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent)
+{
+ Init(parent);
+}
+
+/// Initialisation
+void wxRichTextLine::Init(wxRichTextParagraph* parent)
+{
+ m_parent = parent;
+ m_range.SetRange(-1, -1);
+ m_pos = wxPoint(0, 0);
+ m_size = wxSize(0, 0);
+ m_descent = 0;
+#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
+ m_objectSizes.Clear();
+#endif
+}
+
+/// Copy
+void wxRichTextLine::Copy(const wxRichTextLine& obj)
+{
+ m_range = obj.m_range;
+#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
+ m_objectSizes = obj.m_objectSizes;
+#endif
+}
+
+/// Get the absolute object position
+wxPoint wxRichTextLine::GetAbsolutePosition() const
+{
+ return m_parent->GetPosition() + m_pos;
+}
+
+/// Get the absolute range
+wxRichTextRange wxRichTextLine::GetAbsoluteRange() const
+{
+ wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0);
+ range.SetEnd(range.GetStart() + m_range.GetLength()-1);
+ return range;
+}
+
+/*!
+ * wxRichTextPlainText
+ * This object represents a single piece of text.
+ */
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
+
+wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxRichTextAttr* style):
+ wxRichTextObject(parent)
+{
+ if (style)
+ SetAttributes(*style);
+
+ m_text = text;
+}
+
+#define USE_KERNING_FIX 1
+
+// If insufficient tabs are defined, this is the tab width used
+#define WIDTH_FOR_DEFAULT_TABS 50
+
+/// Draw the item
+bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style))
+{
+ wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
+ wxASSERT (para != NULL);
+
+ wxRichTextAttr textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
+
+ int offset = GetRange().GetStart();
+
+ // Replace line break characters with spaces
+ wxString str = m_text;
+ wxString toRemove = wxRichTextLineBreakChar;
+ str.Replace(toRemove, wxT(" "));
+ if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
+ str.MakeUpper();
+
+ long len = range.GetLength();
+ wxString stringChunk = str.Mid(range.GetStart() - offset, (size_t) len);
+
+ // Test for the optimized situations where all is selected, or none
+ // is selected.
+
+ wxFont textFont(GetBuffer()->GetFontTable().FindFont(textAttr));
+ wxCheckSetFont(dc, textFont);
+ int charHeight = dc.GetCharHeight();
+
+ int x, y;
+ if ( textFont.Ok() )
+ {
+ if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) )
+ {
+ double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
+ textFont.SetPointSize( static_cast<int>(size) );
+ x = rect.x;
+ y = rect.y;
+ wxCheckSetFont(dc, textFont);
+ }
+ else if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) )
+ {
+ double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
+ textFont.SetPointSize( static_cast<int>(size) );
+ x = rect.x;
+ int sub_height = static_cast<int>( static_cast<double>(charHeight) / wxSCRIPT_MUL_FACTOR);
+ y = rect.y + (rect.height - sub_height + (descent - m_descent));
+ wxCheckSetFont(dc, textFont);
+ }
+ else
+ {
+ x = rect.x;
+ y = rect.y + (rect.height - charHeight - (descent - m_descent));
+ }
+ }
+ else
+ {
+ x = rect.x;
+ y = rect.y + (rect.height - charHeight - (descent - m_descent));
+ }
+
+ // (a) All selected.
+ if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd())
+ {
+ DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, true);
+ }
+ // (b) None selected.
+ else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd())
+ {
+ // Draw all unselected
+ DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, false);
+ }
+ else
+ {
+ // (c) Part selected, part not
+ // Let's draw unselected chunk, selected chunk, then unselected chunk.
+
+ dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
+
+ // 1. Initial unselected chunk, if any, up until start of selection.
+ if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd())
+ {
+ int r1 = range.GetStart();
+ int s1 = selectionRange.GetStart()-1;
+ int fragmentLen = s1 - r1 + 1;
+ if (fragmentLen < 0)
+ {
+ wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
+ }
+ wxString stringFragment = str.Mid(r1 - offset, fragmentLen);
+
+ DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
+
+#if USE_KERNING_FIX
+ if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
+ {
+ // Compensate for kerning difference
+ wxString stringFragment2(str.Mid(r1 - offset, fragmentLen+1));
+ wxString stringFragment3(str.Mid(r1 - offset + fragmentLen, 1));
+
+ wxCoord w1, h1, w2, h2, w3, h3;
+ dc.GetTextExtent(stringFragment, & w1, & h1);
+ dc.GetTextExtent(stringFragment2, & w2, & h2);
+ dc.GetTextExtent(stringFragment3, & w3, & h3);
+
+ int kerningDiff = (w1 + w3) - w2;
+ x = x - kerningDiff;
+ }
+#endif
+ }
+
+ // 2. Selected chunk, if any.
+ if (selectionRange.GetEnd() >= range.GetStart())
+ {
+ int s1 = wxMax(selectionRange.GetStart(), range.GetStart());
+ int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd());
+
+ int fragmentLen = s2 - s1 + 1;
+ if (fragmentLen < 0)
+ {
+ wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
+ }
+ wxString stringFragment = str.Mid(s1 - offset, fragmentLen);
+
+ DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
+
+#if USE_KERNING_FIX
+ if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
+ {
+ // Compensate for kerning difference
+ wxString stringFragment2(str.Mid(s1 - offset, fragmentLen+1));
+ wxString stringFragment3(str.Mid(s1 - offset + fragmentLen, 1));
+
+ wxCoord w1, h1, w2, h2, w3, h3;
+ dc.GetTextExtent(stringFragment, & w1, & h1);
+ dc.GetTextExtent(stringFragment2, & w2, & h2);
+ dc.GetTextExtent(stringFragment3, & w3, & h3);
+
+ int kerningDiff = (w1 + w3) - w2;
+ x = x - kerningDiff;
+ }
+#endif
+ }
+
+ // 3. Remaining unselected chunk, if any
+ if (selectionRange.GetEnd() < range.GetEnd())
+ {
+ int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd());
+ int r2 = range.GetEnd();
+
+ int fragmentLen = r2 - s2 + 1;
+ if (fragmentLen < 0)
+ {
+ wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
+ }
+ wxString stringFragment = str.Mid(s2 - offset, fragmentLen);
+
+ DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
+ }
+ }
+
+ return true;
+}
+
+bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxRichTextAttr& attr, const wxRect& rect,wxString& str, wxCoord& x, wxCoord& y, bool selected)
+{
+ bool hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
+
+ wxArrayInt tabArray;
+ int tabCount;
+ if (hasTabs)
+ {
+ if (attr.GetTabs().IsEmpty())
+ tabArray = wxRichTextParagraph::GetDefaultTabs();
+ else
+ tabArray = attr.GetTabs();
+ tabCount = tabArray.GetCount();
+
+ for (int i = 0; i < tabCount; ++i)
+ {
+ int pos = tabArray[i];
+ pos = ConvertTenthsMMToPixels(dc, pos);
+ tabArray[i] = pos;
+ }
+ }
+ else
+ tabCount = 0;
+
+ int nextTabPos = -1;
+ int tabPos = -1;
+ wxCoord w, h;
+
+ if (selected)
+ {
+ wxColour highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
+ wxColour highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
+
+ wxCheckSetBrush(dc, wxBrush(highlightColour));
+ wxCheckSetPen(dc, wxPen(highlightColour));
+ dc.SetTextForeground(highlightTextColour);
+ dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
+ }
+ else
+ {
+ dc.SetTextForeground(attr.GetTextColour());
+
+ if (attr.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) && attr.GetBackgroundColour().IsOk())
+ {
+ dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
+ dc.SetTextBackground(attr.GetBackgroundColour());
+ }
+ else
+ dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
+ }
+
+ wxCoord x_orig = x;
+ while (hasTabs)
+ {
+ // the string has a tab
+ // break up the string at the Tab
+ wxString stringChunk = str.BeforeFirst(wxT('\t'));
+ str = str.AfterFirst(wxT('\t'));
+ dc.GetTextExtent(stringChunk, & w, & h);
+ tabPos = x + w;
+ bool not_found = true;
+ for (int i = 0; i < tabCount && not_found; ++i)
+ {
+ nextTabPos = tabArray.Item(i) + x_orig;
+
+ // Find the next tab position.
+ // Even if we're at the end of the tab array, we must still draw the chunk.
+
+ if (nextTabPos > tabPos || (i == (tabCount - 1)))
+ {
+ if (nextTabPos <= tabPos)
+ {
+ int defaultTabWidth = ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
+ nextTabPos = tabPos + defaultTabWidth;
+ }
+
+ not_found = false;
+ if (selected)
+ {
+ w = nextTabPos - x;
+ wxRect selRect(x, rect.y, w, rect.GetHeight());
+ dc.DrawRectangle(selRect);
+ }
+ dc.DrawText(stringChunk, x, y);
+
+ if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
+ {
+ wxPen oldPen = dc.GetPen();
+ wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1));
+ dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
+ wxCheckSetPen(dc, oldPen);
+ }
+
+ x = nextTabPos;
+ }
+ }
+ hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
+ }
+
+ if (!str.IsEmpty())
+ {
+ dc.GetTextExtent(str, & w, & h);
+ if (selected)
+ {
+ wxRect selRect(x, rect.y, w, rect.GetHeight());
+ dc.DrawRectangle(selRect);
+ }
+ dc.DrawText(str, x, y);
+
+ if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
+ {
+ wxPen oldPen = dc.GetPen();
+ wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1));
+ dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
+ wxCheckSetPen(dc, oldPen);
+ }
+
+ x += w;
+ }
+ return true;
+
+}
+
+/// Lay the item out
+bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style))
+{
+ // Only lay out if we haven't already cached the size
+ if (m_size.x == -1)
+ GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0));
+
+ return true;
+}
+
+/// Copy
+void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj)
+{
+ wxRichTextObject::Copy(obj);
+
+ m_text = obj.m_text;
+}
+
+/// Get/set the object size for the given range. Returns false if the range
+/// is invalid for this object.
+bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position, wxArrayInt* partialExtents) const
+{
+ if (!range.IsWithin(GetRange()))
+ return false;
+
+ wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
+ wxASSERT (para != NULL);
+
+ wxRichTextAttr textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
+
+ // Always assume unformatted text, since at this level we have no knowledge
+ // of line breaks - and we don't need it, since we'll calculate size within
+ // formatted text by doing it in chunks according to the line ranges
+
+ bool bScript(false);
+ wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr));
+ if (font.Ok())
+ {
+ if ( textAttr.HasTextEffects() && ( (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT)
+ || (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) ) )
+ {
+ wxFont textFont = font;
+ double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
+ textFont.SetPointSize( static_cast<int>(size) );
+ wxCheckSetFont(dc, textFont);
+ bScript = true;
+ }
+ else
+ {
+ wxCheckSetFont(dc, font);
+ }
+ }
+
+ bool haveDescent = false;
+ int startPos = range.GetStart() - GetRange().GetStart();
+ long len = range.GetLength();
+
+ wxString str(m_text);
+ wxString toReplace = wxRichTextLineBreakChar;
+ str.Replace(toReplace, wxT(" "));
+
+ wxString stringChunk = str.Mid(startPos, (size_t) len);
+
+ if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
+ stringChunk.MakeUpper();
+
+ wxCoord w, h;
+ int width = 0;
+ if (stringChunk.Find(wxT('\t')) != wxNOT_FOUND)
+ {
+ // the string has a tab
+ wxArrayInt tabArray;
+ if (textAttr.GetTabs().IsEmpty())
+ tabArray = wxRichTextParagraph::GetDefaultTabs();
+ else
+ tabArray = textAttr.GetTabs();
+
+ int tabCount = tabArray.GetCount();
+
+ for (int i = 0; i < tabCount; ++i)
+ {
+ int pos = tabArray[i];
+ pos = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, pos);
+ tabArray[i] = pos;
+ }
+
+ int nextTabPos = -1;
+
+ while (stringChunk.Find(wxT('\t')) >= 0)
+ {
+ int absoluteWidth = 0;
+
+ // the string has a tab
+ // break up the string at the Tab
+ wxString stringFragment = stringChunk.BeforeFirst(wxT('\t'));
+ stringChunk = stringChunk.AfterFirst(wxT('\t'));
+
+ if (partialExtents)
+ {
+ int oldWidth;
+ if (partialExtents->GetCount() > 0)
+ oldWidth = (*partialExtents)[partialExtents->GetCount()-1];
+ else
+ oldWidth = 0;
+
+ // Add these partial extents
+ wxArrayInt p;
+ dc.GetPartialTextExtents(stringFragment, p);
+ size_t j;
+ for (j = 0; j < p.GetCount(); j++)
+ partialExtents->Add(oldWidth + p[j]);
+
+ if (partialExtents->GetCount() > 0)
+ absoluteWidth = (*partialExtents)[(*partialExtents).GetCount()-1] + position.x;
+ else
+ absoluteWidth = position.x;
+ }
+ else
+ {
+ dc.GetTextExtent(stringFragment, & w, & h);
+ width += w;
+ absoluteWidth = width + position.x;
+ haveDescent = true;
+ }
+
+ bool notFound = true;
+ for (int i = 0; i < tabCount && notFound; ++i)
+ {
+ nextTabPos = tabArray.Item(i);
+
+ // Find the next tab position.
+ // Even if we're at the end of the tab array, we must still process the chunk.
+
+ if (nextTabPos > absoluteWidth || (i == (tabCount - 1)))
+ {
+ if (nextTabPos <= absoluteWidth)
+ {
+ int defaultTabWidth = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
+ nextTabPos = absoluteWidth + defaultTabWidth;
+ }
+
+ notFound = false;
+ width = nextTabPos - position.x;
+
+ if (partialExtents)
+ partialExtents->Add(width);
+ }
+ }
+ }
+ }
+
+ if (!stringChunk.IsEmpty())
+ {
+ if (partialExtents)
+ {
+ int oldWidth;
+ if (partialExtents->GetCount() > 0)
+ oldWidth = (*partialExtents)[partialExtents->GetCount()-1];
+ else
+ oldWidth = 0;
+
+ // Add these partial extents
+ wxArrayInt p;
+ dc.GetPartialTextExtents(stringChunk, p);
+ size_t j;
+ for (j = 0; j < p.GetCount(); j++)
+ partialExtents->Add(oldWidth + p[j]);
+ }
+ else
+ {
+ dc.GetTextExtent(stringChunk, & w, & h, & descent);
+ width += w;
+ haveDescent = true;
+ }
+ }
+
+ if (partialExtents)
+ {
+ int charHeight = dc.GetCharHeight();
+ if ((*partialExtents).GetCount() > 0)
+ w = (*partialExtents)[partialExtents->GetCount()-1];
+ else
+ w = 0;
+ size = wxSize(w, charHeight);
+ }
+ else
+ {
+ size = wxSize(width, dc.GetCharHeight());
+ }
+
+ if (!haveDescent)
+ dc.GetTextExtent(wxT("X"), & w, & h, & descent);
+
+ if ( bScript )
+ dc.SetFont(font);
+
+ return true;
+}
+
+/// Do a split, returning an object containing the second part, and setting
+/// the first part in 'this'.
+wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
+{
+ long index = pos - GetRange().GetStart();
+
+ if (index < 0 || index >= (int) m_text.length())
+ return NULL;
+
+ wxString firstPart = m_text.Mid(0, index);
+ wxString secondPart = m_text.Mid(index);
+
+ m_text = firstPart;
+
+ wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart);
+ newObject->SetAttributes(GetAttributes());
+
+ newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd()));
+ GetRange().SetEnd(pos-1);
+
+ return newObject;
+}
+
+/// Calculate range
+void wxRichTextPlainText::CalculateRange(long start, long& end)
+{
+ end = start + m_text.length() - 1;
+ m_range.SetRange(start, end);
+}
+
+/// Delete range
+bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range)
+{
+ wxRichTextRange r = range;
+
+ r.LimitTo(GetRange());
+
+ if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd())
+ {
+ m_text.Empty();
+ return true;
+ }
+
+ long startIndex = r.GetStart() - GetRange().GetStart();
+ long len = r.GetLength();
+
+ m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len);
+ return true;
+}
+
+/// Get text for the given range.
+wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const
+{
+ wxRichTextRange r = range;
+
+ r.LimitTo(GetRange());
+
+ long startIndex = r.GetStart() - GetRange().GetStart();
+ long len = r.GetLength();
+
+ return m_text.Mid(startIndex, len);
+}
+
+/// Returns true if this object can merge itself with the given one.
+bool wxRichTextPlainText::CanMerge(wxRichTextObject* object) const
+{
+ return object->GetClassInfo() == CLASSINFO(wxRichTextPlainText) &&
+ (m_text.empty() || wxTextAttrEq(GetAttributes(), object->GetAttributes()));
+}
+
+/// Returns true if this object merged itself with the given one.
+/// The calling code will then delete the given object.
+bool wxRichTextPlainText::Merge(wxRichTextObject* object)
+{
+ wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText);
+ wxASSERT( textObject != NULL );
+
+ if (textObject)
+ {
+ m_text += textObject->GetText();
+ wxRichTextApplyStyle(m_attributes, textObject->GetAttributes());
+ return true;
+ }
+ else
+ return false;
+}
+
+/// Dump to output stream for debugging
+void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
+{
+ wxRichTextObject::Dump(stream);
+ stream << m_text << wxT("\n");
+}
+
+/// Get the first position from pos that has a line break character.
+long wxRichTextPlainText::GetFirstLineBreakPosition(long pos)
+{
+ int i;
+ int len = m_text.length();
+ int startPos = pos - m_range.GetStart();
+ for (i = startPos; i < len; i++)
+ {
+ wxChar ch = m_text[i];
+ if (ch == wxRichTextLineBreakChar)
+ {
+ return i + m_range.GetStart();
+ }
+ }
+ return -1;
+}
+
+/*!
+ * wxRichTextBuffer
+ * This is a kind of box, used to represent the whole buffer
+ */
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox)
+
+wxList wxRichTextBuffer::sm_handlers;
+wxRichTextRenderer* wxRichTextBuffer::sm_renderer = NULL;
+int wxRichTextBuffer::sm_bulletRightMargin = 20;
+float wxRichTextBuffer::sm_bulletProportion = (float) 0.3;
+
+/// Initialisation
+void wxRichTextBuffer::Init()
+{
+ m_commandProcessor = new wxCommandProcessor;
+ m_styleSheet = NULL;
+ m_modified = false;
+ m_batchedCommandDepth = 0;
+ m_batchedCommand = NULL;
+ m_suppressUndo = 0;
+ m_handlerFlags = 0;
+ m_scale = 1.0;
+}
+
+/// Initialisation
+wxRichTextBuffer::~wxRichTextBuffer()
+{
+ delete m_commandProcessor;
+ delete m_batchedCommand;
+
+ ClearStyleStack();
+ ClearEventHandlers();
+}
+
+void wxRichTextBuffer::ResetAndClearCommands()
+{
+ Reset();
+
+ GetCommandProcessor()->ClearCommands();
+
+ Modify(false);
+ Invalidate(wxRICHTEXT_ALL);
+}
+
+void wxRichTextBuffer::Copy(const wxRichTextBuffer& obj)
+{
+ wxRichTextParagraphLayoutBox::Copy(obj);
+
+ m_styleSheet = obj.m_styleSheet;
+ m_modified = obj.m_modified;
+ m_batchedCommandDepth = 0;
+ if (m_batchedCommand)
+ delete m_batchedCommand;
+ m_batchedCommand = NULL;
+ m_suppressUndo = obj.m_suppressUndo;
+}
+
+/// Push style sheet to top of stack
+bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet* styleSheet)
+{
+ if (m_styleSheet)
+ styleSheet->InsertSheet(m_styleSheet);
+
+ SetStyleSheet(styleSheet);
+
+ return true;
+}
+
+/// Pop style sheet from top of stack
+wxRichTextStyleSheet* wxRichTextBuffer::PopStyleSheet()
+{
+ if (m_styleSheet)
+ {
+ wxRichTextStyleSheet* oldSheet = m_styleSheet;
+ m_styleSheet = oldSheet->GetNextSheet();
+ oldSheet->Unlink();
+
+ return oldSheet;
+ }
+ else
+ return NULL;
+}
+
+/// Submit command to insert paragraphs
+bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
+
+ wxRichTextAttr attr(GetDefaultStyle());
+
+ wxRichTextAttr* p = NULL;
+ wxRichTextAttr paraAttr;
+ if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
+ {
+ paraAttr = GetStyleForNewParagraph(pos);
+ if (!paraAttr.IsDefault())
+ p = & paraAttr;
+ }
+ else
+ p = & attr;
+
+ action->GetNewParagraphs() = paragraphs;
+
+ if (p && !p->IsDefault())
+ {
+ for (wxRichTextObjectList::compatibility_iterator node = action->GetNewParagraphs().GetChildren().GetFirst(); node; node = node->GetNext())
+ {
+ wxRichTextObject* child = node->GetData();
+ child->SetAttributes(*p);
+ }
+ }
+
+ action->SetPosition(pos);
+
+ wxRichTextRange range = wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1);
+ if (!paragraphs.GetPartialParagraph())
+ range.SetEnd(range.GetEnd()+1);
+
+ // Set the range we'll need to delete in Undo
+ action->SetRange(range);
+
+ SubmitAction(action);
+
+ return true;
+}
+
+/// Submit command to insert the given text
+bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
+
+ wxRichTextAttr* p = NULL;
+ wxRichTextAttr paraAttr;
+ if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
+ {
+ // Get appropriate paragraph style
+ paraAttr = GetStyleForNewParagraph(pos, false, false);
+ if (!paraAttr.IsDefault())
+ p = & paraAttr;
+ }
+
+ action->GetNewParagraphs().AddParagraphs(text, p);
+
+ int length = action->GetNewParagraphs().GetRange().GetLength();
+
+ if (text.length() > 0 && text.Last() != wxT('\n'))
+ {
+ // Don't count the newline when undoing
+ length --;
+ action->GetNewParagraphs().SetPartialParagraph(true);
+ }
+ else if (text.length() > 0 && text.Last() == wxT('\n'))
+ length --;
+
+ action->SetPosition(pos);
+
+ // Set the range we'll need to delete in Undo
+ action->SetRange(wxRichTextRange(pos, pos + length - 1));
+
+ SubmitAction(action);
+
+ return true;
+}
+
+/// Submit command to insert the given text
+bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
+
+ wxRichTextAttr* p = NULL;
+ wxRichTextAttr paraAttr;
+ if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
+ {
+ paraAttr = GetStyleForNewParagraph(pos, false, true /* look for next paragraph style */);
+ if (!paraAttr.IsDefault())
+ p = & paraAttr;
+ }
+
+ wxRichTextAttr attr(GetDefaultStyle());
+
+ wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
+ action->GetNewParagraphs().AppendChild(newPara);
+ action->GetNewParagraphs().UpdateRanges();
+ action->GetNewParagraphs().SetPartialParagraph(false);
+ wxRichTextParagraph* para = GetParagraphAtPosition(pos, false);
+ long pos1 = pos;
+
+ if (p)
+ newPara->SetAttributes(*p);
+
+ if (flags & wxRICHTEXT_INSERT_INTERACTIVE)
+ {
+ if (para && para->GetRange().GetEnd() == pos)
+ pos1 ++;
+
+ // Now see if we need to number the paragraph.
+ if (newPara->GetAttributes().HasBulletNumber())
+ {
+ wxRichTextAttr numberingAttr;
+ if (FindNextParagraphNumber(para, numberingAttr))
+ wxRichTextApplyStyle(newPara->GetAttributes(), (const wxRichTextAttr&) numberingAttr);
+ }
+ }
+
+ action->SetPosition(pos);
+
+ // Use the default character style
+ // Use the default character style
+ if (!GetDefaultStyle().IsDefault() && newPara->GetChildren().GetFirst())
+ {
+ // Check whether the default style merely reflects the paragraph/basic style,
+ // in which case don't apply it.
+ wxRichTextAttr defaultStyle(GetDefaultStyle());
+ wxRichTextAttr toApply;
+ if (para)
+ {
+ wxRichTextAttr combinedAttr = para->GetCombinedAttributes();
+ wxRichTextAttr newAttr;
+ // This filters out attributes that are accounted for by the current
+ // paragraph/basic style
+ wxRichTextApplyStyle(toApply, defaultStyle, & combinedAttr);
+ }
+ else
+ toApply = defaultStyle;
+
+ if (!toApply.IsDefault())
+ newPara->GetChildren().GetFirst()->GetData()->SetAttributes(toApply);
+ }
+
+ // Set the range we'll need to delete in Undo
+ action->SetRange(wxRichTextRange(pos1, pos1));
+
+ SubmitAction(action);
+
+ return true;
+}
+
+/// Submit command to insert the given image
+bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags,
+ const wxRichTextAttr& textAttr)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, ctrl, false);
+
+ wxRichTextAttr* p = NULL;
+ wxRichTextAttr paraAttr;
+ if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
+ {
+ paraAttr = GetStyleForNewParagraph(pos);
+ if (!paraAttr.IsDefault())
+ p = & paraAttr;
+ }
+
+ wxRichTextAttr attr(GetDefaultStyle());
+
+ wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
+ if (p)
+ newPara->SetAttributes(*p);
+
+ wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara);
+ newPara->AppendChild(imageObject);
+ imageObject->SetAttributes(textAttr);
+ action->GetNewParagraphs().AppendChild(newPara);
+ action->GetNewParagraphs().UpdateRanges();
+
+ action->GetNewParagraphs().SetPartialParagraph(true);
+
+ action->SetPosition(pos);
+
+ // Set the range we'll need to delete in Undo
+ action->SetRange(wxRichTextRange(pos, pos));
+
+ SubmitAction(action);
+
+ return true;
+}
+
+// Insert an object with no change of it
+bool wxRichTextBuffer::InsertObjectWithUndo(long pos, wxRichTextObject *object, wxRichTextCtrl* ctrl, int flags)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert object"), wxRICHTEXT_INSERT, this, ctrl, false);
+
+ wxRichTextAttr* p = NULL;
+ wxRichTextAttr paraAttr;
+ if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
+ {
+ paraAttr = GetStyleForNewParagraph(pos);
+ if (!paraAttr.IsDefault())
+ p = & paraAttr;
+ }
+
+ wxRichTextAttr attr(GetDefaultStyle());
+
+ wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
+ if (p)
+ newPara->SetAttributes(*p);
+
+ newPara->AppendChild(object);
+ action->GetNewParagraphs().AppendChild(newPara);
+ action->GetNewParagraphs().UpdateRanges();
+
+ action->GetNewParagraphs().SetPartialParagraph(true);
+
+ action->SetPosition(pos);
+
+ // Set the range we'll need to delete in Undo
+ action->SetRange(wxRichTextRange(pos, pos));
+
+ SubmitAction(action);
+
+ return true;
+}
+/// Get the style that is appropriate for a new paragraph at this position.
+/// If the previous paragraph has a paragraph style name, look up the next-paragraph
+/// style.
+wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition, bool lookUpNewParaStyle) const
+{
+ wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
+ if (para)
+ {
+ wxRichTextAttr attr;
+ bool foundAttributes = false;
+
+ // Look for a matching paragraph style
+ if (lookUpNewParaStyle && !para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
+ {
+ wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
+ if (paraDef)
+ {
+ // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
+ if (para->GetRange().GetEnd() == pos && !paraDef->GetNextStyle().IsEmpty())
+ {
+ wxRichTextParagraphStyleDefinition* nextParaDef = GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle());
+ if (nextParaDef)
+ {
+ foundAttributes = true;
+ attr = nextParaDef->GetStyleMergedWithBase(GetStyleSheet());
+ }
+ }
+
+ // If we didn't find the 'next style', use this style instead.
+ if (!foundAttributes)
+ {
+ foundAttributes = true;
+ attr = paraDef->GetStyleMergedWithBase(GetStyleSheet());
+ }
+ }
+ }
+
+ // Also apply list style if present
+ if (lookUpNewParaStyle && !para->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet())
+ {
+ wxRichTextListStyleDefinition* listDef = GetStyleSheet()->FindListStyle(para->GetAttributes().GetListStyleName());
+ if (listDef)
+ {
+ int thisIndent = para->GetAttributes().GetLeftIndent();
+ int thisLevel = para->GetAttributes().HasOutlineLevel() ? para->GetAttributes().GetOutlineLevel() : listDef->FindLevelForIndent(thisIndent);
+
+ // Apply the overall list style, and item style for this level
+ wxRichTextAttr listStyle(listDef->GetCombinedStyleForLevel(thisLevel, GetStyleSheet()));
+ wxRichTextApplyStyle(attr, listStyle);
+ attr.SetOutlineLevel(thisLevel);
+ if (para->GetAttributes().HasBulletNumber())
+ attr.SetBulletNumber(para->GetAttributes().GetBulletNumber());
+ }
+ }
+
+ if (!foundAttributes)
+ {
+ attr = para->GetAttributes();
+ int flags = attr.GetFlags();
+
+ // Eliminate character styles
+ flags &= ( (~ wxTEXT_ATTR_FONT) |
+ (~ wxTEXT_ATTR_TEXT_COLOUR) |
+ (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
+ attr.SetFlags(flags);
+ }
+
+ return attr;
+ }
+ else
+ return wxRichTextAttr();
+}
+
+/// Submit command to delete this range
+bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, wxRichTextCtrl* ctrl)
+{
+ wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl);
+
+ action->SetPosition(ctrl->GetCaretPosition());
+
+ // Set the range to delete
+ action->SetRange(range);
+
+ // Copy the fragment that we'll need to restore in Undo
+ CopyFragment(range, action->GetOldParagraphs());
+
+ // See if we're deleting a paragraph marker, in which case we need to
+ // make a note not to copy the attributes from the 2nd paragraph to the 1st.
+ if (range.GetStart() == range.GetEnd())
+ {
+ wxRichTextParagraph* para = GetParagraphAtPosition(range.GetStart());
+ if (para && para->GetRange().GetEnd() == range.GetEnd())
+ {
+ wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetStart()+1);
+ if (nextPara && nextPara != para)
+ {
+ action->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara->GetAttributes());
+ action->GetOldParagraphs().GetAttributes().SetFlags(action->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE);
+ }
+ }
+ }
+
+ SubmitAction(action);
+
+ return true;
+}
+
+/// Collapse undo/redo commands
+bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
+{
+ if (m_batchedCommandDepth == 0)
+ {
+ wxASSERT(m_batchedCommand == NULL);
+ if (m_batchedCommand)
+ {
+ GetCommandProcessor()->Store(m_batchedCommand);
+ }
+ m_batchedCommand = new wxRichTextCommand(cmdName);
+ }
+
+ m_batchedCommandDepth ++;
+
+ return true;
+}
+
+/// Collapse undo/redo commands
+bool wxRichTextBuffer::EndBatchUndo()
+{
+ m_batchedCommandDepth --;
+
+ wxASSERT(m_batchedCommandDepth >= 0);
+ wxASSERT(m_batchedCommand != NULL);
+
+ if (m_batchedCommandDepth == 0)
+ {
+ GetCommandProcessor()->Store(m_batchedCommand);
+ m_batchedCommand = NULL;
+ }
+
+ return true;
+}
+
+/// Submit immediately, or delay according to whether collapsing is on
+bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
+{
+ if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
+ {
+ wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
+ cmd->AddAction(action);
+ cmd->Do();
+ cmd->GetActions().Clear();
+ delete cmd;
+
+ m_batchedCommand->AddAction(action);
+ }
+ else
+ {
+ wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
+ cmd->AddAction(action);
+
+ // Only store it if we're not suppressing undo.
+ return GetCommandProcessor()->Submit(cmd, !SuppressingUndo());
+ }
+
+ return true;
+}
+
+/// Begin suppressing undo/redo commands.
+bool wxRichTextBuffer::BeginSuppressUndo()
+{
+ m_suppressUndo ++;
+
+ return true;
+}
+
+/// End suppressing undo/redo commands.
+bool wxRichTextBuffer::EndSuppressUndo()
+{
+ m_suppressUndo --;
+
+ return true;
+}
+
+/// Begin using a style
+bool wxRichTextBuffer::BeginStyle(const wxRichTextAttr& style)
+{
+ wxRichTextAttr newStyle(GetDefaultStyle());
+
+ // Save the old default style
+ m_attributeStack.Append((wxObject*) new wxRichTextAttr(GetDefaultStyle()));
+
+ wxRichTextApplyStyle(newStyle, style);
+ newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags());
+
+ SetDefaultStyle(newStyle);
+
+ return true;
+}
+
+/// End the style
+bool wxRichTextBuffer::EndStyle()
+{
+ if (!m_attributeStack.GetFirst())
+ {
+ wxLogDebug(_("Too many EndStyle calls!"));
+ return false;
+ }
+
+ wxList::compatibility_iterator node = m_attributeStack.GetLast();
+ wxRichTextAttr* attr = (wxRichTextAttr*)node->GetData();
+ m_attributeStack.Erase(node);
+
+ SetDefaultStyle(*attr);