Snoke
menu.cpp
Go to the documentation of this file.
1 #include "menu.h"
2 
7 {
8  mvaddstr(y, x, name);
9 }
10 
14 MenuItem::MenuItem(const char* name, int x, int y, int width, int height) : Widget(), x(x), y(y), width(width), height(height), name(name)
15 {}
16 
21 void Menu::add(const char* name)
22 {
23  MenuItem* item = new MenuItem(name, x + (width - strlen(name)) / 2, y + this->childLength() * 2, strlen(name), 1);
24  item->listener(EVENT_FOCUS, std::bind(&Menu::focus, this, this->childLength()));
25  item->listener(EVENT_UNFOCUS, std::bind(&Menu::unfocus, this, this->childLength()));
26  this->add(item);
27 }
28 
35 void Menu::focus(int index)
36 {
37  mvaddch(y + index * 2, x, '>');
38  refresh();
39 }
40 
47 void Menu::unfocus(int index)
48 {
49  mvaddch(y + index * 2, x, ' ');
50  refresh();
51 }
52 
56 void Menu::draw()
57 {
58  Widget* current = firstChild();
59  while (current)
60  {
61  current->draw();
62  current = current->next();
63  }
64 }
65 
73 Menu::Menu(int x, int y, int width, int height) : Widget(), x(x), y(y), width(width), height(height)
74 {}
Definition: menu.h:12
int x
Definition: menu.h:15
int width
Definition: menu.h:15
int y
Definition: menu.h:15
MenuItem(const char *name, int x, int y, int width, int height)
Definition: menu.cpp:14
#define EVENT_FOCUS
Definition: widget.h:14
void add(const char *name)
Definition: menu.cpp:21
void draw()
Definition: menu.cpp:56
Menu(int x, int y, int width, int height)
Definition: menu.cpp:73
unsigned short childLength()
Definition: widget.cpp:112
void unfocus(int index)
Definition: menu.cpp:47
virtual void draw()
Definition: widget.cpp:7
#define EVENT_UNFOCUS
Definition: widget.h:15
void listener(unsigned char name, Listener function)
Definition: widget.cpp:150
int height
Definition: menu.h:15
Definition: widget.h:36
void add(Widget *child)
Definition: widget.cpp:86
Widget * next()
Definition: widget.cpp:59
const char * name
Definition: menu.h:16
void draw()
Definition: menu.cpp:6
void focus(int index)
Definition: menu.cpp:35
Widget * firstChild()
Definition: widget.cpp:32