DUDS
Distributed Update of Data from Something
MenuView.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2019 Jeff Jackowski
9  */
10 #ifndef MENUVIEW_HPP
11 #define MENUVIEW_HPP
12 
14 #include <duds/ui/Page.hpp>
15 #include <boost/any.hpp>
16 #include <list>
17 
18 namespace duds { namespace ui { namespace menu {
19 
20 class Menu;
21 class MenuItem;
22 class MenuOutput;
23 class MenuOutputAccess;
24 
46 class MenuView : public Page {
47 private:
51  boost::any ctx;
55  std::shared_ptr<Menu> parent;
64  int currSel;
68  int nextSel;
72  int outvUsers;
80  int updateIdx;
84  bool choseItem;
89  void incUser();
94  void decUser();
104  int adv(int pos);
113  int retr(int pos);
123  void insertion(std::size_t idx);
132  void removal(std::size_t idx);
133  friend Menu;
134  friend MenuOutput;
135 public:
144  MenuView();
154  void attach(const std::shared_ptr<Menu> &menu);
160  static std::shared_ptr<MenuView> make(const std::shared_ptr<Menu> &menu) {
161  std::shared_ptr<MenuView> mv = std::make_shared<MenuView>();
162  mv->attach(menu);
163  return mv;
164  }
168  const std::shared_ptr<Menu> &menu() const {
169  return parent;
170  }
180  int selectedIndex() const {
181  return currSel;
182  }
228  void backward(int dist = 1);
273  void forward(int dist = 1) {
274  backward(-dist);
275  }
300  void jump(int pos);
306  void jumpToFirst() {
307  jump(std::numeric_limits<int>::min());
308  }
314  void jumpToLast() {
315  jump(-1);
316  }
332  void chose();
342  bool queuedInput();
365  bool update();
369  const boost::any &context() const {
370  return ctx;
371  }
375  boost::any &context() {
376  return ctx;
377  }
382  std::shared_ptr<MenuView> shared_from_this() {
383  return std::static_pointer_cast<MenuView>(Page::shared_from_this());
384  }
385 };
386 
390 typedef std::shared_ptr<MenuView> MenuViewSptr;
391 
395 typedef std::weak_ptr<MenuView> MenuViewWptr;
396 
397 } } }
398 
399 #endif // #ifndef MENUVIEW_HPP
std::shared_ptr< MenuView > MenuViewSptr
A shared pointer to a MenuView.
Definition: MenuView.hpp:390
void decUser()
Decrements the internal count of subviews currently accessing this menu view.
Definition: MenuView.cpp:286
Represents the notion of a page that may be visited, and helps allow a way to track the path of pages...
Definition: Page.hpp:24
void incUser()
Increments the internal count of subviews currently accessing this menu view.
Definition: MenuView.cpp:281
boost::any ctx
Arbitrary context data that is available to MenuItem::chose().
Definition: MenuView.hpp:51
MenuView()
Constructs a new MenuView without attaching it to a Menu.
Definition: MenuView.cpp:17
void chose()
Queues a request to chose what will be the currently selected menu item during input processing when ...
Definition: MenuView.cpp:310
int updateIdx
The menu&#39;s update index value when this subview was last rendered.
Definition: MenuView.hpp:80
int selectedIndex() const
Returns the index of the currently selected item.
Definition: MenuView.hpp:180
void attach(const std::shared_ptr< Menu > &menu)
Attaches the view to a menu so that the view can operate on the menu&#39;s data, and the menu can inform ...
Definition: MenuView.cpp:21
void insertion(std::size_t idx)
Responds to the menu inserting a menu item to a spot other than the end of the menu.
Definition: MenuView.cpp:243
void removal(std::size_t idx)
Responds to the menu removing a menu item.
Definition: MenuView.cpp:262
int currSel
The index of the currently selected menu item.
Definition: MenuView.hpp:64
int adv(int pos)
Find the first menu item that is selectable, starting at and including pos, and advancing toward the ...
Definition: MenuView.cpp:32
void jumpToFirst()
Jumps to the first option in the menu.
Definition: MenuView.hpp:306
const boost::any & context() const
Returns the arbitrary context object for this view.
Definition: MenuView.hpp:369
const std::shared_ptr< Menu > & menu() const
Returns the Menu used by this view.
Definition: MenuView.hpp:168
int retr(int pos)
Find the first menu item that is selectable, starting at and including pos, and advancing toward the ...
Definition: MenuView.cpp:57
std::shared_ptr< Menu > parent
The parent menu that supplies the MenuItems.
Definition: MenuView.hpp:55
void jumpToLast()
Jumps to the last option in the menu.
Definition: MenuView.hpp:314
bool update()
Updates the view&#39;s selected and chosen menu item if there are no MenuOutput objects currently renderi...
Definition: MenuView.cpp:87
int nextSel
The index of the next menu item to select.
Definition: MenuView.hpp:68
std::weak_ptr< MenuView > MenuViewWptr
A weak pointer to a MenuView.
Definition: MenuView.hpp:395
A simple spinlock following the lockable and timed lockable concepts so that it can be used with std:...
Definition: Spinlock.hpp:52
int nextSelOff
Offset from the next selection.
Definition: MenuView.hpp:76
Keeps track of the selected menu item, and updates it based on user input.
Definition: MenuView.hpp:46
duds::general::Spinlock block
Protects this object&#39;s data from inappropriate modification when used in a multithreaded manner...
Definition: MenuView.hpp:60
static std::shared_ptr< MenuView > make(const std::shared_ptr< Menu > &menu)
Constructs a new MenuView, attaches it to a Menu, and returns the std::shared_ptr that manages the vi...
Definition: MenuView.hpp:160
bool choseItem
Flag used to queue an input request to chose the selected item.
Definition: MenuView.hpp:84
int outvUsers
The number of MenuOutput objects currently using this MenuView.
Definition: MenuView.hpp:72
void forward(int dist=1)
Changes the selection toward the front (first item) of the menu.
Definition: MenuView.hpp:273
void backward(int dist=1)
Changes the selection toward the back (last item) of the menu.
Definition: MenuView.cpp:291
boost::any & context()
Returns the arbitrary context object for this view.
Definition: MenuView.hpp:375
std::shared_ptr< MenuView > shared_from_this()
Helper function that returns a shared pointer to this object from the base class Page.
Definition: MenuView.hpp:382
bool queuedInput()
Returns true if any input for the menu view has been queued and is awaiting processing.
Definition: MenuView.cpp:315
void jump(int pos)
Jump to a particular option by position index.
Definition: MenuView.cpp:299