notcurses/include/ncpp/Reel.hh

162 lines
3.3 KiB
C++
Raw Normal View History

2020-02-05 22:29:42 +00:00
#ifndef __NCPP_REEL_HH
#define __NCPP_REEL_HH
2020-01-07 22:51:34 +00:00
#include <memory>
#include <notcurses/notcurses.h>
2020-01-07 22:51:34 +00:00
#include "Tablet.hh"
#include "Root.hh"
namespace ncpp
{
class Plane;
2020-02-05 22:29:42 +00:00
class NCPP_API_EXPORT NcReel : public Root
2020-01-07 22:51:34 +00:00
{
public:
2020-02-05 22:29:42 +00:00
static ncreel_options default_options;
2020-01-07 22:51:34 +00:00
explicit NcReel (Plane &plane, const ncreel_options *popts = nullptr, int efd = -1)
: NcReel (&plane, popts, efd)
{}
explicit NcReel (Plane *plane, const ncreel_options *popts = nullptr, int efd = -1)
2020-01-07 22:51:34 +00:00
{
if (plane == nullptr)
throw invalid_argument ("'plane' must be a valid pointer");
2020-01-07 22:51:34 +00:00
create_reel (reinterpret_cast<ncplane*>(plane), popts, efd);
}
explicit NcReel (ncplane *plane, const ncreel_options *popts = nullptr, int efd = -1)
2020-01-07 22:51:34 +00:00
{
if (plane == nullptr)
throw invalid_argument ("'plane' must be a valid pointer");
2020-01-07 22:51:34 +00:00
create_reel (plane, popts, efd);
}
2020-02-05 22:29:42 +00:00
~NcReel ()
2020-01-07 22:51:34 +00:00
{
if (!is_notcurses_stopped ())
2020-02-05 22:29:42 +00:00
ncreel_destroy (reel);
2020-01-07 22:51:34 +00:00
}
2020-02-05 22:29:42 +00:00
operator ncreel* () const noexcept
2020-01-07 22:51:34 +00:00
{
return reel;
}
2020-02-05 22:29:42 +00:00
operator ncreel const* () const noexcept
2020-01-07 22:51:34 +00:00
{
return reel;
}
// TODO: add an overload using callback that takes NcTablet instance instead of struct tablet
NcTablet* add (NcTablet *after, NcTablet *before, tabletcb cb, void *opaque = nullptr) const
2020-01-07 22:51:34 +00:00
{
nctablet *t = ncreel_add (reel, get_tablet (after), get_tablet (before), cb, opaque);
2020-01-07 22:51:34 +00:00
if (t == nullptr)
throw init_error ("notcurses failed to create a new tablet");
2020-01-07 22:51:34 +00:00
return NcTablet::map_tablet (t);
2020-01-07 22:51:34 +00:00
}
NcTablet* add (NcTablet &after, NcTablet &before, tabletcb cb, void *opaque = nullptr) const noexcept
2020-01-07 22:51:34 +00:00
{
return add (&after, &before, cb, opaque);
}
int get_tabletcount () const noexcept
{
2020-02-05 22:29:42 +00:00
return ncreel_tabletcount (reel);
2020-01-07 22:51:34 +00:00
}
bool touch (NcTablet *t) const noexcept
2020-01-07 22:51:34 +00:00
{
2020-02-05 22:29:42 +00:00
return ncreel_touch (reel, get_tablet (t)) != -1;
2020-01-07 22:51:34 +00:00
}
bool touch (NcTablet &t) const noexcept
2020-01-07 22:51:34 +00:00
{
return touch (&t);
}
bool del (NcTablet *t) const noexcept
2020-01-07 22:51:34 +00:00
{
2020-02-05 22:29:42 +00:00
return ncreel_del (reel, get_tablet (t)) != -1;
2020-01-07 22:51:34 +00:00
}
bool del (NcTablet &t) const noexcept
2020-01-07 22:51:34 +00:00
{
return del (&t);
}
bool del_focused () const noexcept
{
2020-02-05 22:29:42 +00:00
return ncreel_del_focused (reel) != -1;
2020-01-07 22:51:34 +00:00
}
bool move (int x, int y) const noexcept
{
2020-02-05 22:29:42 +00:00
return ncreel_move (reel, x, y) != -1;
2020-01-07 22:51:34 +00:00
}
bool redraw () const noexcept
{
2020-02-05 22:29:42 +00:00
return ncreel_redraw (reel) != -1;
2020-01-07 22:51:34 +00:00
}
NcTablet* get_focused () const noexcept
2020-01-07 22:51:34 +00:00
{
nctablet *t = ncreel_focused (reel);
2020-01-07 22:51:34 +00:00
if (t == nullptr)
return nullptr;
return NcTablet::map_tablet (t);
2020-01-07 22:51:34 +00:00
}
NcTablet* next () const noexcept
2020-01-07 22:51:34 +00:00
{
nctablet *t = ncreel_next (reel);
2020-01-07 22:51:34 +00:00
if (t == nullptr)
return nullptr;
return NcTablet::map_tablet (t);
2020-01-07 22:51:34 +00:00
}
NcTablet* prev () const noexcept
2020-01-07 22:51:34 +00:00
{
nctablet *t = ncreel_prev (reel);
2020-01-07 22:51:34 +00:00
if (t == nullptr)
return nullptr;
return NcTablet::map_tablet (t);
2020-01-07 22:51:34 +00:00
}
Plane* get_plane () const noexcept;
private:
nctablet* get_tablet (NcTablet *t) const noexcept
2020-01-07 22:51:34 +00:00
{
if (t == nullptr)
return nullptr;
return t->get_tablet ();
}
2020-02-05 22:29:42 +00:00
void create_reel (ncplane *plane, const ncreel_options *popts, int efd)
2020-01-07 22:51:34 +00:00
{
2020-02-05 22:29:42 +00:00
reel = ncreel_create (plane, popts == nullptr ? &default_options : popts, efd);
2020-01-07 22:51:34 +00:00
if (reel == nullptr)
throw init_error ("notcurses failed to create a new ncreel");
2020-01-07 22:51:34 +00:00
}
private:
2020-02-05 22:29:42 +00:00
ncreel *reel = nullptr;
2020-01-07 22:51:34 +00:00
friend class Plane;
};
}
#endif