2020-05-08 19:19:37 +00:00
|
|
|
#ifndef __NCPP_READER_HH
|
|
|
|
#define __NCPP_READER_HH
|
|
|
|
|
|
|
|
#include <notcurses/notcurses.h>
|
|
|
|
|
|
|
|
#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-09-30 21:42:42 +00:00
|
|
|
#include "Widget.hh"
|
2020-05-08 19:19:37 +00:00
|
|
|
|
|
|
|
namespace ncpp
|
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
class NCPP_API_EXPORT Reader : public Widget
|
2020-05-08 19:19:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Reader (Plane *plane, const ncreader_options *opts)
|
|
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
2020-05-24 12:41:18 +00:00
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
ensure_valid_plane (plane);
|
|
|
|
common_init (Utilities::to_ncplane (plane), opts);
|
|
|
|
take_plane_ownership (plane);
|
2020-05-24 12:41:18 +00:00
|
|
|
}
|
2020-05-08 19:19:37 +00:00
|
|
|
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Reader (Plane &plane, const ncreader_options *opts)
|
|
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
2020-05-08 19:19:37 +00:00
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
ensure_valid_plane (plane);
|
|
|
|
common_init (Utilities::to_ncplane (plane), opts);
|
|
|
|
take_plane_ownership (plane);
|
2020-05-08 19:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Reader ()
|
|
|
|
{
|
|
|
|
if (!is_notcurses_stopped ())
|
|
|
|
ncreader_destroy (reader, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-05-24 08:07:12 +00:00
|
|
|
bool clear () const NOEXCEPT_MAYBE
|
|
|
|
{
|
|
|
|
bool ret = ncreader_clear (reader) != 0;
|
|
|
|
return error_guard_cond<bool, bool> (ret, ret);
|
|
|
|
}
|
|
|
|
|
2020-09-23 20:48:43 +00:00
|
|
|
//
|
|
|
|
// None of the move* methods should throw since their return value (0 - moved, -1 - not moved) appear to be
|
|
|
|
// purely informational, not errors per se. If we had `can_move*` functions then `move*` could throw exceptions,
|
|
|
|
// potentially.
|
|
|
|
//
|
|
|
|
int move_left () const noexcept
|
|
|
|
{
|
|
|
|
return ncreader_move_left (reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
int move_right () const noexcept
|
|
|
|
{
|
|
|
|
return ncreader_move_right (reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
int move_up () const noexcept
|
|
|
|
{
|
|
|
|
return ncreader_move_up (reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
int move_down () const noexcept
|
|
|
|
{
|
|
|
|
return ncreader_move_down (reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_egc (const char *egc) const NOEXCEPT_MAYBE
|
|
|
|
{
|
|
|
|
return error_guard (ncreader_write_egc (reader, egc), -1);
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:19:37 +00:00
|
|
|
char* get_contents () const noexcept
|
|
|
|
{
|
|
|
|
return ncreader_contents(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
Plane* get_plane () const noexcept
|
|
|
|
{
|
|
|
|
return Plane::map_plane (ncreader_plane (reader));
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:41:18 +00:00
|
|
|
private:
|
2020-09-13 17:53:11 +00:00
|
|
|
void common_init (ncplane *n, const ncreader_options *opts)
|
2020-05-24 12:41:18 +00:00
|
|
|
{
|
2020-09-13 17:53:11 +00:00
|
|
|
reader = ncreader_create (n, opts);
|
2020-05-24 12:41:18 +00:00
|
|
|
if (reader == nullptr)
|
|
|
|
throw init_error ("Notcurses failed to create a new reader");
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:19:37 +00:00
|
|
|
private:
|
|
|
|
ncreader *reader;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|