mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-11 13:17:00 -04:00
Added filter box for keywords
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1615 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2010 Twan van Laarhoven and Sean Hunt |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <gui/control/filter_ctrl.hpp>
|
||||
#include <gui/about_window.hpp> // for HoverButton
|
||||
#include <gui/drop_down_list.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : DropDownMRUList
|
||||
|
||||
/// A drop down list of recent choices, for autocomplete
|
||||
class DropDownMRUList : public DropDownList {
|
||||
public:
|
||||
DropDownMRUList(Window* parent, vector<String> const& choices)
|
||||
: DropDownList(parent)
|
||||
, choices(choices)
|
||||
{}
|
||||
|
||||
vector<String> choices;
|
||||
|
||||
protected:
|
||||
virtual size_t selection() const { return NO_SELECTION; }
|
||||
virtual size_t itemCount() const { return choices.size(); }
|
||||
virtual String itemText(size_t item) const { return choices.at(item); }
|
||||
virtual void select(size_t item);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : FilterControl
|
||||
|
||||
/// Text control that forwards focus events to the parent
|
||||
class TextCtrlWithFocus : public wxTextCtrl {
|
||||
public:
|
||||
DECLARE_EVENT_TABLE();
|
||||
void forwardFocusEvent(wxFocusEvent&);
|
||||
void forwardKeyEvent(wxKeyEvent&);
|
||||
};
|
||||
|
||||
FilterCtrl::FilterCtrl(wxWindow* parent, int id, String const& placeholder)
|
||||
: wxControl(parent, id, wxDefaultPosition, wxSize(160,41), wxSTATIC_BORDER)
|
||||
, changing(false)
|
||||
, placeholder(placeholder)
|
||||
{
|
||||
wxColour bg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
|
||||
SetBackgroundColour(bg);
|
||||
SetCursor(wxCURSOR_IBEAM);
|
||||
filter_ctrl = new TextCtrlWithFocus();
|
||||
filter_ctrl->Create(this, wxID_ANY, _(""), wxDefaultPosition, wxSize(130,-1), wxNO_BORDER);
|
||||
clear_button = new HoverButton(this, wxID_ANY, _("btn_clear_filter"), bg, false);
|
||||
clear_button->SetCursor(*wxSTANDARD_CURSOR);
|
||||
onSize();
|
||||
update();
|
||||
}
|
||||
|
||||
void FilterCtrl::setFilter(const String& new_value, bool event) {
|
||||
if (this->value == new_value) return;
|
||||
// update ui
|
||||
this->value = new_value;
|
||||
update();
|
||||
// send event
|
||||
if (event) {
|
||||
wxCommandEvent ev(wxEVT_COMMAND_TEXT_UPDATED, GetId());
|
||||
GetParent()->HandleWindowEvent(ev);
|
||||
}
|
||||
}
|
||||
|
||||
void FilterCtrl::update() {
|
||||
changing = true;
|
||||
if (!value.empty() || hasFocus()) {
|
||||
filter_ctrl->SetValue(value);
|
||||
wxColour fg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
|
||||
filter_ctrl->SetDefaultStyle(wxTextAttr(fg));
|
||||
filter_ctrl->SetForegroundColour(fg);
|
||||
wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
|
||||
filter_ctrl->SetFont(font);
|
||||
} else {
|
||||
filter_ctrl->SetValue(placeholder);
|
||||
wxColour fg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
|
||||
wxColour bg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
|
||||
filter_ctrl->SetDefaultStyle(wxTextAttr(lerp(fg,bg,0.5)));
|
||||
filter_ctrl->SetForegroundColour(lerp(fg,bg,0.5));
|
||||
wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
|
||||
font.SetStyle(wxFONTSTYLE_ITALIC);
|
||||
filter_ctrl->SetFont(font);
|
||||
}
|
||||
clear_button->Show(!value.empty());
|
||||
changing = false;
|
||||
}
|
||||
|
||||
void FilterCtrl::onChangeEvent(wxCommandEvent&) {
|
||||
if (!changing) {
|
||||
setFilter(filter_ctrl->GetValue(),true);
|
||||
}
|
||||
}
|
||||
void FilterCtrl::onChar(wxKeyEvent& ev) {
|
||||
if (ev.GetKeyCode() == WXK_ESCAPE) {
|
||||
// escape clears the filter box
|
||||
clearFilter(true);
|
||||
} else {
|
||||
ev.Skip();
|
||||
}
|
||||
}
|
||||
|
||||
void FilterCtrl::onClear(wxCommandEvent&) {
|
||||
clearFilter(true);
|
||||
}
|
||||
|
||||
void FilterCtrl::onSizeEvent(wxSizeEvent&) {
|
||||
onSize();
|
||||
}
|
||||
void FilterCtrl::onSize() {
|
||||
wxSize s = GetClientSize();
|
||||
wxSize fs = filter_ctrl->GetBestSize();
|
||||
wxSize cs = clear_button->GetBestSize();
|
||||
int margin = 2;
|
||||
filter_ctrl ->SetSize(margin, max(margin,(s.y-fs.y)/2), s.x - cs.x - 3*margin, fs.y);
|
||||
clear_button->SetSize(s.x - cs.x - margin, (s.y-cs.y)/2, cs.x, cs.y);
|
||||
}
|
||||
|
||||
void FilterCtrl::onSetFocus(wxFocusEvent&) {
|
||||
filter_ctrl->SetFocus();
|
||||
update();
|
||||
}
|
||||
void FilterCtrl::onKillFocus(wxFocusEvent&) {
|
||||
update();
|
||||
}
|
||||
|
||||
bool FilterCtrl::hasFocus() {
|
||||
wxWindow* focus = wxWindow::FindFocus();
|
||||
return focus == this || focus == filter_ctrl || focus == clear_button;
|
||||
}
|
||||
|
||||
BEGIN_EVENT_TABLE(FilterCtrl, wxControl)
|
||||
EVT_BUTTON (wxID_ANY, FilterCtrl::onClear)
|
||||
EVT_TEXT (wxID_ANY, FilterCtrl::onChangeEvent)
|
||||
EVT_SIZE (FilterCtrl::onSizeEvent)
|
||||
EVT_SET_FOCUS (FilterCtrl::onSetFocus)
|
||||
EVT_KILL_FOCUS(FilterCtrl::onKillFocus)
|
||||
EVT_CHAR (FilterCtrl::onChar)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
// ----------------------------------------------------------------------------- : TextCtrlWithFocus
|
||||
|
||||
void TextCtrlWithFocus::forwardFocusEvent(wxFocusEvent& ev) {
|
||||
GetParent()->HandleWindowEvent(ev);
|
||||
}
|
||||
void TextCtrlWithFocus::forwardKeyEvent(wxKeyEvent& ev) {
|
||||
GetParent()->HandleWindowEvent(ev);
|
||||
}
|
||||
|
||||
BEGIN_EVENT_TABLE(TextCtrlWithFocus, wxTextCtrl)
|
||||
EVT_SET_FOCUS (TextCtrlWithFocus::forwardFocusEvent)
|
||||
EVT_KILL_FOCUS(TextCtrlWithFocus::forwardFocusEvent)
|
||||
EVT_CHAR (TextCtrlWithFocus::forwardKeyEvent)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2010 Twan van Laarhoven and Sean Hunt |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
#ifndef HEADER_GUI_CONTROL_FILTER_CTRL
|
||||
#define HEADER_GUI_CONTROL_FILTER_CTRL
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <data/filter.hpp>
|
||||
|
||||
class HoverButton;
|
||||
class TextCtrlWithFocus;
|
||||
|
||||
// ----------------------------------------------------------------------------- : FilterCtrl
|
||||
|
||||
/// A search/filter textbox
|
||||
class FilterCtrl : public wxControl {
|
||||
public:
|
||||
FilterCtrl(wxWindow* parent, int id, String const& placeholder);
|
||||
|
||||
/// Set the filter text
|
||||
void setFilter(const String& filter, bool send_event = false);
|
||||
void clearFilter(bool send_event = false) { setFilter(String(),send_event); }
|
||||
bool hasFilter() const { return !value.empty(); }
|
||||
String const& getFilterString() const { return value; }
|
||||
|
||||
template <typename T>
|
||||
intrusive_ptr<Filter<T> > getFilter() const {
|
||||
if (hasFilter()) {
|
||||
return intrusive(new QuickFilter<T>(getFilterString()));
|
||||
} else {
|
||||
return intrusive_ptr<Filter<T> >();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
DECLARE_EVENT_TABLE();
|
||||
bool changing;
|
||||
String value;
|
||||
String placeholder;
|
||||
TextCtrlWithFocus* filter_ctrl;
|
||||
HoverButton* clear_button;
|
||||
|
||||
void update();
|
||||
bool hasFocus();
|
||||
// wxWidgets appears to have developed an overload allergy
|
||||
void onChangeEvent(wxCommandEvent&);
|
||||
void onClear(wxCommandEvent&);
|
||||
void onSizeEvent(wxSizeEvent&);
|
||||
void onChar(wxKeyEvent&);
|
||||
void onSize();
|
||||
void onSetFocus(wxFocusEvent&);
|
||||
void onKillFocus(wxFocusEvent&);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
@@ -33,18 +33,3 @@ void FilteredCardList::getItems(vector<VoidP>& out) const {
|
||||
filter->getItems(set->cards,out);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListFilter
|
||||
|
||||
void CardListFilter::getItems(const vector<CardP>& cards, vector<VoidP>& out) const {
|
||||
FOR_EACH_CONST(c, cards) {
|
||||
if (keep(c)) {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool QueryCardListFilter::keep(const CardP& card) const {
|
||||
return card->contains_words(query);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,29 +11,9 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <gui/control/card_list.hpp>
|
||||
#include <data/filter.hpp>
|
||||
|
||||
DECLARE_POINTER_TYPE(CardListFilter);
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListFilter
|
||||
|
||||
/// A filter function to determine which items are shown in a card list
|
||||
class CardListFilter : public IntrusivePtrVirtualBase {
|
||||
public:
|
||||
virtual ~CardListFilter() {}
|
||||
/// Should a card be shown in the list?
|
||||
virtual bool keep(const CardP& card) const { return false; }
|
||||
/// Select cards from a card list
|
||||
virtual void getItems(const vector<CardP>& cards, vector<VoidP>& out) const;
|
||||
};
|
||||
|
||||
/// A filter function that searches for cards containing a string
|
||||
class QueryCardListFilter : public CardListFilter {
|
||||
public:
|
||||
QueryCardListFilter(String const& query) : query(query) {}
|
||||
virtual bool keep(const CardP& card) const;
|
||||
private:
|
||||
String query;
|
||||
};
|
||||
typedef intrusive_ptr<Filter<Card> > CardListFilterP;
|
||||
|
||||
// ----------------------------------------------------------------------------- : FilteredCardList
|
||||
|
||||
|
||||
@@ -56,6 +56,11 @@ void KeywordList::onChangeSet() {
|
||||
refreshList();
|
||||
}
|
||||
|
||||
void KeywordList::setFilter(const KeywordListFilterP& filter) {
|
||||
this->filter = filter;
|
||||
refreshList();
|
||||
}
|
||||
|
||||
void KeywordList::onAction(const Action& action, bool undone) {
|
||||
TYPE_CASE(action, AddKeywordAction) {
|
||||
if (action.action.adding != undone) {
|
||||
@@ -151,11 +156,15 @@ String match_string(const Keyword& a) {
|
||||
void KeywordList::getItems(vector<VoidP>& out) const {
|
||||
FOR_EACH(k, set->keywords) {
|
||||
k->fixed = false;
|
||||
out.push_back(k);
|
||||
if (!filter || filter->keep(*k)) {
|
||||
out.push_back(k);
|
||||
}
|
||||
}
|
||||
FOR_EACH(k, set->game->keywords) {
|
||||
k->fixed = true;
|
||||
out.push_back(k);
|
||||
if (!filter || filter->keep(*k)) {
|
||||
out.push_back(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
void KeywordList::sendEvent() {
|
||||
|
||||
@@ -12,8 +12,11 @@
|
||||
#include <util/prec.hpp>
|
||||
#include <gui/control/item_list.hpp>
|
||||
#include <data/keyword.hpp>
|
||||
#include <data/filter.hpp>
|
||||
#include <data/set.hpp>
|
||||
|
||||
typedef intrusive_ptr<Filter<Keyword> > KeywordListFilterP;
|
||||
|
||||
// ----------------------------------------------------------------------------- : Events
|
||||
|
||||
DECLARE_EVENT_TYPE(EVENT_KEYWORD_SELECT, <not used>)
|
||||
@@ -51,6 +54,9 @@ class KeywordList : public ItemList, public SetView {
|
||||
inline KeywordP getKeyword() const { return static_pointer_cast<Keyword>(selected_item); }
|
||||
inline void setKeyword(const KeywordP& kw) { selectItem(kw, true, false); }
|
||||
|
||||
/// Change the filter to use, can be null
|
||||
void setFilter(const KeywordListFilterP& filter);
|
||||
|
||||
// --------------------------------------------------- : Clipboard
|
||||
|
||||
bool canDelete() const;
|
||||
@@ -87,6 +93,7 @@ class KeywordList : public ItemList, public SetView {
|
||||
void storeColumns();
|
||||
|
||||
mutable wxListItemAttr item_attr; // for OnGetItemAttr
|
||||
KeywordListFilterP filter; ///< Which keywords to show?
|
||||
|
||||
/// How often is a keyword used in the set?
|
||||
int usage(const Keyword&) const;
|
||||
|
||||
Reference in New Issue
Block a user