1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_MENU_H
22 #define NCMPCPP_MENU_H
24 #include <boost/iterator/indirect_iterator.hpp>
25 #include <boost/iterator/transform_iterator.hpp>
26 #include <boost/range/detail/any_iterator.hpp>
33 #include "strbuffer.h"
45 Selectable
= (1 << 1),
51 Properties(Type properties
= Selectable
)
52 : m_properties(properties
)
55 void setBold(bool is_bold
)
60 m_properties
&= ~Bold
;
62 void setSelectable(bool is_selectable
)
65 m_properties
|= Selectable
;
67 m_properties
&= ~(Selectable
| Selected
);
69 void setSelected(bool is_selected
)
74 m_properties
|= Selected
;
76 m_properties
&= ~Selected
;
78 void setInactive(bool is_inactive
)
81 m_properties
|= Inactive
;
83 m_properties
&= ~Inactive
;
85 void setSeparator(bool is_separator
)
88 m_properties
|= Separator
;
90 m_properties
&= ~Separator
;
93 bool isBold() const { return m_properties
& Bold
; }
94 bool isSelectable() const { return m_properties
& Selectable
; }
95 bool isSelected() const { return m_properties
& Selected
; }
96 bool isInactive() const { return m_properties
& Inactive
; }
97 bool isSeparator() const { return m_properties
& Separator
; }
100 unsigned m_properties
;
103 template <typename ValueT
>
104 using PropertiesIterator
= boost::range_detail::any_iterator
<
106 boost::random_access_traversal_tag
,
111 typedef PropertiesIterator
<Properties
> Iterator
;
112 typedef PropertiesIterator
<const Properties
> ConstIterator
;
116 virtual bool empty() const = 0;
117 virtual size_t size() const = 0;
118 virtual size_t choice() const = 0;
119 virtual void highlight(size_t pos
) = 0;
121 virtual Iterator
currentP() = 0;
122 virtual ConstIterator
currentP() const = 0;
123 virtual Iterator
beginP() = 0;
124 virtual ConstIterator
beginP() const = 0;
125 virtual Iterator
endP() = 0;
126 virtual ConstIterator
endP() const = 0;
129 inline List::Properties::Type
operator|(List::Properties::Type lhs
, List::Properties::Type rhs
)
131 return List::Properties::Type(unsigned(lhs
) | unsigned(rhs
));
133 inline List::Properties::Type
&operator|=(List::Properties::Type
&lhs
, List::Properties::Type rhs
)
138 inline List::Properties::Type
operator&(List::Properties::Type lhs
, List::Properties::Type rhs
)
140 return List::Properties::Type(unsigned(lhs
) & unsigned(rhs
));
142 inline List::Properties::Type
&operator&=(List::Properties::Type
&lhs
, List::Properties::Type rhs
)
148 // for range-based for loop
149 inline List::Iterator
begin(List
&list
) { return list
.beginP(); }
150 inline List::ConstIterator
begin(const List
&list
) { return list
.beginP(); }
151 inline List::Iterator
end(List
&list
) { return list
.endP(); }
152 inline List::ConstIterator
end(const List
&list
) { return list
.endP(); }
154 /// This template class is generic menu capable of
155 /// holding any std::vector compatible values.
156 template <typename ItemT
> struct Menu
: Window
, List
158 struct Item
: List::Properties
160 template <bool Const
>
161 struct PropertiesExtractor
163 typedef PropertiesExtractor type
;
165 typedef typename
std::conditional
<Const
, const Properties
, Properties
>::type Properties_
;
166 typedef typename
std::conditional
<Const
, const Item
, Item
>::type Item_
;
168 Properties_
&operator()(Item_
&i
) const {
169 return static_cast<Properties_
&>(i
);
175 friend struct Menu
<ItemT
>;
178 Item(ItemT value_
, Properties::Type properties
)
179 : Properties(properties
)
183 ItemT
&value() { return m_value
; }
184 const ItemT
&value() const { return m_value
; }
186 ItemT
&operator*() { return m_value
; }
187 const ItemT
&operator*() const { return m_value
; }
190 static Item
mkSeparator()
193 item
.setSelectable(false);
194 item
.setSeparator(true);
201 typedef typename
std::vector
<Item
>::iterator Iterator
;
202 typedef typename
std::vector
<Item
>::const_iterator ConstIterator
;
203 typedef std::reverse_iterator
<Iterator
> ReverseIterator
;
204 typedef std::reverse_iterator
<ConstIterator
> ConstReverseIterator
;
206 typedef boost::indirect_iterator
<
209 boost::random_access_traversal_tag
211 typedef boost::indirect_iterator
<
214 boost::random_access_traversal_tag
215 > ConstValueIterator
;
216 typedef std::reverse_iterator
<ValueIterator
> ReverseValueIterator
;
217 typedef std::reverse_iterator
<ConstValueIterator
> ConstReverseValueIterator
;
219 typedef boost::transform_iterator
<
220 typename
Item::template PropertiesExtractor
<false>,
222 > PropertiesIterator
;
223 typedef boost::transform_iterator
<
224 typename
Item::template PropertiesExtractor
<true>,
226 > ConstPropertiesIterator
;
228 /// Function helper prototype used to display each option on the screen.
229 /// If not set by setItemDisplayer(), menu won't display anything.
230 /// @see setItemDisplayer()
231 typedef std::function
<void(Menu
<ItemT
> &)> ItemDisplayer
;
235 Menu(size_t startx
, size_t starty
, size_t width
, size_t height
,
236 const std::string
&title
, Color color
, Border border
);
238 Menu(const Menu
&rhs
);
240 Menu
&operator=(Menu rhs
);
242 /// Sets helper function that is responsible for displaying items
243 /// @param ptr function pointer that matches the ItemDisplayer prototype
244 void setItemDisplayer(const ItemDisplayer
&f
) { m_item_displayer
= f
; }
246 /// Resizes the list to given size (adequate to std::vector::resize())
247 /// @param size requested size
248 void resizeList(size_t new_size
);
250 /// Adds a new option to list
251 void addItem(ItemT item
, Properties::Type properties
= Properties::Selectable
);
253 /// Adds separator to list
256 /// Inserts a new option to the list at given position
257 void insertItem(size_t pos
, ItemT item
, Properties::Type properties
= Properties::Selectable
);
259 /// Inserts separator to list at given position
260 /// @param pos initial position of inserted separator
261 void insertSeparator(size_t pos
);
263 /// Deletes item from given position
264 /// @param pos given position of item to be deleted
265 void deleteItem(size_t pos
);
267 /// Moves the highlighted position to the given line of window
268 /// @param y Y position of menu window to be highlighted
269 /// @return true if the position is reachable, false otherwise
272 /// Checks if list is empty
273 /// @return true if list is empty, false otherwise
274 virtual bool empty() const OVERRIDE
{ return m_items
.empty(); }
276 /// @return size of the list
277 virtual size_t size() const OVERRIDE
{ return m_items
.size(); }
279 /// @return currently highlighted position
280 virtual size_t choice() const OVERRIDE
;
282 /// Highlights given position
283 /// @param pos position to be highlighted
284 virtual void highlight(size_t position
) OVERRIDE
;
286 /// Refreshes the menu window
287 /// @see Window::refresh()
288 virtual void refresh() OVERRIDE
;
290 /// Scrolls by given amount of lines
291 /// @param where indicated where exactly one wants to go
292 /// @see Window::scroll()
293 virtual void scroll(Scroll where
) OVERRIDE
;
295 /// Cleares all options, used filters etc. It doesn't reset highlighted position though.
297 virtual void clear() OVERRIDE
;
299 /// Sets highlighted position to 0
302 /// Sets prefix, that is put before each selected item to indicate its selection
303 /// Note that the passed variable is not deleted along with menu object.
304 /// @param b pointer to buffer that contains the prefix
305 void setSelectedPrefix(const Buffer
&b
) { m_selected_prefix
= b
; }
307 /// Sets suffix, that is put after each selected item to indicate its selection
308 /// Note that the passed variable is not deleted along with menu object.
309 /// @param b pointer to buffer that contains the suffix
310 void setSelectedSuffix(const Buffer
&b
) { m_selected_suffix
= b
; }
312 /// Sets custom color of highlighted position
313 /// @param col custom color
314 void setHighlightColor(Color color
) { m_highlight_color
= std::move(color
); }
316 /// @return state of highlighting
317 bool isHighlighted() { return m_highlight_enabled
; }
319 /// Turns on/off highlighting
320 /// @param state state of hihglighting
321 void setHighlighting(bool state
) { m_highlight_enabled
= state
; }
323 /// Turns on/off cyclic scrolling
324 /// @param state state of cyclic scrolling
325 void cyclicScrolling(bool state
) { m_cyclic_scroll_enabled
= state
; }
327 /// Turns on/off centered cursor
328 /// @param state state of centered cursor
329 void centeredCursor(bool state
) { m_autocenter_cursor
= state
; }
331 /// @return currently drawn item. The result is defined only within
332 /// drawing function that is called by refresh()
334 ConstIterator
drawn() const { return begin() + m_drawn_position
; }
336 /// @param pos requested position
337 /// @return reference to item at given position
338 /// @throw std::out_of_range if given position is out of range
339 Menu
<ItemT
>::Item
&at(size_t pos
) { return m_items
.at(pos
); }
341 /// @param pos requested position
342 /// @return const reference to item at given position
343 /// @throw std::out_of_range if given position is out of range
344 const Menu
<ItemT
>::Item
&at(size_t pos
) const { return m_items
.at(pos
); }
346 /// @param pos requested position
347 /// @return const reference to item at given position
348 const Menu
<ItemT
>::Item
&operator[](size_t pos
) const { return m_items
[pos
]; }
350 /// @param pos requested position
351 /// @return const reference to item at given position
352 Menu
<ItemT
>::Item
&operator[](size_t pos
) { return m_items
[pos
]; }
354 Iterator
current() { return Iterator(m_items
.begin() + m_highlight
); }
355 ConstIterator
current() const { return ConstIterator(m_items
.begin() + m_highlight
); }
356 ReverseIterator
rcurrent() { return ReverseIterator(++current()); }
357 ConstReverseIterator
rcurrent() const { return ReverseIterator(++current()); }
359 ValueIterator
currentV() { return ValueIterator(m_items
.begin() + m_highlight
); }
360 ConstValueIterator
currentV() const { return ConstValueIterator(m_items
.begin() + m_highlight
); }
361 ReverseValueIterator
rcurrentV() { return ReverseValueIterator(++currentV()); }
362 ConstReverseValueIterator
rcurrentV() const { return ConstReverseValueIterator(++currentV()); }
364 Iterator
begin() { return Iterator(m_items
.begin()); }
365 ConstIterator
begin() const { return ConstIterator(m_items
.begin()); }
366 Iterator
end() { return Iterator(m_items
.end()); }
367 ConstIterator
end() const { return ConstIterator(m_items
.end()); }
369 ReverseIterator
rbegin() { return ReverseIterator(end()); }
370 ConstReverseIterator
rbegin() const { return ConstReverseIterator(end()); }
371 ReverseIterator
rend() { return ReverseIterator(begin()); }
372 ConstReverseIterator
rend() const { return ConstReverseIterator(begin()); }
374 ValueIterator
beginV() { return ValueIterator(begin()); }
375 ConstValueIterator
beginV() const { return ConstValueIterator(begin()); }
376 ValueIterator
endV() { return ValueIterator(end()); }
377 ConstValueIterator
endV() const { return ConstValueIterator(end()); }
379 ReverseValueIterator
rbeginV() { return ReverseValueIterator(endV()); }
380 ConstReverseIterator
rbeginV() const { return ConstReverseValueIterator(endV()); }
381 ReverseValueIterator
rendV() { return ReverseValueIterator(beginV()); }
382 ConstReverseValueIterator
rendV() const { return ConstReverseValueIterator(beginV()); }
384 virtual List::Iterator
currentP() OVERRIDE
{
385 return List::Iterator(PropertiesIterator(m_items
.begin() + m_highlight
));
387 virtual List::ConstIterator
currentP() const OVERRIDE
{
388 return List::ConstIterator(ConstPropertiesIterator(m_items
.begin() + m_highlight
));
390 virtual List::Iterator
beginP() OVERRIDE
{
391 return List::Iterator(PropertiesIterator(m_items
.begin()));
393 virtual List::ConstIterator
beginP() const OVERRIDE
{
394 return List::ConstIterator(ConstPropertiesIterator(m_items
.begin()));
396 virtual List::Iterator
endP() OVERRIDE
{
397 return List::Iterator(PropertiesIterator(m_items
.end()));
399 virtual List::ConstIterator
endP() const OVERRIDE
{
400 return List::ConstIterator(ConstPropertiesIterator(m_items
.end()));
404 bool isHighlightable(size_t pos
)
406 return !m_items
[pos
].isSeparator()
407 && !m_items
[pos
].isInactive();
410 ItemDisplayer m_item_displayer
;
412 std::vector
<Item
> m_items
;
417 Color m_highlight_color
;
418 bool m_highlight_enabled
;
419 bool m_cyclic_scroll_enabled
;
421 bool m_autocenter_cursor
;
423 size_t m_drawn_position
;
425 Buffer m_selected_prefix
;
426 Buffer m_selected_suffix
;
431 #endif // NCMPCPP_MENU_H