menu: make Item more operational

This commit is contained in:
Andrzej Rybczak
2012-08-30 20:36:15 +02:00
parent 74f0864824
commit 0ba847dc3c
2 changed files with 55 additions and 43 deletions

View File

@@ -28,9 +28,9 @@ template <> std::string Menu<std::string>::GetItem(size_t pos)
if (m_options_ptr->at(pos)) if (m_options_ptr->at(pos))
{ {
if (m_get_string_helper) if (m_get_string_helper)
result = m_get_string_helper((*m_options_ptr)[pos]->Value); result = m_get_string_helper((*m_options_ptr)[pos]->value());
else else
result = (*m_options_ptr)[pos]->Value; result = (*m_options_ptr)[pos]->value();
} }
return result; return result;
} }

View File

@@ -120,14 +120,26 @@ template <typename T> struct Menu : public Window, public List
/// ///
struct Item struct Item
{ {
Item() : isBold(0), isSelected(0), isStatic(0) { } Item() : m_is_bold(false), m_is_selected(false), m_is_inactive(false) { }
Item(const T &t, bool is_bold, bool is_static) : Item(const T &value_, bool is_bold, bool is_inactive)
Value(t), isBold(is_bold), isSelected(0), isStatic(is_static) { } : m_value(value_), m_is_bold(is_bold), m_is_selected(false), m_is_inactive(is_inactive) { }
T Value; T &value() { return m_value; }
bool isBold; const T &value() const { return m_value; }
bool isSelected;
bool isStatic; void setBold(bool is_bold) { m_is_bold = is_bold; }
void setSelected(bool is_selected) { m_is_selected = is_selected; }
void setInactive(bool is_inactive) { m_is_inactive = is_inactive; }
bool isBold() const { return m_is_bold; }
bool isSelected() const { return m_is_selected; }
bool isInactive() const { return m_is_inactive; }
private:
T m_value;
bool m_is_bold;
bool m_is_selected;
bool m_is_inactive;
}; };
template <typename ValueT, typename BaseIterator> class ItemIterator template <typename ValueT, typename BaseIterator> class ItemIterator
@@ -145,7 +157,7 @@ template <typename T> struct Menu : public Window, public List
>::value; >::value;
template <typename Result, bool referenceValue> struct getObject { }; template <typename Result, bool referenceValue> struct getObject { };
template <typename Result> struct getObject<Result, true> { template <typename Result> struct getObject<Result, true> {
static Result &apply(BaseIterator it) { return (*it)->Value; } static Result &apply(BaseIterator it) { return (*it)->value(); }
}; };
template <typename Result> struct getObject<Result, false> { template <typename Result> struct getObject<Result, false> {
static Result &apply(BaseIterator it) { return **it; } static Result &apply(BaseIterator it) { return **it; }
@@ -725,7 +737,7 @@ template <typename T> void Menu<T>::Bold(int pos, bool state)
{ {
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return; return;
(*m_options_ptr)[pos]->isBold = state; (*m_options_ptr)[pos]->setBold(state);
} }
template <typename T> void Menu<T>::Swap(size_t one, size_t two) template <typename T> void Menu<T>::Swap(size_t one, size_t two)
@@ -750,7 +762,7 @@ template <typename T> void Menu<T>::Move(size_t from, size_t to)
template <typename T> bool Menu<T>::Goto(size_t y) template <typename T> bool Menu<T>::Goto(size_t y)
{ {
if (!m_options_ptr->at(itsBeginning+y) || m_options_ptr->at(itsBeginning+y)->isStatic) if (!m_options_ptr->at(itsBeginning+y) || m_options_ptr->at(itsBeginning+y)->isInactive())
return false; return false;
itsHighlight = itsBeginning+y; itsHighlight = itsBeginning+y;
return true; return true;
@@ -776,10 +788,10 @@ template <typename T> void Menu<T>::Refresh()
if (!m_options_ptr->empty() && itsHighlight > int(m_options_ptr->size())-1) if (!m_options_ptr->empty() && itsHighlight > int(m_options_ptr->size())-1)
itsHighlight = m_options_ptr->size()-1; itsHighlight = m_options_ptr->size()-1;
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) // it shouldn't be here if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive()) // it shouldn't be here
{ {
Scroll(wUp); Scroll(wUp);
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
Scroll(wDown); Scroll(wDown);
} }
@@ -798,7 +810,7 @@ template <typename T> void Menu<T>::Refresh()
mvwhline(itsWindow, line++, 0, 0, itsWidth); mvwhline(itsWindow, line++, 0, 0, itsWidth);
continue; continue;
} }
if ((*m_options_ptr)[i]->isBold) if ((*m_options_ptr)[i]->isBold())
*this << fmtBold; *this << fmtBold;
if (m_highlight_enabled && int(i) == itsHighlight) if (m_highlight_enabled && int(i) == itsHighlight)
{ {
@@ -806,18 +818,18 @@ template <typename T> void Menu<T>::Refresh()
*this << m_highlight_color; *this << m_highlight_color;
} }
mvwhline(itsWindow, line, 0, 32, itsWidth); mvwhline(itsWindow, line, 0, 32, itsWidth);
if ((*m_options_ptr)[i]->isSelected && m_selected_prefix) if ((*m_options_ptr)[i]->isSelected() && m_selected_prefix)
*this << *m_selected_prefix; *this << *m_selected_prefix;
if (m_item_displayer) if (m_item_displayer)
m_item_displayer(*this, (*m_options_ptr)[i]->Value); m_item_displayer(*this, (*m_options_ptr)[i]->value());
if ((*m_options_ptr)[i]->isSelected && m_selected_suffix) if ((*m_options_ptr)[i]->isSelected() && m_selected_suffix)
*this << *m_selected_suffix; *this << *m_selected_suffix;
if (m_highlight_enabled && int(i) == itsHighlight) if (m_highlight_enabled && int(i) == itsHighlight)
{ {
*this << clEnd; *this << clEnd;
*this << fmtReverseEnd; *this << fmtReverseEnd;
} }
if ((*m_options_ptr)[i]->isBold) if ((*m_options_ptr)[i]->isBold())
*this << fmtBoldEnd; *this << fmtBoldEnd;
line++; line++;
} }
@@ -849,7 +861,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
{ {
itsHighlight--; itsHighlight--;
} }
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == 0 && !m_cyclic_scroll_enabled ? wDown : wUp); Scroll(itsHighlight == 0 && !m_cyclic_scroll_enabled ? wDown : wUp);
} }
@@ -871,7 +883,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
{ {
itsHighlight++; itsHighlight++;
} }
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == MaxHighlight && !m_cyclic_scroll_enabled ? wUp : wDown); Scroll(itsHighlight == MaxHighlight && !m_cyclic_scroll_enabled ? wUp : wDown);
} }
@@ -889,7 +901,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
if (itsHighlight < 0) if (itsHighlight < 0)
itsHighlight = 0; itsHighlight = 0;
} }
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == 0 && !m_cyclic_scroll_enabled ? wDown : wUp); Scroll(itsHighlight == 0 && !m_cyclic_scroll_enabled ? wDown : wUp);
} }
@@ -907,7 +919,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
if (itsHighlight > MaxHighlight) if (itsHighlight > MaxHighlight)
itsHighlight = MaxHighlight; itsHighlight = MaxHighlight;
} }
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == MaxHighlight && !m_cyclic_scroll_enabled ? wUp : wDown); Scroll(itsHighlight == MaxHighlight && !m_cyclic_scroll_enabled ? wUp : wDown);
} }
@@ -917,7 +929,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
{ {
itsHighlight = 0; itsHighlight = 0;
itsBeginning = 0; itsBeginning = 0;
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == 0 ? wDown : wUp); Scroll(itsHighlight == 0 ? wDown : wUp);
} }
@@ -927,7 +939,7 @@ template <typename T> void Menu<T>::Scroll(Where where)
{ {
itsHighlight = MaxHighlight; itsHighlight = MaxHighlight;
itsBeginning = MaxBeginning; itsBeginning = MaxBeginning;
if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isStatic) if (!(*m_options_ptr)[itsHighlight] || (*m_options_ptr)[itsHighlight]->isInactive())
{ {
Scroll(itsHighlight == MaxHighlight ? wUp : wDown); Scroll(itsHighlight == MaxHighlight ? wUp : wDown);
} }
@@ -968,21 +980,21 @@ template <typename T> bool Menu<T>::isBold(int pos)
pos = pos == -1 ? itsHighlight : pos; pos = pos == -1 ? itsHighlight : pos;
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return 0; return 0;
return (*m_options_ptr)[pos]->isBold; return (*m_options_ptr)[pos]->isBold();
} }
template <typename T> void Menu<T>::Select(int pos, bool state) template <typename T> void Menu<T>::Select(int pos, bool state)
{ {
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return; return;
(*m_options_ptr)[pos]->isSelected = state; (*m_options_ptr)[pos]->setSelected(state);
} }
template <typename T> void Menu<T>::Static(int pos, bool state) template <typename T> void Menu<T>::Static(int pos, bool state)
{ {
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return; return;
(*m_options_ptr)[pos]->isStatic = state; (*m_options_ptr)[pos]->setInactive(state);
} }
template <typename T> bool Menu<T>::isSelected(int pos) const template <typename T> bool Menu<T>::isSelected(int pos) const
@@ -990,7 +1002,7 @@ template <typename T> bool Menu<T>::isSelected(int pos) const
pos = pos == -1 ? itsHighlight : pos; pos = pos == -1 ? itsHighlight : pos;
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return 0; return 0;
return (*m_options_ptr)[pos]->isSelected; return (*m_options_ptr)[pos]->isSelected();
} }
template <typename T> bool Menu<T>::isStatic(int pos) const template <typename T> bool Menu<T>::isStatic(int pos) const
@@ -998,7 +1010,7 @@ template <typename T> bool Menu<T>::isStatic(int pos) const
pos = pos == -1 ? itsHighlight : pos; pos = pos == -1 ? itsHighlight : pos;
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
return 1; return 1;
return (*m_options_ptr)[pos]->isStatic; return (*m_options_ptr)[pos]->isInactive();
} }
template <typename T> bool Menu<T>::isSeparator(int pos) const template <typename T> bool Menu<T>::isSeparator(int pos) const
@@ -1010,7 +1022,7 @@ template <typename T> bool Menu<T>::isSeparator(int pos) const
template <typename T> bool Menu<T>::hasSelected() const template <typename T> bool Menu<T>::hasSelected() const
{ {
for (auto it = m_options_ptr->begin(); it != m_options_ptr->end(); ++it) for (auto it = m_options_ptr->begin(); it != m_options_ptr->end(); ++it)
if (*it && (*it)->isSelected) if (*it && (*it)->isSelected())
return true; return true;
return false; return false;
} }
@@ -1018,7 +1030,7 @@ template <typename T> bool Menu<T>::hasSelected() const
template <typename T> void Menu<T>::GetSelected(std::vector<size_t> &v) const template <typename T> void Menu<T>::GetSelected(std::vector<size_t> &v) const
{ {
for (size_t i = 0; i < m_options_ptr->size(); ++i) for (size_t i = 0; i < m_options_ptr->size(); ++i)
if ((*m_options_ptr)[i] && (*m_options_ptr)[i]->isSelected) if ((*m_options_ptr)[i] && (*m_options_ptr)[i]->isSelected())
v.push_back(i); v.push_back(i);
} }
@@ -1042,7 +1054,7 @@ template <typename T> size_t Menu<T>::RealChoice() const
{ {
size_t result = 0; size_t result = 0;
for (auto it = m_options_ptr->begin(); it != m_options_ptr->begin()+itsHighlight; ++it) for (auto it = m_options_ptr->begin(); it != m_options_ptr->begin()+itsHighlight; ++it)
if (*it && !(*it)->isStatic) if (*it && !(*it)->isInactive())
result++; result++;
return result; return result;
} }
@@ -1052,7 +1064,7 @@ template <typename T> void Menu<T>::ReverseSelection(size_t beginning)
auto it = m_options_ptr->begin()+beginning; auto it = m_options_ptr->begin()+beginning;
for (size_t i = beginning; i < Size(); ++i, ++it) for (size_t i = beginning; i < Size(); ++i, ++it)
if (*it) if (*it)
(*it)->isSelected = !(*it)->isSelected && !(*it)->isStatic; (*it)->setSelected(!(*it)->isSelected() && !(*it)->isInactive());
} }
template <typename T> bool Menu<T>::Search(const std::string &constraint, size_t beginning, int flags) template <typename T> bool Menu<T>::Search(const std::string &constraint, size_t beginning, int flags)
@@ -1135,7 +1147,7 @@ template <typename T> const std::string &Menu<T>::GetFilter()
template <typename T> std::string Menu<T>::GetItem(size_t pos) template <typename T> std::string Menu<T>::GetItem(size_t pos)
{ {
if (m_options_ptr->at(pos) && m_get_string_helper) if (m_options_ptr->at(pos) && m_get_string_helper)
return m_get_string_helper((*m_options_ptr)[pos]->Value); return m_get_string_helper((*m_options_ptr)[pos]->value());
else else
return ""; return "";
} }
@@ -1144,56 +1156,56 @@ template <typename T> T &Menu<T>::Back()
{ {
if (!m_options_ptr->back()) if (!m_options_ptr->back())
FatalError("Menu::Back() has requested separator!"); FatalError("Menu::Back() has requested separator!");
return m_options_ptr->back()->Value; return m_options_ptr->back()->value();
} }
template <typename T> const T &Menu<T>::Back() const template <typename T> const T &Menu<T>::Back() const
{ {
if (!m_options_ptr->back()) if (!m_options_ptr->back())
FatalError("Menu::Back() has requested separator!"); FatalError("Menu::Back() has requested separator!");
return m_options_ptr->back()->Value; return m_options_ptr->back()->value();
} }
template <typename T> T &Menu<T>::Current() template <typename T> T &Menu<T>::Current()
{ {
if (!m_options_ptr->at(itsHighlight)) if (!m_options_ptr->at(itsHighlight))
FatalError("Menu::Current() has requested separator!"); FatalError("Menu::Current() has requested separator!");
return (*m_options_ptr)[itsHighlight]->Value; return (*m_options_ptr)[itsHighlight]->value();
} }
template <typename T> const T &Menu<T>::Current() const template <typename T> const T &Menu<T>::Current() const
{ {
if (!m_options_ptr->at(itsHighlight)) if (!m_options_ptr->at(itsHighlight))
FatalError("Menu::Current() const has requested separator!"); FatalError("Menu::Current() const has requested separator!");
return (*m_options_ptr)[itsHighlight]->Value; return (*m_options_ptr)[itsHighlight]->value();
} }
template <typename T> T &Menu<T>::at(size_t pos) template <typename T> T &Menu<T>::at(size_t pos)
{ {
if (!m_options_ptr->at(pos)) if (!m_options_ptr->at(pos))
FatalError("Menu::at() has requested separator!"); FatalError("Menu::at() has requested separator!");
return (*m_options_ptr)[pos]->Value; return (*m_options_ptr)[pos]->value();
} }
template <typename T> const T &Menu<T>::at(size_t pos) const template <typename T> const T &Menu<T>::at(size_t pos) const
{ {
if (!m_options->at(pos)) if (!m_options->at(pos))
FatalError("Menu::at() const has requested separator!"); FatalError("Menu::at() const has requested separator!");
return (*m_options_ptr)[pos]->Value; return (*m_options_ptr)[pos]->value();
} }
template <typename T> const T &Menu<T>::operator[](size_t pos) const template <typename T> const T &Menu<T>::operator[](size_t pos) const
{ {
if (!(*m_options_ptr)[pos]) if (!(*m_options_ptr)[pos])
FatalError("Menu::operator[] const has requested separator!"); FatalError("Menu::operator[] const has requested separator!");
return (*m_options_ptr)[pos]->Value; return (*m_options_ptr)[pos]->value();
} }
template <typename T> T &Menu<T>::operator[](size_t pos) template <typename T> T &Menu<T>::operator[](size_t pos)
{ {
if (!(*m_options_ptr)[pos]) if (!(*m_options_ptr)[pos])
FatalError("Menu::operator[] has requested separator!"); FatalError("Menu::operator[] has requested separator!");
return (*m_options_ptr)[pos]->Value; return (*m_options_ptr)[pos]->value();
} }
} }