diff --git a/include/ncpp/Pile.hh b/include/ncpp/Pile.hh new file mode 100644 index 000000000..9459094ac --- /dev/null +++ b/include/ncpp/Pile.hh @@ -0,0 +1,57 @@ +#ifndef __NCPP_PILE_HH +#define __NCPP_PILE_HH + +#include +#include + +#include "NotCurses.hh" +#include "Plane.hh" + +namespace ncpp +{ + class NCPP_API_EXPORT Pile : public Plane + { + public: + Pile (ncplane_options const* nopts, NotCurses* ncinst = nullptr) + : Plane (ncinst) + { + if (nopts == nullptr) { + throw invalid_argument ("'nopts' must be a valid pointer"); + } + + notcurses *n; + if (ncinst == nullptr) { + n = NotCurses::get_instance (); + } else { + n = *ncinst; + } + + ncplane *pile = ncpile_create (n, nopts); + if (pile == nullptr) { + throw init_error ("Notcurses failed to create a new pile"); + } + + set_plane (pile); + } + + bool render () const NOEXCEPT_MAYBE + { + return error_guard (ncpile_render (to_ncplane ()), -1); + } + + bool rasterize () const NOEXCEPT_MAYBE + { + return error_guard (ncpile_rasterize (to_ncplane ()), -1); + } + + bool show () const NOEXCEPT_MAYBE + { + if (!render ()) { + return false; + } + + return rasterize (); + } + }; +} +#endif diff --git a/include/ncpp/Plane.hh b/include/ncpp/Plane.hh index b46d2aea5..e5fe13e56 100644 --- a/include/ncpp/Plane.hh +++ b/include/ncpp/Plane.hh @@ -150,7 +150,7 @@ namespace ncpp ncplane_center_abs (plane, y, x); } - ncplane* to_ncplane () noexcept + ncplane* to_ncplane () const noexcept { return plane; } @@ -302,6 +302,16 @@ namespace ncpp get_yx (&y, &x); } + Plane* get_parent () const noexcept + { + ncplane *ret = ncplane_parent (plane); + if (ret == nullptr) { + return nullptr; + } + + return map_plane (ret); + } + Plane* reparent (Plane *newparent = nullptr) const noexcept { ncplane *ret = ncplane_reparent (plane, newparent == nullptr ? nullptr : newparent->plane); @@ -1180,6 +1190,29 @@ namespace ncpp throw invalid_argument ("_plane must be a valid pointer"); } + // This is used by child classes which cannot provide a valid ncplane* in their constructor when initializing + // the parent class (e.g. Pile) + Plane (NotCurses *ncinst = nullptr) + : Root (ncinst), + plane (nullptr) + {} + + // Can be used only once and only if plane == nullptr. Meant to be used by child classes which cannot provide a + // valid ncplane* in their constructor when initializing the parent class (e.g. Pile) + void set_plane (ncplane *_plane) + { + if (_plane == nullptr) { + throw invalid_argument ("_plane must be a valid pointer"); + } + + if (plane != nullptr) { + throw invalid_state_error ("Plane::set_plane can be called only once"); + } + + plane = _plane; + map_plane (plane, this); + } + void release_native_plane () noexcept { if (plane == nullptr) @@ -1221,7 +1254,7 @@ namespace ncpp { ncplane_options nopts = { yoff, - { static_cast(align) }, + static_cast(align), rows, cols, opaque, diff --git a/src/poc/ncpp_build.cpp b/src/poc/ncpp_build.cpp index 9febd9bcc..c2e2a9c47 100644 --- a/src/poc/ncpp_build.cpp +++ b/src/poc/ncpp_build.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/src/poc/ncpp_build_exceptions.cpp b/src/poc/ncpp_build_exceptions.cpp index 1084b2d9e..6f5d70fca 100644 --- a/src/poc/ncpp_build_exceptions.cpp +++ b/src/poc/ncpp_build_exceptions.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include