resize & signal handler

This commit is contained in:
Tristan Krause 2019-04-04 16:16:28 +02:00
parent 793f8a8370
commit 9f4aebef64
9 changed files with 117 additions and 30 deletions

View file

@ -5,7 +5,10 @@ WINDOW* View::win = nullptr;
View::View()
{
if(!win)
throw std::runtime_error("View::win not initialized, missing context!");
{
ERR_MSG = "View::win not initialized, missing context!";
raise(SIGINT);
}
getmaxyx(win, height, width); // init width and height
keypad(win, TRUE);
}
@ -42,7 +45,7 @@ void View::repaint()
start_y = floor((size.ws_row - height) / 2.);
mvwin(win, start_y, start_x);
clear();
wclear(win);
// generic draw
@ -56,3 +59,20 @@ void View::repaint()
refresh();
wrefresh(win);
}
// from: https://stackoverflow.com/a/37454181
std::vector<std::string> View::str_split(const std::string& str, const std::string delim)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}

View file

@ -8,6 +8,9 @@
#include <ncurses.h> // sudo apt-get install libncurses5-dev
#include <sys/ioctl.h>
#include <unistd.h>
#include <signal.h>
extern std::string ERR_MSG;
class View
{
@ -23,6 +26,7 @@ public:
void setTitle(std::string title);
virtual void repaint(void);
static std::vector<std::string> str_split(const std::string& str, const std::string delim);
virtual void draw(void) = 0;
virtual std::function<void(int)> keypress(int& key) = 0;

View file

@ -4,9 +4,28 @@ ViewInfo::ViewInfo()
{
}
void ViewInfo::setText(std::string text)
{
this->text = text;
}
void ViewInfo::setLabelClose(std::string label)
{
this->label_close = label;
}
void ViewInfo::draw()
{
mvwprintw(win, text_offset_y, text_offset_x, "%s", "hello");
int li = 0;
for(std::string line : str_split(text, "\n"))
mvwprintw(win, text_offset_y + li++, text_offset_x, "%s", line.c_str());
close_offset_x = (width - label_close.length()) / 2;
close_offset_y = height - 2;
wattron(win, A_REVERSE);
mvwprintw(win, close_offset_y, close_offset_x, "%s", label_close.c_str());
wattroff(win, A_REVERSE);
}
std::function<void(int)> ViewInfo::keypress(int& key)
@ -19,8 +38,13 @@ std::function<void(int)> ViewInfo::keypress(int& key)
{
// http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
MEVENT event;
if(getmouse(&event) == OK && event.bstate & BUTTON1_CLICKED)
key = -1; // do return from view
if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
{
size_t column = start_x + close_offset_x;
size_t row = start_y + close_offset_y;
if(event.y == row && event.x >= column && event.x < column + label_close.length())
key = -1; // do return from view
}
break;
}
case KEY_ENT:

View file

@ -7,10 +7,16 @@ class ViewInfo : public View
{
public:
ViewInfo(void);
virtual void setText(std::string text);
virtual void setLabelClose(std::string label);
virtual void draw(void) override;
virtual std::function<void(int)> keypress(int& key) override;
private:
std::string text;
std::string label_close;
int close_offset_x = 0;
int close_offset_y = 0;
constexpr static int text_offset_x = 2;
constexpr static int text_offset_y = 3;
};

View file

@ -16,8 +16,8 @@ private:
constexpr static int choice_offset_y = 3;
size_t selection = 0;
const std::vector<std::string> choices = {
"Beobachten",
"Exit"
"[ Informationen ]",
"[ Beenden ]"
};
};