+ return position;
+}
+
+int wxRichTextTable::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int flags)
+{
+ for (int row = 0; row < GetRowCount(); ++row)
+ {
+ for (int col = 0; col < GetColumnCount(); ++col)
+ {
+ wxRichTextCell* cell = GetCell(row, col);
+ if (cell->wxRichTextObject::HitTest(dc, context, pt, textPosition, obj, contextObj, flags) != wxRICHTEXT_HITTEST_NONE)
+ {
+ return cell->HitTest(dc, context, pt, textPosition, obj, contextObj, flags);
+ }
+ }
+ }
+
+ return wxRICHTEXT_HITTEST_NONE;
+}
+
+bool wxRichTextTable::DeleteRows(int startRow, int noRows)
+{
+ wxASSERT((startRow + noRows) <= m_rowCount);
+ if ((startRow + noRows) > m_rowCount)
+ return false;
+
+ wxCHECK_MSG(noRows != m_rowCount, false, "Trying to delete all the cells in a table");
+
+ wxRichTextBuffer* buffer = GetBuffer();
+ wxRichTextCtrl* rtc = buffer->GetRichTextCtrl();
+
+ wxRichTextAction* action = NULL;
+ wxRichTextTable* clone = NULL;
+ if (!rtc->SuppressingUndo())
+ {
+ // Create a clone containing the current state of the table. It will be used to Undo the action
+ clone = wxStaticCast(this->Clone(), wxRichTextTable);
+ clone->SetParent(GetParent());
+ action = new wxRichTextAction(NULL, _("Delete Row"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, rtc);
+ action->SetObject(this);
+ action->SetPosition(GetRange().GetStart());
+ }
+
+ int i, j;
+ for (i = startRow; i < (startRow+noRows); i++)
+ {
+ wxRichTextObjectPtrArray& colArray = m_cells[startRow];
+ for (j = 0; j < (int) colArray.GetCount(); j++)
+ {
+ wxRichTextObject* cell = colArray[j];
+ RemoveChild(cell, true);
+ }
+
+ // Keep deleting at the same position, since we move all
+ // the others up
+ m_cells.RemoveAt(startRow);
+ }
+
+ m_rowCount = m_rowCount - noRows;
+
+ if (!rtc->SuppressingUndo())
+ {
+ buffer->SubmitAction(action);
+ // Finally store the original-state clone; doing so earlier would cause various failures
+ action->StoreObject(clone);
+ }
+
+ return true;
+}
+
+bool wxRichTextTable::DeleteColumns(int startCol, int noCols)
+{
+ wxASSERT((startCol + noCols) <= m_colCount);
+ if ((startCol + noCols) > m_colCount)
+ return false;
+
+ wxCHECK_MSG(noCols != m_colCount, false, "Trying to delete all the cells in a table");
+
+ wxRichTextBuffer* buffer = GetBuffer();
+ wxRichTextCtrl* rtc = buffer->GetRichTextCtrl();
+
+ wxRichTextAction* action = NULL;
+ wxRichTextTable* clone = NULL;
+ if (!rtc->SuppressingUndo())
+ {
+ // Create a clone containing the current state of the table. It will be used to Undo the action
+ clone = wxStaticCast(this->Clone(), wxRichTextTable);
+ clone->SetParent(GetParent());
+ action = new wxRichTextAction(NULL, _("Delete Column"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, rtc);
+ action->SetObject(this);
+ action->SetPosition(GetRange().GetStart());
+ }
+
+ bool deleteRows = (noCols == m_colCount);
+
+ int i, j;
+ for (i = 0; i < m_rowCount; i++)
+ {
+ wxRichTextObjectPtrArray& colArray = m_cells[deleteRows ? 0 : i];
+ for (j = 0; j < noCols; j++)
+ {
+ wxRichTextObject* cell = colArray[startCol];
+ RemoveChild(cell, true);
+ colArray.RemoveAt(startCol);
+ }
+
+ if (deleteRows)
+ m_cells.RemoveAt(0);
+ }
+
+ if (deleteRows)
+ m_rowCount = 0;
+ m_colCount = m_colCount - noCols;
+
+ if (!rtc->SuppressingUndo())
+ {
+ buffer->SubmitAction(action);
+ // Finally store the original-state clone; doing so earlier would cause various failures
+ action->StoreObject(clone);
+ }
+
+ return true;
+}
+
+bool wxRichTextTable::AddRows(int startRow, int noRows, const wxRichTextAttr& attr)
+{
+ wxASSERT(startRow <= m_rowCount);
+ if (startRow > m_rowCount)
+ return false;
+
+ wxRichTextBuffer* buffer = GetBuffer();
+ wxRichTextAction* action = NULL;
+ wxRichTextTable* clone = NULL;
+
+ if (!buffer->GetRichTextCtrl()->SuppressingUndo())
+ {
+ // Create a clone containing the current state of the table. It will be used to Undo the action
+ clone = wxStaticCast(this->Clone(), wxRichTextTable);
+ clone->SetParent(GetParent());
+ action = new wxRichTextAction(NULL, _("Add Row"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, buffer->GetRichTextCtrl());
+ action->SetObject(this);
+ action->SetPosition(GetRange().GetStart());
+ }
+
+ wxRichTextAttr cellattr = attr;
+ if (!cellattr.GetTextColour().IsOk())
+ cellattr.SetTextColour(buffer->GetBasicStyle().GetTextColour());
+
+ int i, j;
+ for (i = 0; i < noRows; i++)
+ {
+ int idx;
+ if (startRow == m_rowCount)
+ {
+ m_cells.Add(wxRichTextObjectPtrArray());
+ idx = m_cells.GetCount() - 1;
+ }
+ else
+ {
+ m_cells.Insert(wxRichTextObjectPtrArray(), startRow+i);
+ idx = startRow+i;
+ }
+
+ wxRichTextObjectPtrArray& colArray = m_cells[idx];
+ for (j = 0; j < m_colCount; j++)
+ {
+ wxRichTextCell* cell = new wxRichTextCell;
+ cell->GetAttributes() = cellattr;
+
+ AppendChild(cell);
+ cell->AddParagraph(wxEmptyString);
+ colArray.Add(cell);
+ }
+ }
+
+ m_rowCount = m_rowCount + noRows;
+
+ if (!buffer->GetRichTextCtrl()->SuppressingUndo())
+ {
+ buffer->SubmitAction(action);
+ // Finally store the original-state clone; doing so earlier would cause various failures
+ action->StoreObject(clone);
+ }
+
+ return true;
+}
+
+bool wxRichTextTable::AddColumns(int startCol, int noCols, const wxRichTextAttr& attr)
+{
+ wxASSERT(startCol <= m_colCount);
+ if (startCol > m_colCount)
+ return false;
+
+ wxRichTextBuffer* buffer = GetBuffer();
+ wxRichTextAction* action = NULL;
+ wxRichTextTable* clone = NULL;
+
+ if (!buffer->GetRichTextCtrl()->SuppressingUndo())
+ {
+ // Create a clone containing the current state of the table. It will be used to Undo the action
+ clone = wxStaticCast(this->Clone(), wxRichTextTable);
+ clone->SetParent(GetParent());
+ action = new wxRichTextAction(NULL, _("Add Column"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, buffer->GetRichTextCtrl());
+ action->SetObject(this);
+ action->SetPosition(GetRange().GetStart());
+ }
+
+ wxRichTextAttr cellattr = attr;
+ if (!cellattr.GetTextColour().IsOk())
+ cellattr.SetTextColour(buffer->GetBasicStyle().GetTextColour());
+
+ int i, j;
+ for (i = 0; i < m_rowCount; i++)
+ {
+ wxRichTextObjectPtrArray& colArray = m_cells[i];
+ for (j = 0; j < noCols; j++)
+ {
+ wxRichTextCell* cell = new wxRichTextCell;
+ cell->GetAttributes() = cellattr;
+
+ AppendChild(cell);
+ cell->AddParagraph(wxEmptyString);
+
+ if (startCol == m_colCount)
+ colArray.Add(cell);
+ else
+ colArray.Insert(cell, startCol+j);
+ }
+ }
+
+ m_colCount = m_colCount + noCols;
+
+ if (!buffer->GetRichTextCtrl()->SuppressingUndo())
+ {
+ buffer->SubmitAction(action);
+ // Finally store the original-state clone; doing so earlier would cause various failures
+ action->StoreObject(clone);
+ }
+
+ return true;
+}
+
+// Edit properties via a GUI
+bool wxRichTextTable::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
+{
+ wxRichTextObjectPropertiesDialog boxDlg(this, wxGetTopLevelParent(parent), wxID_ANY, _("Table Properties"));
+ boxDlg.SetAttributes(GetAttributes());
+
+ if (boxDlg.ShowModal() == wxID_OK)
+ {
+ boxDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
+ return true;
+ }
+ else
+ return false;
+}
+
+bool wxRichTextTableBlock::ComputeBlockForSelection(wxRichTextTable* table, wxRichTextCtrl* ctrl, bool requireCellSelection)
+{
+ if (!ctrl)
+ return false;
+
+ ColStart() = 0;
+ ColEnd() = table->GetColumnCount()-1;
+ RowStart() = 0;
+ RowEnd() = table->GetRowCount()-1;
+
+ wxRichTextSelection selection = ctrl->GetSelection();
+ if (selection.IsValid() && selection.GetContainer() == table)
+ {
+ // Start with an invalid block and increase.
+ wxRichTextTableBlock selBlock(-1, -1, -1, -1);
+ wxRichTextRangeArray ranges = selection.GetRanges();
+ int row, col;
+ for (row = 0; row < table->GetRowCount(); row++)
+ {
+ for (col = 0; col < table->GetColumnCount(); col++)
+ {
+ if (selection.WithinSelection(table->GetCell(row, col)->GetRange().GetStart()))
+ {
+ if (selBlock.ColStart() == -1)
+ selBlock.ColStart() = col;
+ if (selBlock.ColEnd() == -1)
+ selBlock.ColEnd() = col;
+ if (col < selBlock.ColStart())
+ selBlock.ColStart() = col;
+ if (col > selBlock.ColEnd())
+ selBlock.ColEnd() = col;
+
+ if (selBlock.RowStart() == -1)
+ selBlock.RowStart() = row;
+ if (selBlock.RowEnd() == -1)
+ selBlock.RowEnd() = row;
+ if (row < selBlock.RowStart())
+ selBlock.RowStart() = row;
+ if (row > selBlock.RowEnd())
+ selBlock.RowEnd() = row;
+ }
+ }
+ }
+
+ if (selBlock.RowStart() != -1 && selBlock.RowEnd() != -1 && selBlock.ColStart() != -1 && selBlock.ColEnd() != -1)
+ (*this) = selBlock;
+ }
+ else
+ {
+ // See if a whole cell's contents is selected, in which case we can treat the cell as selected.
+ // wxRTC lacks the ability to select a single cell.
+ wxRichTextCell* cell = wxDynamicCast(ctrl->GetFocusObject(), wxRichTextCell);
+ if (cell && (!requireCellSelection || (ctrl->HasSelection() && ctrl->GetSelectionRange() == cell->GetOwnRange())))
+ {
+ int row, col;
+ if (table->GetCellRowColumnPosition(cell->GetRange().GetStart(), row, col))
+ {
+ RowStart() = row;
+ RowEnd() = row;
+ ColStart() = col;
+ ColEnd() = col;
+ }
+ }
+ }
+
+ return true;
+}
+
+// Does this block represent the whole table?
+bool wxRichTextTableBlock::IsWholeTable(wxRichTextTable* table) const
+{
+ return (ColStart() == 0 && RowStart() == 0 && ColEnd() == (table->GetColumnCount()-1) && RowEnd() == (table->GetRowCount()-1));
+}
+
+// Returns the cell focused in the table, if any
+wxRichTextCell* wxRichTextTableBlock::GetFocusedCell(wxRichTextCtrl* ctrl)
+{
+ if (!ctrl)
+ return NULL;
+
+ wxRichTextCell* cell = wxDynamicCast(ctrl->GetFocusObject(), wxRichTextCell);
+ return cell;
+}
+
+/*
+ * Module to initialise and clean up handlers
+ */
+
+class wxRichTextModule: public wxModule
+{
+DECLARE_DYNAMIC_CLASS(wxRichTextModule)
+public:
+ wxRichTextModule() {}
+ bool OnInit()
+ {
+ wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer);
+ wxRichTextBuffer::InitStandardHandlers();
+ wxRichTextParagraph::InitDefaultTabs();
+
+ wxRichTextXMLHandler::RegisterNodeName(wxT("text"), wxT("wxRichTextPlainText"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("symbol"), wxT("wxRichTextPlainText"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("image"), wxT("wxRichTextImage"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("paragraph"), wxT("wxRichTextParagraph"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("paragraphlayout"), wxT("wxRichTextParagraphLayoutBox"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("textbox"), wxT("wxRichTextBox"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("cell"), wxT("wxRichTextCell"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("table"), wxT("wxRichTextTable"));
+ wxRichTextXMLHandler::RegisterNodeName(wxT("field"), wxT("wxRichTextField"));
+
+ return true;
+ }
+ void OnExit()
+ {
+ wxRichTextBuffer::CleanUpHandlers();
+ wxRichTextBuffer::CleanUpDrawingHandlers();
+ wxRichTextBuffer::CleanUpFieldTypes();
+ wxRichTextXMLHandler::ClearNodeToClassMap();
+ wxRichTextDecimalToRoman(-1);
+ wxRichTextParagraph::ClearDefaultTabs();
+ wxRichTextCtrl::ClearAvailableFontNames();
+ wxRichTextBuffer::SetRenderer(NULL);
+ }
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
+
+
+// If the richtext lib is dynamically loaded after the app has already started
+// (such as from wxPython) then the built-in module system will not init this
+// module. Provide this function to do it manually.
+void wxRichTextModuleInit()
+{
+ wxModule* module = new wxRichTextModule;
+ wxModule::RegisterModule(module);
+ wxModule::InitializeModules();
+}
+
+
+/*!
+ * Commands for undo/redo
+ *
+ */
+
+wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
+ wxRichTextParagraphLayoutBox* container, wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name)
+{
+ /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, container, ctrl, ignoreFirstTime);
+}
+
+wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name)
+{
+}
+
+wxRichTextCommand::~wxRichTextCommand()
+{
+ ClearActions();
+}
+
+void wxRichTextCommand::AddAction(wxRichTextAction* action)
+{
+ if (!m_actions.Member(action))
+ m_actions.Append(action);
+}
+
+bool wxRichTextCommand::Do()
+{
+ for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext())
+ {
+ wxRichTextAction* action = (wxRichTextAction*) node->GetData();
+ action->Do();
+ }
+
+ return true;
+}
+
+bool wxRichTextCommand::Undo()
+{
+ for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious())
+ {
+ wxRichTextAction* action = (wxRichTextAction*) node->GetData();
+ action->Undo();
+ }
+
+ return true;
+}
+
+void wxRichTextCommand::ClearActions()
+{
+ WX_CLEAR_LIST(wxList, m_actions);
+}
+
+/*!
+ * Individual action
+ *
+ */
+
+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->GetUnscaledSize(m_ctrl->GetClientSize());
+ wxPoint firstVisiblePt = m_ctrl->GetUnscaledPoint(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_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_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:
+ case wxRICHTEXT_CHANGE_PROPERTIES:
+ {
+ ApplyParagraphs(GetNewParagraphs());
+
+ // Invalidate the whole buffer if there were floating objects
+ if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
+ m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
+ else
+ {
+ // 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(
+ m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_RICHTEXT_STYLE_CHANGED : wxEVT_RICHTEXT_PROPERTIES_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.
+ // Invalidate the whole buffer if there were floating objects
+ if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
+ m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
+ else
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition());
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_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);
+ if (obj && m_object && m_ctrl)
+ {
+ // The plan is to swap the current object with the stored, previous-state, clone
+ // We can't get 'node' from the containing buffer (as it doesn't directly store objects)
+ // so use the parent paragraph
+ wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
+ wxCHECK_MSG(para, false, "Invalid parent paragraph");
+
+ // The stored object, m_object, may have a stale parent paragraph. This would cause
+ // a crash during layout, so use obj's parent para, which should be the correct one.
+ // (An alternative would be to return the parent too from m_objectAddress.GetObject(),
+ // or to set obj's parent there before returning)
+ m_object->SetParent(para);
+
+ wxRichTextObjectList::compatibility_iterator node = para->GetChildren().Find(obj);
+ if (node)
+ {
+ wxRichTextObject* obj = node->GetData();
+ node->SetData(m_object);
+ m_object = obj;
+ }
+ }
+
+ // We can't rely on the current focus-object remaining valid, if it's e.g. a table's cell.
+ // And we can't cope with this in the calling code: a user may later click in the cell
+ // before deciding to Undo() or Redo(). So play safe and set focus to the buffer.
+ if (m_ctrl)
+ m_ctrl->SetFocusObject(m_buffer, false);
+
+ // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
+ // Layout() would stop prematurely at the top level.
+ // Invalidate the whole buffer if there were floating objects
+ if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
+ m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
+ else
+ container->InvalidateHierarchy(GetRange());
+
+ UpdateAppearance(GetPosition(), true);
+
+ // 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_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_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:
+ case wxRICHTEXT_CHANGE_PROPERTIES:
+ {
+ 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(
+ m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_RICHTEXT_STYLE_CHANGED : wxEVT_RICHTEXT_PROPERTIES_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 ((wxRichTextBuffer::GetFloatingLayoutMode() && 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->GetUnscaledSize(m_ctrl->GetClientSize());
+ wxPoint firstVisiblePt = m_ctrl->GetUnscaledPoint(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.
+ long pos = GetRange().GetStart();
+
+ wxRichTextParagraph* para = container->GetParagraphAtPosition(pos, false /* is not caret pos */);
+ // Since we support floating layout, we should redraw the whole para instead of just
+ // the first line touching the invalid range.
+ if (para)
+ {
+ // In case something was drawn above the paragraph,
+ // such as a line break, allow a little extra.
+ firstY = para->GetPosition().y - 4;
+ }
+
+ 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 + line->GetSize().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(m_ctrl->GetScaledPoint(wxPoint(firstVisiblePt.x, firstY))), m_ctrl->GetScaledSize(wxSize(clientSize.x, lastY - firstY)));
+ m_ctrl->RefreshRect(rect);
+ }
+ else
+#endif
+ m_ctrl->Refresh(false);
+
+ m_ctrl->PositionCaret();
+
+ // This causes styles to persist when doing programmatic
+ // content creation except when Freeze/Thaw is used, so
+ // disable this and check for the consequences.
+ // 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)
+{
+ Init();
+ m_imageBlock.MakeImageBlockDefaultQuality(image, wxBITMAP_TYPE_PNG);
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent, wxRichTextAttr* charStyle):
+ wxRichTextObject(parent)
+{
+ Init();
+ m_imageBlock = imageBlock;
+ if (charStyle)
+ SetAttributes(*charStyle);
+}
+
+wxRichTextImage::~wxRichTextImage()
+{
+}
+
+void wxRichTextImage::Init()
+{
+ m_originalImageSize = wxSize(-1, -1);
+}
+
+/// Create a cached image at the required size
+bool wxRichTextImage::LoadImageCache(wxDC& dc, bool resetCache, const wxSize& parentSize)
+{
+ if (!m_imageBlock.IsOk())
+ return false;
+
+ // If we have an original image size, use that to compute the cached bitmap size
+ // instead of loading the image each time. This way we can avoid loading
+ // the image so long as the new cached bitmap size hasn't changed.
+
+ wxImage image;
+ if (resetCache || m_originalImageSize.GetWidth() <= 0 || m_originalImageSize.GetHeight() <= 0)
+ {
+ m_imageCache = wxNullBitmap;
+
+ m_imageBlock.Load(image);
+ if (!image.IsOk())
+ return false;
+
+ m_originalImageSize = wxSize(image.GetWidth(), image.GetHeight());
+ }
+
+ int width = m_originalImageSize.GetWidth();
+ int height = m_originalImageSize.GetHeight();
+
+ int parentWidth = 0;
+ int parentHeight = 0;
+
+ int maxWidth = -1;
+ int maxHeight = -1;
+
+ wxSize sz = parentSize;
+ if (sz == wxDefaultSize)
+ {
+ if (GetParent() && GetParent()->GetParent())
+ sz = GetParent()->GetParent()->GetCachedSize();
+ }
+
+ if (sz != wxDefaultSize)
+ {
+ wxRichTextBuffer* buffer = GetBuffer();
+ if (buffer)
+ {
+ // Find the actual space available when margin is taken into account
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ marginRect = wxRect(0, 0, sz.x, sz.y);
+ if (GetParent() && GetParent()->GetParent())
+ {
+ buffer->GetBoxRects(dc, buffer, GetParent()->GetParent()->GetAttributes(), marginRect, borderRect, contentRect, paddingRect, outlineRect);
+ sz = contentRect.GetSize();
+ }
+
+ // Use a minimum size to stop images becoming very small
+ parentWidth = wxMax(100, sz.GetWidth());
+ parentHeight = wxMax(100, sz.GetHeight());
+
+ if (buffer->GetRichTextCtrl())
+ // Start with a maximum width of the control size, even if not specified by the content,
+ // to minimize the amount of picture overlapping the right-hand side
+ maxWidth = parentWidth;
+ }
+ }
+
+ if (GetAttributes().GetTextBoxAttr().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
+ {
+ if (parentWidth > 0 && GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
+ width = (int) ((GetAttributes().GetTextBoxAttr().GetWidth().GetValue() * parentWidth)/100.0);
+ else if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ width = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
+ else if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ width = GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
+ }
+
+ // Limit to max width
+
+ if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue() > 0)
+ {
+ int mw = -1;
+
+ if (parentWidth > 0 && GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
+ mw = (int) ((GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue() * parentWidth)/100.0);
+ else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ mw = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue());
+ else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ mw = GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue();
+
+ // If we already have a smaller max width due to the constraints of the control size,
+ // don't use the larger max width.
+ if (mw != -1 && ((maxWidth == -1) || (mw < maxWidth)))
+ maxWidth = mw;
+ }
+
+ if (maxWidth > 0 && width > maxWidth)
+ width = maxWidth;
+
+ // Preserve the aspect ratio
+ if (width != m_originalImageSize.GetWidth())
+ height = (int) (float(m_originalImageSize.GetHeight()) * (float(width)/float(m_originalImageSize.GetWidth())));
+
+ if (GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
+ {
+ if (parentHeight > 0 && GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
+ height = (int) ((GetAttributes().GetTextBoxAttr().GetHeight().GetValue() * parentHeight)/100.0);
+ else if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ height = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
+ else if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ height = GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
+
+ // Preserve the aspect ratio
+ if (height != m_originalImageSize.GetHeight())
+ width = (int) (float(m_originalImageSize.GetWidth()) * (float(height)/float(m_originalImageSize.GetHeight())));
+ }
+
+ // Limit to max height
+
+ if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue() > 0)
+ {
+ if (parentHeight > 0 && GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
+ maxHeight = (int) ((GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue() * parentHeight)/100.0);
+ else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ maxHeight = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue());
+ else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ maxHeight = GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue();
+ }
+
+ if (maxHeight > 0 && height > maxHeight)
+ {
+ height = maxHeight;
+
+ // Preserve the aspect ratio
+ if (height != m_originalImageSize.GetHeight())
+ width = (int) (float(m_originalImageSize.GetWidth()) * (float(height)/float(m_originalImageSize.GetHeight())));
+ }
+
+ // Prevent the use of zero size
+ width = wxMax(1, width);
+ height = wxMax(1, height);
+
+ if (m_imageCache.IsOk() && m_imageCache.GetWidth() == width && m_imageCache.GetHeight() == height)
+ {
+ // Do nothing, we didn't need to change the image cache
+ }
+ else
+ {
+ if (!image.IsOk())
+ {
+ m_imageBlock.Load(image);
+ if (!image.IsOk())
+ return false;
+ }
+
+ 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, wxRichTextDrawingContext& context, const wxRichTextRange& WXUNUSED(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;
+
+ wxRichTextAttr attr(GetAttributes());
+ context.ApplyVirtualAttributes(attr, this);
+
+ DrawBoxAttributes(dc, GetBuffer(), attr, wxRect(rect.GetPosition(), GetCachedSize()));
+
+ wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ marginRect = rect; // outer rectangle, will calculate contentRect
+ GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
+
+ dc.DrawBitmap(m_imageCache, contentRect.x, contentRect.y, true);
+
+ if (selection.WithinSelection(GetRange().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, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& WXUNUSED(parentRect), 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);
+
+ wxRichTextAttr attr(GetAttributes());
+ context.ApplyVirtualAttributes(attr, this);
+
+ GetBoxRects(dc, GetBuffer(), attr, 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, wxRichTextDrawingContext& context, int WXUNUSED(flags), const wxPoint& WXUNUSED(position), const wxSize& parentSize, wxArrayInt* partialExtents) const
+{
+ if (!range.IsWithin(GetRange()))
+ return false;
+
+ if (!((wxRichTextImage*)this)->LoadImageCache(dc, false, parentSize))
+ {
+ size.x = 0; size.y = 0;
+ if (partialExtents)
+ partialExtents->Add(0);
+ return false;
+ }
+
+ wxRichTextAttr attr(GetAttributes());
+ context.ApplyVirtualAttributes(attr, (wxRichTextObject*) this);
+
+ wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
+ wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
+ contentRect = wxRect(wxPoint(0,0), imageSize);
+ GetBoxRects(dc, GetBuffer(), attr, 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;
+ m_originalImageSize = obj.m_originalImageSize;
+}
+
+/// 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);
+}
+
+/// 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.IsOk())
+ return LoadFile(buffer, stream);
+
+ return false;
+}
+
+bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
+{
+ wxFFileOutputStream stream(filename);
+ if (stream.IsOk())
+ 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)
+{
+ if (m_dataSize == 0)
+ return true;
+
+ int bufSize = 100000;
+ if (int(2*m_dataSize) < bufSize)
+ bufSize = 2*m_dataSize;
+ char* buf = new char[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);
+ }
+ delete[] buf;
+ 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.IsOk())
+ 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.IsOk())
+ 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("wxRichText");
+
+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, double fontScale);
+
+ wxRichTextFontTableHashMap m_hashMap;
+};
+
+wxFont wxRichTextFontTableData::FindFont(const wxRichTextAttr& fontSpec, double fontScale)
+{
+ wxString facename(fontSpec.GetFontFaceName());
+
+ int fontSize = fontSpec.GetFontSize();
+ if (fontScale != 1.0)
+ fontSize = (int) ((double(fontSize) * fontScale) + 0.5);
+
+ wxString units;
+ if (fontSpec.HasFontPixelSize() && !fontSpec.HasFontPointSize())
+ units = wxT("px");
+ else
+ units = wxT("pt");
+ wxString spec = wxString::Format(wxT("%d-%s-%d-%d-%d-%d-%s-%d"),
+ fontSize, units.c_str(), fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), (int) fontSpec.GetFontUnderlined(), (int) fontSpec.GetFontStrikethrough(),
+ facename.c_str(), (int) fontSpec.GetFontEncoding());
+
+ wxRichTextFontTableHashMap::iterator entry = m_hashMap.find(spec);
+ if ( entry == m_hashMap.end() )
+ {
+ if (fontSpec.HasFontPixelSize() && !fontSpec.HasFontPointSize())
+ {
+ wxFont font(wxSize(0, fontSize), wxFONTFAMILY_DEFAULT, fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), fontSpec.GetFontUnderlined(), facename);
+ if (fontSpec.HasFontStrikethrough() && fontSpec.GetFontStrikethrough())
+ font.SetStrikethrough(true);
+ m_hashMap[spec] = font;
+ return font;
+ }
+ else
+ {
+ wxFont font(fontSize, wxFONTFAMILY_DEFAULT, fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), fontSpec.GetFontUnderlined(), facename.c_str());
+ if (fontSpec.HasFontStrikethrough() && fontSpec.GetFontStrikethrough())
+ font.SetStrikethrough(true);
+
+ m_hashMap[spec] = font;
+ return font;
+ }
+ }
+ else
+ {
+ return entry->second;
+ }
+}
+
+IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable, wxObject)
+
+wxRichTextFontTable::wxRichTextFontTable()
+{
+ m_refData = new wxRichTextFontTableData;
+ m_fontScale = 1.0;
+}
+
+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);
+ m_fontScale = table.m_fontScale;
+}
+
+wxFont wxRichTextFontTable::FindFont(const wxRichTextAttr& fontSpec)
+{
+ wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
+ if (data)
+ return data->FindFont(fontSpec, m_fontScale);
+ else
+ return wxFont();
+}
+
+void wxRichTextFontTable::Clear()
+{
+ wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
+ if (data)
+ data->m_hashMap.clear();
+}
+
+void wxRichTextFontTable::SetFontScale(double fontScale)
+{
+ if (fontScale != m_fontScale)
+ Clear();
+ m_fontScale = fontScale;
+}
+
+// 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_boxStyleName = wxEmptyString;
+
+ m_margins.Reset();
+ m_padding.Reset();
+ m_position.Reset();
+
+ m_size.Reset();
+ m_minSize.Reset();
+ m_maxSize.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_minSize == attr.m_minSize &&
+ m_maxSize == attr.m_maxSize &&
+
+ m_border == attr.m_border &&
+ m_outline == attr.m_outline &&
+
+ m_boxStyleName == attr.m_boxStyleName
+ );
+}
+
+// Partial equality test
+bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr& attr, bool weakTest) const
+{
+ if (!weakTest &&
+ ((!HasFloatMode() && attr.HasFloatMode()) ||
+ (!HasClearMode() && attr.HasClearMode()) ||
+ (!HasCollapseBorders() && attr.HasCollapseBorders()) ||
+ (!HasVerticalAlignment() && attr.HasVerticalAlignment()) ||
+ (!HasBoxStyleName() && attr.HasBoxStyleName())))
+ {
+ return false;
+ }
+ 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;
+
+ if (attr.HasBoxStyleName() && HasBoxStyleName() && (attr.GetBoxStyleName() != GetBoxStyleName()))
+ return false;
+
+ // Position
+
+ if (!m_position.EqPartial(attr.m_position, weakTest))
+ return false;
+
+ // Size
+
+ if (!m_size.EqPartial(attr.m_size, weakTest))
+ return false;
+ if (!m_minSize.EqPartial(attr.m_minSize, weakTest))
+ return false;
+ if (!m_maxSize.EqPartial(attr.m_maxSize, weakTest))
+ return false;
+
+ // Margins
+
+ if (!m_margins.EqPartial(attr.m_margins, weakTest))
+ return false;
+
+ // Padding
+
+ if (!m_padding.EqPartial(attr.m_padding, weakTest))
+ return false;
+
+ // Border
+
+ if (!GetBorder().EqPartial(attr.GetBorder(), weakTest))
+ return false;
+
+ // Outline
+
+ if (!GetOutline().EqPartial(attr.GetOutline(), weakTest))
+ 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());
+ }
+
+ if (attr.HasBoxStyleName())
+ {
+ if (!(compareWith && compareWith->HasBoxStyleName() && compareWith->GetBoxStyleName() == attr.GetBoxStyleName()))
+ SetBoxStyleName(attr.GetBoxStyleName());
+ }
+
+ 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_minSize.Apply(attr.m_minSize, compareWith ? (& attr.m_minSize) : (const wxTextAttrSize*) NULL);
+ m_maxSize.Apply(attr.m_maxSize, compareWith ? (& attr.m_maxSize) : (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);
+
+ if (attr.HasBoxStyleName())
+ {
+ SetBoxStyleName(wxEmptyString);
+ RemoveFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
+ }
+
+ 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_minSize.RemoveStyle(attr.m_minSize);
+ m_maxSize.RemoveStyle(attr.m_maxSize);
+
+ 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);
+
+ if (attr.HasVerticalAlignment())
+ {
+ if (!clashingAttr.HasVerticalAlignment() && !absentAttr.HasVerticalAlignment())
+ {
+ if (HasVerticalAlignment())
+ {
+ if (GetVerticalAlignment() != attr.GetVerticalAlignment())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
+ RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
+ }
+ }
+ else
+ SetVerticalAlignment(attr.GetVerticalAlignment());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
+
+ if (attr.HasBoxStyleName())
+ {
+ if (!clashingAttr.HasBoxStyleName() && !absentAttr.HasBoxStyleName())
+ {
+ if (HasBoxStyleName())
+ {
+ if (GetBoxStyleName() != attr.GetBoxStyleName())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
+ RemoveFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
+ }
+ }
+ else
+ SetBoxStyleName(attr.GetBoxStyleName());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
+
+ m_margins.CollectCommonAttributes(attr.m_margins, clashingAttr.m_margins, absentAttr.m_margins);
+ m_padding.CollectCommonAttributes(attr.m_padding, clashingAttr.m_padding, absentAttr.m_padding);
+ m_position.CollectCommonAttributes(attr.m_position, clashingAttr.m_position, absentAttr.m_position);
+
+ m_size.CollectCommonAttributes(attr.m_size, clashingAttr.m_size, absentAttr.m_size);
+ m_minSize.CollectCommonAttributes(attr.m_minSize, clashingAttr.m_minSize, absentAttr.m_minSize);
+ m_maxSize.CollectCommonAttributes(attr.m_maxSize, clashingAttr.m_maxSize, absentAttr.m_maxSize);
+
+ m_border.CollectCommonAttributes(attr.m_border, clashingAttr.m_border, absentAttr.m_border);
+ m_outline.CollectCommonAttributes(attr.m_outline, clashingAttr.m_outline, absentAttr.m_outline);
+}
+
+bool wxTextBoxAttr::IsDefault() const
+{
+ return GetFlags() == 0 && !m_border.IsValid() && !m_outline.IsValid() &&
+ !m_size.IsValid() && !m_minSize.IsValid() && !m_maxSize.IsValid() &&
+ !m_position.IsValid() && !m_padding.IsValid() && !m_margins.IsValid();
+}
+
+// wxRichTextAttr
+
+void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
+{
+ wxTextAttr::Copy(attr);
+
+ m_textBoxAttr = attr.m_textBoxAttr;
+}
+
+bool wxRichTextAttr::operator==(const wxRichTextAttr& attr) const
+{
+ if (!(wxTextAttr::operator==(attr)))
+ return false;
+
+ return (m_textBoxAttr == attr.m_textBoxAttr);
+}
+
+// Partial equality test
+bool wxRichTextAttr::EqPartial(const wxRichTextAttr& attr, bool weakTest) const
+{
+ if (!(wxTextAttr::EqPartial(attr, weakTest)))
+ return false;
+
+ return m_textBoxAttr.EqPartial(attr.m_textBoxAttr, weakTest);
+}
+
+// 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 wxRichTextAttr::Apply(const wxRichTextAttr& style, const wxRichTextAttr* compareWith)
+{
+ wxTextAttr::Apply(style, compareWith);
+
+ return m_textBoxAttr.Apply(style.m_textBoxAttr, compareWith ? (& compareWith->m_textBoxAttr) : (const wxTextBoxAttr*) NULL);
+}
+
+// Remove specified attributes from this object
+bool wxRichTextAttr::RemoveStyle(const wxRichTextAttr& attr)
+{
+ wxTextAttr::RemoveStyle(*this, attr);
+
+ return m_textBoxAttr.RemoveStyle(attr.m_textBoxAttr);
+}
+
+// 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 wxRichTextAttr::CollectCommonAttributes(const wxRichTextAttr& attr, wxRichTextAttr& clashingAttr, wxRichTextAttr& absentAttr)
+{
+ wxTextAttrCollectCommonAttributes(*this, attr, clashingAttr, absentAttr);
+
+ m_textBoxAttr.CollectCommonAttributes(attr.m_textBoxAttr, clashingAttr.m_textBoxAttr, absentAttr.m_textBoxAttr);
+}
+
+// Partial equality test
+bool wxTextAttrBorder::EqPartial(const wxTextAttrBorder& border, bool weakTest) const
+{
+ if (!weakTest &&
+ ((!HasStyle() && border.HasStyle()) ||
+ (!HasColour() && border.HasColour()) ||
+ (!HasWidth() && border.HasWidth())))
+ {
+ return false;
+ }
+
+ if (border.HasStyle() && HasStyle() && (border.GetStyle() != GetStyle()))
+ return false;
+
+ if (border.HasColour() && HasColour() && (border.GetColourLong() != GetColourLong()))
+ return false;
+
+ if (border.HasWidth() && HasWidth() && !(border.GetWidth() == GetWidth()))
+ return false;
+
+ return true;
+}
+
+// Apply border to 'this', but not if the same as compareWith
+bool wxTextAttrBorder::Apply(const wxTextAttrBorder& border, const wxTextAttrBorder* compareWith)
+{
+ if (border.HasStyle())
+ {
+ if (!(compareWith && (border.GetStyle() == compareWith->GetStyle())))
+ SetStyle(border.GetStyle());
+ }
+ if (border.HasColour())
+ {
+ if (!(compareWith && (border.GetColourLong() == compareWith->GetColourLong())))
+ SetColour(border.GetColourLong());
+ }
+ if (border.HasWidth())
+ {
+ if (!(compareWith && (border.GetWidth() == compareWith->GetWidth())))
+ SetWidth(border.GetWidth());
+ }
+
+ return true;
+}
+
+// Remove specified attributes from this object
+bool wxTextAttrBorder::RemoveStyle(const wxTextAttrBorder& attr)
+{
+ if (attr.HasStyle() && HasStyle())
+ SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_STYLE);
+ if (attr.HasColour() && HasColour())
+ SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_COLOUR);
+ if (attr.HasWidth() && HasWidth())
+ m_borderWidth.Reset();
+
+ 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 wxTextAttrBorder::CollectCommonAttributes(const wxTextAttrBorder& attr, wxTextAttrBorder& clashingAttr, wxTextAttrBorder& absentAttr)
+{
+ if (attr.HasStyle())
+ {
+ if (!clashingAttr.HasStyle() && !absentAttr.HasStyle())
+ {
+ if (HasStyle())
+ {
+ if (GetStyle() != attr.GetStyle())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
+ RemoveFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
+ }
+ }
+ else
+ SetStyle(attr.GetStyle());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
+
+ if (attr.HasColour())
+ {
+ if (!clashingAttr.HasColour() && !absentAttr.HasColour())
+ {
+ if (HasColour())
+ {
+ if (GetColour() != attr.GetColour())
+ {
+ clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
+ RemoveFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
+ }
+ }
+ else
+ SetColour(attr.GetColourLong());
+ }
+ }
+ else
+ absentAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
+
+ m_borderWidth.CollectCommonAttributes(attr.m_borderWidth, clashingAttr.m_borderWidth, absentAttr.m_borderWidth);
+}
+
+// Partial equality test
+bool wxTextAttrBorders::EqPartial(const wxTextAttrBorders& borders, bool weakTest) const
+{
+ return m_left.EqPartial(borders.m_left, weakTest) && m_right.EqPartial(borders.m_right, weakTest) &&
+ m_top.EqPartial(borders.m_top, weakTest) && m_bottom.EqPartial(borders.m_bottom, weakTest);
+}
+
+// Apply border to 'this', but not if the same as compareWith
+bool wxTextAttrBorders::Apply(const wxTextAttrBorders& borders, const wxTextAttrBorders* compareWith)
+{
+ m_left.Apply(borders.m_left, compareWith ? (& compareWith->m_left) : (const wxTextAttrBorder*) NULL);
+ m_right.Apply(borders.m_right, compareWith ? (& compareWith->m_right) : (const wxTextAttrBorder*) NULL);
+ m_top.Apply(borders.m_top, compareWith ? (& compareWith->m_top) : (const wxTextAttrBorder*) NULL);
+ m_bottom.Apply(borders.m_bottom, compareWith ? (& compareWith->m_bottom) : (const wxTextAttrBorder*) NULL);
+ return true;
+}
+
+// Remove specified attributes from this object
+bool wxTextAttrBorders::RemoveStyle(const wxTextAttrBorders& attr)
+{
+ m_left.RemoveStyle(attr.m_left);
+ m_right.RemoveStyle(attr.m_right);
+ m_top.RemoveStyle(attr.m_top);
+ m_bottom.RemoveStyle(attr.m_bottom);
+ 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 wxTextAttrBorders::CollectCommonAttributes(const wxTextAttrBorders& attr, wxTextAttrBorders& clashingAttr, wxTextAttrBorders& absentAttr)
+{
+ m_left.CollectCommonAttributes(attr.m_left, clashingAttr.m_left, absentAttr.m_left);
+ m_right.CollectCommonAttributes(attr.m_right, clashingAttr.m_right, absentAttr.m_right);
+ m_top.CollectCommonAttributes(attr.m_top, clashingAttr.m_top, absentAttr.m_top);
+ m_bottom.CollectCommonAttributes(attr.m_bottom, clashingAttr.m_bottom, absentAttr.m_bottom);
+}
+
+// Set style of all borders
+void wxTextAttrBorders::SetStyle(int style)
+{
+ m_left.SetStyle(style);
+ m_right.SetStyle(style);
+ m_top.SetStyle(style);
+ m_bottom.SetStyle(style);
+}
+
+// Set colour of all borders
+void wxTextAttrBorders::SetColour(unsigned long colour)
+{
+ m_left.SetColour(colour);
+ m_right.SetColour(colour);
+ m_top.SetColour(colour);
+ m_bottom.SetColour(colour);
+}
+
+void wxTextAttrBorders::SetColour(const wxColour& colour)
+{
+ m_left.SetColour(colour);
+ m_right.SetColour(colour);
+ m_top.SetColour(colour);
+ m_bottom.SetColour(colour);
+}
+
+// Set width of all borders
+void wxTextAttrBorders::SetWidth(const wxTextAttrDimension& width)
+{
+ m_left.SetWidth(width);
+ m_right.SetWidth(width);
+ m_top.SetWidth(width);
+ m_bottom.SetWidth(width);
+}
+
+// Partial equality test
+bool wxTextAttrDimension::EqPartial(const wxTextAttrDimension& dim, bool weakTest) const
+{
+ if (!weakTest && !IsValid() && dim.IsValid())
+ return false;
+
+ if (dim.IsValid() && IsValid() && !((*this) == dim))
+ return false;
+ else
+ return true;
+}
+
+bool wxTextAttrDimension::Apply(const wxTextAttrDimension& dim, const wxTextAttrDimension* compareWith)
+{
+ if (dim.IsValid())
+ {
+ if (!(compareWith && dim == (*compareWith)))
+ (*this) = dim;
+ }
+
+ 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 wxTextAttrDimension::CollectCommonAttributes(const wxTextAttrDimension& attr, wxTextAttrDimension& clashingAttr, wxTextAttrDimension& absentAttr)
+{
+ if (attr.IsValid())
+ {
+ if (!clashingAttr.IsValid() && !absentAttr.IsValid())
+ {
+ if (IsValid())
+ {
+ if (!((*this) == attr))
+ {
+ clashingAttr.SetValid(true);
+ SetValid(false);
+ }
+ }
+ else
+ (*this) = attr;
+ }
+ }
+ else
+ absentAttr.SetValid(true);
+}
+
+wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(wxDC& dc, double scale, const wxSize& parentSize)
+{
+ m_ppi = dc.GetPPI().x; m_scale = scale; m_parentSize = parentSize;
+}
+
+wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(int ppi, double scale, const wxSize& parentSize)
+{
+ m_ppi = ppi; m_scale = scale; m_parentSize = parentSize;
+}
+
+int wxTextAttrDimensionConverter::ConvertTenthsMMToPixels(int units) const
+{
+ return wxRichTextObject::ConvertTenthsMMToPixels(m_ppi, units, m_scale);
+}
+
+int wxTextAttrDimensionConverter::ConvertPixelsToTenthsMM(int pixels) const
+{
+ return wxRichTextObject::ConvertPixelsToTenthsMM(m_ppi, pixels, m_scale);
+}
+
+int wxTextAttrDimensionConverter::GetPixels(const wxTextAttrDimension& dim, int direction) const
+{
+ if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ return ConvertTenthsMMToPixels(dim.GetValue());
+ else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ return dim.GetValue();
+ else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
+ {
+ wxASSERT(m_parentSize != wxDefaultSize);
+ if (direction == wxHORIZONTAL)
+ return (int) (double(m_parentSize.x) * double(dim.GetValue()) / 100.0);
+ else
+ return (int) (double(m_parentSize.y) * double(dim.GetValue()) / 100.0);
+ }
+ else
+ {
+ wxASSERT(false);
+ return 0;
+ }
+}
+
+int wxTextAttrDimensionConverter::GetTenthsMM(const wxTextAttrDimension& dim) const
+{
+ if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
+ return dim.GetValue();
+ else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
+ return ConvertPixelsToTenthsMM(dim.GetValue());
+ else
+ {
+ wxASSERT(false);
+ return 0;
+ }
+}
+
+// Partial equality test
+bool wxTextAttrDimensions::EqPartial(const wxTextAttrDimensions& dims, bool weakTest) const
+{
+ if (!m_left.EqPartial(dims.m_left, weakTest))
+ return false;
+
+ if (!m_right.EqPartial(dims.m_right, weakTest))
+ return false;
+
+ if (!m_top.EqPartial(dims.m_top, weakTest))
+ return false;
+
+ if (!m_bottom.EqPartial(dims.m_bottom, weakTest))
+ return false;
+
+ return true;
+}
+
+// Apply border to 'this', but not if the same as compareWith
+bool wxTextAttrDimensions::Apply(const wxTextAttrDimensions& dims, const wxTextAttrDimensions* compareWith)
+{
+ m_left.Apply(dims.m_left, compareWith ? (& compareWith->m_left) : (const wxTextAttrDimension*) NULL);
+ m_right.Apply(dims.m_right, compareWith ? (& compareWith->m_right): (const wxTextAttrDimension*) NULL);
+ m_top.Apply(dims.m_top, compareWith ? (& compareWith->m_top): (const wxTextAttrDimension*) NULL);
+ m_bottom.Apply(dims.m_bottom, compareWith ? (& compareWith->m_bottom): (const wxTextAttrDimension*) NULL);
+
+ return true;
+}
+
+// Remove specified attributes from this object
+bool wxTextAttrDimensions::RemoveStyle(const wxTextAttrDimensions& attr)
+{
+ if (attr.m_left.IsValid())
+ m_left.Reset();
+ if (attr.m_right.IsValid())
+ m_right.Reset();
+ if (attr.m_top.IsValid())
+ m_top.Reset();
+ if (attr.m_bottom.IsValid())
+ m_bottom.Reset();
+
+ 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 wxTextAttrDimensions::CollectCommonAttributes(const wxTextAttrDimensions& attr, wxTextAttrDimensions& clashingAttr, wxTextAttrDimensions& absentAttr)
+{
+ m_left.CollectCommonAttributes(attr.m_left, clashingAttr.m_left, absentAttr.m_left);
+ m_right.CollectCommonAttributes(attr.m_right, clashingAttr.m_right, absentAttr.m_right);
+ m_top.CollectCommonAttributes(attr.m_top, clashingAttr.m_top, absentAttr.m_top);
+ m_bottom.CollectCommonAttributes(attr.m_bottom, clashingAttr.m_bottom, absentAttr.m_bottom);
+}
+
+// Partial equality test
+bool wxTextAttrSize::EqPartial(const wxTextAttrSize& size, bool weakTest) const
+{
+ if (!m_width.EqPartial(size.m_width, weakTest))
+ return false;
+
+ if (!m_height.EqPartial(size.m_height, weakTest))
+ return false;
+
+ return true;
+}
+
+// Apply border to 'this', but not if the same as compareWith
+bool wxTextAttrSize::Apply(const wxTextAttrSize& size, const wxTextAttrSize* compareWith)
+{
+ m_width.Apply(size.m_width, compareWith ? (& compareWith->m_width) : (const wxTextAttrDimension*) NULL);
+ m_height.Apply(size.m_height, compareWith ? (& compareWith->m_height): (const wxTextAttrDimension*) NULL);
+
+ return true;
+}
+
+// Remove specified attributes from this object
+bool wxTextAttrSize::RemoveStyle(const wxTextAttrSize& attr)
+{
+ if (attr.m_width.IsValid())
+ m_width.Reset();
+ if (attr.m_height.IsValid())
+ m_height.Reset();
+
+ 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 wxTextAttrSize::CollectCommonAttributes(const wxTextAttrSize& attr, wxTextAttrSize& clashingAttr, wxTextAttrSize& absentAttr)
+{
+ m_width.CollectCommonAttributes(attr.m_width, clashingAttr.m_width, absentAttr.m_width);
+ m_height.CollectCommonAttributes(attr.m_height, clashingAttr.m_height, absentAttr.m_height);
+}
+
+// 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 wxTextAttrCollectCommonAttributes(wxTextAttr& currentStyle, const wxTextAttr& attr, wxTextAttr& clashingAttr, wxTextAttr& absentAttr)
+{
+ absentAttr.SetFlags(absentAttr.GetFlags() | (~attr.GetFlags() & wxTEXT_ATTR_ALL));
+ absentAttr.SetTextEffectFlags(absentAttr.GetTextEffectFlags() | (~attr.GetTextEffectFlags() & 0xFFFF));
+
+ long forbiddenFlags = clashingAttr.GetFlags()|absentAttr.GetFlags();
+
+ // If different font size units are being used, this is a clash.
+ if (((attr.GetFlags() & wxTEXT_ATTR_FONT_SIZE) | (currentStyle.GetFlags() & wxTEXT_ATTR_FONT_SIZE)) == wxTEXT_ATTR_FONT_SIZE)
+ {
+ currentStyle.SetFontSize(0);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_SIZE);
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_SIZE);
+ }
+ else
+ {
+ if (attr.HasFontPointSize() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_POINT_SIZE))
+ {
+ if (currentStyle.HasFontPointSize())
+ {
+ if (currentStyle.GetFontSize() != attr.GetFontSize())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
+ }
+ }
+ else
+ currentStyle.SetFontSize(attr.GetFontSize());
+ }
+ else if (!attr.HasFontPointSize() && currentStyle.HasFontPointSize())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
+ }
+
+ if (attr.HasFontPixelSize() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_PIXEL_SIZE))
+ {
+ if (currentStyle.HasFontPixelSize())
+ {
+ if (currentStyle.GetFontSize() != attr.GetFontSize())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
+ }
+ }
+ else
+ currentStyle.SetFontPixelSize(attr.GetFontSize());
+ }
+ else if (!attr.HasFontPixelSize() && currentStyle.HasFontPixelSize())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
+ }
+ }
+
+ if (attr.HasFontItalic() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_ITALIC))
+ {
+ if (currentStyle.HasFontItalic())
+ {
+ if (currentStyle.GetFontStyle() != attr.GetFontStyle())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_ITALIC);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC);
+ }
+ }
+ else
+ currentStyle.SetFontStyle(attr.GetFontStyle());
+ }
+ else if (!attr.HasFontItalic() && currentStyle.HasFontItalic())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_ITALIC);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC);
+ }
+
+ if (attr.HasFontFamily() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_FAMILY))
+ {
+ if (currentStyle.HasFontFamily())
+ {
+ if (currentStyle.GetFontFamily() != attr.GetFontFamily())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FAMILY);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY);
+ }
+ }
+ else
+ currentStyle.SetFontFamily(attr.GetFontFamily());
+ }
+ else if (!attr.HasFontFamily() && currentStyle.HasFontFamily())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FAMILY);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY);
+ }
+
+ if (attr.HasFontWeight() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_WEIGHT))
+ {
+ if (currentStyle.HasFontWeight())
+ {
+ if (currentStyle.GetFontWeight() != attr.GetFontWeight())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_WEIGHT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT);
+ }
+ }
+ else
+ currentStyle.SetFontWeight(attr.GetFontWeight());
+ }
+ else if (!attr.HasFontWeight() && currentStyle.HasFontWeight())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_WEIGHT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT);
+ }
+
+ if (attr.HasFontFaceName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_FACE))
+ {
+ if (currentStyle.HasFontFaceName())
+ {
+ wxString faceName1(currentStyle.GetFontFaceName());
+ wxString faceName2(attr.GetFontFaceName());
+
+ if (faceName1 != faceName2)
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FACE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FACE);
+ }
+ }
+ else
+ currentStyle.SetFontFaceName(attr.GetFontFaceName());
+ }
+ else if (!attr.HasFontFaceName() && currentStyle.HasFontFaceName())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FACE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FACE);
+ }
+
+ if (attr.HasFontUnderlined() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_UNDERLINE))
+ {
+ if (currentStyle.HasFontUnderlined())
+ {
+ if (currentStyle.GetFontUnderlined() != attr.GetFontUnderlined())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE);
+ }
+ }
+ else
+ currentStyle.SetFontUnderlined(attr.GetFontUnderlined());
+ }
+ else if (!attr.HasFontUnderlined() && currentStyle.HasFontUnderlined())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE);
+ }
+
+ if (attr.HasFontStrikethrough() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_STRIKETHROUGH))
+ {
+ if (currentStyle.HasFontStrikethrough())
+ {
+ if (currentStyle.GetFontStrikethrough() != attr.GetFontStrikethrough())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
+ }
+ }
+ else
+ currentStyle.SetFontStrikethrough(attr.GetFontStrikethrough());
+ }
+ else if (!attr.HasFontStrikethrough() && currentStyle.HasFontStrikethrough())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
+ }
+
+ if (attr.HasTextColour() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_TEXT_COLOUR))
+ {
+ if (currentStyle.HasTextColour())
+ {
+ if (currentStyle.GetTextColour() != attr.GetTextColour())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_TEXT_COLOUR);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR);
+ }
+ }
+ else
+ currentStyle.SetTextColour(attr.GetTextColour());
+ }
+ else if (!attr.HasTextColour() && currentStyle.HasTextColour())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_TEXT_COLOUR);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR);
+ }
+
+ if (attr.HasBackgroundColour() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BACKGROUND_COLOUR))
+ {
+ if (currentStyle.HasBackgroundColour())
+ {
+ if (currentStyle.GetBackgroundColour() != attr.GetBackgroundColour())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
+ }
+ }
+ else
+ currentStyle.SetBackgroundColour(attr.GetBackgroundColour());
+ }
+ else if (!attr.HasBackgroundColour() && currentStyle.HasBackgroundColour())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
+ }
+
+ if (attr.HasAlignment() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_ALIGNMENT))
+ {
+ if (currentStyle.HasAlignment())
+ {
+ if (currentStyle.GetAlignment() != attr.GetAlignment())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_ALIGNMENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_ALIGNMENT);
+ }
+ }
+ else
+ currentStyle.SetAlignment(attr.GetAlignment());
+ }
+ else if (!attr.HasAlignment() && currentStyle.HasAlignment())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_ALIGNMENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_ALIGNMENT);
+ }
+
+ if (attr.HasTabs() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_TABS))
+ {
+ if (currentStyle.HasTabs())
+ {
+ if (!wxRichTextTabsEq(currentStyle.GetTabs(), attr.GetTabs()))
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_TABS);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_TABS);
+ }
+ }
+ else
+ currentStyle.SetTabs(attr.GetTabs());
+ }
+ else if (!attr.HasTabs() && currentStyle.HasTabs())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_TABS);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_TABS);
+ }
+
+ if (attr.HasLeftIndent() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LEFT_INDENT))
+ {
+ if (currentStyle.HasLeftIndent())
+ {
+ if (currentStyle.GetLeftIndent() != attr.GetLeftIndent() || currentStyle.GetLeftSubIndent() != attr.GetLeftSubIndent())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_LEFT_INDENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT);
+ }
+ }
+ else
+ currentStyle.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
+ }
+ else if (!attr.HasLeftIndent() && currentStyle.HasLeftIndent())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_LEFT_INDENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT);
+ }
+
+ if (attr.HasRightIndent() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_RIGHT_INDENT))
+ {
+ if (currentStyle.HasRightIndent())
+ {
+ if (currentStyle.GetRightIndent() != attr.GetRightIndent())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_RIGHT_INDENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT);
+ }
+ }
+ else
+ currentStyle.SetRightIndent(attr.GetRightIndent());
+ }
+ else if (!attr.HasRightIndent() && currentStyle.HasRightIndent())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_RIGHT_INDENT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT);
+ }
+
+ if (attr.HasParagraphSpacingAfter() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARA_SPACING_AFTER))
+ {
+ if (currentStyle.HasParagraphSpacingAfter())
+ {
+ if (currentStyle.GetParagraphSpacingAfter() != attr.GetParagraphSpacingAfter())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
+ }
+ }
+ else
+ currentStyle.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
+ }
+ else if (!attr.HasParagraphSpacingAfter() && currentStyle.HasParagraphSpacingAfter())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
+ }
+
+ if (attr.HasParagraphSpacingBefore() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARA_SPACING_BEFORE))
+ {
+ if (currentStyle.HasParagraphSpacingBefore())
+ {
+ if (currentStyle.GetParagraphSpacingBefore() != attr.GetParagraphSpacingBefore())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
+ }
+ }
+ else
+ currentStyle.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
+ }
+ else if (!attr.HasParagraphSpacingBefore() && currentStyle.HasParagraphSpacingBefore())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
+ }
+
+ if (attr.HasLineSpacing() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LINE_SPACING))
+ {
+ if (currentStyle.HasLineSpacing())
+ {
+ if (currentStyle.GetLineSpacing() != attr.GetLineSpacing())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_LINE_SPACING);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LINE_SPACING);
+ }
+ }
+ else
+ currentStyle.SetLineSpacing(attr.GetLineSpacing());
+ }
+ else if (!attr.HasLineSpacing() && currentStyle.HasLineSpacing())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_LINE_SPACING);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LINE_SPACING);
+ }
+
+ if (attr.HasCharacterStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_CHARACTER_STYLE_NAME))
+ {
+ if (currentStyle.HasCharacterStyleName())
+ {
+ if (currentStyle.GetCharacterStyleName() != attr.GetCharacterStyleName())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
+ }
+ }
+ else
+ currentStyle.SetCharacterStyleName(attr.GetCharacterStyleName());
+ }
+ else if (!attr.HasCharacterStyleName() && currentStyle.HasCharacterStyleName())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
+ }
+
+ if (attr.HasParagraphStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME))
+ {
+ if (currentStyle.HasParagraphStyleName())
+ {
+ if (currentStyle.GetParagraphStyleName() != attr.GetParagraphStyleName())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
+ }
+ }
+ else
+ currentStyle.SetParagraphStyleName(attr.GetParagraphStyleName());
+ }
+ else if (!attr.HasParagraphStyleName() && currentStyle.HasParagraphStyleName())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
+ }
+
+ if (attr.HasListStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LIST_STYLE_NAME))
+ {
+ if (currentStyle.HasListStyleName())
+ {
+ if (currentStyle.GetListStyleName() != attr.GetListStyleName())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
+ }
+ }
+ else
+ currentStyle.SetListStyleName(attr.GetListStyleName());
+ }
+ else if (!attr.HasListStyleName() && currentStyle.HasListStyleName())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
+ }
+
+ if (attr.HasBulletStyle() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_STYLE))
+ {
+ if (currentStyle.HasBulletStyle())
+ {
+ if (currentStyle.GetBulletStyle() != attr.GetBulletStyle())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_STYLE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE);
+ }
+ }
+ else
+ currentStyle.SetBulletStyle(attr.GetBulletStyle());
+ }
+ else if (!attr.HasBulletStyle() && currentStyle.HasBulletStyle())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_STYLE);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE);
+ }
+
+ if (attr.HasBulletNumber() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_NUMBER))
+ {
+ if (currentStyle.HasBulletNumber())
+ {
+ if (currentStyle.GetBulletNumber() != attr.GetBulletNumber())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NUMBER);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER);
+ }
+ }
+ else
+ currentStyle.SetBulletNumber(attr.GetBulletNumber());
+ }
+ else if (!attr.HasBulletNumber() && currentStyle.HasBulletNumber())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NUMBER);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER);
+ }
+
+ if (attr.HasBulletText() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_TEXT))
+ {
+ if (currentStyle.HasBulletText())
+ {
+ if (currentStyle.GetBulletText() != attr.GetBulletText())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_TEXT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT);
+ }
+ }
+ else
+ {
+ currentStyle.SetBulletText(attr.GetBulletText());
+ currentStyle.SetBulletFont(attr.GetBulletFont());
+ }
+ }
+ else if (!attr.HasBulletText() && currentStyle.HasBulletText())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_TEXT);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT);
+ }
+
+ if (attr.HasBulletName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_NAME))
+ {
+ if (currentStyle.HasBulletName())
+ {
+ if (currentStyle.GetBulletName() != attr.GetBulletName())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NAME);
+ }
+ }
+ else
+ {
+ currentStyle.SetBulletName(attr.GetBulletName());
+ }
+ }
+ else if (!attr.HasBulletName() && currentStyle.HasBulletName())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NAME);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NAME);
+ }
+
+ if (attr.HasURL() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_URL))
+ {
+ if (currentStyle.HasURL())
+ {
+ if (currentStyle.GetURL() != attr.GetURL())
+ {
+ // Clash of attr - mark as such
+ clashingAttr.AddFlag(wxTEXT_ATTR_URL);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_URL);
+ }
+ }
+ else
+ {
+ currentStyle.SetURL(attr.GetURL());
+ }
+ }
+ else if (!attr.HasURL() && currentStyle.HasURL())
+ {
+ clashingAttr.AddFlag(wxTEXT_ATTR_URL);
+ currentStyle.RemoveFlag(wxTEXT_ATTR_URL);
+ }
+
+ if (attr.HasTextEffects() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_EFFECTS))
+ {
+ if (currentStyle.HasTextEffects())
+ {
+ // We need to find the bits in the new attr that are different:
+ // just look at those bits that are specified by the new attr.
+
+ // We need to remove the bits and flags that are not common between current attr
+ // and new attr. In so doing we need to take account of the styles absent from one or more of the
+ // previous styles.
+
+ int currentRelevantTextEffects = currentStyle.GetTextEffects() & attr.GetTextEffectFlags();
+ int newRelevantTextEffects = attr.GetTextEffects() & attr.GetTextEffectFlags();
+
+ if (currentRelevantTextEffects != newRelevantTextEffects)
+ {
+ // Find the text effects that were different, using XOR
+ int differentEffects = currentRelevantTextEffects ^ newRelevantTextEffects;
+
+ // Clash of attr - mark as such
+ clashingAttr.SetTextEffectFlags(clashingAttr.GetTextEffectFlags() | differentEffects);
+ currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~differentEffects);
+ }
+ }
+ else
+ {
+ currentStyle.SetTextEffects(attr.GetTextEffects());
+ currentStyle.SetTextEffectFlags(attr.GetTextEffectFlags());
+ }
+
+ // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
+ // that we've looked at so far
+ currentStyle.SetTextEffects(currentStyle.GetTextEffects() & ~absentAttr.GetTextEffectFlags());
+ currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~absentAttr.GetTextEffectFlags());