mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 21:06:59 -04:00
split ItemList from CardList, this class can also be used to list keywords
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@229 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+53
-179
@@ -36,14 +36,8 @@ DEFINE_EVENT_TYPE(EVENT_CARD_SELECT);
|
||||
// ----------------------------------------------------------------------------- : CardListBase
|
||||
|
||||
CardListBase::CardListBase(Window* parent, int id, long additional_style)
|
||||
: wxListView(parent, id, wxDefaultPosition, wxDefaultSize, additional_style | wxLC_REPORT | wxLC_VIRTUAL | wxLC_SINGLE_SEL)
|
||||
{
|
||||
// create image list
|
||||
wxImageList* il = new wxImageList(18,14);
|
||||
il->Add(load_resource_image(_("sort_asc")), Color(255,0,255));
|
||||
il->Add(load_resource_image(_("sort_desc")), Color(255,0,255));
|
||||
AssignImageList(il, wxIMAGE_LIST_SMALL);
|
||||
}
|
||||
: ItemList(parent, id, additional_style)
|
||||
{}
|
||||
|
||||
CardListBase::~CardListBase() {
|
||||
storeColumns();
|
||||
@@ -64,44 +58,44 @@ void CardListBase::onAction(const Action& action, bool undone) {
|
||||
// Let some other card list else do the selecting
|
||||
return;
|
||||
}
|
||||
selectCardPos((long)sorted_card_list.size() - 1, true);
|
||||
selectItemPos(GetItemCount() - 1, true);
|
||||
} else {
|
||||
// select the new card
|
||||
selectCard(action.card, false /*list will be refreshed anyway*/, true);
|
||||
selectItem(action.card, false /*list will be refreshed anyway*/, true);
|
||||
refreshList();
|
||||
}
|
||||
}
|
||||
TYPE_CASE(action, RemoveCardAction) {
|
||||
if (undone) {
|
||||
// select the re-added card
|
||||
selectCard(action.card, false /*list will be refreshed anyway*/, true);
|
||||
selectItem(action.card, false /*list will be refreshed anyway*/, true);
|
||||
refreshList();
|
||||
} else {
|
||||
long pos = selected_card_pos;
|
||||
long pos = selected_item_pos;
|
||||
refreshList();
|
||||
if (!allowModify()) {
|
||||
// Let some other card list else do the selecting
|
||||
return;
|
||||
}
|
||||
if (action.card == selected_card) {
|
||||
if (action.card == selected_item) {
|
||||
// select the next card, if not possible, select the last
|
||||
if ((size_t)pos + 1 < sorted_card_list.size()) {
|
||||
selectCardPos(pos, true);
|
||||
if (pos + 1 < GetItemCount()) {
|
||||
selectItemPos(pos, true);
|
||||
} else {
|
||||
selectCardPos((long)sorted_card_list.size() - 1, true);
|
||||
selectItemPos(GetItemCount() - 1, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
TYPE_CASE(action, ReorderCardsAction) {
|
||||
if (sort_criterium) return; // nothing changes for us
|
||||
if ((long)action.card_id1 == selected_card_pos || (long)action.card_id2 == selected_card_pos) {
|
||||
if (sort_by_column >= 0) return; // nothing changes for us
|
||||
if ((long)action.card_id1 == selected_item_pos || (long)action.card_id2 == selected_item_pos) {
|
||||
// Selected card has moved; also move in the sorted card list
|
||||
swap(sorted_card_list[action.card_id1],sorted_card_list[action.card_id2]);
|
||||
swap(sorted_list[action.card_id1], sorted_list[action.card_id2]);
|
||||
// reselect the current card, it has moved
|
||||
selected_card_pos = (long)action.card_id1 == selected_card_pos ? (long)action.card_id2 : (long)action.card_id1;
|
||||
selected_item_pos = (long)action.card_id1 == selected_item_pos ? (long)action.card_id2 : (long)action.card_id1;
|
||||
// select the right card
|
||||
selectCurrentCard();
|
||||
selectCurrentItem();
|
||||
}
|
||||
RefreshItem((long)action.card_id1);
|
||||
RefreshItem((long)action.card_id2);
|
||||
@@ -116,83 +110,19 @@ void CardListBase::onAction(const Action& action, bool undone) {
|
||||
}
|
||||
}
|
||||
|
||||
const vector<CardP>& CardListBase::getCards() const {
|
||||
return set->cards;
|
||||
}
|
||||
const CardP& CardListBase::getCard(long pos) const {
|
||||
return sorted_card_list[pos];
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Selection
|
||||
|
||||
bool CardListBase::canSelectPrevious() const {
|
||||
return selected_card_pos - 1 >= 0;
|
||||
}
|
||||
bool CardListBase::canSelectNext() const {
|
||||
return selected_card_pos >= 0 && static_cast<size_t>(selected_card_pos + 1) < sorted_card_list.size();
|
||||
}
|
||||
void CardListBase::selectPrevious() {
|
||||
assert(selected_card_pos >= 1);
|
||||
selectCardPos(selected_card_pos - 1, true);
|
||||
}
|
||||
void CardListBase::selectNext() {
|
||||
assert(selected_card_pos + 1 < (long)sorted_card_list.size());
|
||||
selectCardPos(selected_card_pos + 1, true);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Selection (private)
|
||||
|
||||
void CardListBase::selectCard(const CardP& card, bool focus, bool event) {
|
||||
selected_card = card;
|
||||
if (event) {
|
||||
CardSelectEvent ev(card);
|
||||
ProcessEvent(ev);
|
||||
}
|
||||
findSelectedCardPos();
|
||||
if (focus) {
|
||||
selectCurrentCard();
|
||||
void CardListBase::getItems(vector<VoidP>& out) const {
|
||||
FOR_EACH(c, set->cards) {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
void CardListBase::selectCardPos(long pos, bool focus) {
|
||||
if (selected_card_pos == pos && !focus) return; // this card is already selected
|
||||
if ((size_t)pos < sorted_card_list.size()) {
|
||||
// only if there is something to select
|
||||
selectCard(getCard(pos), false, true);
|
||||
} else {
|
||||
selectCard(CardP(), false, true);
|
||||
}
|
||||
selected_card_pos = pos;
|
||||
if (focus) selectCurrentCard();
|
||||
}
|
||||
|
||||
void CardListBase::findSelectedCardPos() {
|
||||
// find the position of the selected card
|
||||
long count = GetItemCount();
|
||||
selected_card_pos = -1;
|
||||
for (long pos = 0 ; pos < count ; ++pos) {
|
||||
if (getCard(pos) == selected_card) {
|
||||
selected_card_pos = pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void CardListBase::selectCurrentCard() {
|
||||
if (GetItemCount() > 0) {
|
||||
if (selected_card_pos == -1 || (size_t)selected_card_pos > sorted_card_list.size()) {
|
||||
// deselect currently selected item, if any
|
||||
long sel = GetFirstSelected();
|
||||
Select(sel, false);
|
||||
} else {
|
||||
Select(selected_card_pos);
|
||||
Focus(selected_card_pos);
|
||||
}
|
||||
}
|
||||
void CardListBase::sendEvent() {
|
||||
CardSelectEvent ev(getCard());
|
||||
ProcessEvent(ev);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Clipboard
|
||||
|
||||
bool CardListBase::canCopy() const { return !!selected_card; }
|
||||
bool CardListBase::canCopy() const { return !!selected_item; }
|
||||
bool CardListBase::canCut() const { return canCopy() && allowModify(); }
|
||||
bool CardListBase::canPaste() const {
|
||||
return allowModify() && wxTheClipboard->IsSupported(CardDataObject::format);
|
||||
@@ -201,7 +131,7 @@ bool CardListBase::canPaste() const {
|
||||
bool CardListBase::doCopy() {
|
||||
if (!canCopy()) return false;
|
||||
if (!wxTheClipboard->Open()) return false;
|
||||
bool ok = wxTheClipboard->SetData(new CardOnClipboard(set, selected_card)); // ignore result
|
||||
bool ok = wxTheClipboard->SetData(new CardOnClipboard(set, getCard())); // ignore result
|
||||
wxTheClipboard->Close();
|
||||
return ok;
|
||||
}
|
||||
@@ -209,7 +139,7 @@ bool CardListBase::doCut() {
|
||||
// cut = copy + delete
|
||||
if (!canCut()) return false;
|
||||
if (!doCopy()) return false;
|
||||
set->actions.add(new RemoveCardAction(*set, selected_card));
|
||||
set->actions.add(new RemoveCardAction(*set, getCard()));
|
||||
return true;
|
||||
}
|
||||
bool CardListBase::doPaste() {
|
||||
@@ -233,37 +163,23 @@ bool CardListBase::doPaste() {
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Building the list
|
||||
|
||||
// Comparison object for comparing cards
|
||||
struct CardListBase::CardComparer {
|
||||
CardComparer(CardListBase& cl) : cl(cl) {}
|
||||
CardListBase& cl; // 'this' pointer
|
||||
// Compare two cards using the current criterium and order
|
||||
bool operator () (const CardP& a, const CardP& b) {
|
||||
ValueP va = a->data[cl.sort_criterium];
|
||||
ValueP vb = b->data[cl.sort_criterium];
|
||||
if (cl.sort_ascending) {
|
||||
if (!va || !vb) return va < vb; // got to do something, compare pointers
|
||||
return smart_less( va->toString() , vb->toString() );
|
||||
} else {
|
||||
if (!va || !vb) return vb < va;
|
||||
return smart_less( vb->toString() , va->toString() );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void CardListBase::sortList() {
|
||||
sorted_card_list.clear();
|
||||
FOR_EACH_CONST(card, getCards()) {
|
||||
sorted_card_list.push_back(card);
|
||||
}
|
||||
if (sort_criterium) {
|
||||
sort(sorted_card_list.begin(), sorted_card_list.end(), CardComparer(*this));
|
||||
bool CardListBase::compareItems(void* a, void* b) const {
|
||||
FieldP sort_field = column_fields[sort_by_column];
|
||||
ValueP va = reinterpret_cast<Card*>(a)->data[sort_field];
|
||||
ValueP vb = reinterpret_cast<Card*>(b)->data[sort_field];
|
||||
if (sort_ascending) {
|
||||
if (!va || !vb) return va < vb; // got to do something, compare pointers
|
||||
return smart_less( va->toString() , vb->toString() );
|
||||
} else {
|
||||
if (!va || !vb) return vb < va;
|
||||
return smart_less( vb->toString() , va->toString() );
|
||||
}
|
||||
}
|
||||
|
||||
void CardListBase::rebuild() {
|
||||
ClearAll();
|
||||
column_fields.clear();
|
||||
selected_card_pos = -1;
|
||||
selected_item_pos = -1;
|
||||
onRebuild();
|
||||
// determine column order
|
||||
map<int,FieldP> new_column_fields;
|
||||
@@ -290,36 +206,23 @@ void CardListBase::rebuild() {
|
||||
// determine sort settings
|
||||
GameSettings& gs = settings.gameSettingsFor(*set->game);
|
||||
sort_ascending = gs.sort_cards_ascending;
|
||||
sort_criterium = FieldP();
|
||||
int i = 0;
|
||||
sort_by_column = -1;
|
||||
long i = 0;
|
||||
FOR_EACH(f, column_fields) {
|
||||
if (f->name == gs.sort_cards_by) {
|
||||
// we are sorting by this column, store the field
|
||||
sort_criterium = f;
|
||||
// we are sorting by this column
|
||||
sort_by_column = i;
|
||||
// and display an arrow in the header
|
||||
wxListItem li;
|
||||
li.m_mask = wxLIST_MASK_IMAGE;
|
||||
li.m_image = sort_ascending ? 0 : 1; // arrow up/down
|
||||
SetColumn(i, li);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
refreshList();
|
||||
// select a card if possible
|
||||
selectCardPos(0, true);
|
||||
}
|
||||
|
||||
void CardListBase::refreshList() {
|
||||
// ensure correct list size
|
||||
long items = (long) getCards().size();
|
||||
SetItemCount(items);
|
||||
// (re)sort the list
|
||||
sortList();
|
||||
// refresh
|
||||
RefreshItems(0, items - 1);
|
||||
if (items == 0) Refresh();
|
||||
// select
|
||||
findSelectedCardPos();
|
||||
selectCurrentCard();
|
||||
selectItemPos(0, true);
|
||||
}
|
||||
|
||||
ChoiceStyleP CardListBase::findColorStyle() {
|
||||
@@ -344,8 +247,8 @@ void CardListBase::storeColumns() {
|
||||
}
|
||||
// store sorting
|
||||
GameSettings& gs = settings.gameSettingsFor(*set->game);
|
||||
if (sort_criterium) gs.sort_cards_by = sort_criterium->name;
|
||||
else gs.sort_cards_by = wxEmptyString;
|
||||
if (sort_by_column >= 0) gs.sort_cards_by = column_fields.at(sort_by_column)->name;
|
||||
else gs.sort_cards_by = wxEmptyString;
|
||||
gs.sort_cards_ascending = sort_ascending;
|
||||
}
|
||||
void CardListBase::selectColumns() {
|
||||
@@ -381,31 +284,6 @@ wxListItemAttr* CardListBase::OnGetItemAttr(long pos) const {
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Window events
|
||||
|
||||
void CardListBase::onColumnClick(wxListEvent& ev) {
|
||||
FieldP new_sort_criterium = column_fields[ev.GetColumn()];
|
||||
if (sort_criterium == new_sort_criterium) {
|
||||
if (sort_ascending) {
|
||||
sort_ascending = false; // 2nd click on same column -> sort descending
|
||||
} else {
|
||||
new_sort_criterium.reset(); // 3rd click on same column -> don't sort
|
||||
}
|
||||
} else {
|
||||
sort_ascending = true;
|
||||
}
|
||||
// Change image in column header
|
||||
int i = 0;
|
||||
FOR_EACH(f, column_fields) {
|
||||
if (f == new_sort_criterium) {
|
||||
SetColumnImage(i, sort_ascending ? 0 : 1); // arrow up/down
|
||||
} else if (f == sort_criterium) {
|
||||
ClearColumnImage(i);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
sort_criterium = new_sort_criterium;
|
||||
refreshList();
|
||||
}
|
||||
|
||||
void CardListBase::onColumnRightClick(wxListEvent&) {
|
||||
// show menu
|
||||
wxMenu* m = new wxMenu;
|
||||
@@ -417,13 +295,9 @@ void CardListBase::onSelectColumns(wxCommandEvent&) {
|
||||
selectColumns();
|
||||
}
|
||||
|
||||
void CardListBase::onItemFocus(wxListEvent& ev) {
|
||||
selectCardPos(ev.GetIndex(), false);
|
||||
}
|
||||
|
||||
void CardListBase::onChar(wxKeyEvent& ev) {
|
||||
if (ev.GetKeyCode() == WXK_DELETE && allowModify()) {
|
||||
set->actions.add(new RemoveCardAction(*set, selected_card));
|
||||
set->actions.add(new RemoveCardAction(*set, getCard()));
|
||||
} else if (ev.GetKeyCode() == WXK_TAB) {
|
||||
// send a navigation event to our parent, to select another control
|
||||
// we need this because tabs are not handled on the cards panel
|
||||
@@ -437,17 +311,17 @@ void CardListBase::onChar(wxKeyEvent& ev) {
|
||||
|
||||
void CardListBase::onDrag(wxMouseEvent& ev) {
|
||||
if (!allowModify()) return;
|
||||
if (ev.Dragging() && selected_card && !sort_criterium) {
|
||||
if (ev.Dragging() && selected_item && sort_by_column < 0) {
|
||||
// reorder card list
|
||||
int flags;
|
||||
long item = HitTest(ev.GetPosition(), flags);
|
||||
if (flags & wxLIST_HITTEST_ONITEM) {
|
||||
if (item > 0) EnsureVisible(item-1);
|
||||
if (item < GetItemCount()-1) EnsureVisible(item+1);
|
||||
findSelectedCardPos();
|
||||
if (item != selected_card_pos) {
|
||||
findSelectedItemPos();
|
||||
if (item != selected_item_pos) {
|
||||
// move card in the set
|
||||
set->actions.add(new ReorderCardsAction(*set, item, selected_card_pos));
|
||||
set->actions.add(new ReorderCardsAction(*set, item, selected_item_pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -456,12 +330,12 @@ void CardListBase::onDrag(wxMouseEvent& ev) {
|
||||
void CardListBase::onContextMenu(wxContextMenuEvent&) {
|
||||
if (allowModify()) {
|
||||
IconMenu m;
|
||||
m.Append(wxID_CUT, _("cut"), _("Cu&t"), _("Move the selected card to the clipboard"));
|
||||
m.Append(wxID_COPY, _("copy"), _("&Copy"), _("Place the selected card on the clipboard"));
|
||||
m.Append(wxID_PASTE, _("paste"), _("&Paste"), _("Inserts the card from the clipboard"));
|
||||
m.Append(wxID_CUT, _("cut"), _CONTEXT_MENU_("cut"), _HELP_("cut card"));
|
||||
m.Append(wxID_COPY, _("copy"), _CONTEXT_MENU_("copy"), _HELP_("copy card"));
|
||||
m.Append(wxID_PASTE, _("paste"), _CONTEXT_MENU_("paste"), _HELP_("paste card"));
|
||||
m.AppendSeparator();
|
||||
m.Append(ID_CARD_ADD, _("card_add"), _("&Add Card"), _("Add a new, blank, card to this set"));
|
||||
m.Append(ID_CARD_REMOVE,_("card_del"), _("&Remove Select Card"), _("Delete the selected card from this set"));
|
||||
m.Append(ID_CARD_ADD, _("card_add"), _CONTEXT_MENU_("add card"), _HELP_("add card"));
|
||||
m.Append(ID_CARD_REMOVE,_("card_del"), _CONTEXT_MENU_("remove card"), _HELP_("remove card"));
|
||||
PopupMenu(&m);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user