1 #ifndef __NCPP_SELECTOR_HH
2 #define __NCPP_SELECTOR_HH
3 
4 #include <notcurses/notcurses.h>
5 
6 #include "NCAlign.hh"
7 #include "Plane.hh"
8 #include "Utilities.hh"
9 #include "Widget.hh"
10 
11 namespace ncpp
12 {
13 	class NCPP_API_EXPORT Selector : public Widget
14 	{
15 	public:
16 		static ncselector_options default_options;
17 
18 	public:
Selector(Plane * plane,const ncselector_options * opts=nullptr)19 		explicit Selector (Plane *plane, const ncselector_options *opts = nullptr)
20 			: Widget (Utilities::get_notcurses_cpp (plane))
21 		{
22 			if (plane == nullptr)
23 				throw invalid_argument ("'plane' must be a valid pointer");
24 
25 			ensure_valid_plane (plane);
26 			common_init (Utilities::to_ncplane (plane), opts);
27 			take_plane_ownership (plane);
28 		}
29 
Selector(Plane & plane,const ncselector_options * opts=nullptr)30 		explicit Selector (Plane &plane, const ncselector_options *opts = nullptr)
31 			: Widget (Utilities::get_notcurses_cpp (plane))
32 		{
33 			ensure_valid_plane (plane);
34 			common_init (Utilities::to_ncplane (plane), opts);
35 			take_plane_ownership (plane);
36 		}
37 
~Selector()38 		~Selector ()
39 		{
40 			if (!is_notcurses_stopped ())
41 				ncselector_destroy (selector, nullptr);
42 		}
43 
additem(const ncselector_item * item) const44 		int additem (const ncselector_item *item) const NOEXCEPT_MAYBE
45 		{
46 			return error_guard<int> (ncselector_additem (selector, item), -1);
47 		}
48 
delitem(const char * item) const49 		int delitem (const char *item) const NOEXCEPT_MAYBE
50 		{
51 			return error_guard<int> (ncselector_delitem (selector, item), -1);
52 		}
53 
previtem() const54 		const char* previtem () const noexcept
55 		{
56 			return ncselector_previtem (selector);
57 		}
58 
nextitem() const59 		const char* nextitem () const noexcept
60 		{
61 			return ncselector_nextitem (selector);
62 		}
63 
get_selected() const64 		const char* get_selected () const noexcept
65 		{
66 			return ncselector_selected (selector);
67 		}
68 
offer_input(const struct ncinput * ni) const69 		bool offer_input (const struct ncinput* ni) const noexcept
70 		{
71 			return ncselector_offer_input (selector, ni);
72 		}
73 
74 		Plane* get_plane () const noexcept;
75 
76 	private:
common_init(ncplane * plane,const ncselector_options * opts=nullptr)77 		void common_init (ncplane *plane, const ncselector_options *opts = nullptr)
78 		{
79 			if (plane == nullptr)
80 				throw invalid_argument ("'plane' must be a valid pointer");
81 
82 			selector = ncselector_create (plane, opts == nullptr ? &default_options : opts);
83 			if (selector == nullptr)
84 				throw init_error ("Notcurses failed to create a new selector");
85 		}
86 
87 	private:
88 		ncselector *selector;
89 	};
90 }
91 #endif
92