Commit 224379c0 authored by twanvl's avatar twanvl

Selection in cardlist is correctly moved when changes are made;

cuting/copying/pasting/deleting multiple cards now works.
parent 9e75298e
...@@ -60,8 +60,15 @@ void CardListBase::onChangeSet() { ...@@ -60,8 +60,15 @@ void CardListBase::onChangeSet() {
rebuild(); rebuild();
} }
struct Freezer{
Window* window;
Freezer(Window* window) : window(window) { window->Freeze(); }
~Freezer() { window->Thaw(); }
};
void CardListBase::onAction(const Action& action, bool undone) { void CardListBase::onAction(const Action& action, bool undone) {
TYPE_CASE(action, AddCardAction) { TYPE_CASE(action, AddCardAction) {
Freezer freeze(this);
if (action.action.adding != undone) { if (action.action.adding != undone) {
// select the new cards // select the new cards
focusNone(); focusNone();
...@@ -69,21 +76,8 @@ void CardListBase::onAction(const Action& action, bool undone) { ...@@ -69,21 +76,8 @@ void CardListBase::onAction(const Action& action, bool undone) {
refreshList(); refreshList();
FOR_EACH_CONST(s, action.action.steps) focusItem(s.item); // focus all the new cards FOR_EACH_CONST(s, action.action.steps) focusItem(s.item); // focus all the new cards
} else { } else {
long pos = -1; long pos = selected_item_pos;
// adjust focus for all the removed cards // 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(); refreshList();
if (!allowModify()) { if (!allowModify()) {
// Let some other card list do the selecting, otherwise we get conflicting events // Let some other card list do the selecting, otherwise we get conflicting events
...@@ -129,11 +123,14 @@ void CardListBase::sendEvent() { ...@@ -129,11 +123,14 @@ void CardListBase::sendEvent() {
// ----------------------------------------------------------------------------- : CardListBase : Clipboard // ----------------------------------------------------------------------------- : CardListBase : Clipboard
bool CardListBase::canCopy() const { return !!selected_item; } bool CardListBase::canCut() const { return canDelete(); }
bool CardListBase::canCut() const { return canCopy() && allowModify(); } bool CardListBase::canCopy() const { return focusCount() > 0; }
bool CardListBase::canPaste() const { bool CardListBase::canPaste() const {
return allowModify() && wxTheClipboard->IsSupported(CardsDataObject::format); return allowModify() && wxTheClipboard->IsSupported(CardsDataObject::format);
} }
bool CardListBase::canDelete() const {
return allowModify() && focusCount() > 0; // TODO: check for selection?
}
bool CardListBase::doCopy() { bool CardListBase::doCopy() {
if (!canCopy()) return false; if (!canCopy()) return false;
...@@ -152,13 +149,6 @@ bool CardListBase::doCopy() { ...@@ -152,13 +149,6 @@ bool CardListBase::doCopy() {
wxTheClipboard->Close(); wxTheClipboard->Close();
return ok; return ok;
} }
bool CardListBase::doCut() {
// cut = copy + delete
if (!canCut()) return false;
if (!doCopy()) return false;
doDelete();
return true;
}
bool CardListBase::doPaste() { bool CardListBase::doPaste() {
// get data // get data
if (!canPaste()) return false; if (!canPaste()) return false;
......
...@@ -64,11 +64,11 @@ class CardListBase : public ItemList, public SetView { ...@@ -64,11 +64,11 @@ class CardListBase : public ItemList, public SetView {
// --------------------------------------------------- : Clipboard // --------------------------------------------------- : Clipboard
bool canCut() const; bool canCut() const;
bool canCopy() const; bool canCopy() const;
bool canPaste() const; bool canPaste() const;
bool canDelete() const;
// Try to perform a clipboard operation, return success // Try to perform a clipboard operation, return success
bool doCut();
bool doCopy(); bool doCopy();
bool doPaste(); bool doPaste();
bool doDelete(); bool doDelete();
......
...@@ -46,6 +46,14 @@ void ItemList::selectFirst() { ...@@ -46,6 +46,14 @@ void ItemList::selectFirst() {
selectItemPos(0, true); selectItemPos(0, true);
} }
bool ItemList::doCut() {
// cut = copy + delete
if (!canCut()) return false;
if (!doCopy()) return false;
doDelete();
return true;
}
// ----------------------------------------------------------------------------- : ItemList : Selection (private) // ----------------------------------------------------------------------------- : ItemList : Selection (private)
void ItemList::selectItem(const VoidP& item, bool focus, bool event) { void ItemList::selectItem(const VoidP& item, bool focus, bool event) {
...@@ -109,6 +117,14 @@ void ItemList::focusItem(const VoidP& item, bool focus) { ...@@ -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<ItemList*>(this)->IsSelected(pos)) focused++;
}
return focused;
}
// ----------------------------------------------------------------------------- : ItemList : Building the list // ----------------------------------------------------------------------------- : ItemList : Building the list
...@@ -127,6 +143,7 @@ struct ItemList::ItemComparer { ...@@ -127,6 +143,7 @@ struct ItemList::ItemComparer {
}; };
void ItemList::refreshList() { void ItemList::refreshList() {
Freeze();
// Get all items // Get all items
sorted_list.clear(); sorted_list.clear();
getItems(sorted_list); getItems(sorted_list);
...@@ -141,7 +158,9 @@ void ItemList::refreshList() { ...@@ -141,7 +158,9 @@ void ItemList::refreshList() {
if (item_count == 0) Refresh(); if (item_count == 0) Refresh();
// (re)select current item // (re)select current item
findSelectedItemPos(); findSelectedItemPos();
focusSelectedItem(); focusNone();
focusSelectedItem(true);
Thaw();
} }
void ItemList::sortBy(long column, bool ascending) { void ItemList::sortBy(long column, bool ascending) {
......
...@@ -46,7 +46,7 @@ class ItemList : public wxListView { ...@@ -46,7 +46,7 @@ class ItemList : public wxListView {
virtual bool canPaste() const { return false; } virtual bool canPaste() const { return false; }
virtual bool canDelete() const { return false; } virtual bool canDelete() const { return false; }
// Try to perform a clipboard operation, return success // Try to perform a clipboard operation, return success
virtual bool doCut() { return false; } virtual bool doCut();
virtual bool doCopy() { return false; } virtual bool doCopy() { return false; }
virtual bool doPaste() { return false; } virtual bool doPaste() { return false; }
virtual bool doDelete() { return false; } virtual bool doDelete() { return false; }
...@@ -90,6 +90,8 @@ class ItemList : public wxListView { ...@@ -90,6 +90,8 @@ class ItemList : public wxListView {
void focusNone(); void focusNone();
/// Actually select a certain item in the control /// Actually select a certain item in the control
void focusItem(const VoidP& item, bool focus = true); void focusItem(const VoidP& item, bool focus = true);
/// Count the number of focused items
long focusCount() const;
// --------------------------------------------------- : Data // --------------------------------------------------- : Data
VoidP selected_item; ///< The currently selected item VoidP selected_item; ///< The currently selected item
......
...@@ -168,7 +168,7 @@ void CardsPanel::onUpdateUI(wxUpdateUIEvent& ev) { ...@@ -168,7 +168,7 @@ void CardsPanel::onUpdateUI(wxUpdateUIEvent& ev) {
break; break;
} }
case ID_CARD_ADD_MULT: ev.Enable(false); break; // not implemented 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: { case ID_FORMAT_BOLD: case ID_FORMAT_ITALIC: case ID_FORMAT_SYMBOL: case ID_FORMAT_REMINDER: {
if (focused_control(this) == ID_EDITOR) { if (focused_control(this) == ID_EDITOR) {
ev.Enable(editor->canFormat(ev.GetId())); ev.Enable(editor->canFormat(ev.GetId()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment