From 0fbd417057756aa8e2be0b70aebd6336ea997dc8 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 22 Mar 2007 22:50:01 +0000 Subject: [PATCH] (partially working) keyword list git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@230 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/control/card_list.cpp | 9 +-- src/gui/control/item_list.cpp | 7 ++- src/gui/control/item_list.hpp | 2 +- src/gui/control/keyword_list.cpp | 95 ++++++++++++++++++++++++++++++++ src/gui/control/keyword_list.hpp | 77 ++++++++++++++++++++++++++ src/gui/set/keywords_panel.cpp | 40 ++++---------- src/gui/set/keywords_panel.hpp | 3 + 7 files changed, 193 insertions(+), 40 deletions(-) create mode 100644 src/gui/control/keyword_list.cpp create mode 100644 src/gui/control/keyword_list.hpp diff --git a/src/gui/control/card_list.cpp b/src/gui/control/card_list.cpp index f9e1421b..3dacc134 100644 --- a/src/gui/control/card_list.cpp +++ b/src/gui/control/card_list.cpp @@ -167,13 +167,8 @@ 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() ); - } + if (!va || !vb) return va < vb; // got to do something, compare pointers + return smart_less( va->toString() , vb->toString() ); } void CardListBase::rebuild() { diff --git a/src/gui/control/item_list.cpp b/src/gui/control/item_list.cpp index 11c12a1a..cc4ebbe5 100644 --- a/src/gui/control/item_list.cpp +++ b/src/gui/control/item_list.cpp @@ -93,7 +93,11 @@ struct ItemList::ItemComparer { 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()); + if (list.sort_ascending) { + return list.compareItems(a.get(), b.get()); + } else { + return list.compareItems(b.get(), a.get()); + } } }; @@ -124,7 +128,6 @@ void ItemList::sortBy(long column, bool ascending) { } else if (i == sort_by_column) { ClearColumnImage(i); } - ++i; } // sort list sort_by_column = column; diff --git a/src/gui/control/item_list.hpp b/src/gui/control/item_list.hpp index 9bf930c7..401e9437 100644 --- a/src/gui/control/item_list.hpp +++ b/src/gui/control/item_list.hpp @@ -45,7 +45,7 @@ class ItemList : public wxListView { /// Is sorting required? virtual bool mustSort() const { return false; } - /// Compare two items for < + /// Compare two items for < based on sort_by_column (not on sort_ascending) virtual bool compareItems(void* a, void* b) const = 0; // --------------------------------------------------- : Protected interface diff --git a/src/gui/control/keyword_list.cpp b/src/gui/control/keyword_list.cpp new file mode 100644 index 00000000..ea86c50f --- /dev/null +++ b/src/gui/control/keyword_list.cpp @@ -0,0 +1,95 @@ +//+----------------------------------------------------------------------------+ +//| 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 +#include +#include + +DECLARE_TYPEOF_COLLECTION(KeywordP); + +// ----------------------------------------------------------------------------- : Events + +DEFINE_EVENT_TYPE(EVENT_KEYWORD_SELECT); + +// ----------------------------------------------------------------------------- : KeywordList + +KeywordList::KeywordList(Window* parent, int id, long additional_style) + : ItemList(parent, id, additional_style) +{ + // Add columns + InsertColumn(0, _LABEL_("keyword"), wxLIST_FORMAT_LEFT, 100); + InsertColumn(1, _LABEL_("match"), wxLIST_FORMAT_LEFT, 200); + InsertColumn(2, _LABEL_("mode"), wxLIST_FORMAT_LEFT, 100); + InsertColumn(3, _LABEL_("uses"), wxLIST_FORMAT_RIGHT, 80); + InsertColumn(4, _LABEL_("reminder"), wxLIST_FORMAT_LEFT, 300); +} + +KeywordList::~KeywordList() { + storeColumns(); +} +void KeywordList::storeColumns() { + // TODO +} + +void KeywordList::onBeforeChangeSet() { + storeColumns(); +} +void KeywordList::onChangeSet() { + refreshList(); +} + +void KeywordList::onAction(const Action& action, bool undone) { + //TYPE_CASE(action, AddKeywordAction) { + //} +} + +// ----------------------------------------------------------------------------- : KeywordListBase : for ItemList + +void KeywordList::getItems(vector& out) const { + FOR_EACH(k, set->keywords) { + out.push_back(k); + } + FOR_EACH(k, set->game->keywords) { + out.push_back(k); + } +} +void KeywordList::sendEvent() { + KeywordSelectEvent ev(getKeyword()); + ProcessEvent(ev); +} +bool KeywordList::compareItems(void* a, void* b) const { + const Keyword& ka = *(Keyword*)a; + const Keyword& kb = *(Keyword*)b; + switch(sort_by_column) { + case 0: return ka.keyword < kb.keyword; + case 1: return ka.match < kb.match; + case 2: return ka.mode < kb.mode; + //case 3: + //case 4: + default: // TODO: 3 and 4 + return ka.keyword < kb.keyword; + } +} + +// ----------------------------------------------------------------------------- : KeywordListBase : Item text + +String KeywordList::OnGetItemText (long pos, long col) const { + const Keyword& kw = *getKeyword(pos); + switch(col) { + case 0: return kw.keyword; + case 1: return kw.match; + case 2: return kw.mode; + case 3: return _("TODO"); + case 4: return _("TODO"); + default: return wxEmptyString; + } +} +int KeywordList::OnGetItemImage(long pos) const { + return -1; +} diff --git a/src/gui/control/keyword_list.hpp b/src/gui/control/keyword_list.hpp new file mode 100644 index 00000000..096b1d58 --- /dev/null +++ b/src/gui/control/keyword_list.hpp @@ -0,0 +1,77 @@ +//+----------------------------------------------------------------------------+ +//| 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_KEYWORD_LIST +#define HEADER_GUI_CONTROL_KEYWORD_LIST + +// ----------------------------------------------------------------------------- : Includes + +#include +#include +#include +#include + +// ----------------------------------------------------------------------------- : Events + +DECLARE_EVENT_TYPE(EVENT_KEYWORD_SELECT, ) +/// Handle KeywordSelectEvents +#define EVT_KEYWORD_SELECT(id, handler) \ + DECLARE_EVENT_TABLE_ENTRY(EVENT_KEYWORD_SELECT, id, -1, \ + (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) \ + (void (wxEvtHandler::*)(KeywordSelectEvent&)) (&handler), (wxObject*) NULL), + +/// The event of selecting a keyword +struct KeywordSelectEvent : public wxCommandEvent { + KeywordP keyword; ///< The selected keyword + inline KeywordSelectEvent(const KeywordP& keyword) + : wxCommandEvent(EVENT_KEYWORD_SELECT), keyword(keyword) + {} +}; + +// ----------------------------------------------------------------------------- : KeywordList + +/// A control that lists the keywords in a set and its game +class KeywordList : public ItemList, public SetView { + public: + KeywordList(Window* parent, int id, long additional_style = 0); + ~KeywordList(); + + // --------------------------------------------------- : Set stuff + + virtual void onBeforeChangeSet(); + virtual void onChangeSet(); + virtual void onAction(const Action&, bool); + + // --------------------------------------------------- : Selection + + inline KeywordP getKeyword() const { return static_pointer_cast(selected_item); } + inline void setKeyword(const KeywordP& kw) { selectItem(kw, true, false); } + + // --------------------------------------------------- : The keywords + protected: + /// Get a list of all keywords + virtual void getItems(vector& out) const; + /// Return the keyword at the given position in the sorted keyword list + inline KeywordP getKeyword(long pos) const { return static_pointer_cast(getItem(pos)); } + + /// 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; + + /// Get the text of an item in a specific column + /** Overrides a function from wxListCtrl */ + virtual String OnGetItemText (long pos, long col) const; + /// Get the image of an item, by default no image is used + /** Overrides a function from wxListCtrl */ + virtual int OnGetItemImage(long pos) const; + + private: + void storeColumns(); +}; + +// ----------------------------------------------------------------------------- : EOF +#endif diff --git a/src/gui/set/keywords_panel.cpp b/src/gui/set/keywords_panel.cpp index b2296765..5657c820 100644 --- a/src/gui/set/keywords_panel.cpp +++ b/src/gui/set/keywords_panel.cpp @@ -7,48 +7,28 @@ // ----------------------------------------------------------------------------- : Includes #include +#include #include #include -// ----------------------------------------------------------------------------- : KeywordsList - -/// A control that lists the keywords in a set or game -class KeywordList : public wxListView { - public: - KeywordList(Window* parent, int id); - - /// Set the list of keywords to show - void setData(vector& dat); - - // --------------------------------------------------- : Selection - - inline KeywordP getKeyword() const { return selected_keyword; } - inline void setKeyword(const KeywordP& kw) { /* TODO */ } - - bool canSelectPrevious() const; - bool canSelectNext() const; - void selectPrevious(); - void selectNext(); - - protected: - /// Get the text of an item in a specific column - /** Overrides a function from wxListCtrl */ - virtual String OnGetItemText (long pos, long col) const; - private: - KeywordP selected_keyword; - long selected_keyword_pos; -}; - // ----------------------------------------------------------------------------- : KeywordsPanel KeywordsPanel::KeywordsPanel(Window* parent, int id) : SetWindowPanel(parent, id) { + // init controls + list = new KeywordList(this, wxID_ANY); + // init sizer wxSizer* s = new wxBoxSizer(wxHORIZONTAL); - s->Add(new wxStaticText(this, wxID_ANY, _("Sorry, no keywords for now"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTER), 1, wxALIGN_CENTER); // TODO: Remove + s->Add(list, 1, wxEXPAND); + //s->Add(new wxStaticText(this, wxID_ANY, _("Sorry, no keywords for now"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTER), 1, wxALIGN_CENTER); // TODO: Remove /* wxSizer* s2 = new wxBoxSizer(wxVERTICAL); s2->Add(list_active, 1, wxEXPAND); s2->Add(list_inactive, 1, wxEXPAND);*/ s->SetSizeHints(this); SetSizer(s); } + +void KeywordsPanel::onChangeSet() { + list->setSet(set); +} \ No newline at end of file diff --git a/src/gui/set/keywords_panel.hpp b/src/gui/set/keywords_panel.hpp index 2a759d56..94a07e7a 100644 --- a/src/gui/set/keywords_panel.hpp +++ b/src/gui/set/keywords_panel.hpp @@ -20,6 +20,9 @@ class KeywordList; class KeywordsPanel : public SetWindowPanel { public: KeywordsPanel(Window* parent, int id); + + virtual void onChangeSet(); + private: KeywordList* list; };