+wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id,
+ wxRichTextBuffer* buffer, wxRichTextParagraphLayoutBox* container,
+ wxRichTextCtrl* ctrl, bool ignoreFirstTime)
+{
+ m_buffer = buffer;
+ m_object = NULL;
+ m_containerAddress.Create(buffer, container);
+ m_ignoreThis = ignoreFirstTime;
+ m_cmdId = id;
+ m_position = -1;
+ m_ctrl = ctrl;
+ m_name = name;
+ m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle());
+ m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle());
+ if (cmd)
+ cmd->AddAction(this);
+}
+
+wxRichTextAction::~wxRichTextAction()
+{
+ if (m_object)
+ delete m_object;
+}
+
+// Returns the container that this action refers to, using the container address and top-level buffer.
+wxRichTextParagraphLayoutBox* wxRichTextAction::GetContainer() const
+{
+ wxRichTextParagraphLayoutBox* container = wxDynamicCast(GetContainerAddress().GetObject(m_buffer), wxRichTextParagraphLayoutBox);
+ return container;
+}
+
+
+void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt& optimizationLineCharPositions, wxArrayInt& optimizationLineYPositions)
+{
+ // Store a list of line start character and y positions so we can figure out which area
+ // we need to refresh
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ wxRichTextParagraphLayoutBox* container = GetContainer();
+ wxASSERT(container != NULL);
+ if (!container)
+ return;
+
+ // NOTE: we're assuming that the buffer is laid out correctly at this point.
+ // If we had several actions, which only invalidate and leave layout until the
+ // paint handler is called, then this might not be true. So we may need to switch
+ // optimisation on only when we're simply adding text and not simultaneously
+ // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
+ // first, but of course this means we'll be doing it twice.
+ if (!m_buffer->IsDirty() && m_ctrl) // can only do optimisation if the buffer is already laid out correctly
+ {
+ wxSize clientSize = m_ctrl->GetClientSize();
+ wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint();
+ int lastY = firstVisiblePt.y + clientSize.y;
+
+ wxRichTextParagraph* para = container->GetParagraphAtPosition(GetRange().GetStart());
+ wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(para);
+ while (node)
+ {
+ wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData();
+ wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
+ while (node2)
+ {
+ wxRichTextLine* line = node2->GetData();
+ wxPoint pt = line->GetAbsolutePosition();
+ wxRichTextRange range = line->GetAbsoluteRange();
+
+ if (pt.y > lastY)
+ {
+ node2 = wxRichTextLineList::compatibility_iterator();
+ node = wxRichTextObjectList::compatibility_iterator();
+ }
+ else if (range.GetStart() > GetPosition() && pt.y >= firstVisiblePt.y)
+ {
+ optimizationLineCharPositions.Add(range.GetStart());
+ optimizationLineYPositions.Add(pt.y);
+ }
+
+ if (node2)
+ node2 = node2->GetNext();
+ }
+
+ if (node)
+ node = node->GetNext();
+ }
+ }
+#endif
+}
+
+bool wxRichTextAction::Do()
+{
+ m_buffer->Modify(true);
+
+ wxRichTextParagraphLayoutBox* container = GetContainer();
+ wxASSERT(container != NULL);
+ if (!container)
+ return false;
+
+ switch (m_cmdId)
+ {
+ case wxRICHTEXT_INSERT:
+ {
+ // Store a list of line start character and y positions so we can figure out which area
+ // we need to refresh
+ wxArrayInt optimizationLineCharPositions;
+ wxArrayInt optimizationLineYPositions;
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
+#endif
+
+ container->InsertFragment(GetRange().GetStart(), m_newParagraphs);
+ container->UpdateRanges();
+
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
+
+ long newCaretPosition = GetPosition() + m_newParagraphs.GetOwnRange().GetLength();
+
+ // Character position to caret position
+ newCaretPosition --;
+
+ // Don't take into account the last newline
+ if (m_newParagraphs.GetPartialParagraph())
+ newCaretPosition --;
+ else
+ if (m_newParagraphs.GetChildren().GetCount() > 1)
+ {
+ wxRichTextObject* p = (wxRichTextObject*) m_newParagraphs.GetChildren().GetLast()->GetData();
+ if (p->GetRange().GetLength() == 1)
+ newCaretPosition --;
+ }
+
+ newCaretPosition = wxMin(newCaretPosition, (container->GetOwnRange().GetEnd()-1));
+
+ UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */);
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_DELETE:
+ {
+ wxArrayInt optimizationLineCharPositions;
+ wxArrayInt optimizationLineYPositions;
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
+#endif
+
+ container->DeleteRange(GetRange());
+ container->UpdateRanges();
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
+
+ long caretPos = GetRange().GetStart()-1;
+ if (caretPos >= container->GetOwnRange().GetEnd())
+ caretPos --;
+
+ UpdateAppearance(caretPos, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */);
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_CHANGE_STYLE:
+ {
+ ApplyParagraphs(GetNewParagraphs());
+
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition());
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_CHANGE_ATTRIBUTES:
+ {
+ wxRichTextObject* obj = m_objectAddress.GetObject(m_buffer); // container->GetChildAtPosition(GetRange().GetStart());
+ if (obj)
+ {
+ wxRichTextAttr oldAttr = obj->GetAttributes();
+ obj->GetAttributes() = m_attributes;
+ m_attributes = oldAttr;
+ }
+
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition());
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_CHANGE_OBJECT:
+ {
+ wxRichTextObject* obj = m_objectAddress.GetObject(m_buffer);
+ // wxRichTextObject* obj = container->GetChildAtPosition(GetRange().GetStart());
+ if (obj && m_object)
+ {
+ wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(obj);
+ if (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ node->SetData(m_object);
+ m_object = obj;
+ }
+ }
+
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition());
+
+ // TODO: send new kind of modification event
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ return true;
+}
+
+bool wxRichTextAction::Undo()
+{
+ m_buffer->Modify(true);
+
+ wxRichTextParagraphLayoutBox* container = GetContainer();
+ wxASSERT(container != NULL);
+ if (!container)
+ return false;
+
+ switch (m_cmdId)
+ {
+ case wxRICHTEXT_INSERT:
+ {
+ wxArrayInt optimizationLineCharPositions;
+ wxArrayInt optimizationLineYPositions;
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
+#endif
+
+ container->DeleteRange(GetRange());
+ container->UpdateRanges();
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
+
+ long newCaretPosition = GetPosition() - 1;
+
+ UpdateAppearance(newCaretPosition, true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */);
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_DELETE:
+ {
+ wxArrayInt optimizationLineCharPositions;
+ wxArrayInt optimizationLineYPositions;
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
+#endif
+
+ container->InsertFragment(GetRange().GetStart(), m_oldParagraphs);
+ container->UpdateRanges();
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */);
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_CHANGE_STYLE:
+ {
+ ApplyParagraphs(GetOldParagraphs());
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition());
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED,
+ m_ctrl ? m_ctrl->GetId() : -1);
+ cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+ cmdEvent.SetRange(GetRange());
+ cmdEvent.SetPosition(GetRange().GetStart());
+ cmdEvent.SetContainer(container);
+
+ m_buffer->SendEvent(cmdEvent);
+
+ break;
+ }
+ case wxRICHTEXT_CHANGE_ATTRIBUTES:
+ case wxRICHTEXT_CHANGE_OBJECT:
+ {
+ return Do();
+ }
+ default:
+ break;
+ }
+
+ return true;
+}
+
+/// Update the control appearance
+void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent, wxArrayInt* optimizationLineCharPositions, wxArrayInt* optimizationLineYPositions, bool isDoCmd)
+{
+ wxRichTextParagraphLayoutBox* container = GetContainer();
+ wxASSERT(container != NULL);
+ if (!container)
+ return;
+
+ if (m_ctrl)
+ {
+ m_ctrl->SetFocusObject(container);
+ m_ctrl->SetCaretPosition(caretPosition);
+
+ if (!m_ctrl->IsFrozen())
+ {
+ wxRect containerRect = container->GetRect();
+
+ m_ctrl->LayoutContent();
+
+ // Refresh everything if there were floating objects or the container changed size
+ // (we can't yet optimize in these cases, since more complex interaction with other content occurs)
+ if (container->GetFloatingObjectCount() > 0 || (container->GetParent() && containerRect != container->GetRect()))
+ {
+ m_ctrl->Refresh(false);
+ }
+ else
+
+#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
+ // Find refresh rectangle if we are in a position to optimise refresh
+ if ((m_cmdId == wxRICHTEXT_INSERT || m_cmdId == wxRICHTEXT_DELETE) && optimizationLineCharPositions)
+ {
+ size_t i;
+
+ wxSize clientSize = m_ctrl->GetClientSize();
+ wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint();
+
+ // Start/end positions
+ int firstY = 0;
+ int lastY = firstVisiblePt.y + clientSize.y;
+
+ bool foundEnd = false;
+
+ // position offset - how many characters were inserted
+ int positionOffset = GetRange().GetLength();
+
+ // Determine whether this is Do or Undo, and adjust positionOffset accordingly
+ if ((m_cmdId == wxRICHTEXT_DELETE && isDoCmd) || (m_cmdId == wxRICHTEXT_INSERT && !isDoCmd))
+ positionOffset = - positionOffset;
+
+ // find the first line which is being drawn at the same position as it was
+ // before. Since we're talking about a simple insertion, we can assume
+ // that the rest of the window does not need to be redrawn.
+
+ wxRichTextParagraph* para = container->GetParagraphAtPosition(GetPosition());
+ // Since we support floating layout, we should redraw the whole para instead of just
+ // the first line touching the invalid range.
+ if (para)
+ {
+ firstY = para->GetPosition().y;
+ }
+
+ wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(para);
+ while (node)
+ {
+ wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData();
+ wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
+ while (node2)
+ {
+ wxRichTextLine* line = node2->GetData();
+ wxPoint pt = line->GetAbsolutePosition();
+ wxRichTextRange range = line->GetAbsoluteRange();
+
+ // we want to find the first line that is in the same position
+ // as before. This will mean we're at the end of the changed text.
+
+ if (pt.y > lastY) // going past the end of the window, no more info
+ {
+ node2 = wxRichTextLineList::compatibility_iterator();
+ node = wxRichTextObjectList::compatibility_iterator();
+ }
+ // Detect last line in the buffer
+ else if (!node2->GetNext() && para->GetRange().Contains(container->GetOwnRange().GetEnd()))
+ {
+ // If deleting text, make sure we refresh below as well as above
+ if (positionOffset >= 0)
+ {
+ foundEnd = true;
+ lastY = pt.y + line->GetSize().y;
+ }
+
+ node2 = wxRichTextLineList::compatibility_iterator();
+ node = wxRichTextObjectList::compatibility_iterator();
+
+ break;
+ }
+ else
+ {
+ // search for this line being at the same position as before
+ for (i = 0; i < optimizationLineCharPositions->GetCount(); i++)
+ {
+ if (((*optimizationLineCharPositions)[i] + positionOffset == range.GetStart()) &&
+ ((*optimizationLineYPositions)[i] == pt.y))
+ {
+ // Stop, we're now the same as we were
+ foundEnd = true;
+
+ lastY = pt.y;
+
+ node2 = wxRichTextLineList::compatibility_iterator();
+ node = wxRichTextObjectList::compatibility_iterator();
+
+ break;
+ }
+ }
+ }
+
+ if (node2)
+ node2 = node2->GetNext();
+ }
+
+ if (node)
+ node = node->GetNext();
+ }
+
+ firstY = wxMax(firstVisiblePt.y, firstY);
+ if (!foundEnd)
+ lastY = firstVisiblePt.y + clientSize.y;
+
+ // Convert to device coordinates
+ wxRect rect(m_ctrl->GetPhysicalPoint(wxPoint(firstVisiblePt.x, firstY)), wxSize(clientSize.x, lastY - firstY));
+ m_ctrl->RefreshRect(rect);
+ }
+ else
+#endif
+ m_ctrl->Refresh(false);
+
+ m_ctrl->PositionCaret();
+ m_ctrl->SetDefaultStyleToCursorStyle();
+
+ if (sendUpdateEvent)
+ wxTextCtrl::SendTextUpdatedEvent(m_ctrl);
+ }
+ }
+}
+
+/// Replace the buffer paragraphs with the new ones.
+void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment)
+{
+ wxRichTextParagraphLayoutBox* container = GetContainer();
+ wxASSERT(container != NULL);
+ if (!container)
+ return;
+
+ wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst();
+ while (node)
+ {
+ wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
+ wxASSERT (para != NULL);
+
+ // We'll replace the existing paragraph by finding the paragraph at this position,
+ // delete its node data, and setting a copy as the new node data.
+ // TODO: make more efficient by simply swapping old and new paragraph objects.
+
+ wxRichTextParagraph* existingPara = container->GetParagraphAtPosition(para->GetRange().GetStart());
+ if (existingPara)
+ {
+ wxRichTextObjectList::compatibility_iterator bufferParaNode = container->GetChildren().Find(existingPara);
+ if (bufferParaNode)
+ {
+ wxRichTextParagraph* newPara = new wxRichTextParagraph(*para);
+ newPara->SetParent(container);
+
+ bufferParaNode->SetData(newPara);
+
+ delete existingPara;
+ }
+ }
+
+ node = node->GetNext();
+ }
+}
+
+
+/*!
+ * wxRichTextRange
+ * This stores beginning and end positions for a range of data.
+ */
+
+WX_DEFINE_OBJARRAY(wxRichTextRangeArray);
+
+/// Limit this range to be within 'range'
+bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
+{
+ if (m_start < range.m_start)
+ m_start = range.m_start;
+
+ if (m_end > range.m_end)
+ m_end = range.m_end;
+
+ return true;
+}
+
+/*!
+ * wxRichTextImage implementation
+ * This object represents an image.
+ */
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
+
+wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent, wxRichTextAttr* charStyle):
+ wxRichTextObject(parent)
+{
+ m_imageBlock.MakeImageBlockDefaultQuality(image, wxBITMAP_TYPE_PNG);
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent, wxRichTextAttr* charStyle):
+ wxRichTextObject(parent)
+{
+ m_imageBlock = imageBlock;
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+/// Create a cached image at the required size
+bool wxRichTextImage::LoadImageCache(wxDC& dc, bool resetCache)
+{
+ if (resetCache || !m_imageCache.IsOk() /* || m_imageCache.GetWidth() != size.x || m_imageCache.GetHeight() != size.y */)
+ {
+ if (!m_imageBlock.IsOk())
+ return false;
+
+ wxImage image;
+ m_imageBlock.Load(image);
+ if (!image.IsOk())
+ return false;
+
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+
+ if (GetAttributes().GetTextBoxAttr().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
+ {
+ if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ width = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
+ else
+ width = GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
+ }
+ if (GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
+ {
+ if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ height = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
+ else
+ height = GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
+ }
+
+ if (image.GetWidth() == width && image.GetHeight() == height)
+ m_imageCache = wxBitmap(image);
+ else
+ {
+ // If the original width and height is small, e.g. 400 or below,
+ // scale up and then down to improve image quality. This can make
+ // a big difference, with not much performance hit.
+ int upscaleThreshold = 400;
+ wxImage img;
+ if (image.GetWidth() <= upscaleThreshold || image.GetHeight() <= upscaleThreshold)
+ {
+ img = image.Scale(image.GetWidth()*2, image.GetHeight()*2);
+ img.Rescale(width, height, wxIMAGE_QUALITY_HIGH);
+ }
+ else
+ img = image.Scale(width, height, wxIMAGE_QUALITY_HIGH);
+ m_imageCache = wxBitmap(img);
+ }
+ }
+
+ return m_imageCache.IsOk();
+}
+
+/// Draw the item
+bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
+{
+ if (!IsShown())
+ return true;
+
+ // Don't need cached size AFAIK
+ // wxSize size = GetCachedSize();
+ if (!LoadImageCache(dc))
+ return false;
+
+ DrawBoxAttributes(dc, GetBuffer(), GetAttributes(), wxRect(GetPosition(), GetCachedSize()));
+
+#if 0
+ int y = rect.y + (rect.height - m_imageCache.GetHeight());
+
+ dc.DrawBitmap(m_imageCache, rect.x, y, true);
+#endif
+
+ wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ marginRect = rect; // outer rectangle, will calculate contentRect
+ GetBoxRects(dc, GetBuffer(), GetAttributes(), marginRect, borderRect, contentRect, paddingRect, outlineRect);
+
+ dc.DrawBitmap(m_imageCache, contentRect.x, contentRect.y, true);
+
+ if (selection.WithinSelection(range.GetStart(), this))
+ {
+ wxCheckSetBrush(dc, *wxBLACK_BRUSH);
+ wxCheckSetPen(dc, *wxBLACK_PEN);
+ dc.SetLogicalFunction(wxINVERT);
+ dc.DrawRectangle(contentRect);
+ dc.SetLogicalFunction(wxCOPY);
+ }
+
+ return true;
+}
+
+/// Lay the item out
+bool wxRichTextImage::Layout(wxDC& dc, const wxRect& rect, int WXUNUSED(style))
+{
+ if (!LoadImageCache(dc))
+ return false;
+
+ wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ contentRect = wxRect(wxPoint(0,0), imageSize);
+ GetBoxRects(dc, GetBuffer(), GetAttributes(), marginRect, borderRect, contentRect, paddingRect, outlineRect);
+
+ wxSize overallSize = marginRect.GetSize();
+
+ SetCachedSize(overallSize);
+ SetMaxSize(overallSize);
+ SetMinSize(overallSize);
+ SetPosition(rect.GetPosition());
+
+ return true;
+}
+
+/// Get/set the object size for the given range. Returns false if the range
+/// is invalid for this object.
+bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& dc, int WXUNUSED(flags), wxPoint WXUNUSED(position), wxArrayInt* partialExtents) const
+{
+ if (!range.IsWithin(GetRange()))
+ return false;
+
+ if (!((wxRichTextImage*)this)->LoadImageCache(dc))
+ {
+ size.x = 0; size.y = 0;
+ if (partialExtents)
+ partialExtents->Add(0);
+ return false;
+ }
+
+ wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ contentRect = wxRect(wxPoint(0,0), imageSize);
+ GetBoxRects(dc, GetBuffer(), GetAttributes(), marginRect, borderRect, contentRect, paddingRect, outlineRect);
+
+ wxSize overallSize = marginRect.GetSize();
+
+ if (partialExtents)
+ partialExtents->Add(overallSize.x);
+
+ size = overallSize;
+
+ return true;
+}
+
+// Get the 'natural' size for an object. For an image, it would be the
+// image size.
+wxTextAttrSize wxRichTextImage::GetNaturalSize() const
+{
+ wxTextAttrSize size;
+ if (GetImageCache().IsOk())
+ {
+ size.SetWidth(GetImageCache().GetWidth(), wxTEXT_ATTR_UNITS_PIXELS);
+ size.SetHeight(GetImageCache().GetHeight(), wxTEXT_ATTR_UNITS_PIXELS);
+ }
+ return size;
+}
+
+
+/// Copy
+void wxRichTextImage::Copy(const wxRichTextImage& obj)
+{
+ wxRichTextObject::Copy(obj);
+
+ m_imageBlock = obj.m_imageBlock;
+}
+
+/// Edit properties via a GUI
+bool wxRichTextImage::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
+{
+ wxRichTextObjectPropertiesDialog imageDlg(this, wxGetTopLevelParent(parent), wxID_ANY, _("Picture Properties"));
+ imageDlg.SetAttributes(GetAttributes());
+
+ if (imageDlg.ShowModal() == wxID_OK)
+ {
+ // By passing wxRICHTEXT_SETSTYLE_RESET, indeterminate attributes set by the user will be set as
+ // indeterminate in the object.
+ imageDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
+ return true;
+ }
+ else
+ return false;
+}
+
+/*!
+ * Utilities
+ *
+ */
+
+/// Compare two attribute objects
+bool wxTextAttrEq(const wxRichTextAttr& attr1, const wxRichTextAttr& attr2)
+{
+ return (attr1 == attr2);
+}
+
+// Partial equality test taking flags into account
+bool wxTextAttrEqPartial(const wxRichTextAttr& attr1, const wxRichTextAttr& attr2)
+{
+ return attr1.EqPartial(attr2);
+}
+
+/// Compare tabs
+bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
+{
+ if (tabs1.GetCount() != tabs2.GetCount())
+ return false;
+
+ size_t i;
+ for (i = 0; i < tabs1.GetCount(); i++)
+ {
+ if (tabs1[i] != tabs2[i])
+ return false;
+ }
+ return true;
+}
+
+bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
+{
+ return destStyle.Apply(style, compareWith);
+}
+
+// Remove attributes
+bool wxRichTextRemoveStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style)
+{
+ return destStyle.RemoveStyle(style);
+}
+
+/// Combine two bitlists, specifying the bits of interest with separate flags.
+bool wxRichTextCombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
+{
+ return wxRichTextAttr::CombineBitlists(valueA, valueB, flagsA, flagsB);
+}
+
+/// Compare two bitlists
+bool wxRichTextBitlistsEqPartial(int valueA, int valueB, int flags)
+{
+ return wxRichTextAttr::BitlistsEqPartial(valueA, valueB, flags);
+}
+
+/// Split into paragraph and character styles
+bool wxRichTextSplitParaCharStyles(const wxRichTextAttr& style, wxRichTextAttr& parStyle, wxRichTextAttr& charStyle)
+{
+ return wxRichTextAttr::SplitParaCharStyles(style, parStyle, charStyle);
+}
+
+/// Convert a decimal to Roman numerals
+wxString wxRichTextDecimalToRoman(long n)
+{
+ static wxArrayInt decimalNumbers;
+ static wxArrayString romanNumbers;
+
+ // Clean up arrays
+ if (n == -1)
+ {
+ decimalNumbers.Clear();
+ romanNumbers.Clear();
+ return wxEmptyString;
+ }
+
+ if (decimalNumbers.GetCount() == 0)
+ {
+ #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
+
+ wxRichTextAddDecRom(1000, wxT("M"));
+ wxRichTextAddDecRom(900, wxT("CM"));
+ wxRichTextAddDecRom(500, wxT("D"));
+ wxRichTextAddDecRom(400, wxT("CD"));
+ wxRichTextAddDecRom(100, wxT("C"));
+ wxRichTextAddDecRom(90, wxT("XC"));
+ wxRichTextAddDecRom(50, wxT("L"));
+ wxRichTextAddDecRom(40, wxT("XL"));
+ wxRichTextAddDecRom(10, wxT("X"));
+ wxRichTextAddDecRom(9, wxT("IX"));
+ wxRichTextAddDecRom(5, wxT("V"));
+ wxRichTextAddDecRom(4, wxT("IV"));
+ wxRichTextAddDecRom(1, wxT("I"));
+ }
+
+ int i = 0;
+ wxString roman;
+
+ while (n > 0 && i < 13)
+ {
+ if (n >= decimalNumbers[i])
+ {
+ n -= decimalNumbers[i];
+ roman += romanNumbers[i];
+ }
+ else
+ {
+ i ++;
+ }
+ }
+ if (roman.IsEmpty())
+ roman = wxT("0");
+ return roman;
+}
+
+/*!
+ * wxRichTextFileHandler
+ * Base class for file handlers
+ */
+
+IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
+
+#if wxUSE_FFILE && wxUSE_STREAMS
+bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
+{
+ wxFFileInputStream stream(filename);
+ if (stream.Ok())
+ return LoadFile(buffer, stream);
+
+ return false;
+}
+
+bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
+{
+ wxFFileOutputStream stream(filename);
+ if (stream.Ok())
+ return SaveFile(buffer, stream);
+
+ return false;
+}
+#endif // wxUSE_FFILE && wxUSE_STREAMS
+
+/// Can we handle this filename (if using files)? By default, checks the extension.
+bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
+{
+ wxString path, file, ext;
+ wxFileName::SplitPath(filename, & path, & file, & ext);
+
+ return (ext.Lower() == GetExtension());
+}
+
+/*!
+ * wxRichTextTextHandler
+ * Plain text handler
+ */
+
+IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
+
+#if wxUSE_STREAMS
+bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
+{
+ if (!stream.IsOk())
+ return false;
+
+ wxString str;
+ int lastCh = 0;
+
+ while (!stream.Eof())
+ {
+ int ch = stream.GetC();
+
+ if (!stream.Eof())
+ {
+ if (ch == 10 && lastCh != 13)
+ str += wxT('\n');
+
+ if (ch > 0 && ch != 10)
+ str += wxChar(ch);
+
+ lastCh = ch;
+ }
+ }
+
+ buffer->ResetAndClearCommands();
+ buffer->Clear();
+ buffer->AddParagraphs(str);
+ buffer->UpdateRanges();
+
+ return true;
+}
+
+bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
+{
+ if (!stream.IsOk())
+ return false;
+
+ wxString text = buffer->GetText();
+
+ wxString newLine = wxRichTextLineBreakChar;
+ text.Replace(newLine, wxT("\n"));
+
+ wxCharBuffer buf = text.ToAscii();
+
+ stream.Write((const char*) buf, text.length());
+ return true;
+}
+#endif // wxUSE_STREAMS
+
+/*
+ * Stores information about an image, in binary in-memory form
+ */
+
+wxRichTextImageBlock::wxRichTextImageBlock()
+{
+ Init();
+}
+
+wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
+{
+ Init();
+ Copy(block);
+}
+
+wxRichTextImageBlock::~wxRichTextImageBlock()
+{
+ wxDELETEA(m_data);
+}
+
+void wxRichTextImageBlock::Init()
+{
+ m_data = NULL;
+ m_dataSize = 0;
+ m_imageType = wxBITMAP_TYPE_INVALID;
+}
+
+void wxRichTextImageBlock::Clear()
+{
+ wxDELETEA(m_data);
+ m_dataSize = 0;
+ m_imageType = wxBITMAP_TYPE_INVALID;
+}
+
+
+// Load the original image into a memory block.
+// If the image is not a JPEG, we must convert it into a JPEG
+// to conserve space.
+// If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
+// load the image a 2nd time.
+
+bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, wxBitmapType imageType,
+ wxImage& image, bool convertToJPEG)
+{
+ m_imageType = imageType;
+
+ wxString filenameToRead(filename);
+ bool removeFile = false;
+
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
+ {
+ wxString tempFile =
+ wxFileName::CreateTempFileName(_("image"));
+
+ wxASSERT(!tempFile.IsEmpty());
+
+ image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
+ filenameToRead = tempFile;
+ removeFile = true;
+
+ m_imageType = wxBITMAP_TYPE_JPEG;
+ }
+ wxFile file;
+ if (!file.Open(filenameToRead))
+ return false;
+
+ m_dataSize = (size_t) file.Length();
+ file.Close();
+
+ if (m_data)
+ delete[] m_data;
+ m_data = ReadBlock(filenameToRead, m_dataSize);
+
+ if (removeFile)
+ wxRemoveFile(filenameToRead);
+
+ return (m_data != NULL);
+}
+
+// Make an image block from the wxImage in the given
+// format.
+bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, wxBitmapType imageType, int quality)
+{
+ image.SetOption(wxT("quality"), quality);
+
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ return DoMakeImageBlock(image, imageType);
+}
+
+// Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
+bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage& image, wxBitmapType imageType)
+{
+ if (imageType == wxBITMAP_TYPE_INVALID)
+ return false; // Could not determine image type
+
+ return DoMakeImageBlock(image, imageType);
+}
+
+// Makes the image block
+bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage& image, wxBitmapType imageType)
+{
+ wxMemoryOutputStream memStream;
+ if (!image.SaveFile(memStream, imageType))
+ {
+ return false;
+ }
+
+ unsigned char* block = new unsigned char[memStream.GetSize()];
+ if (!block)
+ return false;
+
+ if (m_data)
+ delete[] m_data;
+ m_data = block;
+
+ m_imageType = imageType;
+ m_dataSize = memStream.GetSize();
+
+ memStream.CopyTo(m_data, m_dataSize);
+
+ return (m_data != NULL);
+}
+
+// Write to a file
+bool wxRichTextImageBlock::Write(const wxString& filename)
+{
+ return WriteBlock(filename, m_data, m_dataSize);
+}
+
+void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
+{
+ m_imageType = block.m_imageType;
+ wxDELETEA(m_data);
+ m_dataSize = block.m_dataSize;
+ if (m_dataSize == 0)
+ return;
+
+ m_data = new unsigned char[m_dataSize];
+ unsigned int i;
+ for (i = 0; i < m_dataSize; i++)
+ m_data[i] = block.m_data[i];
+}
+
+//// Operators
+void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
+{
+ Copy(block);
+}
+
+// Load a wxImage from the block
+bool wxRichTextImageBlock::Load(wxImage& image)
+{
+ if (!m_data)
+ return false;
+
+ // Read in the image.
+#if wxUSE_STREAMS
+ wxMemoryInputStream mstream(m_data, m_dataSize);
+ bool success = image.LoadFile(mstream, GetImageType());
+#else
+ wxString tempFile = wxFileName::CreateTempFileName(_("image"));
+ wxASSERT(!tempFile.IsEmpty());
+
+ if (!WriteBlock(tempFile, m_data, m_dataSize))
+ {
+ return false;
+ }
+ success = image.LoadFile(tempFile, GetImageType());
+ wxRemoveFile(tempFile);
+#endif
+
+ return success;
+}
+
+// Write data in hex to a stream
+bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
+{
+ const int bufSize = 512;
+ char buf[bufSize+1];
+
+ int left = m_dataSize;
+ int n, i, j;
+ j = 0;
+ while (left > 0)
+ {
+ if (left*2 > bufSize)
+ {
+ n = bufSize; left -= (bufSize/2);
+ }
+ else
+ {
+ n = left*2; left = 0;
+ }
+
+ char* b = buf;
+ for (i = 0; i < (n/2); i++)
+ {
+ wxDecToHex(m_data[j], b, b+1);
+ b += 2; j ++;
+ }
+
+ buf[n] = 0;
+ stream.Write((const char*) buf, n);
+ }
+ return true;
+}
+
+// Read data in hex from a stream
+bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, wxBitmapType imageType)
+{
+ int dataSize = length/2;
+
+ if (m_data)
+ delete[] m_data;
+
+ // create a null terminated temporary string:
+ char str[3];
+ str[2] = '\0';
+
+ m_data = new unsigned char[dataSize];
+ int i;
+ for (i = 0; i < dataSize; i ++)
+ {
+ str[0] = (char)stream.GetC();
+ str[1] = (char)stream.GetC();
+
+ m_data[i] = (unsigned char)wxHexToDec(str);
+ }
+
+ m_dataSize = dataSize;
+ m_imageType = imageType;
+
+ return true;
+}
+
+// Allocate and read from stream as a block of memory
+unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
+{
+ unsigned char* block = new unsigned char[size];
+ if (!block)
+ return NULL;
+
+ stream.Read(block, size);
+
+ return block;
+}
+
+unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
+{
+ wxFileInputStream stream(filename);
+ if (!stream.Ok())
+ return NULL;
+
+ return ReadBlock(stream, size);
+}
+
+// Write memory block to stream
+bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
+{
+ stream.Write((void*) block, size);
+ return stream.IsOk();
+
+}
+
+// Write memory block to file
+bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
+{
+ wxFileOutputStream outStream(filename);
+ if (!outStream.Ok())
+ return false;
+
+ return WriteBlock(outStream, block, size);
+}
+
+// Gets the extension for the block's type
+wxString wxRichTextImageBlock::GetExtension() const
+{
+ wxImageHandler* handler = wxImage::FindHandler(GetImageType());
+ if (handler)
+ return handler->GetExtension();
+ else
+ return wxEmptyString;
+}
+
+#if wxUSE_DATAOBJ
+
+/*!
+ * The data object for a wxRichTextBuffer
+ */
+
+const wxChar *wxRichTextBufferDataObject::ms_richTextBufferFormatId = wxT("wxShape");
+
+wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer)
+{
+ m_richTextBuffer = richTextBuffer;
+
+ // this string should uniquely identify our format, but is otherwise
+ // arbitrary
+ m_formatRichTextBuffer.SetId(GetRichTextBufferFormatId());
+
+ SetFormat(m_formatRichTextBuffer);
+}
+
+wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
+{
+ delete m_richTextBuffer;
+}
+
+// after a call to this function, the richTextBuffer is owned by the caller and it
+// is responsible for deleting it!
+wxRichTextBuffer* wxRichTextBufferDataObject::GetRichTextBuffer()
+{
+ wxRichTextBuffer* richTextBuffer = m_richTextBuffer;
+ m_richTextBuffer = NULL;
+
+ return richTextBuffer;
+}
+
+wxDataFormat wxRichTextBufferDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
+{
+ return m_formatRichTextBuffer;
+}
+
+size_t wxRichTextBufferDataObject::GetDataSize() const
+{
+ if (!m_richTextBuffer)
+ return 0;
+
+ wxString bufXML;
+
+ {
+ wxStringOutputStream stream(& bufXML);
+ if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
+ {
+ wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
+ return 0;
+ }
+ }
+
+#if wxUSE_UNICODE
+ wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
+ return strlen(buffer) + 1;
+#else
+ return bufXML.Length()+1;
+#endif
+}
+
+bool wxRichTextBufferDataObject::GetDataHere(void *pBuf) const
+{
+ if (!pBuf || !m_richTextBuffer)
+ return false;
+
+ wxString bufXML;
+
+ {
+ wxStringOutputStream stream(& bufXML);
+ if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
+ {
+ wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
+ return 0;
+ }
+ }
+
+#if wxUSE_UNICODE
+ wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
+ size_t len = strlen(buffer);
+ memcpy((char*) pBuf, (const char*) buffer, len);
+ ((char*) pBuf)[len] = 0;
+#else
+ size_t len = bufXML.Length();
+ memcpy((char*) pBuf, (const char*) bufXML.c_str(), len);
+ ((char*) pBuf)[len] = 0;
+#endif
+
+ return true;
+}
+
+bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf)
+{
+ wxDELETE(m_richTextBuffer);
+
+ wxString bufXML((const char*) buf, wxConvUTF8);
+
+ m_richTextBuffer = new wxRichTextBuffer;
+
+ wxStringInputStream stream(bufXML);
+ if (!m_richTextBuffer->LoadFile(stream, wxRICHTEXT_TYPE_XML))
+ {
+ wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
+
+ wxDELETE(m_richTextBuffer);
+
+ return false;
+ }
+ return true;
+}
+
+#endif
+ // wxUSE_DATAOBJ
+
+
+/*
+ * wxRichTextFontTable
+ * Manages quick access to a pool of fonts for rendering rich text
+ */
+
+WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont, wxRichTextFontTableHashMap, class WXDLLIMPEXP_RICHTEXT);
+
+class wxRichTextFontTableData: public wxObjectRefData
+{
+public:
+ wxRichTextFontTableData() {}
+
+ wxFont FindFont(const wxRichTextAttr& fontSpec);
+
+ wxRichTextFontTableHashMap m_hashMap;
+};
+
+wxFont wxRichTextFontTableData::FindFont(const wxRichTextAttr& fontSpec)
+{
+ wxString facename(fontSpec.GetFontFaceName());
+ 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()));
+ wxRichTextFontTableHashMap::iterator entry = m_hashMap.find(spec);
+
+ if ( entry == m_hashMap.end() )
+ {
+ wxFont font(fontSpec.GetFontSize(), wxDEFAULT, fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), fontSpec.GetFontUnderlined(), facename.c_str());
+ m_hashMap[spec] = font;
+ return font;
+ }
+ else
+ {
+ return entry->second;
+ }
+}
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable, wxObject)
+
+wxRichTextFontTable::wxRichTextFontTable()
+{
+ m_refData = new wxRichTextFontTableData;
+}
+
+wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable& table)
+ : wxObject()
+{
+ (*this) = table;
+}
+
+wxRichTextFontTable::~wxRichTextFontTable()
+{
+ UnRef();
+}
+
+bool wxRichTextFontTable::operator == (const wxRichTextFontTable& table) const
+{
+ return (m_refData == table.m_refData);
+}
+
+void wxRichTextFontTable::operator= (const wxRichTextFontTable& table)
+{
+ Ref(table);
+}
+
+wxFont wxRichTextFontTable::FindFont(const wxRichTextAttr& fontSpec)
+{
+ wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
+ if (data)
+ return data->FindFont(fontSpec);
+ else
+ return wxFont();
+}
+
+void wxRichTextFontTable::Clear()
+{
+ wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
+ if (data)
+ data->m_hashMap.clear();
+}
+
+// wxTextBoxAttr
+
+
+void wxTextBoxAttr::Reset()
+{
+ m_flags = 0;
+ m_floatMode = wxTEXT_BOX_ATTR_FLOAT_NONE;
+ m_clearMode = wxTEXT_BOX_ATTR_CLEAR_NONE;
+ m_collapseMode = wxTEXT_BOX_ATTR_COLLAPSE_NONE;
+ m_verticalAlignment = wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_NONE;
+
+ m_margins.Reset();
+ m_padding.Reset();
+ m_position.Reset();
+
+ m_size.Reset();
+
+ m_border.Reset();
+ m_outline.Reset();
+}
+
+// Equality test
+bool wxTextBoxAttr::operator== (const wxTextBoxAttr& attr) const
+{
+ return (
+ m_flags == attr.m_flags &&
+ m_floatMode == attr.m_floatMode &&
+ m_clearMode == attr.m_clearMode &&
+ m_collapseMode == attr.m_collapseMode &&
+ m_verticalAlignment == attr.m_verticalAlignment &&
+
+ m_margins == attr.m_margins &&
+ m_padding == attr.m_padding &&
+ m_position == attr.m_position &&
+
+ m_size == attr.m_size &&
+
+ m_border == attr.m_border &&
+ m_outline == attr.m_outline
+ );
+}
+
+// Partial equality test
+bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr& attr) const
+{
+ if (attr.HasFloatMode() && HasFloatMode() && (GetFloatMode() != attr.GetFloatMode()))
+ return false;
+
+ if (attr.HasClearMode() && HasClearMode() && (GetClearMode() != attr.GetClearMode()))
+ return false;
+
+ if (attr.HasCollapseBorders() && HasCollapseBorders() && (attr.GetCollapseBorders() != GetCollapseBorders()))
+ return false;
+
+ if (attr.HasVerticalAlignment() && HasVerticalAlignment() && (attr.GetVerticalAlignment() != GetVerticalAlignment()))
+ return false;
+
+ // Position
+
+ if (!m_position.EqPartial(attr.m_position))
+ return false;
+
+ // Margins
+
+ if (!m_margins.EqPartial(attr.m_margins))
+ return false;
+
+ // Padding
+
+ if (!m_padding.EqPartial(attr.m_padding))
+ return false;
+
+ // Border
+
+ if (!GetBorder().EqPartial(attr.GetBorder()))
+ return false;
+
+ // Outline
+
+ if (!GetOutline().EqPartial(attr.GetOutline()))
+ return false;
+
+ return true;
+}
+
+// Merges the given attributes. If compareWith
+// is non-NULL, then it will be used to mask out those attributes that are the same in style
+// and compareWith, for situations where we don't want to explicitly set inherited attributes.
+bool wxTextBoxAttr::Apply(const wxTextBoxAttr& attr, const wxTextBoxAttr* compareWith)
+{
+ if (attr.HasFloatMode())
+ {
+ if (!(compareWith && compareWith->HasFloatMode() && compareWith->GetFloatMode() == attr.GetFloatMode()))
+ SetFloatMode(attr.GetFloatMode());
+ }
+
+ if (attr.HasClearMode())
+ {
+ if (!(compareWith && compareWith->HasClearMode() && compareWith->GetClearMode() == attr.GetClearMode()))
+ SetClearMode(attr.GetClearMode());
+ }
+
+ if (attr.HasCollapseBorders())
+ {
+ if (!(compareWith && compareWith->HasCollapseBorders() && compareWith->GetCollapseBorders() == attr.GetCollapseBorders()))
+ SetCollapseBorders(attr.GetCollapseBorders());
+ }
+
+ if (attr.HasVerticalAlignment())
+ {
+ if (!(compareWith && compareWith->HasVerticalAlignment() && compareWith->GetVerticalAlignment() == attr.GetVerticalAlignment()))
+ SetVerticalAlignment(attr.GetVerticalAlignment());
+ }
+
+ m_margins.Apply(attr.m_margins, compareWith ? (& attr.m_margins) : (const wxTextAttrDimensions*) NULL);
+ m_padding.Apply(attr.m_padding, compareWith ? (& attr.m_padding) : (const wxTextAttrDimensions*) NULL);
+ m_position.Apply(attr.m_position, compareWith ? (& attr.m_position) : (const wxTextAttrDimensions*) NULL);
+
+ m_size.Apply(attr.m_size, compareWith ? (& attr.m_size) : (const wxTextAttrSize*) NULL);
+
+ m_border.Apply(attr.m_border, compareWith ? (& attr.m_border) : (const wxTextAttrBorders*) NULL);
+ m_outline.Apply(attr.m_outline, compareWith ? (& attr.m_outline) : (const wxTextAttrBorders*) NULL);
+
+ return true;
+}
+
+// Remove specified attributes from this object
+bool wxTextBoxAttr::RemoveStyle(const wxTextBoxAttr& attr)
+{
+ if (attr.HasFloatMode())
+ RemoveFlag(wxTEXT_BOX_ATTR_FLOAT);
+
+ if (attr.HasClearMode())
+ RemoveFlag(wxTEXT_BOX_ATTR_CLEAR);
+
+ if (attr.HasCollapseBorders())
+ RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
+
+ if (attr.HasVerticalAlignment())
+ RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
+
+ m_margins.RemoveStyle(attr.m_margins);
+ m_padding.RemoveStyle(attr.m_padding);
+ m_position.RemoveStyle(attr.m_position);
+
+ m_size.RemoveStyle(attr.m_size);
+
+ m_border.RemoveStyle(attr.m_border);
+ m_outline.RemoveStyle(attr.m_outline);
+
+ return true;
+}
+
+// Collects the attributes that are common to a range of content, building up a note of
+// which attributes are absent in some objects and which clash in some objects.
+void wxTextBoxAttr::CollectCommonAttributes(const wxTextBoxAttr& attr, wxTextBoxAttr& clashingAttr, wxTextBoxAttr& absentAttr)
+{
+ if (attr.HasFloatMode())
+ {
+ if (!clashingAttr.HasFloatMode() && !absentAttr.HasFloatMode())
+ {
+ if (HasFloatMode())
+ {
+ if (GetFloatMode() != attr.GetFloatMode())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_FLOAT);
+ RemoveFlag(wxTEXT_BOX_ATTR_FLOAT);
+ }
+ }
+ else
+ SetFloatMode(attr.GetFloatMode());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_FLOAT);
+
+ if (attr.HasClearMode())
+ {
+ if (!clashingAttr.HasClearMode() && !absentAttr.HasClearMode())
+ {
+ if (HasClearMode())
+ {
+ if (GetClearMode() != attr.GetClearMode())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_CLEAR);
+ RemoveFlag(wxTEXT_BOX_ATTR_CLEAR);
+ }
+ }
+ else
+ SetClearMode(attr.GetClearMode());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_CLEAR);
+
+ if (attr.HasCollapseBorders())
+ {
+ if (!clashingAttr.HasCollapseBorders() && !absentAttr.HasCollapseBorders())
+ {
+ if (HasCollapseBorders())
+ {
+ if (GetCollapseBorders() != attr.GetCollapseBorders())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
+ RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
+ }
+ }
+ else
+ SetCollapseBorders(attr.GetCollapseBorders());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);