2020-02-15 22:04:39 +00:00
|
|
|
#ifndef __NCPP_SELECTOR_HH
|
|
|
|
#define __NCPP_SELECTOR_HH
|
|
|
|
|
2020-02-18 17:36:16 +00:00
|
|
|
#include <notcurses/notcurses.h>
|
2020-02-15 22:04:39 +00:00
|
|
|
|
|
|
|
#include "NCAlign.hh"
|
2020-05-24 12:41:18 +00:00
|
|
|
#include "Plane.hh"
|
2020-05-22 20:11:59 +00:00
|
|
|
#include "Utilities.hh"
|
2020-02-15 22:04:39 +00:00
|
|
|
|
|
|
|
namespace ncpp
|
|
|
|
{
|
|
|
|
class NCPP_API_EXPORT Selector : public Root
|
|
|
|
{
|
|
|
|
public:
|
2020-05-09 00:58:00 +00:00
|
|
|
static ncselector_options default_options;
|
2020-02-15 22:04:39 +00:00
|
|
|
|
|
|
|
public:
|
2020-09-13 10:39:17 +00:00
|
|
|
explicit Selector (Plane *plane, const ncselector_options *opts = nullptr)
|
|
|
|
: Selector (static_cast<const Plane*>(plane), opts)
|
2020-02-15 22:04:39 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-13 10:39:17 +00:00
|
|
|
explicit Selector (Plane const* plane, const ncselector_options *opts = nullptr)
|
2020-05-24 12:41:18 +00:00
|
|
|
: Root (Utilities::get_notcurses_cpp (plane))
|
|
|
|
{
|
|
|
|
if (plane == nullptr)
|
|
|
|
throw invalid_argument ("'plane' must be a valid pointer");
|
2020-09-13 10:39:17 +00:00
|
|
|
common_init (Utilities::to_ncplane (plane), opts);
|
2020-05-24 12:41:18 +00:00
|
|
|
}
|
2020-02-15 22:04:39 +00:00
|
|
|
|
2020-09-13 10:39:17 +00:00
|
|
|
explicit Selector (Plane &plane, const ncselector_options *opts = nullptr)
|
|
|
|
: Selector (static_cast<Plane const&>(plane), opts)
|
2020-02-15 22:04:39 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-13 10:39:17 +00:00
|
|
|
explicit Selector (Plane const& plane, const ncselector_options *opts = nullptr)
|
2020-05-24 12:41:18 +00:00
|
|
|
: Root (Utilities::get_notcurses_cpp (plane))
|
2020-02-15 22:04:39 +00:00
|
|
|
{
|
2020-09-13 10:39:17 +00:00
|
|
|
common_init (Utilities::to_ncplane (plane), opts);
|
2020-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Selector ()
|
|
|
|
{
|
|
|
|
if (!is_notcurses_stopped ())
|
|
|
|
ncselector_destroy (selector, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-05-09 00:58:00 +00:00
|
|
|
int additem (const ncselector_item *item) const NOEXCEPT_MAYBE
|
2020-02-15 22:04:39 +00:00
|
|
|
{
|
2020-04-12 21:09:03 +00:00
|
|
|
return error_guard<int> (ncselector_additem (selector, item), -1);
|
2020-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 21:09:03 +00:00
|
|
|
int delitem (const char *item) const NOEXCEPT_MAYBE
|
2020-02-15 22:04:39 +00:00
|
|
|
{
|
2020-04-12 21:09:03 +00:00
|
|
|
return error_guard<int> (ncselector_delitem (selector, item), -1);
|
2020-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* previtem () const noexcept
|
|
|
|
{
|
|
|
|
return ncselector_previtem (selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* nextitem () const noexcept
|
|
|
|
{
|
|
|
|
return ncselector_nextitem (selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* get_selected () const noexcept
|
|
|
|
{
|
|
|
|
return ncselector_selected (selector);
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:41:18 +00:00
|
|
|
bool offer_input (const struct ncinput* ni) const noexcept
|
2020-02-15 22:04:39 +00:00
|
|
|
{
|
2020-05-24 12:41:18 +00:00
|
|
|
return ncselector_offer_input (selector, ni);
|
2020-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Plane* get_plane () const noexcept;
|
|
|
|
|
2020-05-24 12:41:18 +00:00
|
|
|
private:
|
2020-09-13 10:39:17 +00:00
|
|
|
void common_init (ncplane *plane, const ncselector_options *opts = nullptr)
|
2020-05-24 12:41:18 +00:00
|
|
|
{
|
|
|
|
if (plane == nullptr)
|
|
|
|
throw invalid_argument ("'plane' must be a valid pointer");
|
|
|
|
|
2020-09-13 10:39:17 +00:00
|
|
|
selector = ncselector_create (plane, opts == nullptr ? &default_options : opts);
|
2020-05-24 12:41:18 +00:00
|
|
|
if (selector == nullptr)
|
|
|
|
throw init_error ("Notcurses failed to create a new selector");
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:04:39 +00:00
|
|
|
private:
|
|
|
|
ncselector *selector;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|