put classes related to ncurses into NCurses namespace

This commit is contained in:
Andrzej Rybczak
2009-03-05 17:43:13 +01:00
parent 8f092d5490
commit ea973b6b4a
10 changed files with 445 additions and 429 deletions

View File

@@ -20,6 +20,8 @@
#include "menu.h"
using namespace NCurses;
void List::SelectCurrent()
{
if (Empty())

View File

@@ -25,6 +25,8 @@
#include "strbuffer.h"
#include "misc.h"
namespace NCurses
{
class List
{
public:
@@ -55,9 +57,6 @@ class List
virtual std::string GetOption(size_t) = 0;
virtual bool isFiltered() = 0;
//virtual void ShowAll() = 0;
//virtual void ShowFiltered() = 0;
};
template <class T> class Menu : public Window, public List
@@ -172,7 +171,10 @@ template <class T> class Menu : public Window, public List
Buffer *itsSelectedSuffix;
};
template <class T> Menu<T>::Menu(size_t startx,
template <> std::string Menu<std::string>::GetOption(size_t pos);
}
template <class T> NCurses::Menu<T>::Menu(size_t startx,
size_t starty,
size_t width,
size_t height,
@@ -194,7 +196,7 @@ template <class T> Menu<T>::Menu(size_t startx,
{
}
template <class T> Menu<T>::Menu(const Menu &m) : Window(m)
template <class T> NCurses::Menu<T>::Menu(const Menu &m) : Window(m)
{
itsOptions = m.itsOptions;
itsItemDisplayer = m.itsItemDisplayer;
@@ -205,18 +207,18 @@ template <class T> Menu<T>::Menu(const Menu &m) : Window(m)
highlightEnabled = m.highlightEnabled;
}
template <class T> Menu<T>::~Menu()
template <class T> NCurses::Menu<T>::~Menu()
{
for (option_iterator it = itsOptions.begin(); it != itsOptions.end(); it++)
delete *it;
}
template <class T> void Menu<T>::Reserve(size_t size)
template <class T> void NCurses::Menu<T>::Reserve(size_t size)
{
itsOptions.reserve(size);
}
template <class T> void Menu<T>::ResizeBuffer(size_t size)
template <class T> void NCurses::Menu<T>::ResizeBuffer(size_t size)
{
itsOptions.resize(size);
for (size_t i = 0; i < size; i++)
@@ -224,27 +226,27 @@ template <class T> void Menu<T>::ResizeBuffer(size_t size)
itsOptions[i] = new Option();
}
template <class T> void Menu<T>::AddOption(const T &item, bool is_bold, bool is_static)
template <class T> void NCurses::Menu<T>::AddOption(const T &item, bool is_bold, bool is_static)
{
itsOptions.push_back(new Option(item, is_bold, is_static));
}
template <class T> void Menu<T>::AddSeparator()
template <class T> void NCurses::Menu<T>::AddSeparator()
{
itsOptions.push_back(0);
}
template <class T> void Menu<T>::InsertOption(size_t pos, const T &item, bool is_bold, bool is_static)
template <class T> void NCurses::Menu<T>::InsertOption(size_t pos, const T &item, bool is_bold, bool is_static)
{
itsOptions.insert(itsOptions.begin()+pos, new Option(item, is_bold, is_static));
}
template <class T> void Menu<T>::InsertSeparator(size_t pos)
template <class T> void NCurses::Menu<T>::InsertSeparator(size_t pos)
{
itsOptions.insert(itsOptions.begin()+pos, 0);
}
template <class T> void Menu<T>::DeleteOption(size_t pos)
template <class T> void NCurses::Menu<T>::DeleteOption(size_t pos)
{
if (itsOptions.empty())
return;
@@ -266,13 +268,13 @@ template <class T> void Menu<T>::DeleteOption(size_t pos)
Window::Clear();
}
template <class T> void Menu<T>::IntoSeparator(size_t pos)
template <class T> void NCurses::Menu<T>::IntoSeparator(size_t pos)
{
delete itsOptions.at(pos);
itsOptions[pos] = 0;
}
template <class T> void Menu<T>::BoldOption(int index, bool bold)
template <class T> void NCurses::Menu<T>::BoldOption(int index, bool bold)
{
if (!itsOptions.at(index))
return;
@@ -280,12 +282,12 @@ template <class T> void Menu<T>::BoldOption(int index, bool bold)
}
template <class T>
void Menu<T>::Swap(size_t one, size_t two)
void NCurses::Menu<T>::Swap(size_t one, size_t two)
{
std::swap(itsOptions.at(one), itsOptions.at(two));
}
template <class T> void Menu<T>::Refresh()
template <class T> void NCurses::Menu<T>::Refresh()
{
if (itsOptionsPtr->empty())
{
@@ -348,7 +350,7 @@ template <class T> void Menu<T>::Refresh()
Window::Refresh();
}
template <class T> void Menu<T>::Scroll(Where where)
template <class T> void NCurses::Menu<T>::Scroll(Where where)
{
if (itsOptionsPtr->empty())
return;
@@ -454,13 +456,13 @@ template <class T> void Menu<T>::Scroll(Where where)
}
}
template <class T> void Menu<T>::Reset()
template <class T> void NCurses::Menu<T>::Reset()
{
itsHighlight = 0;
itsBeginning = 0;
}
template <class T> void Menu<T>::Clear(bool clrscr)
template <class T> void NCurses::Menu<T>::Clear(bool clrscr)
{
for (option_iterator it = itsOptions.begin(); it != itsOptions.end(); it++)
delete *it;
@@ -473,7 +475,7 @@ template <class T> void Menu<T>::Clear(bool clrscr)
Window::Clear();
}
template <class T> bool Menu<T>::isBold(int id)
template <class T> bool NCurses::Menu<T>::isBold(int id)
{
id = id == -1 ? itsHighlight : id;
if (!itsOptionsPtr->at(id))
@@ -481,21 +483,21 @@ template <class T> bool Menu<T>::isBold(int id)
return (*itsOptionsPtr)[id]->isBold;
}
template <class T> void Menu<T>::Select(int id, bool value)
template <class T> void NCurses::Menu<T>::Select(int id, bool value)
{
if (!itsOptionsPtr->at(id))
return;
(*itsOptionsPtr)[id]->isSelected = value;
}
template <class T> void Menu<T>::Static(int id, bool value)
template <class T> void NCurses::Menu<T>::Static(int id, bool value)
{
if (!itsOptionsPtr->at(id))
return;
(*itsOptionsPtr)[id]->isStatic = value;
}
template <class T> bool Menu<T>::isSelected(int id) const
template <class T> bool NCurses::Menu<T>::isSelected(int id) const
{
id = id == -1 ? itsHighlight : id;
if (!itsOptionsPtr->at(id))
@@ -503,7 +505,7 @@ template <class T> bool Menu<T>::isSelected(int id) const
return (*itsOptionsPtr)[id]->isSelected;
}
template <class T> bool Menu<T>::isStatic(int id) const
template <class T> bool NCurses::Menu<T>::isStatic(int id) const
{
id = id == -1 ? itsHighlight : id;
if (!itsOptionsPtr->at(id))
@@ -511,7 +513,7 @@ template <class T> bool Menu<T>::isStatic(int id) const
return (*itsOptionsPtr)[id]->isStatic;
}
template <class T> bool Menu<T>::hasSelected() const
template <class T> bool NCurses::Menu<T>::hasSelected() const
{
for (option_const_iterator it = itsOptionsPtr->begin(); it != itsOptionsPtr->end(); it++)
if (*it && (*it)->isSelected)
@@ -519,30 +521,30 @@ template <class T> bool Menu<T>::hasSelected() const
return false;
}
template <class T> void Menu<T>::GetSelected(std::vector<size_t> &v) const
template <class T> void NCurses::Menu<T>::GetSelected(std::vector<size_t> &v) const
{
for (size_t i = 0; i < itsOptionsPtr->size(); i++)
if ((*itsOptionsPtr)[i]->isSelected)
v.push_back(i);
}
template <class T> void Menu<T>::Highlight(size_t pos)
template <class T> void NCurses::Menu<T>::Highlight(size_t pos)
{
itsHighlight = pos;
itsBeginning = pos-itsHeight/2;
}
template <class T> size_t Menu<T>::Size() const
template <class T> size_t NCurses::Menu<T>::Size() const
{
return itsOptionsPtr->size();
}
template <class T> size_t Menu<T>::Choice() const
template <class T> size_t NCurses::Menu<T>::Choice() const
{
return itsHighlight;
}
template <class T> size_t Menu<T>::RealChoice() const
template <class T> size_t NCurses::Menu<T>::RealChoice() const
{
size_t result = 0;
for (option_const_iterator it = itsOptionsPtr->begin(); it != itsOptionsPtr->begin()+itsHighlight; it++)
@@ -551,7 +553,7 @@ template <class T> size_t Menu<T>::RealChoice() const
return result;
}
template <class T> void Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, bool case_sensitive)
template <class T> void NCurses::Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, bool case_sensitive)
{
if (filter == itsFilter)
return;
@@ -585,12 +587,12 @@ template <class T> void Menu<T>::ApplyFilter(const std::string &filter, size_t b
Window::Clear();
}
template <class T> const std::string &Menu<T>::GetFilter()
template <class T> const std::string &NCurses::Menu<T>::GetFilter()
{
return itsFilter;
}
template <class T> std::string Menu<T>::GetOption(size_t pos)
template <class T> std::string NCurses::Menu<T>::GetOption(size_t pos)
{
if (itsOptionsPtr->at(pos) && itsGetStringFunction)
return itsGetStringFunction((*itsOptionsPtr)[pos]->Item, itsGetStringFunctionUserData);
@@ -598,67 +600,65 @@ template <class T> std::string Menu<T>::GetOption(size_t pos)
return "";
}
template <> std::string Menu<std::string>::GetOption(size_t pos);
template <class T> T &Menu<T>::Back()
template <class T> T &NCurses::Menu<T>::Back()
{
if (!itsOptionsPtr->back())
throw InvalidItem();
return itsOptionsPtr->back()->Item;
}
template <class T> const T &Menu<T>::Back() const
template <class T> const T &NCurses::Menu<T>::Back() const
{
if (!itsOptionsPtr->back())
throw InvalidItem();
return itsOptionsPtr->back()->Item;
}
template <class T> T &Menu<T>::Current()
template <class T> T &NCurses::Menu<T>::Current()
{
if (!itsOptionsPtr->at(itsHighlight))
throw InvalidItem();
return (*itsOptionsPtr)[itsHighlight]->Item;
}
template <class T> const T &Menu<T>::Current() const
template <class T> const T &NCurses::Menu<T>::Current() const
{
if (!itsOptionsPtr->at(itsHighlight))
throw InvalidItem();
return (*itsOptionsPtr)[itsHighlight]->Item;
}
template <class T> T &Menu<T>::at(size_t i)
template <class T> T &NCurses::Menu<T>::at(size_t i)
{
if (!itsOptionsPtr->at(i))
throw InvalidItem();
return (*itsOptionsPtr)[i]->Item;
}
template <class T> const T &Menu<T>::at(size_t i) const
template <class T> const T &NCurses::Menu<T>::at(size_t i) const
{
if (!itsOptions->at(i))
throw InvalidItem();
return (*itsOptionsPtr)[i]->Item;
}
template <class T> const T &Menu<T>::operator[](size_t i) const
template <class T> const T &NCurses::Menu<T>::operator[](size_t i) const
{
if (!(*itsOptionsPtr)[i])
throw InvalidItem();
return (*itsOptionsPtr)[i]->Item;
}
template <class T> T &Menu<T>::operator[](size_t i)
template <class T> T &NCurses::Menu<T>::operator[](size_t i)
{
if (!(*itsOptionsPtr)[i])
throw InvalidItem();
return (*itsOptionsPtr)[i]->Item;
}
template <class T> Menu<T> *Menu<T>::EmptyClone() const
template <class T> NCurses::Menu<T> *NCurses::Menu<T>::EmptyClone() const
{
return new Menu<T>(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
return new NCurses::Menu<T>(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
}
#endif

View File

@@ -76,52 +76,52 @@ std::string IntoStr(mpd_TagItems tag) // this is only for left column's title in
}
}
std::string IntoStr(Color color)
std::string IntoStr(NCurses::Color color)
{
std::string result;
if (color == clDefault)
if (color == NCurses::clDefault)
result = "default";
else if (color == clBlack)
else if (color == NCurses::clBlack)
result = "black";
else if (color == clRed)
else if (color == NCurses::clRed)
result = "red";
else if (color == clGreen)
else if (color == NCurses::clGreen)
result = "green";
else if (color == clYellow)
else if (color == NCurses::clYellow)
result = "yellow";
else if (color == clBlue)
else if (color == NCurses::clBlue)
result = "blue";
else if (color == clMagenta)
else if (color == NCurses::clMagenta)
result = "magenta";
else if (color == clCyan)
else if (color == NCurses::clCyan)
result = "cyan";
else if (color == clWhite)
else if (color == NCurses::clWhite)
result = "white";
return result;
}
Color IntoColor(const std::string &color)
NCurses::Color IntoColor(const std::string &color)
{
Color result = clDefault;
NCurses::Color result = NCurses::clDefault;
if (color == "black")
result = clBlack;
result = NCurses::clBlack;
else if (color == "red")
result = clRed;
result = NCurses::clRed;
else if (color == "green")
result = clGreen;
result = NCurses::clGreen;
else if (color == "yellow")
result = clYellow;
result = NCurses::clYellow;
else if (color == "blue")
result = clBlue;
result = NCurses::clBlue;
else if (color == "magenta")
result = clMagenta;
result = NCurses::clMagenta;
else if (color == "cyan")
result = clCyan;
result = NCurses::clCyan;
else if (color == "white")
result = clWhite;
result = NCurses::clWhite;
return result;
}

View File

@@ -34,9 +34,9 @@ std::string IntoStr(int);
std::string IntoStr(mpd_TagItems);
std::string IntoStr(Color);
std::string IntoStr(NCurses::Color);
Color IntoColor(const std::string &);
NCurses::Color IntoColor(const std::string &);
mpd_TagItems IntoTagItem(char);

View File

@@ -33,6 +33,8 @@
vFoundPositions.clear(); \
} while (0)
using namespace NCurses;
typedef std::pair<std::string, std::string> string_pair;
const int ncmpcpp_window_timeout = 500;

View File

@@ -20,6 +20,7 @@
#include "scrollpad.h"
using namespace NCurses;
using std::string;
Scrollpad::Scrollpad(size_t startx,

View File

@@ -24,6 +24,8 @@
#include "window.h"
#include "strbuffer.h"
namespace NCurses
{
class Scrollpad: public Window
{
public:
@@ -67,6 +69,7 @@ class Scrollpad: public Window
size_t itsRealHeight;
};
}
#endif

View File

@@ -26,6 +26,8 @@
#include <sstream>
#include <list>
namespace NCurses
{
template <class C> class basic_buffer
{
struct FormatPos
@@ -67,13 +69,14 @@ template <class C> class basic_buffer
typedef basic_buffer<char> Buffer;
typedef basic_buffer<wchar_t> WBuffer;
}
template <class C> std::basic_string<C> basic_buffer<C>::Str() const
template <class C> std::basic_string<C> NCurses::basic_buffer<C>::Str() const
{
return itsString.str();
}
template <class C> void basic_buffer<C>::SetFormatting(short vb, const std::basic_string<C> &s, short ve, bool for_each)
template <class C> void NCurses::basic_buffer<C>::SetFormatting(short vb, const std::basic_string<C> &s, short ve, bool for_each)
{
std::basic_string<C> base = itsString.str();
FormatPos fp;
@@ -92,24 +95,24 @@ template <class C> void basic_buffer<C>::SetFormatting(short vb, const std::basi
}
}
template <class C> void basic_buffer<C>::SetTemp(std::basic_string<C> *tmp)
template <class C> void NCurses::basic_buffer<C>::SetTemp(std::basic_string<C> *tmp)
{
itsTempString = tmp;
}
template <class C> void basic_buffer<C>::Clear()
template <class C> void NCurses::basic_buffer<C>::Clear()
{
itsString.str(std::basic_string<C>());
itsFormat.clear();
}
template <class C> basic_buffer<C> &basic_buffer<C>::operator<<(std::ostream &(*os)(std::ostream&))
template <class C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(std::ostream &(*os)(std::ostream&))
{
itsString << os;
return *this;
}
template <class C> basic_buffer<C> &basic_buffer<C>::operator<<(const Color &color)
template <class C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const Color &color)
{
FormatPos f;
f.Position = itsString.str().length();
@@ -118,24 +121,24 @@ template <class C> basic_buffer<C> &basic_buffer<C>::operator<<(const Color &col
return *this;
}
template <class C> basic_buffer<C> &basic_buffer<C>::operator<<(const Format &f)
template <class C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const Format &f)
{
return operator<<(Color(f));
}
template <class C> basic_buffer<C> &basic_buffer<C>::operator<<(const basic_buffer<C> &buf)
template <class C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const NCurses::basic_buffer<C> &buf)
{
size_t len = itsString.str().length();
itsString << buf.itsString.str();
std::list<FormatPos> tmp = buf.itsFormat;
if (len)
for (typename std::list<typename basic_buffer<C>::FormatPos>::iterator it = tmp.begin(); it != tmp.end(); it++)
for (typename std::list<typename NCurses::basic_buffer<C>::FormatPos>::iterator it = tmp.begin(); it != tmp.end(); it++)
it->Position += len;
itsFormat.merge(tmp);
return *this;
}
template <class C> Window &operator<<(Window &w, const basic_buffer<C> &buf)
template <class C> NCurses::Window &operator<<(NCurses::Window &w, const NCurses::basic_buffer<C> &buf)
{
const std::basic_string<C> &s = buf.itsTempString ? *buf.itsTempString : buf.itsString.str();
if (buf.itsFormat.empty())
@@ -145,8 +148,8 @@ template <class C> Window &operator<<(Window &w, const basic_buffer<C> &buf)
else
{
std::basic_string<C> tmp;
typename std::list<typename basic_buffer<C>::FormatPos>::const_iterator b = buf.itsFormat.begin();
typename std::list<typename basic_buffer<C>::FormatPos>::const_iterator e = buf.itsFormat.end();
typename std::list<typename NCurses::basic_buffer<C>::FormatPos>::const_iterator b = buf.itsFormat.begin();
typename std::list<typename NCurses::basic_buffer<C>::FormatPos>::const_iterator e = buf.itsFormat.end();
for (size_t i = 0; i < s.length() || b != e; i++)
{
while (b != e && i == b->Position)
@@ -156,10 +159,10 @@ template <class C> Window &operator<<(Window &w, const basic_buffer<C> &buf)
w << tmp;
tmp.clear();
}
if (b->Value < fmtNone)
w << Color(b->Value);
if (b->Value < NCurses::fmtNone)
w << NCurses::Color(b->Value);
else
w << Format(b->Value);
w << NCurses::Format(b->Value);
b++;
}
if (i < s.length())

View File

@@ -23,10 +23,12 @@
#include "window.h"
using namespace NCurses;
using std::string;
using std::wstring;
void InitScreen(bool enable_colors)
void NCurses::InitScreen(bool enable_colors)
{
setlocale(LC_ALL, "");
initscr();
@@ -44,7 +46,7 @@ void InitScreen(bool enable_colors)
curs_set(0);
}
void DestroyScreen()
void NCurses::DestroyScreen()
{
curs_set(1);
endwin();

View File

@@ -41,6 +41,13 @@
# define TO_WSTRING(x) x
#endif
char *ToString(const wchar_t *);
wchar_t *ToWString(const char *);
std::string ToString(const std::wstring &);
std::wstring ToWString(const std::string &);
namespace NCurses
{
enum Color { clDefault, clBlack, clRed, clGreen, clYellow, clBlue, clMagenta, clCyan, clWhite, clEnd };
enum Format { fmtNone = 100, fmtBold, fmtBoldEnd, fmtReverse, fmtReverseEnd, fmtAltCharset, fmtAltCharsetEnd };
enum Border { brNone, brBlack, brRed, brGreen, brYellow, brBlue, brMagenta, brCyan, brWhite };
@@ -65,11 +72,6 @@ struct XY
int y;
};
char *ToString(const wchar_t *);
wchar_t *ToWString(const char *);
std::string ToString(const std::wstring &);
std::wstring ToWString(const std::string &);
class Window
{
public:
@@ -176,5 +178,6 @@ class Window
Border itsBorder;
};
}
#endif