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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <gui/control/item_list.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <wx/listctrl.h>
|
||||
|
||||
DECLARE_POINTER_TYPE(ChoiceStyle);
|
||||
DECLARE_POINTER_TYPE(Field);
|
||||
@@ -39,28 +39,19 @@ struct CardSelectEvent : public wxCommandEvent {
|
||||
/* This class allows the cards to be sorted, and has a _('currentCard'), the selected card
|
||||
* when a card is selected, it raises a CardSelectEvent, that will propage to the parent window.
|
||||
*
|
||||
* Note: (long) pos refers to position in the sorted_card_list,
|
||||
* (size_t) index refers to the index in the actual card list (as returned by getCards).
|
||||
* Note: (long) pos refers to position in the sorted_list,
|
||||
* (size_t) index refers to the index in the actual card list.
|
||||
*/
|
||||
class CardListBase : public wxListView, public SetView {
|
||||
class CardListBase : public ItemList, public SetView {
|
||||
public:
|
||||
CardListBase(Window* parent, int id, long additional_style = 0);
|
||||
~CardListBase();
|
||||
|
||||
// --------------------------------------------------- : Selection
|
||||
|
||||
inline CardP getCard() const { return selected_card; }
|
||||
inline void setCard(const CardP& card) { selectCard(card, true, false); }
|
||||
|
||||
/// Is there a previous card to select?
|
||||
bool canSelectPrevious() const;
|
||||
/// Is there a next card to select?
|
||||
bool canSelectNext() const;
|
||||
/// Move the selection to the previous card (if possible)
|
||||
void selectPrevious();
|
||||
/// Move the selection to the next card (if possible)
|
||||
void selectNext();
|
||||
|
||||
inline CardP getCard() const { return static_pointer_cast<Card>(selected_item); }
|
||||
inline void setCard(const CardP& card) { selectItem(card, true, false); }
|
||||
|
||||
// --------------------------------------------------- : Clipboard
|
||||
|
||||
bool canCut() const;
|
||||
@@ -79,10 +70,10 @@ class CardListBase : public wxListView, public SetView {
|
||||
|
||||
// --------------------------------------------------- : The cards
|
||||
protected:
|
||||
/// What cards should be shown?
|
||||
virtual const vector<CardP>& getCards() const;
|
||||
/// Get a list of all cards
|
||||
virtual void getItems(vector<VoidP>& out) const;
|
||||
/// Return the card at the given position in the sorted card list
|
||||
const CardP& getCard(long pos) const;
|
||||
inline CardP getCard(long pos) const { return static_pointer_cast<Card>(getItem(pos)); }
|
||||
|
||||
/// Rebuild the card list (clear all vectors and fill them again)
|
||||
void rebuild();
|
||||
@@ -91,6 +82,11 @@ class CardListBase : public wxListView, public SetView {
|
||||
/// Can the card list be modified?
|
||||
virtual bool allowModify() const { return false; }
|
||||
|
||||
/// Send an 'item selected' event for the currently selected item (selected_item)
|
||||
virtual void sendEvent();
|
||||
/// Compare cards
|
||||
virtual bool compareItems(void* a, void* b) const;
|
||||
|
||||
// --------------------------------------------------- : Item 'events'
|
||||
|
||||
/// Get the text of an item in a specific column
|
||||
@@ -103,37 +99,13 @@ class CardListBase : public wxListView, public SetView {
|
||||
virtual wxListItemAttr* OnGetItemAttr(long pos) const;
|
||||
|
||||
// --------------------------------------------------- : Data
|
||||
protected:
|
||||
CardP selected_card; ///< The currently selected card, or -1 if no card is selected
|
||||
long selected_card_pos;///< Position of the selected card in the sorted_card_list
|
||||
private:
|
||||
// display stuff
|
||||
ChoiceStyleP color_style; ///< Style (and field) to use for text color (optional)
|
||||
vector<FieldP> column_fields; ///< The field to use for each column (by column index)
|
||||
// sorted list stuff
|
||||
vector<CardP> sorted_card_list; ///< Sorted list of cards, can be considered a map: pos->card
|
||||
FieldP sort_criterium; ///< Field to sort by
|
||||
bool sort_ascending; ///< Sort order
|
||||
|
||||
|
||||
mutable wxListItemAttr item_attr; // for OnGetItemAttr
|
||||
|
||||
/// Select a card, send an event to the parent
|
||||
/** If focus then the card is also focused and selected in the actual control.
|
||||
* This should abviously not be done when the card is selected because it was selected (leading to a loop).
|
||||
*/
|
||||
void selectCard(const CardP& card, bool focus, bool event);
|
||||
/// Select a card at the specified position
|
||||
void selectCardPos(long pos, bool focus);
|
||||
/// Find the position for the selected_card
|
||||
void findSelectedCardPos();
|
||||
/// Actually select the card at selected_card_pos in the control
|
||||
void selectCurrentCard();
|
||||
|
||||
/// Sorts the list by the current sorting criterium
|
||||
void sortList();
|
||||
struct CardComparer; // for comparing cards
|
||||
/// Refresh the card list (resort, refresh and reselect current item)
|
||||
void refreshList();
|
||||
/// Find the field that determines the color, if any.
|
||||
/** Note: Uses only fields from the set's default style */
|
||||
ChoiceStyleP findColorStyle();
|
||||
@@ -148,10 +120,8 @@ class CardListBase : public wxListView, public SetView {
|
||||
// --------------------------------------------------- : Window events
|
||||
DECLARE_EVENT_TABLE();
|
||||
|
||||
void onColumnClick (wxListEvent& ev);
|
||||
void onColumnRightClick(wxListEvent& ev);
|
||||
void onSelectColumns (wxCommandEvent& ev);
|
||||
void onItemFocus (wxListEvent& ev);
|
||||
void onChar (wxKeyEvent& ev);
|
||||
void onDrag (wxMouseEvent& ev);
|
||||
void onContextMenu (wxContextMenuEvent&);
|
||||
|
||||
@@ -16,21 +16,16 @@ FilteredCardList::FilteredCardList(Window* parent, int id, long style)
|
||||
: CardListBase(parent, id, style)
|
||||
{}
|
||||
|
||||
const vector<CardP>& FilteredCardList::getCards() const {
|
||||
return cards;
|
||||
}
|
||||
|
||||
void FilteredCardList::setFilter(const CardListFilterP& filter) {
|
||||
this->filter = filter;
|
||||
rebuild();
|
||||
}
|
||||
|
||||
void FilteredCardList::onRebuild() {
|
||||
cards.clear();
|
||||
void FilteredCardList::getItems(vector<VoidP>& out) const {
|
||||
if (filter) {
|
||||
FOR_EACH(c, set->cards) {
|
||||
if (filter->keep(c)) {
|
||||
cards.push_back(c);
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,13 +36,10 @@ class FilteredCardList : public CardListBase {
|
||||
|
||||
protected:
|
||||
/// Get only the subset of the cards
|
||||
virtual const vector<CardP>& getCards() const;
|
||||
/// Rebuild the filtered card list
|
||||
virtual void onRebuild();
|
||||
virtual void getItems(vector<VoidP>& out) const;
|
||||
|
||||
private:
|
||||
CardListFilterP filter; ///< Filter with which this.cards is made
|
||||
vector<CardP> cards; ///< The cards that are shown
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <gui/control/item_list.hpp>
|
||||
#include <gui/util.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList
|
||||
|
||||
ItemList::ItemList(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 : Selection
|
||||
|
||||
bool ItemList::canSelectPrevious() const {
|
||||
return selected_item_pos - 1 >= 0;
|
||||
}
|
||||
bool ItemList::canSelectNext() const {
|
||||
return selected_item_pos >= 0 && static_cast<size_t>(selected_item_pos + 1) < sorted_list.size();
|
||||
}
|
||||
void ItemList::selectPrevious() {
|
||||
assert(selected_item_pos >= 1);
|
||||
selectItemPos(selected_item_pos - 1, true);
|
||||
}
|
||||
void ItemList::selectNext() {
|
||||
assert(selected_item_pos + 1 < (long)sorted_list.size());
|
||||
selectItemPos(selected_item_pos + 1, true);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList : Selection (private)
|
||||
|
||||
void ItemList::selectItem(const VoidP& item, bool focus, bool event) {
|
||||
selected_item = item;
|
||||
if (event) sendEvent();
|
||||
findSelectedItemPos();
|
||||
if (focus) {
|
||||
selectCurrentItem();
|
||||
}
|
||||
}
|
||||
|
||||
void ItemList::selectItemPos(long pos, bool focus) {
|
||||
if (selected_item_pos == pos && !focus) return; // this item is already selected
|
||||
if ((size_t)pos < sorted_list.size()) {
|
||||
// only if there is something to select
|
||||
selectItem(getItem(pos), false, true);
|
||||
} else {
|
||||
selectItem(VoidP(), false, true);
|
||||
}
|
||||
selected_item_pos = pos;
|
||||
if (focus) selectCurrentItem();
|
||||
}
|
||||
|
||||
void ItemList::findSelectedItemPos() {
|
||||
// find the position of the selected item
|
||||
long count = GetItemCount();
|
||||
selected_item_pos = -1;
|
||||
for (long pos = 0 ; pos < count ; ++pos) {
|
||||
if (getItem(pos) == selected_item) {
|
||||
selected_item_pos = pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ItemList::selectCurrentItem() {
|
||||
if (GetItemCount() > 0) {
|
||||
if (selected_item_pos == -1 || (size_t)selected_item_pos > sorted_list.size()) {
|
||||
// deselect currently selected item, if any
|
||||
long sel = GetFirstSelected();
|
||||
Select(sel, false);
|
||||
} else {
|
||||
Select(selected_item_pos);
|
||||
Focus (selected_item_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList : Building the list
|
||||
|
||||
// Comparison object for comparing items
|
||||
struct ItemList::ItemComparer {
|
||||
ItemComparer(ItemList& list) : list(list) {}
|
||||
ItemList& list; // 'this' pointer
|
||||
// Compare two items using the current criterium and order
|
||||
bool operator () (const VoidP& a, const VoidP& b) {
|
||||
return list.compareItems(a.get(), b.get());
|
||||
}
|
||||
};
|
||||
|
||||
void ItemList::refreshList() {
|
||||
// Get all items
|
||||
sorted_list.clear();
|
||||
getItems(sorted_list);
|
||||
long item_count = (long)sorted_list.size();
|
||||
SetItemCount(item_count);
|
||||
// Sort the list
|
||||
if (sort_by_column >= 0) {
|
||||
sort(sorted_list.begin(), sorted_list.end(), ItemComparer(*this));
|
||||
}
|
||||
// refresh
|
||||
RefreshItems(0, item_count - 1);
|
||||
if (item_count == 0) Refresh();
|
||||
// (re)select current item
|
||||
findSelectedItemPos();
|
||||
selectCurrentItem();
|
||||
}
|
||||
|
||||
void ItemList::sortBy(long column, bool ascending) {
|
||||
// Change image in column header
|
||||
long count = GetColumnCount();
|
||||
for (long i = 0 ; i < count ; ++i) {
|
||||
if (i == column) {
|
||||
SetColumnImage(i, sort_ascending ? 0 : 1); // arrow up/down
|
||||
} else if (i == sort_by_column) {
|
||||
ClearColumnImage(i);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
// sort list
|
||||
sort_by_column = column;
|
||||
sort_ascending = ascending;
|
||||
refreshList();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList : Window events
|
||||
|
||||
void ItemList::onColumnClick(wxListEvent& ev) {
|
||||
long new_sort_by_column = ev.GetColumn();
|
||||
if (sort_by_column == new_sort_by_column) {
|
||||
if (sort_ascending) {
|
||||
sort_ascending = false; // 2nd click on same column -> sort descending
|
||||
} else if (mustSort()) {
|
||||
sort_ascending = true; // 3rd click on same column -> sort ascending again
|
||||
} else {
|
||||
new_sort_by_column = -1; // 3rd click on same column -> don't sort
|
||||
}
|
||||
} else {
|
||||
sort_ascending = true;
|
||||
}
|
||||
sortBy(new_sort_by_column, sort_ascending);
|
||||
}
|
||||
|
||||
void ItemList::onItemFocus(wxListEvent& ev) {
|
||||
selectItemPos(ev.GetIndex(), false);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList : Event table
|
||||
|
||||
BEGIN_EVENT_TABLE(ItemList, wxListView)
|
||||
EVT_LIST_COL_CLICK (wxID_ANY, ItemList::onColumnClick)
|
||||
EVT_LIST_ITEM_FOCUSED (wxID_ANY, ItemList::onItemFocus)
|
||||
END_EVENT_TABLE ()
|
||||
@@ -0,0 +1,89 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
#ifndef HEADER_GUI_CONTROL_ITEM_LIST
|
||||
#define HEADER_GUI_CONTROL_ITEM_LIST
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <wx/listctrl.h>
|
||||
|
||||
typedef shared_ptr<void> VoidP;
|
||||
|
||||
// ----------------------------------------------------------------------------- : ItemList
|
||||
|
||||
/// A generic list of items
|
||||
/** The list is shown in report mode, and allows sorting.
|
||||
* Currently used for cards and keywords.
|
||||
*/
|
||||
class ItemList : public wxListView {
|
||||
public:
|
||||
ItemList(Window* parent, int id, long additional_style = 0);
|
||||
|
||||
// --------------------------------------------------- : Selection
|
||||
|
||||
/// Is there a previous item to select?
|
||||
bool canSelectPrevious() const;
|
||||
/// Is there a next item to select?
|
||||
bool canSelectNext() const;
|
||||
/// Move the selection to the previous item (if possible)
|
||||
void selectPrevious();
|
||||
/// Move the selection to the next item (if possible)
|
||||
void selectNext();
|
||||
|
||||
// --------------------------------------------------- : Virtual interface
|
||||
protected:
|
||||
/// Get a list of all items
|
||||
virtual void getItems(vector<VoidP>& out) const = 0;
|
||||
|
||||
/// Send an 'item selected' event for the currently selected item (selected_item)
|
||||
virtual void sendEvent() = 0;
|
||||
|
||||
/// Is sorting required?
|
||||
virtual bool mustSort() const { return false; }
|
||||
/// Compare two items for <
|
||||
virtual bool compareItems(void* a, void* b) const = 0;
|
||||
|
||||
// --------------------------------------------------- : Protected interface
|
||||
/// Return the card at the given position in the sorted list
|
||||
inline const VoidP& getItem(long pos) const { return sorted_list[pos]; }
|
||||
/// Sort by the given column
|
||||
void sortBy(long column, bool ascending);
|
||||
/// Refresh the card list (resort, refresh and reselect current item)
|
||||
void refreshList();
|
||||
|
||||
/// Select an item, send an event to the parent
|
||||
/** If focus then the item is also focused and selected in the actual control.
|
||||
* This should abviously not be done when the item is selected because it was focused (leading to a loop).
|
||||
*/
|
||||
void selectItem(const VoidP& item, bool focus, bool event);
|
||||
/// Select a item at the specified position
|
||||
void selectItemPos(long pos, bool focus);
|
||||
/// Find the position for the selected_item
|
||||
void findSelectedItemPos();
|
||||
/// Actually select the card at selected_item_pos in the control
|
||||
void selectCurrentItem();
|
||||
|
||||
// --------------------------------------------------- : Data
|
||||
VoidP selected_item; ///< The currently selected item
|
||||
long selected_item_pos; ///< Position of the selected item in the sorted_list, or -1 if no card is selected
|
||||
bool sort_ascending; ///< Sort order
|
||||
long sort_by_column; ///< Column to use for sorting, or -1 if not sorted
|
||||
vector<VoidP> sorted_list; ///< Sorted list of items, can be considered a map: pos->item
|
||||
|
||||
private:
|
||||
struct ItemComparer; // for comparing items
|
||||
|
||||
// --------------------------------------------------- : Window events
|
||||
DECLARE_EVENT_TABLE();
|
||||
|
||||
void onColumnClick(wxListEvent& ev);
|
||||
void onItemFocus (wxListEvent& ev);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
@@ -26,7 +26,7 @@ SelectCardList::SelectCardList(Window* parent, int id, long additional_style)
|
||||
}
|
||||
|
||||
void SelectCardList::selectAll() {
|
||||
FOR_EACH_CONST(c, getCards()) {
|
||||
FOR_EACH_CONST(c, set->cards) {
|
||||
selected.insert(c);
|
||||
}
|
||||
Refresh(false);
|
||||
@@ -61,25 +61,25 @@ void SelectCardList::toggle(const CardP& card) {
|
||||
}
|
||||
|
||||
void SelectCardList::onKeyDown(wxKeyEvent& ev) {
|
||||
if (selected_card_pos == -1 || !selected_card) {
|
||||
if (selected_item_pos == -1 || !selected_item) {
|
||||
// no selection
|
||||
ev.Skip();
|
||||
return;
|
||||
}
|
||||
switch (ev.GetKeyCode()) {
|
||||
case WXK_SPACE: {
|
||||
toggle(selected_card);
|
||||
RefreshItem(selected_card_pos);
|
||||
toggle(getCard());
|
||||
RefreshItem(selected_item_pos);
|
||||
break;
|
||||
}
|
||||
case WXK_NUMPAD_ADD: case '+': {
|
||||
selected.insert(selected_card);
|
||||
RefreshItem(selected_card_pos);
|
||||
selected.insert(getCard());
|
||||
RefreshItem(selected_item_pos);
|
||||
break;
|
||||
}
|
||||
case WXK_NUMPAD_SUBTRACT: case '-': {
|
||||
selected.erase(selected_card);
|
||||
RefreshItem(selected_card_pos);
|
||||
selected.erase(getCard());
|
||||
RefreshItem(selected_item_pos);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user