X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/49b18c6cd9dbb87fa53f4000d63e0667bce50227..c37b0f0907b07878551a00165b0ad323bd2ccdaf:/src/richtext/richtextbuffer.cpp diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index 217e41e663..cc52bc9a10 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -4,7 +4,6 @@ // Author: Julian Smart // Modified by: // Created: 2005-09-30 -// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -3318,7 +3317,17 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextRemoveStyle(child->GetAttributes(), style); } else if (resetExistingStyle) + { + // Preserve the URL as it's not really a formatting style but a property of the object + wxString url; + if (child->GetAttributes().HasURL() && !characterAttributes.HasURL()) + url = child->GetAttributes().GetURL(); + child->GetAttributes() = characterAttributes; + + if (!url.IsEmpty()) + child->GetAttributes().SetURL(url); + } else { if (applyMinimal) @@ -3778,7 +3787,7 @@ void wxRichTextParagraphLayoutBox::Reset() wxRichTextBuffer* buffer = GetBuffer(); if (buffer && buffer->GetRichTextCtrl()) { - wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, buffer->GetRichTextCtrl()->GetId()); + wxRichTextEvent event(wxEVT_RICHTEXT_BUFFER_RESET, buffer->GetRichTextCtrl()->GetId()); event.SetEventObject(buffer->GetRichTextCtrl()); event.SetContainer(this); @@ -8307,6 +8316,7 @@ wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* t return wildcard; } +#if wxUSE_FFILE && wxUSE_STREAMS /// Load a file bool wxRichTextBuffer::LoadFile(const wxString& filename, wxRichTextFileType type) { @@ -8335,7 +8345,9 @@ bool wxRichTextBuffer::SaveFile(const wxString& filename, wxRichTextFileType typ else return false; } +#endif // wxUSE_FFILE && wxUSE_STREAMS +#if wxUSE_STREAMS /// Load from a stream bool wxRichTextBuffer::LoadFile(wxInputStream& stream, wxRichTextFileType type) { @@ -8364,6 +8376,7 @@ bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, wxRichTextFileType type) else return false; } +#endif // wxUSE_STREAMS /// Copy the range to the clipboard bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range) @@ -8593,7 +8606,7 @@ bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet) if (GetRichTextCtrl()) winid = GetRichTextCtrl()->GetId(); - wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, winid); + wxRichTextEvent event(wxEVT_RICHTEXT_STYLESHEET_REPLACING, winid); event.SetEventObject(GetRichTextCtrl()); event.SetContainer(GetRichTextCtrl()->GetFocusObject()); event.SetOldStyleSheet(oldSheet); @@ -8613,7 +8626,7 @@ bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet) SetStyleSheet(sheet); - event.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED); + event.SetEventType(wxEVT_RICHTEXT_STYLESHEET_REPLACED); event.SetOldStyleSheet(NULL); event.Allow(); @@ -9291,6 +9304,29 @@ bool wxRichTextCell::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer) return false; } +// The next 2 methods return span values. Note that the default is 1, not 0 +int wxRichTextCell::GetColspan() const +{ + int span = 1; + if (GetProperties().HasProperty(wxT("colspan"))) + { + span = GetProperties().GetPropertyLong(wxT("colspan")); + } + + return span; +} + +int wxRichTextCell::GetRowspan() const +{ + int span = 1; + if (GetProperties().HasProperty(wxT("rowspan"))) + { + span = GetProperties().GetPropertyLong(wxT("rowspan")); + } + + return span; +} + WX_DEFINE_OBJARRAY(wxRichTextObjectPtrArrayArray) IMPLEMENT_DYNAMIC_CLASS(wxRichTextTable, wxRichTextBox) @@ -9310,6 +9346,157 @@ bool wxRichTextTable::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wx WX_DECLARE_OBJARRAY(wxRect, wxRichTextRectArray); WX_DEFINE_OBJARRAY(wxRichTextRectArray); + + // Helper function for Layout() that clears the space needed by a cell with rowspan > 1 +int GetRowspanDisplacement(const wxRichTextTable* table, int row, int col, int paddingX, const wxArrayInt& colWidths) +{ + // If one or more cells above-left of this one has rowspan > 1, the affected cells below it + // will have been hidden and have width 0. As a result they are ignored by the layout algorithm, + // and all cells to their right are effectively shifted left. As a result there's no hole for + // the spanning cell to fill. + // So search back along the current row for hidden cells. However there's also the annoying issue of a + // rowspanning cell that also has colspam. So we can't rely on the rowspanning cell being directly above + // the first hidden one we come to. We also can't rely on a cell being hidden only by one type of span; + // there's nothing to stop a cell being hidden by colspan, and then again hidden from above by rowspan. + // The answer is to look above each hidden cell in turn, which I think covers all bases. + int deltaX = 0; + for (int prevcol = 0; prevcol < col; ++prevcol) + { + if (!table->GetCell(row, prevcol)->IsShown()) + { + // We've found a hidden cell. If it's hidden because of colspan to its left, it's + // already been taken into account; but not if there's a rowspanning cell above + for (int prevrow = row-1; prevrow >= 0; --prevrow) + { + wxRichTextCell* cell = table->GetCell(prevrow, prevcol); + if (cell && cell->IsShown()) + { + int rowSpan = cell->GetRowspan(); + if (rowSpan > 1 && rowSpan > (row-prevrow)) + { + // There is a rowspanning cell above above the hidden one, so we need + // to right-shift the index cell by this column's width. Furthermore, + // if the cell also colspans, we need to shift by all affected columns + for (int colSpan = 0; colSpan < cell->GetColspan(); ++colSpan) + deltaX += (colWidths[prevcol+colSpan] + paddingX); + break; + } + } + } + } + } + return deltaX; +} + + // Helper function for Layout() that expands any cell with rowspan > 1 +void ExpandCellsWithRowspan(const wxRichTextTable* table, int paddingY, int& bottomY, wxDC& dc, wxRichTextDrawingContext& context, const wxRect& availableSpace, int style) +{ + // This is called when the table's cell layout is otherwise complete. + // For any cell with rowspan > 1, expand downwards into the row(s) below. + + // Start by finding the current 'y' of the top of each row, plus the bottom of the available area for cells. + // Deduce this from the top of a visible cell in the row below. (If none are visible, the row will be invisible anyway and can be ignored.) + const int rowCount = table->GetRowCount(); + const int colCount = table->GetColumnCount(); + wxArrayInt rowTops; + rowTops.Add(0, rowCount+1); + int row; + for (row = 0; row < rowCount; ++row) + { + for (int column = 0; column < colCount; ++column) + { + wxRichTextCell* cell = table->GetCell(row, column); + if (cell && cell->IsShown()) + { + rowTops[row] = cell->GetPosition().y; + break; + } + } + } + rowTops[rowCount] = bottomY + paddingY; // The table bottom, which was passed to us + + bool needsRelay = false; + + for (row = 0; row < rowCount-1; ++row) // -1 as the bottom row can't rowspan + { + for (int col = 0; col < colCount; ++col) + { + wxRichTextCell* cell = table->GetCell(row, col); + if (cell && cell->IsShown()) + { + int span = cell->GetRowspan(); + if (span > 1) + { + span = wxMin(span, rowCount-row); // Don't try to span below the table! + if (span < 2) + continue; + + int availableHeight = rowTops[row+span] - rowTops[row] - paddingY; + wxSize newSize = wxSize(cell->GetCachedSize().GetWidth(), availableHeight); + wxRect availableCellSpace = wxRect(cell->GetPosition(), newSize); + cell->Invalidate(wxRICHTEXT_ALL); + cell->Layout(dc, context, availableCellSpace, availableSpace, style); + // Ensure there's room in the span to display its contents, else it'll overwrite lower rows + int overhang = cell->GetCachedSize().GetHeight() - availableHeight; + cell->SetCachedSize(newSize); + + if (overhang > 0) + { + // There are 3 things to get right: + // 1) The easiest is the rows below the span: they need to be downshifted by the overhang, and so does the table bottom itself + // 2) The rows within the span, including the one holding this cell, need to be deepened by their share of the overhang + // e.g. if rowspan == 3, each row should increase in depth by 1/3rd of the overhang. + // 3) The cell with the rowspan shouldn't be touched in 2); its height will be set to the whole span later. + int deltaY = overhang / span; + int spare = overhang % span; + + // Each row in the span needs to by deepened by its share of the overhang (give the first row any spare). + // This is achieved by increasing the value stored in the following row's rowTops + for (int spannedRows = 0; spannedRows < span; ++spannedRows) + { + rowTops[row+spannedRows+1] += ((deltaY * (spannedRows+1)) + (spannedRows == 0 ? spare:0)); + } + + // Any rows below the span need shifting down + for (int rowsBelow = row + span+1; rowsBelow <= rowCount; ++rowsBelow) + { + rowTops[rowsBelow] += overhang; + } + + needsRelay = true; + } + } + } + } + } + + if (!needsRelay) + return; + + // There were overflowing rowspanning cells, so layout yet again to make the increased row depths show + for (row = 0; row < rowCount; ++row) + { + for (int col = 0; col < colCount; ++col) + { + wxRichTextCell* cell = table->GetCell(row, col); + if (cell && cell->IsShown()) + { + wxPoint position(cell->GetPosition().x, rowTops[row]); + + // GetRowspan() will usually return 1, but may be greater + wxSize size(cell->GetCachedSize().GetWidth(), rowTops[row + cell->GetRowspan()] - rowTops[row] - paddingY); + + wxRect availableCellSpace = wxRect(position, size); + cell->Invalidate(wxRICHTEXT_ALL); + cell->Layout(dc, context, availableCellSpace, availableSpace, style); + cell->SetCachedSize(size); + } + } + + bottomY = rowTops[rowCount] - paddingY; + } +} + // Lays the object out. rect is the space available for layout. Often it will // be the specified overall space for this object, if trying to constrain // layout to a particular size, or it could be the total space available in the @@ -9319,7 +9506,7 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const { SetPosition(rect.GetPosition()); - // TODO: the meaty bit. Calculate sizes of all cells and rows. Try to use + // The meaty bit. Calculate sizes of all cells and rows. Try to use // minimum size if within alloted size, then divide up remaining size // between rows/cols. @@ -9333,12 +9520,13 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextAttr attr(GetAttributes()); context.ApplyVirtualAttributes(attr, this); + bool tableHasPercentWidth = (attr.GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE); // If we have no fixed table size, and assuming we're not pushed for // space, then we don't have to try to stretch the table to fit the contents. - bool stretchToFitTableWidth = false; - + bool stretchToFitTableWidth = tableHasPercentWidth; + int tableWidth = rect.width; - if (attr.GetTextBoxAttr().GetWidth().IsValid()) + if (attr.GetTextBoxAttr().GetWidth().IsValid() && !tableHasPercentWidth) { tableWidth = converter.GetPixels(attr.GetTextBoxAttr().GetWidth()); @@ -9435,12 +9623,9 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const { for (i = 0; i < m_colCount; i++) { - wxRichTextBox* cell = GetCell(j, i); - int colSpan = 1, rowSpan = 1; - if (cell->GetProperties().HasProperty(wxT("colspan"))) - colSpan = cell->GetProperties().GetPropertyLong(wxT("colspan")); - if (cell->GetProperties().HasProperty(wxT("rowspan"))) - rowSpan = cell->GetProperties().GetPropertyLong(wxT("rowspan")); + wxRichTextCell* cell = GetCell(j, i); + int colSpan = cell->GetColspan(); + int rowSpan = cell->GetRowspan(); if (colSpan > 1 || rowSpan > 1) { rectArray.Add(wxRect(i, j, colSpan, rowSpan)); @@ -9452,18 +9637,16 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const { for (i = 0; i < m_colCount; i++) { - wxRichTextBox* cell = GetCell(j, i); + wxRichTextCell* cell = GetCell(j, i); if (rectArray.GetCount() == 0) { cell->Show(true); } else { - int colSpan = 1, rowSpan = 1; - if (cell->GetProperties().HasProperty(wxT("colspan"))) - colSpan = cell->GetProperties().GetPropertyLong(wxT("colspan")); - if (cell->GetProperties().HasProperty(wxT("rowspan"))) - rowSpan = cell->GetProperties().GetPropertyLong(wxT("rowspan")); + int colSpan = cell->GetColspan(); + int rowSpan = cell->GetRowspan(); + if (colSpan > 1 || rowSpan > 1) { // Assume all spanning cells are shown @@ -9486,7 +9669,7 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const } } - // TODO: find the first spanned cell in each row that spans the most columns and doesn't + // Find the first spanned cell in each row that spans the most columns and doesn't // overlap with a spanned cell starting at a previous column position. // This means we need to keep an array of rects so we can check. However // it does also mean that some spans simply may not be taken into account @@ -9526,12 +9709,10 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const for (i = 0; i < m_colCount; i++) { - wxRichTextBox* cell = GetCell(j, i); + wxRichTextCell* cell = GetCell(j, i); if (cell->IsShown()) { - int colSpan = 1; - if (cell->GetProperties().HasProperty(wxT("colspan"))) - colSpan = cell->GetProperties().GetPropertyLong(wxT("colspan")); + int colSpan = cell->GetColspan(); // Lay out cell to find min/max widths cell->Invalidate(wxRICHTEXT_ALL); @@ -9651,13 +9832,10 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const for (i = 0; i < m_colCount; i++) { - wxRichTextBox* cell = GetCell(j, i); + wxRichTextCell* cell = GetCell(j, i); if (cell->IsShown()) { - int colSpan = 1; - if (cell->GetProperties().HasProperty(wxT("colspan"))) - colSpan = cell->GetProperties().GetPropertyLong(wxT("colspan")); - + int colSpan = cell->GetColspan(); if (colSpan > 1) { int spans = wxMin(colSpan, m_colCount - i); @@ -9740,7 +9918,6 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const int stretchColCount = 0; for (i = 0; i < m_colCount; i++) { - // TODO: we need to take into account min widths. // Subtract min width from width left, then // add the colShare to the min width if (colWidths[i] > 0) // absolute or proportional width has been specified @@ -9846,19 +10023,16 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const if (colWidths[i] > 0) // absolute or proportional width has been specified { - int colSpan = 1; - if (cell->GetProperties().HasProperty(wxT("colspan"))) - colSpan = cell->GetProperties().GetPropertyLong(wxT("colspan")); - + int colSpan = cell->GetColspan(); wxRect availableCellSpace; - // TODO: take into acount spans + // Take into account spans if (colSpan > 1) { // Calculate the size of this spanning cell from its constituent columns - int xx = x; + int xx = 0; int spans = wxMin(colSpan, m_colCount - i); - for (k = i; k < spans; k++) + for (k = i; k < (i+spans); k++) { if (k != i) xx += paddingX; @@ -9872,6 +10046,10 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const // Store actual width so we can force cell to be the appropriate width on the final loop actualWidths[i] = availableCellSpace.GetWidth(); + // We now need to shift right by the width of any rowspanning cells above-left of us + int deltaX = GetRowspanDisplacement(this, j, i, paddingX, colWidths); + availableCellSpace.SetX(availableCellSpace.GetX() + deltaX); + // Lay out cell cell->Invalidate(wxRICHTEXT_ALL); cell->Layout(dc, context, availableCellSpace, availableSpace, style); @@ -9879,7 +10057,7 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const // TODO: use GetCachedSize().x to compute 'natural' size x += (availableCellSpace.GetWidth() + paddingX); - if (cell->GetCachedSize().y > maxCellHeight) + if ((cell->GetCachedSize().y > maxCellHeight) && (cell->GetRowspan() < 2)) maxCellHeight = cell->GetCachedSize().y; } } @@ -9909,6 +10087,9 @@ bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const if (j < (m_rowCount-1)) y += paddingY; } + + // Finally we need to expand any cell with rowspan > 1. We couldn't earlier; lower rows' heights weren't known + ExpandCellsWithRowspan(this, paddingY, y, dc, context, availableSpace, style); // We need to add back the margins etc. { @@ -10077,6 +10258,8 @@ void wxRichTextTable::ClearTable() { m_cells.Clear(); DeleteChildren(); + m_rowCount = 0; + m_colCount = 0; } bool wxRichTextTable::CreateTable(int rows, int cols) @@ -10229,12 +10412,68 @@ bool wxRichTextTable::SetCellStyle(const wxRichTextSelection& selection, const w return true; } +wxPosition wxRichTextTable::GetFocusedCell() const +{ + wxPosition position(-1, -1); + const wxRichTextObject* focus = GetBuffer()->GetRichTextCtrl()->GetFocusObject(); + + for (int row = 0; row < GetRowCount(); ++row) + { + for (int col = 0; col < GetColumnCount(); ++col) + { + if (GetCell(row, col) == focus) + { + position.SetRow(row); + position.SetCol(col); + return position; + } + } + } + + return position; +} + bool wxRichTextTable::DeleteRows(int startRow, int noRows) { - wxASSERT((startRow + noRows) < m_rowCount); - if ((startRow + noRows) >= m_rowCount) + 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(); + + wxPosition position = GetFocusedCell(); + int focusCol = position.GetCol(); + int focusRow = position.GetRow(); + if (focusRow >= startRow && focusRow < (startRow+noRows)) + { + // Deleting a focused cell causes a segfault later when laying out, due to GetFocusedObject() returning an invalid object + if ((startRow + noRows) < m_rowCount) + { + // There are more rows after the one(s) to be deleted, so set focus in the first of them + rtc->SetFocusObject(GetCell(startRow + noRows, focusCol)); + } + else + { + // Otherwise set focus in the preceding row + rtc->SetFocusObject(GetCell(startRow - 1, focusCol)); + } + } + + 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++) { @@ -10252,25 +10491,68 @@ bool wxRichTextTable::DeleteRows(int startRow, int noRows) 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) + 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(); + + wxPosition position = GetFocusedCell(); + int focusCol = position.GetCol(); + int focusRow = position.GetRow(); + if (focusCol >= startCol && focusCol < (startCol+noCols)) + { + // Deleting a focused cell causes a segfault later when laying out, due to GetFocusedObject() returning an invalid object + if ((startCol + noCols) < m_colCount) + { + // There are more columns after the one(s) to be deleted, so set focus in the first of them + rtc->SetFocusObject(GetCell(focusRow, startCol + noCols)); + } + else + { + // Otherwise set focus in the preceding column + rtc->SetFocusObject(GetCell(focusRow, startCol - 1)); + } + } + + 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 = startCol; j < (startCol+noCols); j++) + for (j = 0; j < noCols; j++) { - wxRichTextObject* cell = colArray[j]; + wxRichTextObject* cell = colArray[startCol]; RemoveChild(cell, true); + colArray.RemoveAt(startCol); } if (deleteRows) @@ -10281,6 +10563,13 @@ bool wxRichTextTable::DeleteColumns(int startCol, int noCols) 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; } @@ -10290,6 +10579,19 @@ bool wxRichTextTable::AddRows(int startRow, int noRows, const wxRichTextAttr& at 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()); + } + int i, j; for (i = 0; i < noRows; i++) { @@ -10312,11 +10614,20 @@ bool wxRichTextTable::AddRows(int startRow, int noRows, const wxRichTextAttr& at cell->GetAttributes() = attr; 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; } @@ -10326,6 +10637,19 @@ bool wxRichTextTable::AddColumns(int startCol, int noCols, const wxRichTextAttr& 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()); + } + int i, j; for (i = 0; i < m_rowCount; i++) { @@ -10336,6 +10660,7 @@ bool wxRichTextTable::AddColumns(int startCol, int noCols, const wxRichTextAttr& cell->GetAttributes() = attr; AppendChild(cell); + cell->AddParagraph(wxEmptyString); if (startCol == m_colCount) colArray.Add(cell); @@ -10346,6 +10671,13 @@ bool wxRichTextTable::AddColumns(int startCol, int noCols, const wxRichTextAttr& 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; } @@ -10413,8 +10745,8 @@ IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule) void wxRichTextModuleInit() { wxModule* module = new wxRichTextModule; - module->Init(); wxModule::RegisterModule(module); + wxModule::InitializeModules(); } @@ -10615,7 +10947,7 @@ bool wxRichTextAction::Do() UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */); wxRichTextEvent cmdEvent( - wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, + wxEVT_RICHTEXT_CONTENT_INSERTED, m_ctrl ? m_ctrl->GetId() : -1); cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer); cmdEvent.SetRange(GetRange()); @@ -10648,7 +10980,7 @@ bool wxRichTextAction::Do() UpdateAppearance(caretPos, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */); wxRichTextEvent cmdEvent( - wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, + wxEVT_RICHTEXT_CONTENT_DELETED, m_ctrl ? m_ctrl->GetId() : -1); cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer); cmdEvent.SetRange(GetRange()); @@ -10677,7 +11009,7 @@ bool wxRichTextAction::Do() UpdateAppearance(GetPosition()); wxRichTextEvent cmdEvent( - m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED : wxEVT_COMMAND_RICHTEXT_PROPERTIES_CHANGED, + 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()); @@ -10709,7 +11041,7 @@ bool wxRichTextAction::Do() UpdateAppearance(GetPosition()); wxRichTextEvent cmdEvent( - wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, + wxEVT_RICHTEXT_STYLE_CHANGED, m_ctrl ? m_ctrl->GetId() : -1); cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer); cmdEvent.SetRange(GetRange()); @@ -10723,10 +11055,21 @@ bool wxRichTextAction::Do() case wxRICHTEXT_CHANGE_OBJECT: { wxRichTextObject* obj = m_objectAddress.GetObject(m_buffer); - // wxRichTextObject* obj = container->GetChildAtPosition(GetRange().GetStart()); - if (obj && m_object) + if (obj && m_object && m_ctrl) { - wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(obj); + // 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(); @@ -10788,7 +11131,7 @@ bool wxRichTextAction::Undo() UpdateAppearance(newCaretPosition, true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */); wxRichTextEvent cmdEvent( - wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, + wxEVT_RICHTEXT_CONTENT_DELETED, m_ctrl ? m_ctrl->GetId() : -1); cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer); cmdEvent.SetRange(GetRange()); @@ -10818,7 +11161,7 @@ bool wxRichTextAction::Undo() UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */); wxRichTextEvent cmdEvent( - wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, + wxEVT_RICHTEXT_CONTENT_INSERTED, m_ctrl ? m_ctrl->GetId() : -1); cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer); cmdEvent.SetRange(GetRange()); @@ -10840,7 +11183,7 @@ bool wxRichTextAction::Undo() UpdateAppearance(GetPosition()); wxRichTextEvent cmdEvent( - m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED : wxEVT_COMMAND_RICHTEXT_PROPERTIES_CHANGED, + 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()); @@ -10915,13 +11258,16 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent // 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(GetPosition()); + 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) { - firstY = para->GetPosition().y; + // 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); @@ -10969,7 +11315,7 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent // Stop, we're now the same as we were foundEnd = true; - lastY = pt.y; + lastY = pt.y + line->GetSize().y; node2 = wxRichTextLineList::compatibility_iterator(); node = wxRichTextObjectList::compatibility_iterator();