B15F
Board 15 Famulus Edition
view.cpp
1 #include "view.h"
2 
3 WINDOW* View::win = nullptr;
4 
6 {
7  if(!win)
8  {
9  B15F::abort("View::win not initialized, missing context");
10  }
11  getmaxyx(win, height, width); // init width and height
12  keypad(win, TRUE);
13 }
14 
16 {
17 }
18 
19 void View::setWinContext(WINDOW* win)
20 {
21  View::win = win;
22 }
23 
25 {
26  return win;
27 }
28 
29 // from: https://stackoverflow.com/a/37454181
30 std::vector<std::string> View::str_split(const std::string& str, const std::string delim)
31 {
32  std::vector<std::string> tokens;
33  size_t prev = 0, pos = 0;
34  do
35  {
36  pos = str.find(delim, prev);
37  if (pos == std::string::npos) pos = str.length();
38  std::string token = str.substr(prev, pos-prev);
39  if (!token.empty()) tokens.push_back(token);
40  prev = pos + delim.length();
41  }
42  while (pos < str.length() && prev < str.length());
43  return tokens;
44 }
45 
46 
47 void View::setTitle(std::string title)
48 {
49  this->title = title;
50 }
51 
53 {
54  // get screen size
55  struct winsize size;
56  if (ioctl(0, TIOCGWINSZ, (char *) &size) < 0)
57  throw std::runtime_error("TIOCGWINSZ error");
58 
59 
60  start_x = floor((size.ws_col - width) / 2.);
61  start_y = floor((size.ws_row - height) / 2.);
62 
63  curs_set(0); // hide cursor
64  mvwin(win, start_y, start_x);
65  clear();
66  wclear(win);
67 
68  // generic draw
69  box(win, 0, 0);
70  int offset_x = (width - title.length()) / 2;
71  mvwprintw(win, 1, offset_x, "%s", title.c_str());
72 
73  // specific draw
74  draw();
75 
76  refresh();
77  wrefresh(win);
78 }
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::title
std::string title
title of the view
Definition: view.h:83
View::~View
virtual ~View(void)
Definition: view.cpp:15
View::getWinContext
static WINDOW * getWinContext(void)
Definition: view.cpp:24
B15F::abort
static void abort(std::string msg)
Definition: b15f.cpp:175
View::win
static WINDOW * win
static window contexts for all views
Definition: view.h:86
View::setWinContext
static void setWinContext(WINDOW *win)
Definition: view.cpp:19
View::str_split
static std::vector< std::string > str_split(const std::string &str, const std::string delim)
Definition: view.cpp:30
View::repaint
virtual void repaint(void)
Definition: view.cpp:52
View::start_x
int start_x
x offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:81
View::View
View(void)
Definition: view.cpp:5
View::setTitle
virtual void setTitle(std::string title)
Definition: view.cpp:47
View::start_y
int start_y
y offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:82
View::draw
virtual void draw(void)=0