B15F
Board 15 Famulus Edition
view_info.cpp
1 #include "view_info.h"
2 
3 ViewInfo::ViewInfo()
4 {
5  calls.push_back(nullptr);
6 }
7 
8 void ViewInfo::setText(std::string text)
9 {
10  this->text = text;
11 }
12 
13 void ViewInfo::setLabelClose(std::string label)
14 {
15  this->label_close = label;
16 }
17 
18 void ViewInfo::setCall(call_t call)
19 {
20  calls[0] = call;
21 }
22 
24 {
25  int li = 0;
26  for(std::string line : str_split(text, "\n"))
27  mvwprintw(win, text_offset_y + li++, text_offset_x, "%s", line.c_str());
28 
29  close_offset_x = (width - label_close.length()) / 2;
30  close_offset_y = height - 2;
31 
32  wattron(win, A_REVERSE);
33  mvwprintw(win, close_offset_y, close_offset_x, "%s", label_close.c_str());
34  wattroff(win, A_REVERSE);
35 }
36 
37 call_t ViewInfo::keypress(int& key)
38 {
39  switch(key)
40  {
41 
42  case KEY_MOUSE:
43  {
44  // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
45  MEVENT event;
46  if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
47  {
48  size_t column = start_x + close_offset_x;
49  size_t row = start_y + close_offset_y;
50  size_t mouse_x = event.x, mouse_y = event.y;
51  if(mouse_y == row && mouse_x >= column && mouse_x < column + label_close.length())
52  key = -1; // do return from view
53  }
54  break;
55  }
56  case KEY_ENT:
57  key = -1; // do return from view
58  break;
59  default:
60  break;
61  }
62  return calls[0];
63 }
View::height
int height
height of view in terminal characters
Definition: view.h:80
View::width
int width
width of view in terminal characters
Definition: view.h:79
View::calls
std::vector< call_t > calls
calls (function pointers) for different button actions in the view (if any)
Definition: view.h:84
View::win
static WINDOW * win
static window contexts for all views
Definition: view.h:86
View::str_split
static std::vector< std::string > str_split(const std::string &str, const std::string delim)
Definition: view.cpp:30
ViewInfo::keypress
virtual call_t keypress(int &key) override
Definition: view_info.cpp:37
View::start_x
int start_x
x offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:81
View::start_y
int start_y
y offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:82
ViewInfo::draw
virtual void draw(void) override
Definition: view_info.cpp:23
View::KEY_ENT
constexpr static int KEY_ENT
Key value for the Enter key.
Definition: view.h:87