Commit dae1f4f1 authored by twanvl's avatar twanvl

split ItemList from CardList, this class can also be used to list keywords

parent 1e652532
...@@ -14,8 +14,7 @@ DECLARE_TYPEOF(map<Char COMMA KeywordTrie*>); ...@@ -14,8 +14,7 @@ DECLARE_TYPEOF(map<Char COMMA KeywordTrie*>);
DECLARE_TYPEOF_COLLECTION(KeywordTrie*); DECLARE_TYPEOF_COLLECTION(KeywordTrie*);
DECLARE_TYPEOF_COLLECTION(KeywordP); DECLARE_TYPEOF_COLLECTION(KeywordP);
DECLARE_TYPEOF_COLLECTION(KeywordParamP); DECLARE_TYPEOF_COLLECTION(KeywordParamP);
DECLARE_TYPEOF_COLLECTION(KeywordExpansionP); DECLARE_TYPEOF_COLLECTION(const Keyword*);
DECLARE_TYPEOF_COLLECTION(const KeywordExpansion*);
// ----------------------------------------------------------------------------- : Reflection // ----------------------------------------------------------------------------- : Reflection
...@@ -29,41 +28,33 @@ IMPLEMENT_REFLECTION(KeywordMode) { ...@@ -29,41 +28,33 @@ IMPLEMENT_REFLECTION(KeywordMode) {
REFLECT(name); REFLECT(name);
REFLECT(description); REFLECT(description);
} }
IMPLEMENT_REFLECTION(KeywordExpansion) {
REFLECT(match);
REFLECT(reminder);
}
// backwards compatability // backwards compatability
template <typename T> void read_compat(T&, const Keyword*) {} template <typename T> void read_compat(T&, const Keyword*) {}
void read_compat(Reader& tag, Keyword* k) { void read_compat(Reader& tag, Keyword* k) {
String separator, parameter, reminder; if (!k->match.empty()) return;
String separator, parameter;
REFLECT(separator); REFLECT(separator);
REFLECT(parameter); REFLECT(parameter);
REFLECT(reminder); // create a match string from the keyword
if (!separator.empty() || !parameter.empty() || !reminder.empty()) { k->match = k->keyword;
// old style keyword declaration, no separate expansion size_t start = separator.find_first_of('[');
KeywordExpansionP e(new KeywordExpansion); size_t end = separator.find_first_of(']');
e->match = k->keyword; if (start != String::npos && end != String::npos) {
size_t start = separator.find_first_of('['); k->match += separator.substr(start + 1, end - start - 1);
size_t end = separator.find_first_of(']'); }
if (start != String::npos && end != String::npos) { if (!parameter.empty()) {
e->match += separator.substr(start + 1, end - start - 1); k->match += _("<param>") + parameter + _("</param>");
}
if (!parameter.empty()) {
e->match += _("<param>") + parameter + _("</param>");
}
e->reminder.set(reminder);
k->expansions.push_back(e);
} }
} }
IMPLEMENT_REFLECTION(Keyword) { IMPLEMENT_REFLECTION(Keyword) {
REFLECT(keyword); REFLECT(keyword);
read_compat(tag, this); read_compat(tag, this);
REFLECT(expansions); REFLECT(match);
REFLECT(reminder);
REFLECT(rules); REFLECT(rules);
// REFLECT(mode); REFLECT(mode);
} }
...@@ -112,7 +103,7 @@ String regex_escape(Char c) { ...@@ -112,7 +103,7 @@ String regex_escape(Char c) {
} }
} }
void KeywordExpansion::prepare(const vector<KeywordParamP>& param_types, bool force) { void Keyword::prepare(const vector<KeywordParamP>& param_types, bool force) {
if (!force && matchRe.IsValid()) return; if (!force && matchRe.IsValid()) return;
parameters.clear(); parameters.clear();
// Prepare regex // Prepare regex
...@@ -159,9 +150,9 @@ class KeywordTrie { ...@@ -159,9 +150,9 @@ class KeywordTrie {
KeywordTrie(); KeywordTrie();
~KeywordTrie(); ~KeywordTrie();
map<Char, KeywordTrie*> children; ///< children after a given character (owned) map<Char, KeywordTrie*> children; ///< children after a given character (owned)
KeywordTrie* on_any_star; ///< children on /.*/ (owned or this) KeywordTrie* on_any_star; ///< children on /.*/ (owned or this)
vector<const KeywordExpansion*> finished; ///< keywords expansions that end in this node vector<const Keyword*> finished; ///< keywordss that end in this node
/// Insert nodes representing the given character /// Insert nodes representing the given character
/** return the node where the evaluation will be after matching the character */ /** return the node where the evaluation will be after matching the character */
...@@ -228,41 +219,32 @@ void KeywordDatabase::add(const vector<KeywordP>& kws) { ...@@ -228,41 +219,32 @@ void KeywordDatabase::add(const vector<KeywordP>& kws) {
add(*kw); 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) { if (!root) {
root = new KeywordTrie; root = new KeywordTrie;
root->on_any_star = root; root->on_any_star = root;
} }
KeywordTrie* cur = root->insertAnyStar(); KeywordTrie* cur = root->insertAnyStar();
for (size_t i = 0 ; i < e.match.size() ;) { for (size_t i = 0 ; i < kw.match.size() ;) {
Char c = e.match.GetChar(i); Char c = kw.match.GetChar(i);
if (is_substr(e.match, i, _("<param"))) { if (is_substr(kw.match, i, _("<param"))) {
// tag, parameter, match anything // tag, parameter, match anything
cur = cur->insertAnyStar(); cur = cur->insertAnyStar();
i = match_close_tag_end(e.match, i); i = match_close_tag_end(kw.match, i);
} else { } else {
cur = cur->insert(c); cur = cur->insert(c);
i++; i++;
} }
} }
// now cur is the trie after matching the keyword anywhere in the input text // 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<KeywordParamP>& ps, const vector<KeywordP>& kws) { void KeywordDatabase::prepare_parameters(const vector<KeywordParamP>& ps, const vector<KeywordP>& kws) {
FOR_EACH_CONST(kw, kws) { FOR_EACH_CONST(kw, kws) {
prepare_parameters(ps, *kw); kw->prepare(ps);
}
}
void KeywordDatabase::prepare_parameters(const vector<KeywordParamP>& ps, const Keyword& kw) {
FOR_EACH_CONST(e, kw.expansions) {
e->prepare(ps);
} }
} }
...@@ -311,7 +293,7 @@ String KeywordDatabase::expand(const String& text, ...@@ -311,7 +293,7 @@ String KeywordDatabase::expand(const String& text,
while (!s.empty()) { while (!s.empty()) {
vector<KeywordTrie*> current; // current location(s) in the trie vector<KeywordTrie*> current; // current location(s) in the trie
vector<KeywordTrie*> next; // location(s) after this step vector<KeywordTrie*> next; // location(s) after this step
set<const KeywordExpansion*> used; // keywords already investigated set<const Keyword*> used; // keywords already investigated
current.push_back(root); current.push_back(root);
closure(current); closure(current);
char expand_type = 'a'; // is the keyword expanded? From <kw-?> tag char expand_type = 'a'; // is the keyword expanded? From <kw-?> tag
...@@ -355,7 +337,7 @@ String KeywordDatabase::expand(const String& text, ...@@ -355,7 +337,7 @@ String KeywordDatabase::expand(const String& text,
// are we done? // are we done?
FOR_EACH(n, current) { FOR_EACH(n, current) {
FOR_EACH(f, n->finished) { FOR_EACH(f, n->finished) {
const KeywordExpansion* kw = f; const Keyword* kw = f;
if (used.insert(kw).second) { if (used.insert(kw).second) {
// we have found a possible match, which we have not seen before // we have found a possible match, which we have not seen before
assert(kw->matchRe.IsValid()); assert(kw->matchRe.IsValid());
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include <wx/regex.h> #include <wx/regex.h>
DECLARE_POINTER_TYPE(KeywordParam); DECLARE_POINTER_TYPE(KeywordParam);
DECLARE_POINTER_TYPE(KeywordExpansion);
DECLARE_POINTER_TYPE(KeywordMode); DECLARE_POINTER_TYPE(KeywordMode);
DECLARE_POINTER_TYPE(Keyword); DECLARE_POINTER_TYPE(Keyword);
class KeywordTrie; class KeywordTrie;
...@@ -45,9 +44,11 @@ class KeywordMode { ...@@ -45,9 +44,11 @@ class KeywordMode {
// ----------------------------------------------------------------------------- : Keyword expansion // ----------------------------------------------------------------------------- : Keyword expansion
/// A way to use a keyword /// A keyword for a set or a game
class KeywordExpansion { class Keyword {
public: public:
String keyword; ///< The keyword, only for human use
String rules; ///< Rules/explanation
String match; ///< String to match, <param> tags are used for parameters String match; ///< String to match, <param> tags are used for parameters
vector<KeywordParamP> parameters; ///< The types of parameters vector<KeywordParamP> parameters; ///< The types of parameters
StringScript reminder; ///< Reminder text of the keyword StringScript reminder; ///< Reminder text of the keyword
...@@ -58,7 +59,6 @@ class KeywordExpansion { ...@@ -58,7 +59,6 @@ class KeywordExpansion {
* captures 2,4,... capture the parameters * captures 2,4,... capture the parameters
*/ */
wxRegEx matchRe; wxRegEx matchRe;
//% . Default is the mode of the Keyword.
/// Prepare the expansion: (re)generate matchRe and the list of parameters. /// Prepare the expansion: (re)generate matchRe and the list of parameters.
/** Throws when there is an error in the input /** Throws when there is an error in the input
...@@ -70,19 +70,6 @@ class KeywordExpansion { ...@@ -70,19 +70,6 @@ class KeywordExpansion {
DECLARE_REFLECTION(); DECLARE_REFLECTION();
}; };
// ----------------------------------------------------------------------------- : Keyword
/// A keyword for a set or a game
class Keyword {
public:
String keyword; ///< The keyword
vector<KeywordExpansionP> 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 // ----------------------------------------------------------------------------- : Using keywords
...@@ -99,12 +86,9 @@ class KeywordDatabase { ...@@ -99,12 +86,9 @@ class KeywordDatabase {
void add(const vector<KeywordP>&); void add(const vector<KeywordP>&);
/// Add a keyword to be matched /// Add a keyword to be matched
void add(const Keyword&); 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 /// Prepare the parameters and match regex for a list of keywords
static void prepare_parameters(const vector<KeywordParamP>&, const vector<KeywordP>&); static void prepare_parameters(const vector<KeywordParamP>&, const vector<KeywordP>&);
static void prepare_parameters(const vector<KeywordParamP>&, const Keyword&);
/// Clear the database /// Clear the database
void clear(); void clear();
......
This diff is collapsed.
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
// ----------------------------------------------------------------------------- : Includes // ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp> #include <util/prec.hpp>
#include <gui/control/item_list.hpp>
#include <data/set.hpp> #include <data/set.hpp>
#include <wx/listctrl.h>
DECLARE_POINTER_TYPE(ChoiceStyle); DECLARE_POINTER_TYPE(ChoiceStyle);
DECLARE_POINTER_TYPE(Field); DECLARE_POINTER_TYPE(Field);
...@@ -39,28 +39,19 @@ struct CardSelectEvent : public wxCommandEvent { ...@@ -39,28 +39,19 @@ struct CardSelectEvent : public wxCommandEvent {
/* This class allows the cards to be sorted, and has a _('currentCard'), the selected card /* 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. * 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, * Note: (long) pos refers to position in the sorted_list,
* (size_t) index refers to the index in the actual card list (as returned by getCards). * (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: public:
CardListBase(Window* parent, int id, long additional_style = 0); CardListBase(Window* parent, int id, long additional_style = 0);
~CardListBase(); ~CardListBase();
// --------------------------------------------------- : Selection // --------------------------------------------------- : Selection
inline CardP getCard() const { return selected_card; } inline CardP getCard() const { return static_pointer_cast<Card>(selected_item); }
inline void setCard(const CardP& card) { selectCard(card, true, false); } inline void setCard(const CardP& card) { selectItem(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();
// --------------------------------------------------- : Clipboard // --------------------------------------------------- : Clipboard
bool canCut() const; bool canCut() const;
...@@ -79,10 +70,10 @@ class CardListBase : public wxListView, public SetView { ...@@ -79,10 +70,10 @@ class CardListBase : public wxListView, public SetView {
// --------------------------------------------------- : The cards // --------------------------------------------------- : The cards
protected: protected:
/// What cards should be shown? /// Get a list of all cards
virtual const vector<CardP>& getCards() const; virtual void getItems(vector<VoidP>& out) const;
/// Return the card at the given position in the sorted card list /// 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) /// Rebuild the card list (clear all vectors and fill them again)
void rebuild(); void rebuild();
...@@ -91,6 +82,11 @@ class CardListBase : public wxListView, public SetView { ...@@ -91,6 +82,11 @@ class CardListBase : public wxListView, public SetView {
/// Can the card list be modified? /// Can the card list be modified?
virtual bool allowModify() const { return false; } 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' // --------------------------------------------------- : Item 'events'
/// Get the text of an item in a specific column /// Get the text of an item in a specific column
...@@ -103,37 +99,13 @@ class CardListBase : public wxListView, public SetView { ...@@ -103,37 +99,13 @@ class CardListBase : public wxListView, public SetView {
virtual wxListItemAttr* OnGetItemAttr(long pos) const; virtual wxListItemAttr* OnGetItemAttr(long pos) const;
// --------------------------------------------------- : Data // --------------------------------------------------- : 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: private:
// display stuff // display stuff
ChoiceStyleP color_style; ///< Style (and field) to use for text color (optional) 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) 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 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. /// Find the field that determines the color, if any.
/** Note: Uses only fields from the set's default style */ /** Note: Uses only fields from the set's default style */
ChoiceStyleP findColorStyle(); ChoiceStyleP findColorStyle();
...@@ -148,10 +120,8 @@ class CardListBase : public wxListView, public SetView { ...@@ -148,10 +120,8 @@ class CardListBase : public wxListView, public SetView {
// --------------------------------------------------- : Window events // --------------------------------------------------- : Window events
DECLARE_EVENT_TABLE(); DECLARE_EVENT_TABLE();
void onColumnClick (wxListEvent& ev);
void onColumnRightClick(wxListEvent& ev); void onColumnRightClick(wxListEvent& ev);
void onSelectColumns (wxCommandEvent& ev); void onSelectColumns (wxCommandEvent& ev);
void onItemFocus (wxListEvent& ev);
void onChar (wxKeyEvent& ev); void onChar (wxKeyEvent& ev);
void onDrag (wxMouseEvent& ev); void onDrag (wxMouseEvent& ev);
void onContextMenu (wxContextMenuEvent&); void onContextMenu (wxContextMenuEvent&);
......
...@@ -16,21 +16,16 @@ FilteredCardList::FilteredCardList(Window* parent, int id, long style) ...@@ -16,21 +16,16 @@ FilteredCardList::FilteredCardList(Window* parent, int id, long style)
: CardListBase(parent, id, style) : CardListBase(parent, id, style)
{} {}
const vector<CardP>& FilteredCardList::getCards() const {
return cards;
}
void FilteredCardList::setFilter(const CardListFilterP& filter) { void FilteredCardList::setFilter(const CardListFilterP& filter) {
this->filter = filter; this->filter = filter;
rebuild(); rebuild();
} }
void FilteredCardList::onRebuild() { void FilteredCardList::getItems(vector<VoidP>& out) const {
cards.clear();
if (filter) { if (filter) {
FOR_EACH(c, set->cards) { FOR_EACH(c, set->cards) {
if (filter->keep(c)) { if (filter->keep(c)) {
cards.push_back(c); out.push_back(c);
} }
} }
} }
......
...@@ -36,13 +36,10 @@ class FilteredCardList : public CardListBase { ...@@ -36,13 +36,10 @@ class FilteredCardList : public CardListBase {
protected: protected:
/// Get only the subset of the cards /// Get only the subset of the cards
virtual const vector<CardP>& getCards() const; virtual void getItems(vector<VoidP>& out) const;
/// Rebuild the filtered card list
virtual void onRebuild();
private: private:
CardListFilterP filter; ///< Filter with which this.cards is made CardListFilterP filter; ///< Filter with which this.cards is made
vector<CardP> cards; ///< The cards that are shown
}; };
......
//+----------------------------------------------------------------------------+
//| 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 ()
//+----------------------------------------------------------------------------+
//| 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) ...@@ -26,7 +26,7 @@ SelectCardList::SelectCardList(Window* parent, int id, long additional_style)
} }
void SelectCardList::selectAll() { void SelectCardList::selectAll() {
FOR_EACH_CONST(c, getCards()) { FOR_EACH_CONST(c, set->cards) {
selected.insert(c); selected.insert(c);
} }
Refresh(false); Refresh(false);
...@@ -61,25 +61,25 @@ void SelectCardList::toggle(const CardP& card) { ...@@ -61,25 +61,25 @@ void SelectCardList::toggle(const CardP& card) {
} }
void SelectCardList::onKeyDown(wxKeyEvent& ev) { void SelectCardList::onKeyDown(wxKeyEvent& ev) {
if (selected_card_pos == -1 || !selected_card) { if (selected_item_pos == -1 || !selected_item) {
// no selection // no selection
ev.Skip(); ev.Skip();
return; return;
} }
switch (ev.GetKeyCode()) { switch (ev.GetKeyCode()) {
case WXK_SPACE: { case WXK_SPACE: {
toggle(selected_card); toggle(getCard());
RefreshItem(selected_card_pos); RefreshItem(selected_item_pos);
break; break;
} }
case WXK_NUMPAD_ADD: case '+': { case WXK_NUMPAD_ADD: case '+': {
selected.insert(selected_card); selected.insert(getCard());
RefreshItem(selected_card_pos); RefreshItem(selected_item_pos);
break; break;
} }
case WXK_NUMPAD_SUBTRACT: case '-': { case WXK_NUMPAD_SUBTRACT: case '-': {
selected.erase(selected_card); selected.erase(getCard());
RefreshItem(selected_card_pos); RefreshItem(selected_item_pos);
break; break;
} }
default: default:
......
This diff is collapsed.
...@@ -60,7 +60,7 @@ String tr(const StyleSheet&, const String& key, const String& def); ...@@ -60,7 +60,7 @@ String tr(const StyleSheet&, const String& key, const String& def);
String tr(const SymbolFont&, 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)) #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))
/// A localized string for help/statusbar text /// A localized string for help/statusbar text
#define _HELP_(s) tr(LOCALE_CAT_HELP, _(s)) #define _HELP_(s) tr(LOCALE_CAT_HELP, _(s))
...@@ -81,21 +81,23 @@ String tr(const SymbolFont&, const String& key, const String& def); ...@@ -81,21 +81,23 @@ String tr(const SymbolFont&, const String& key, const String& def);
/// A localized string for error messages /// A localized string for error messages
#define _ERROR_(s) tr(LOCALE_CAT_ERROR, _(s)) #define _ERROR_(s) tr(LOCALE_CAT_ERROR, _(s))
/// A localized string for menus/toolbar buttons, with 1 argument (printf style) /// A localized string for menus, with 1 argument (printf style)
#define _MENU_1_(s,a) format_string(tr(LOCALE_CAT_MENU, _(s)), a) #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) /// 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) /// 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) /// 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) /// 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) /// 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 /// Format a string
/** Equivalent to sprintf / String::Format, but allows strings to be passed as arguments (gcc) /** Equivalent to sprintf / String::Format, but allows strings to be passed as arguments (gcc)
......
...@@ -166,6 +166,12 @@ String singular_form(const String& str) { ...@@ -166,6 +166,12 @@ String singular_form(const String& str) {
return str.substr(0, str.size() - 1); 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 // ----------------------------------------------------------------------------- : Comparing / finding
bool smart_less(const String& as, const String& bs) { bool smart_less(const String& as, const String& bs) {
......
...@@ -118,6 +118,11 @@ String cannocial_name_form(const String&); ...@@ -118,6 +118,11 @@ String cannocial_name_form(const String&);
*/ */
String singular_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 // ----------------------------------------------------------------------------- : Comparing / finding
/// Compare two strings, is the first less than the first? /// Compare two strings, is the first less than the first?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment