From e51af9677b11eed35b249bbb1c30fd0f1bd3ddc2 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 22 Mar 2007 20:23:12 +0000 Subject: [PATCH] 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 --- src/data/keyword.cpp | 76 ++-- src/data/keyword.hpp | 24 +- src/gui/control/card_list.cpp | 232 +++-------- src/gui/control/card_list.hpp | 62 +-- src/gui/control/filtered_card_list.cpp | 9 +- src/gui/control/filtered_card_list.hpp | 5 +- src/gui/control/item_list.cpp | 162 ++++++++ src/gui/control/item_list.hpp | 89 ++++ src/gui/control/select_card_list.cpp | 16 +- src/mse.vcproj | 536 +++++++++++++------------ src/util/locale.hpp | 18 +- src/util/string.cpp | 6 + src/util/string.hpp | 5 + 13 files changed, 673 insertions(+), 567 deletions(-) create mode 100644 src/gui/control/item_list.cpp create mode 100644 src/gui/control/item_list.hpp diff --git a/src/data/keyword.cpp b/src/data/keyword.cpp index c05ce63c..6ef7e9cb 100644 --- a/src/data/keyword.cpp +++ b/src/data/keyword.cpp @@ -14,8 +14,7 @@ DECLARE_TYPEOF(map); DECLARE_TYPEOF_COLLECTION(KeywordTrie*); DECLARE_TYPEOF_COLLECTION(KeywordP); DECLARE_TYPEOF_COLLECTION(KeywordParamP); -DECLARE_TYPEOF_COLLECTION(KeywordExpansionP); -DECLARE_TYPEOF_COLLECTION(const KeywordExpansion*); +DECLARE_TYPEOF_COLLECTION(const Keyword*); // ----------------------------------------------------------------------------- : Reflection @@ -29,41 +28,33 @@ IMPLEMENT_REFLECTION(KeywordMode) { REFLECT(name); REFLECT(description); } -IMPLEMENT_REFLECTION(KeywordExpansion) { - REFLECT(match); - REFLECT(reminder); -} // backwards compatability template void read_compat(T&, const Keyword*) {} void read_compat(Reader& tag, Keyword* k) { - String separator, parameter, reminder; + if (!k->match.empty()) return; + String separator, parameter; REFLECT(separator); REFLECT(parameter); - REFLECT(reminder); - if (!separator.empty() || !parameter.empty() || !reminder.empty()) { - // old style keyword declaration, no separate expansion - KeywordExpansionP e(new KeywordExpansion); - e->match = k->keyword; - size_t start = separator.find_first_of('['); - size_t end = separator.find_first_of(']'); - if (start != String::npos && end != String::npos) { - e->match += separator.substr(start + 1, end - start - 1); - } - if (!parameter.empty()) { - e->match += _("") + parameter + _(""); - } - e->reminder.set(reminder); - k->expansions.push_back(e); + // create a match string from the keyword + k->match = k->keyword; + size_t start = separator.find_first_of('['); + size_t end = separator.find_first_of(']'); + if (start != String::npos && end != String::npos) { + k->match += separator.substr(start + 1, end - start - 1); + } + if (!parameter.empty()) { + k->match += _("") + parameter + _(""); } } IMPLEMENT_REFLECTION(Keyword) { REFLECT(keyword); read_compat(tag, this); - REFLECT(expansions); + REFLECT(match); + REFLECT(reminder); REFLECT(rules); -// REFLECT(mode); + REFLECT(mode); } @@ -112,7 +103,7 @@ String regex_escape(Char c) { } } -void KeywordExpansion::prepare(const vector& param_types, bool force) { +void Keyword::prepare(const vector& param_types, bool force) { if (!force && matchRe.IsValid()) return; parameters.clear(); // Prepare regex @@ -159,9 +150,9 @@ class KeywordTrie { KeywordTrie(); ~KeywordTrie(); - map children; ///< children after a given character (owned) - KeywordTrie* on_any_star; ///< children on /.*/ (owned or this) - vector finished; ///< keywords expansions that end in this node + map children; ///< children after a given character (owned) + KeywordTrie* on_any_star; ///< children on /.*/ (owned or this) + vector finished; ///< keywordss that end in this node /// Insert nodes representing the given character /** return the node where the evaluation will be after matching the character */ @@ -228,41 +219,32 @@ void KeywordDatabase::add(const vector& kws) { add(*kw); } } -void KeywordDatabase::add(const Keyword& kw) { - FOR_EACH_CONST(e, kw.expansions) { - add(*e); - } -} -void KeywordDatabase::add(const KeywordExpansion& e) { +void KeywordDatabase::add(const Keyword& kw) { + if (kw.match.empty()) return; // can't handle empty keywords if (!root) { root = new KeywordTrie; root->on_any_star = root; } KeywordTrie* cur = root->insertAnyStar(); - for (size_t i = 0 ; i < e.match.size() ;) { - Char c = e.match.GetChar(i); - if (is_substr(e.match, i, _("insertAnyStar(); - i = match_close_tag_end(e.match, i); + i = match_close_tag_end(kw.match, i); } else { cur = cur->insert(c); i++; } } // now cur is the trie after matching the keyword anywhere in the input text - cur->finished.push_back(&e); + cur->finished.push_back(&kw); } void KeywordDatabase::prepare_parameters(const vector& ps, const vector& kws) { FOR_EACH_CONST(kw, kws) { - prepare_parameters(ps, *kw); - } -} -void KeywordDatabase::prepare_parameters(const vector& ps, const Keyword& kw) { - FOR_EACH_CONST(e, kw.expansions) { - e->prepare(ps); + kw->prepare(ps); } } @@ -311,7 +293,7 @@ String KeywordDatabase::expand(const String& text, while (!s.empty()) { vector current; // current location(s) in the trie vector next; // location(s) after this step - set used; // keywords already investigated + set used; // keywords already investigated current.push_back(root); closure(current); char expand_type = 'a'; // is the keyword expanded? From tag @@ -355,7 +337,7 @@ String KeywordDatabase::expand(const String& text, // are we done? FOR_EACH(n, current) { FOR_EACH(f, n->finished) { - const KeywordExpansion* kw = f; + const Keyword* kw = f; if (used.insert(kw).second) { // we have found a possible match, which we have not seen before assert(kw->matchRe.IsValid()); diff --git a/src/data/keyword.hpp b/src/data/keyword.hpp index 5ca9f95d..1d2cba4e 100644 --- a/src/data/keyword.hpp +++ b/src/data/keyword.hpp @@ -14,7 +14,6 @@ #include DECLARE_POINTER_TYPE(KeywordParam); -DECLARE_POINTER_TYPE(KeywordExpansion); DECLARE_POINTER_TYPE(KeywordMode); DECLARE_POINTER_TYPE(Keyword); class KeywordTrie; @@ -45,9 +44,11 @@ class KeywordMode { // ----------------------------------------------------------------------------- : Keyword expansion -/// A way to use a keyword -class KeywordExpansion { +/// A keyword for a set or a game +class Keyword { public: + String keyword; ///< The keyword, only for human use + String rules; ///< Rules/explanation String match; ///< String to match, tags are used for parameters vector parameters; ///< The types of parameters StringScript reminder; ///< Reminder text of the keyword @@ -58,7 +59,6 @@ class KeywordExpansion { * captures 2,4,... capture the parameters */ wxRegEx matchRe; -//% . Default is the mode of the Keyword. /// Prepare the expansion: (re)generate matchRe and the list of parameters. /** Throws when there is an error in the input @@ -70,19 +70,6 @@ class KeywordExpansion { DECLARE_REFLECTION(); }; -// ----------------------------------------------------------------------------- : Keyword - -/// A keyword for a set or a game -class Keyword { - public: - String keyword; ///< The keyword - vector expansions; ///< Expansions, i.e. ways to use this keyword - String rules; ///< Rules/explanation -// String mode; ///< Mode of use, can be used by scripts (only gives the name) - - DECLARE_REFLECTION(); -}; - // ----------------------------------------------------------------------------- : Using keywords @@ -99,12 +86,9 @@ class KeywordDatabase { void add(const vector&); /// Add a keyword to be matched void add(const Keyword&); - /// Add an expansion of a keyword to be matched - void add(const KeywordExpansion&); /// Prepare the parameters and match regex for a list of keywords static void prepare_parameters(const vector&, const vector&); - static void prepare_parameters(const vector&, const Keyword&); /// Clear the database void clear(); diff --git a/src/gui/control/card_list.cpp b/src/gui/control/card_list.cpp index 2a667963..f9e1421b 100644 --- a/src/gui/control/card_list.cpp +++ b/src/gui/control/card_list.cpp @@ -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& 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(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& 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(a)->data[sort_field]; + ValueP vb = reinterpret_cast(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 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); } } diff --git a/src/gui/control/card_list.hpp b/src/gui/control/card_list.hpp index 17c29905..ef3d09e7 100644 --- a/src/gui/control/card_list.hpp +++ b/src/gui/control/card_list.hpp @@ -10,8 +10,8 @@ // ----------------------------------------------------------------------------- : Includes #include +#include #include -#include 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(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& getCards() const; + /// Get a list of all cards + virtual void getItems(vector& 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(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 column_fields; ///< The field to use for each column (by column index) - // sorted list stuff - vector 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&); diff --git a/src/gui/control/filtered_card_list.cpp b/src/gui/control/filtered_card_list.cpp index 25955f5c..b025c647 100644 --- a/src/gui/control/filtered_card_list.cpp +++ b/src/gui/control/filtered_card_list.cpp @@ -16,21 +16,16 @@ FilteredCardList::FilteredCardList(Window* parent, int id, long style) : CardListBase(parent, id, style) {} -const vector& FilteredCardList::getCards() const { - return cards; -} - void FilteredCardList::setFilter(const CardListFilterP& filter) { this->filter = filter; rebuild(); } -void FilteredCardList::onRebuild() { - cards.clear(); +void FilteredCardList::getItems(vector& out) const { if (filter) { FOR_EACH(c, set->cards) { if (filter->keep(c)) { - cards.push_back(c); + out.push_back(c); } } } diff --git a/src/gui/control/filtered_card_list.hpp b/src/gui/control/filtered_card_list.hpp index c20740fa..acbdac53 100644 --- a/src/gui/control/filtered_card_list.hpp +++ b/src/gui/control/filtered_card_list.hpp @@ -36,13 +36,10 @@ class FilteredCardList : public CardListBase { protected: /// Get only the subset of the cards - virtual const vector& getCards() const; - /// Rebuild the filtered card list - virtual void onRebuild(); + virtual void getItems(vector& out) const; private: CardListFilterP filter; ///< Filter with which this.cards is made - vector cards; ///< The cards that are shown }; diff --git a/src/gui/control/item_list.cpp b/src/gui/control/item_list.cpp new file mode 100644 index 00000000..11c12a1a --- /dev/null +++ b/src/gui/control/item_list.cpp @@ -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 +#include + +// ----------------------------------------------------------------------------- : 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(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 () diff --git a/src/gui/control/item_list.hpp b/src/gui/control/item_list.hpp new file mode 100644 index 00000000..9bf930c7 --- /dev/null +++ b/src/gui/control/item_list.hpp @@ -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 +#include + +typedef shared_ptr 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& 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 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 diff --git a/src/gui/control/select_card_list.cpp b/src/gui/control/select_card_list.cpp index 466a9cd8..a97e5d7c 100644 --- a/src/gui/control/select_card_list.cpp +++ b/src/gui/control/select_card_list.cpp @@ -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: diff --git a/src/mse.vcproj b/src/mse.vcproj index 63cc8bc6..99e3e0ee 100644 --- a/src/mse.vcproj +++ b/src/mse.vcproj @@ -531,18 +531,6 @@ - - - - - - - - @@ -555,24 +543,12 @@ - - - - - - - - @@ -607,6 +583,50 @@ RelativePath=".\gui\control\select_card_list.hpp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1683,85 +1643,74 @@ RelativePath=".\data\symbol_font.hpp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1935,6 +1884,85 @@ RelativePath=".\util\version.hpp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2298,6 +2239,105 @@ RelativePath=".\script\functions\util.hpp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + RelativePath="..\data\en.mse-locale\locale"> + RelativePath="..\data\nl.mse-locale\locale"> diff --git a/src/util/locale.hpp b/src/util/locale.hpp index 8ad8fc07..0206c163 100644 --- a/src/util/locale.hpp +++ b/src/util/locale.hpp @@ -60,7 +60,7 @@ String tr(const StyleSheet&, const String& key, const String& def); String tr(const SymbolFont&, const String& key, const String& def); -/// A localized string for menus/toolbar buttons +/// A localized string for menus #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s)) /// A localized string for help/statusbar text #define _HELP_(s) tr(LOCALE_CAT_HELP, _(s)) @@ -81,21 +81,23 @@ String tr(const SymbolFont&, const String& key, const String& def); /// A localized string for error messages #define _ERROR_(s) tr(LOCALE_CAT_ERROR, _(s)) -/// A localized string for menus/toolbar buttons, with 1 argument (printf style) -#define _MENU_1_(s,a) format_string(tr(LOCALE_CAT_MENU, _(s)), a) +/// A localized string for menus, with 1 argument (printf style) +#define _MENU_1_(s,a) format_string(_MENU_(s), a) +/// A localized string for context menus, contains no "\tshortcut" +#define _CONTEXT_MENU_(s) remove_shortcut(_MENU_(s)) /// A localized string for tooltip text, with 1 argument (printf style) -#define _HELP_1_(s,a) format_string(tr(LOCALE_CAT_HELP, _(s)), a) +#define _HELP_1_(s,a) format_string(_HELP_(s), a) /// A localized string for tooltip text, with 1 argument (printf style) -#define _TOOLTIP_1_(s,a) format_string(tr(LOCALE_CAT_TOOLTIP, _(s)), a) +#define _TOOLTIP_1_(s,a) format_string(_TOOLTIP_(s), a) /// A localized string for error messages, with 1 argument (printf style) -#define _ERROR_1_(s,a) format_string(tr(LOCALE_CAT_ERROR, _(s)), a) +#define _ERROR_1_(s,a) format_string(_ERROR_(s), a) /// A localized string for error messages, with 2 argument (printf style) -#define _ERROR_2_(s,a,b) format_string(tr(LOCALE_CAT_ERROR, _(s)), a, b) +#define _ERROR_2_(s,a,b) format_string(_ERROR_(s), a, b) /// A localized string for error messages, with 3 argument (printf style) -#define _ERROR_3_(s,a,b,c) format_string(tr(LOCALE_CAT_ERROR, _(s)), a, b, c) +#define _ERROR_3_(s,a,b,c) format_string(_ERROR_(s), a, b, c) /// Format a string /** Equivalent to sprintf / String::Format, but allows strings to be passed as arguments (gcc) diff --git a/src/util/string.cpp b/src/util/string.cpp index 702414ec..763ca775 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -166,6 +166,12 @@ String singular_form(const String& str) { return str.substr(0, str.size() - 1); } +String remove_shortcut(const String& str) { + size_t tab = str.find_last_of(_('\t')); + if (tab == String::npos) return str; + else return str.substr(0, tab); +} + // ----------------------------------------------------------------------------- : Comparing / finding bool smart_less(const String& as, const String& bs) { diff --git a/src/util/string.hpp b/src/util/string.hpp index f26253de..e221aa17 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -118,6 +118,11 @@ String cannocial_name_form(const String&); */ String singular_form(const String&); +/// Remove a shortcut from a menu string +/** e.g. "Cut\tCtrl+X" --> "Cut" + */ +String remove_shortcut(const String&); + // ----------------------------------------------------------------------------- : Comparing / finding /// Compare two strings, is the first less than the first?