notcurses/include/ncpp/Menu.hh
Marek Habersack b5d8549bb3 [C++] Allow multiple instances of NotCurses
This is to make it possible, in the future, to create multiple instances
of `NotCurses` for multiple terminals.  The first instance of
`NotCurses` becomes the default one, so that any instances of other
classes that aren't explicitly created with a pointer to another
`NotCurses` instance still work as expected.

Note that currently trying to call `notcurses_init` twice results in the
following error for me:

    0x55555559bfc0 is already registered for signals
    Couldn't drop signals: 0x55555559bfc0 != 0x5555555b6720
    terminate called after throwing an instance of 'ncpp::init_error*'

    Program received signal SIGABRT, Aborted.

The error is signalled by `setup_signals` and the pointer shown in the
message points to the first `struct notcurses` instance created.
2020-05-26 04:34:31 -04:00

80 lines
1.5 KiB
C++

#ifndef __NCPP_MENU_HH
#define __NCPP_MENU_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
namespace ncpp
{
class NotCurses;
class Plane;
class NCPP_API_EXPORT Menu : public Root
{
public:
static ncmenu_options default_options;
public:
explicit Menu (const ncmenu_options *opts = nullptr, NotCurses *ncinst = nullptr)
: Root (ncinst)
{
menu = ncmenu_create (notcurses_stdplane (get_notcurses ()), opts == nullptr ? &default_options : opts);
if (menu == nullptr)
throw init_error ("Notcurses failed to create a new menu");
}
~Menu ()
{
if (!is_notcurses_stopped ())
ncmenu_destroy (menu);
}
bool unroll (int sectionidx) const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_unroll (menu, sectionidx), -1);
}
bool rollup () const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_rollup (menu), -1);
}
bool nextsection () const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_nextsection (menu), -1);
}
bool prevsection () const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_prevsection (menu), -1);
}
bool nextitem () const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_nextitem (menu), -1);
}
bool previtem () const NOEXCEPT_MAYBE
{
return error_guard (ncmenu_previtem (menu), -1);
}
const char* get_selected (ncinput *ni = nullptr) const noexcept
{
return ncmenu_selected (menu, ni);
}
bool offer_input (const struct ncinput* ni) const noexcept
{
return ncmenu_offer_input (menu, ni);
}
Plane* get_plane () const noexcept;
private:
ncmenu *menu;
};
}
#endif