mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-11 21:27:01 -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:
+3
-178
@@ -11,10 +11,10 @@
|
||||
#include <gui/control/image_card_list.hpp>
|
||||
#include <gui/control/card_editor.hpp>
|
||||
#include <gui/control/text_ctrl.hpp>
|
||||
#include <gui/control/filter_ctrl.hpp>
|
||||
#include <gui/about_window.hpp> // for HoverButton
|
||||
#include <gui/update_checker.hpp>
|
||||
#include <gui/icon_menu.hpp>
|
||||
#include <gui/drop_down_list.hpp>
|
||||
#include <gui/util.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/game.hpp>
|
||||
@@ -35,177 +35,6 @@ DECLARE_TYPEOF_COLLECTION(AddCardsScriptP);
|
||||
#define HAVE_TOOLBAR_DROPDOWN_MENU 1
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------- : 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&);
|
||||
};
|
||||
|
||||
/// A search/filter textbox
|
||||
class FilterCtrl : public wxControl {
|
||||
public:
|
||||
FilterCtrl(wxWindow* parent, int id);
|
||||
/// Set the filter text
|
||||
void setFilter(const String& filter, bool event = false);
|
||||
void clearFilter(bool event = false) { setFilter(String(),event); }
|
||||
bool hasFilter() const { return !value.empty(); }
|
||||
String const& getFilter() const { return value; }
|
||||
|
||||
//bool AcceptsFocus() const { return false; }
|
||||
private:
|
||||
DECLARE_EVENT_TABLE();
|
||||
bool changing;
|
||||
wxString value;
|
||||
TextCtrlWithFocus* filter_ctrl;
|
||||
HoverButton* clear_button;
|
||||
|
||||
void update();
|
||||
bool hasFocus();
|
||||
// wxWidgets appears to have developed an overload allergy
|
||||
void onChange();
|
||||
void onChangeEvent(wxCommandEvent&);
|
||||
void onClear(wxCommandEvent&);
|
||||
void onSizeEvent(wxSizeEvent&);
|
||||
void onChar(wxKeyEvent&);
|
||||
void onSize();
|
||||
public:
|
||||
void onSetFocus(wxFocusEvent&);
|
||||
void onKillFocus(wxFocusEvent&);
|
||||
};
|
||||
|
||||
FilterCtrl::FilterCtrl(wxWindow* parent, int id)
|
||||
: wxControl(parent, id, wxDefaultPosition, wxSize(160,41), wxSTATIC_BORDER)
|
||||
, changing(false)
|
||||
{
|
||||
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);
|
||||
} else {
|
||||
filter_ctrl->SetValue(_LABEL_("search cards"));
|
||||
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));
|
||||
}
|
||||
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()
|
||||
|
||||
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()
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardsPanel
|
||||
|
||||
CardsPanel::CardsPanel(Window* parent, int id)
|
||||
@@ -387,7 +216,7 @@ void CardsPanel::initUI(wxToolBar* tb, wxMenuBar* mb) {
|
||||
#endif
|
||||
// Filter/search textbox
|
||||
tb->AddSeparator();
|
||||
if (!filter) filter = new FilterCtrl(tb, ID_CARD_FILTER);
|
||||
if (!filter) filter = new FilterCtrl(tb, ID_CARD_FILTER, _LABEL_("search cards"));
|
||||
tb->AddControl(filter);
|
||||
tb->Realize();
|
||||
// Menus
|
||||
@@ -518,11 +347,7 @@ void CardsPanel::onCommand(int id) {
|
||||
}
|
||||
case ID_CARD_FILTER: {
|
||||
// card filter has changed, update the card list
|
||||
if (filter->hasFilter()) {
|
||||
card_list->setFilter(intrusive(new QueryCardListFilter(filter->getFilter())));
|
||||
} else {
|
||||
card_list->setFilter(CardListFilterP());
|
||||
}
|
||||
card_list->setFilter(filter->getFilter<Card>());
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
|
||||
@@ -49,6 +49,7 @@ void KeywordsPanel::initControls() {
|
||||
ref_param = new wxButton(panel, ID_KEYWORD_REF_PARAM, _BUTTON_("refer parameter"));
|
||||
rules = new TextCtrl(panel, ID_RULES, true);
|
||||
errors = new wxStaticText(panel, wxID_ANY, _(""));
|
||||
filter = nullptr;
|
||||
errors->SetForegroundColour(*wxRED);
|
||||
// warning about fixed keywords
|
||||
fixedL = new wxStaticText(panel, wxID_ANY, _(""));
|
||||
@@ -132,6 +133,10 @@ void KeywordsPanel::initUI(wxToolBar* tb, wxMenuBar* mb) {
|
||||
// Toolbar
|
||||
tb->AddTool(ID_KEYWORD_ADD, _(""), load_resource_tool_image(_("keyword_add")), wxNullBitmap, wxITEM_NORMAL,_TOOLTIP_("add keyword"), _HELP_("add keyword"));
|
||||
tb->AddTool(ID_KEYWORD_REMOVE, _(""), load_resource_tool_image(_("keyword_del")), wxNullBitmap, wxITEM_NORMAL,_TOOLTIP_("remove keyword"),_HELP_("remove keyword"));
|
||||
// Filter/search textbox
|
||||
tb->AddSeparator();
|
||||
if (!filter) filter = new FilterCtrl(tb, ID_KEYWORD_FILTER, _LABEL_("search keywords"));
|
||||
tb->AddControl(filter);
|
||||
tb->Realize();
|
||||
// Menus
|
||||
mb->Insert(2, menuKeyword, _MENU_("keywords"));
|
||||
@@ -141,6 +146,9 @@ void KeywordsPanel::destroyUI(wxToolBar* tb, wxMenuBar* mb) {
|
||||
// Toolbar
|
||||
tb->DeleteTool(ID_KEYWORD_ADD);
|
||||
tb->DeleteTool(ID_KEYWORD_REMOVE);
|
||||
tb->DeleteTool(filter->GetId()); filter = nullptr;
|
||||
// HACK: hardcoded size of rest of toolbar
|
||||
tb->DeleteToolByPos(12); // delete separator
|
||||
// Menus
|
||||
mb->Remove(2);
|
||||
|
||||
@@ -203,6 +211,11 @@ void KeywordsPanel::onCommand(int id) {
|
||||
ref_param->PopupMenu(&ref_menu, 0, ref_param->GetSize().y);
|
||||
break;
|
||||
}
|
||||
case ID_KEYWORD_FILTER: {
|
||||
// keyword filter has changed, update the list
|
||||
list->setFilter(filter->getFilter<Keyword>());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (id >= ID_PARAM_TYPE_MIN && id < ID_PARAM_TYPE_MAX) {
|
||||
// add parameter
|
||||
|
||||
@@ -11,12 +11,14 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <gui/set/panel.hpp>
|
||||
#include <gui/control/filter_ctrl.hpp>
|
||||
|
||||
class wxSplitterWindow;
|
||||
class KeywordList;
|
||||
class TextCtrl;
|
||||
class IconMenu;
|
||||
struct KeywordSelectEvent;
|
||||
class FilterCtrl;
|
||||
|
||||
// ----------------------------------------------------------------------------- : KeywordsPanel
|
||||
|
||||
@@ -69,6 +71,7 @@ class KeywordsPanel : public SetWindowPanel {
|
||||
wxChoice* mode;
|
||||
wxButton* add_param;
|
||||
wxButton* ref_param;
|
||||
FilterCtrl* filter;
|
||||
|
||||
// --------------------------------------------------- : Events
|
||||
void onKeywordSelect(KeywordSelectEvent& ev);
|
||||
|
||||
@@ -530,7 +530,7 @@ void StatsPanel::onGraphSelect(wxCommandEvent&) {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Filtering card list
|
||||
|
||||
class StatsFilter : public CardListFilter {
|
||||
class StatsFilter : public Filter<Card> {
|
||||
public:
|
||||
StatsFilter(GraphData& data, const vector<int> match) {
|
||||
data.indices(match, indices);
|
||||
|
||||
Reference in New Issue
Block a user