From 674fa1a32a2406b47fdc83c7d2867a3f99379641 Mon Sep 17 00:00:00 2001 From: twanvl Date: Fri, 30 May 2008 18:13:56 +0000 Subject: [PATCH] Selection in cardlist is correctly moved when changes are made; cuting/copying/pasting/deleting multiple cards now works. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@888 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/control/card_list.cpp | 36 +++++++++++++---------------------- src/gui/control/card_list.hpp | 8 ++++---- src/gui/control/item_list.cpp | 21 +++++++++++++++++++- src/gui/control/item_list.hpp | 4 +++- src/gui/set/cards_panel.cpp | 2 +- 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/gui/control/card_list.cpp b/src/gui/control/card_list.cpp index 13b76070..1ed39646 100644 --- a/src/gui/control/card_list.cpp +++ b/src/gui/control/card_list.cpp @@ -60,8 +60,15 @@ void CardListBase::onChangeSet() { rebuild(); } +struct Freezer{ + Window* window; + Freezer(Window* window) : window(window) { window->Freeze(); } + ~Freezer() { window->Thaw(); } +}; + void CardListBase::onAction(const Action& action, bool undone) { TYPE_CASE(action, AddCardAction) { + Freezer freeze(this); if (action.action.adding != undone) { // select the new cards focusNone(); @@ -69,21 +76,8 @@ void CardListBase::onAction(const Action& action, bool undone) { refreshList(); FOR_EACH_CONST(s, action.action.steps) focusItem(s.item); // focus all the new cards } else { - long pos = -1; + long pos = selected_item_pos; // adjust focus for all the removed cards - //FOR_EACH_CONST(s, action.action.steps) focusItem(s.item, false); - long count = GetItemCount(); - long delta = 0; - for (long i = 0 ; i < count ; ++i) { - if (delta < (long)action.action.steps.size() && getItem(i) == action.action.steps[delta].item) { - Select(i - delta, false); - delta++; - } else if (delta > 0) { - Select(i - delta, IsSelected(i)); - } - if (pos == -1 && IsSelected(i - delta)) pos = i - delta; - } - if (pos == -1) pos = selected_item_pos; // select next item if selection would become empty refreshList(); if (!allowModify()) { // Let some other card list do the selecting, otherwise we get conflicting events @@ -129,11 +123,14 @@ void CardListBase::sendEvent() { // ----------------------------------------------------------------------------- : CardListBase : Clipboard -bool CardListBase::canCopy() const { return !!selected_item; } -bool CardListBase::canCut() const { return canCopy() && allowModify(); } +bool CardListBase::canCut() const { return canDelete(); } +bool CardListBase::canCopy() const { return focusCount() > 0; } bool CardListBase::canPaste() const { return allowModify() && wxTheClipboard->IsSupported(CardsDataObject::format); } +bool CardListBase::canDelete() const { + return allowModify() && focusCount() > 0; // TODO: check for selection? +} bool CardListBase::doCopy() { if (!canCopy()) return false; @@ -152,13 +149,6 @@ bool CardListBase::doCopy() { wxTheClipboard->Close(); return ok; } -bool CardListBase::doCut() { - // cut = copy + delete - if (!canCut()) return false; - if (!doCopy()) return false; - doDelete(); - return true; -} bool CardListBase::doPaste() { // get data if (!canPaste()) return false; diff --git a/src/gui/control/card_list.hpp b/src/gui/control/card_list.hpp index ed8a71ac..59e90b45 100644 --- a/src/gui/control/card_list.hpp +++ b/src/gui/control/card_list.hpp @@ -64,11 +64,11 @@ class CardListBase : public ItemList, public SetView { // --------------------------------------------------- : Clipboard - bool canCut() const; - bool canCopy() const; - bool canPaste() const; + bool canCut() const; + bool canCopy() const; + bool canPaste() const; + bool canDelete() const; // Try to perform a clipboard operation, return success - bool doCut(); bool doCopy(); bool doPaste(); bool doDelete(); diff --git a/src/gui/control/item_list.cpp b/src/gui/control/item_list.cpp index 38570aea..1fdc3fc4 100644 --- a/src/gui/control/item_list.cpp +++ b/src/gui/control/item_list.cpp @@ -46,6 +46,14 @@ void ItemList::selectFirst() { selectItemPos(0, true); } +bool ItemList::doCut() { + // cut = copy + delete + if (!canCut()) return false; + if (!doCopy()) return false; + doDelete(); + return true; +} + // ----------------------------------------------------------------------------- : ItemList : Selection (private) void ItemList::selectItem(const VoidP& item, bool focus, bool event) { @@ -109,6 +117,14 @@ void ItemList::focusItem(const VoidP& item, bool focus) { } } } +long ItemList::focusCount() const { + long count = GetItemCount(); + long focused = 0; + for (long pos = 0 ; pos < count ; ++pos) { + if (const_cast(this)->IsSelected(pos)) focused++; + } + return focused; +} // ----------------------------------------------------------------------------- : ItemList : Building the list @@ -127,6 +143,7 @@ struct ItemList::ItemComparer { }; void ItemList::refreshList() { + Freeze(); // Get all items sorted_list.clear(); getItems(sorted_list); @@ -141,7 +158,9 @@ void ItemList::refreshList() { if (item_count == 0) Refresh(); // (re)select current item findSelectedItemPos(); - focusSelectedItem(); + focusNone(); + focusSelectedItem(true); + Thaw(); } void ItemList::sortBy(long column, bool ascending) { diff --git a/src/gui/control/item_list.hpp b/src/gui/control/item_list.hpp index 269e07ac..f80051a3 100644 --- a/src/gui/control/item_list.hpp +++ b/src/gui/control/item_list.hpp @@ -46,7 +46,7 @@ class ItemList : public wxListView { virtual bool canPaste() const { return false; } virtual bool canDelete() const { return false; } // Try to perform a clipboard operation, return success - virtual bool doCut() { return false; } + virtual bool doCut(); virtual bool doCopy() { return false; } virtual bool doPaste() { return false; } virtual bool doDelete() { return false; } @@ -90,6 +90,8 @@ class ItemList : public wxListView { void focusNone(); /// Actually select a certain item in the control void focusItem(const VoidP& item, bool focus = true); + /// Count the number of focused items + long focusCount() const; // --------------------------------------------------- : Data VoidP selected_item; ///< The currently selected item diff --git a/src/gui/set/cards_panel.cpp b/src/gui/set/cards_panel.cpp index aded3c91..e4fa4c85 100644 --- a/src/gui/set/cards_panel.cpp +++ b/src/gui/set/cards_panel.cpp @@ -168,7 +168,7 @@ void CardsPanel::onUpdateUI(wxUpdateUIEvent& ev) { break; } case ID_CARD_ADD_MULT: ev.Enable(false); break; // not implemented - case ID_CARD_REMOVE: ev.Enable(set->cards.size() > 1); break; + case ID_CARD_REMOVE: ev.Enable(card_list->canDelete()); break; case ID_FORMAT_BOLD: case ID_FORMAT_ITALIC: case ID_FORMAT_SYMBOL: case ID_FORMAT_REMINDER: { if (focused_control(this) == ID_EDITOR) { ev.Enable(editor->canFormat(ev.GetId()));