]> git.saurik.com Git - wxWidgets.git/commitdiff
Applied #15539: wxRichTextCtrl: demonstrate adding and deleting table rows and column...
authorJulian Smart <julian@anthemion.co.uk>
Sun, 29 Sep 2013 12:14:19 +0000 (12:14 +0000)
committerJulian Smart <julian@anthemion.co.uk>
Sun, 29 Sep 2013 12:14:19 +0000 (12:14 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74869 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/richtext/richtext.cpp

index 2620ee56d9e3f7b051a3ac5c0e962fbd82198779..685504109b96fcf70139fbc928fa0a526de485b2 100644 (file)
@@ -183,6 +183,16 @@ public:
     */
     virtual bool CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const;
 
+    /**
+        Finds a table,  either selected or near the cursor
+    */
+    wxRichTextTable* FindTable() const;
+
+    /**
+        Helper for FindTable()
+    */
+    wxRichTextObject* FindCurrentPosition() const;
+
     long    m_lockId;
     bool    m_locked;
 };
@@ -275,6 +285,13 @@ public:
     void OnDemoteList(wxCommandEvent& event);
     void OnClearList(wxCommandEvent& event);
 
+    void OnTableAddColumn(wxCommandEvent& event);
+    void OnTableAddRow(wxCommandEvent& event);
+    void OnTableDeleteColumn(wxCommandEvent& event);
+    void OnTableDeleteRow(wxCommandEvent& event);
+    void OnTableFocusedUpdateUI(wxUpdateUIEvent& event);
+    void OnTableHasCellsUpdateUI(wxUpdateUIEvent& event);
+
     void OnReload(wxCommandEvent& event);
 
     void OnViewHTML(wxCommandEvent& event);
@@ -359,6 +376,11 @@ enum
     ID_FORMAT_DEMOTE_LIST,
     ID_FORMAT_CLEAR_LIST,
 
+    ID_TABLE_ADD_COLUMN,
+    ID_TABLE_ADD_ROW,
+    ID_TABLE_DELETE_COLUMN,
+    ID_TABLE_DELETE_ROW,
+
     ID_SET_FONT_SCALE,
     ID_SET_DIMENSION_SCALE,
 
@@ -446,6 +468,13 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(ID_FORMAT_DEMOTE_LIST, MyFrame::OnDemoteList)
     EVT_MENU(ID_FORMAT_CLEAR_LIST, MyFrame::OnClearList)
 
+    EVT_MENU(ID_TABLE_ADD_COLUMN, MyFrame::OnTableAddColumn)
+    EVT_MENU(ID_TABLE_ADD_ROW, MyFrame::OnTableAddRow)
+    EVT_MENU(ID_TABLE_DELETE_COLUMN, MyFrame::OnTableDeleteColumn)
+    EVT_MENU(ID_TABLE_DELETE_ROW, MyFrame::OnTableDeleteRow)
+    EVT_UPDATE_UI_RANGE(ID_TABLE_ADD_COLUMN, ID_TABLE_ADD_ROW, MyFrame::OnTableFocusedUpdateUI)
+    EVT_UPDATE_UI_RANGE(ID_TABLE_DELETE_COLUMN, ID_TABLE_DELETE_ROW, MyFrame::OnTableHasCellsUpdateUI)
+
     EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
     EVT_MENU(ID_SWITCH_STYLE_SHEETS, MyFrame::OnSwitchStyleSheets)
     EVT_MENU(ID_MANAGE_STYLES, MyFrame::OnManageStyles)
@@ -802,6 +831,12 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
     listsMenu->Append(ID_FORMAT_DEMOTE_LIST, _("Demote List Items"));
     listsMenu->Append(ID_FORMAT_CLEAR_LIST, _("Clear List Formatting"));
 
+    wxMenu* tableMenu = new wxMenu;
+    tableMenu->Append(ID_TABLE_ADD_COLUMN, _("&Add Column"));
+    tableMenu->Append(ID_TABLE_ADD_ROW, _("Add &Row"));
+    tableMenu->Append(ID_TABLE_DELETE_COLUMN, _("Delete &Column"));
+    tableMenu->Append(ID_TABLE_DELETE_ROW, _("&Delete Row"));
+
     wxMenu* insertMenu = new wxMenu;
     insertMenu->Append(ID_INSERT_SYMBOL, _("&Symbol...\tCtrl+I"));
     insertMenu->Append(ID_INSERT_URL, _("&URL..."));
@@ -813,6 +848,7 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
     menuBar->Append(editMenu, wxT("&Edit"));
     menuBar->Append(formatMenu, wxT("F&ormat"));
     menuBar->Append(listsMenu, wxT("&Lists"));
+    menuBar->Append(tableMenu, wxT("&Tables"));
     menuBar->Append(insertMenu, wxT("&Insert"));
     menuBar->Append(helpMenu, wxT("&Help"));
 
@@ -1163,6 +1199,7 @@ void MyFrame::WriteInitialText()
         r.SetInsertionPointEnd();
     }
 #endif
+
 #if 1
     {
         // Add a table
@@ -1216,7 +1253,7 @@ void MyFrame::WriteInitialText()
         r.SetFocusObject(cell);
         cell->Clear();
         r.WriteText("This cell spans 2 columns and 3 rows");
-                
+
         r.SetFocusObject(NULL); // Set the focus back to the main buffer
         r.SetInsertionPointEnd();
     }
@@ -1929,6 +1966,80 @@ void MyFrame::OnClearList(wxCommandEvent& WXUNUSED(event))
     }
 }
 
+void MyFrame::OnTableAddColumn(wxCommandEvent& WXUNUSED(event))
+{
+    wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
+    if (table)
+    {
+        wxRichTextAttr cellAttr = table->GetCell(0, 0)->GetAttributes();
+        table->AddColumns(table->GetColumnCount(), 1, cellAttr);
+    }
+}
+
+void MyFrame::OnTableAddRow(wxCommandEvent& WXUNUSED(event))
+{
+    wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
+    if (table)
+    {
+        wxRichTextAttr cellAttr = table->GetCell(0, 0)->GetAttributes();
+        table->AddRows(table->GetRowCount(), 1, cellAttr);
+    }
+}
+
+void MyFrame::OnTableDeleteColumn(wxCommandEvent& WXUNUSED(event))
+{
+    wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
+    if (table)
+    {
+        int col = table->GetFocusedCell().GetCol();
+        if (col == -1)
+        {
+            col = table->GetColumnCount() - 1;
+        }
+            
+        table->DeleteColumns(col, 1);
+    }
+}
+
+void MyFrame::OnTableDeleteRow(wxCommandEvent& WXUNUSED(event))
+{
+    wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
+    if (table)
+    {
+        int row = table->GetFocusedCell().GetRow();
+        if (row == -1)
+        {
+            row = table->GetRowCount() - 1;
+        }
+            
+        table->DeleteRows(row, 1);
+    }
+}
+
+void MyFrame::OnTableFocusedUpdateUI(wxUpdateUIEvent& event)
+{
+    event.Enable(m_richTextCtrl->FindTable() != NULL);
+}
+
+void MyFrame::OnTableHasCellsUpdateUI(wxUpdateUIEvent& event)
+{
+    bool enable(false);
+    wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
+    if (table)
+    {
+        if (event.GetId() == ID_TABLE_DELETE_COLUMN)
+        {
+            enable = table->GetColumnCount() > 1;
+        }
+        else
+        {
+            enable = table->GetRowCount() > 1;
+        }
+    }
+
+    event.Enable(enable);
+}
+
 void MyFrame::OnInsertURL(wxCommandEvent& WXUNUSED(event))
 {
     wxString url = wxGetTextFromUser(_("URL:"), _("Insert URL"));
@@ -2152,3 +2263,50 @@ void MyRichTextCtrl::SetEnhancedDrawingHandler()
 {
     wxRichTextBuffer::AddDrawingHandler(new wxRichTextEnhancedDrawingHandler);
 }
+
+wxRichTextObject* MyRichTextCtrl::FindCurrentPosition() const
+{
+    long position = -1;
+
+    if (HasSelection())  // First see if there's a selection
+    {
+        wxRichTextRange range = GetSelectionRange();
+        if (range.ToInternal().GetLength() == 1)
+        {
+            position = range.GetStart();
+        }
+    }
+    if (position == -1)  // Failing that, near cursor
+    {
+        position = GetAdjustedCaretPosition(GetCaretPosition());
+    }
+
+
+    wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position);
+
+    return obj;
+}
+
+wxRichTextTable* MyRichTextCtrl::FindTable() const
+{
+    wxRichTextObject* obj = FindCurrentPosition();
+
+    // It could be a table or a cell (or neither)
+    wxRichTextTable* table = wxDynamicCast(obj, wxRichTextTable);
+    if (table)
+    {
+        return table;
+    }
+
+    while (obj)
+    {
+        obj = obj->GetParent();
+        wxRichTextTable* table = wxDynamicCast(obj, wxRichTextTable);
+        if (table)
+        {
+            return table;
+        }
+    }
+
+    return NULL;
+}