reading screen size and handling resize signal

master
denes 8 years ago
parent d2936e76d0
commit 9b5f74da21
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

@ -1,7 +1,8 @@
#include <cstring> #include <cstring>
#include <algorithm>
#include <cassert> #include <cassert>
#include <algorithm>
#include "graph_browser.hpp" #include "graph_browser.hpp"
namespace { namespace {
@ -47,6 +48,7 @@ namespace {
return retval; return retval;
} }
} }
@ -77,6 +79,12 @@ GraphBrowser::GraphBrowser(Graph<std::string>& g)
, n_of_n_win_(0) , n_of_n_win_(0)
, graph_(g) , graph_(g)
, history_() , history_()
{
updateDimensions();
initLayout();
}
void GraphBrowser::initLayout()
{ {
// window of the current node // window of the current node
menu_ = new_menu((ITEM **)0); menu_ = new_menu((ITEM **)0);
@ -103,7 +111,7 @@ GraphBrowser::GraphBrowser(Graph<std::string>& g)
wrefresh(n_win); wrefresh(n_win);
// window of the neighbours'neighbours of the current vertex // window of the neighbours'neighbours of the current vertex
n_of_n_win_ = newwin(window_height, TERM_MAX_X-current_window_width-n_window_width, n_of_n_win_ = newwin(window_height, term_max_x_-current_window_width-n_window_width,
1, n_window_width+current_window_width); 1, n_window_width+current_window_width);
wborder(n_of_n_win_, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, wborder(n_of_n_win_, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
ACS_TTEE, ACS_URCORNER, ACS_BTEE, ACS_LRCORNER); ACS_TTEE, ACS_URCORNER, ACS_BTEE, ACS_LRCORNER);
@ -111,10 +119,11 @@ GraphBrowser::GraphBrowser(Graph<std::string>& g)
wrefresh(n_of_n_win_); wrefresh(n_of_n_win_);
// bottom text // bottom text
mvprintw(TERM_MAX_Y-1, 0, "ESC:exit, cursors:navigate, d/i:del/ins edge, D/I:del/ins node"); mvprintw(term_max_y_-1, 0, "ESC:exit, cursors:navigate, d/i:del/ins edge, D/I:del/ins node");
refresh(); refresh();
} }
GraphBrowser::~GraphBrowser() GraphBrowser::~GraphBrowser()
{ {
unpost_menu(menu_); unpost_menu(menu_);
@ -238,8 +247,8 @@ void GraphBrowser::updateCurrent(const std::string& s)
addItems(n); addItems(n);
updateNeighbours(); updateNeighbours();
mvprintw(0, 0, "%s",std::string(TERM_MAX_X,' ').c_str()); mvprintw(0, 0, "%s",std::string(term_max_x_,' ').c_str());
mvprintw(0, 0, historyToString(history_, TERM_MAX_X).c_str()); mvprintw(0, 0, historyToString(history_, term_max_x_).c_str());
refresh(); refresh();
} }
@ -247,7 +256,7 @@ void GraphBrowser::updateNeighbours()
{ {
const size_t n_width = n_window_width-1; const size_t n_width = n_window_width-1;
const size_t n_of_n_width = TERM_MAX_X-current_window_width-n_window_width-2; const size_t n_of_n_width = term_max_x_-current_window_width-n_window_width-2;
for (int i = 1; i < window_height-1; ++i) { for (int i = 1; i < window_height-1; ++i) {
mvwprintw(n_win, i, 1, "%s", std::string(n_width,' ').c_str()); mvwprintw(n_win, i, 1, "%s", std::string(n_width,' ').c_str());
mvwprintw(n_of_n_win_, i, 1, "%s", std::string(n_of_n_width,' ').c_str()); mvwprintw(n_of_n_win_, i, 1, "%s", std::string(n_of_n_width,' ').c_str());
@ -304,3 +313,28 @@ void GraphBrowser::addItems(const std::vector<std::string>& stringVector)
for (int i = 0; i < old_items_count; ++i) for (int i = 0; i < old_items_count; ++i)
assert(free_item(old_items[i]) == E_OK); assert(free_item(old_items[i]) == E_OK);
} }
void GraphBrowser::updateDimensions()
{
term_max_x_ = COLS;
term_max_y_ = LINES;
window_height = term_max_y_-2;
current_window_width = term_max_x_/4;
n_window_width = term_max_x_/4;
}
void GraphBrowser::terminalResizedEvent(int)
{
endwin();
refresh();
clear();
free_menu(menu_);
delwin(current_win_);
delwin(n_win);
delwin(n_of_n_win_);
updateDimensions();
initLayout();
updateCurrent(history_.back());
}

@ -9,10 +9,6 @@
#include <graph/graph.hpp> #include <graph/graph.hpp>
class GraphBrowser { class GraphBrowser {
public:
static constexpr size_t TERM_MAX_X = 80;
static constexpr size_t TERM_MAX_Y = 24;
static constexpr int KEY_ESC = 27; static constexpr int KEY_ESC = 27;
static constexpr int KEY_LOWER_CASE_D = 100; static constexpr int KEY_LOWER_CASE_D = 100;
@ -20,9 +16,13 @@ public:
static constexpr int KEY_UPPER_CASE_D = 68; static constexpr int KEY_UPPER_CASE_D = 68;
static constexpr int KEY_UPPER_CASE_I = 73; static constexpr int KEY_UPPER_CASE_I = 73;
static constexpr size_t window_height = TERM_MAX_Y-2; size_t term_max_x_ = 80;
static constexpr size_t current_window_width = TERM_MAX_X/4; size_t term_max_y_ = 24;
static constexpr size_t n_window_width = TERM_MAX_X/4; size_t window_height = term_max_y_-2;
size_t current_window_width = term_max_x_/4;
size_t n_window_width = term_max_x_/4;
public:
void static init(); void static init();
void static destroy(); void static destroy();
@ -32,13 +32,16 @@ public:
void mainLoop(); void mainLoop();
void setStartVertex(const std::string& s); void setStartVertex(const std::string& s);
void terminalResizedEvent(int);
private: private:
void initLayout();
void updateCurrent(const std::string& s); void updateCurrent(const std::string& s);
void updateNeighbours(); void updateNeighbours();
void addItems(const std::vector<std::string>& stringVector); void addItems(const std::vector<std::string>& stringVector);
void deleteElement(bool delete_node); void deleteElement(bool delete_node);
void updateDimensions();
MENU *menu_; MENU *menu_;
WINDOW *current_win_, *n_win, * n_of_n_win_; WINDOW *current_win_, *n_win, * n_of_n_win_;

@ -1,9 +1,14 @@
#include <graph/graph.hpp> #include <graph/graph.hpp>
#include <string> #include <string>
#include <signal.h>
#include <functional>
#include "graph_browser.hpp" #include "graph_browser.hpp"
using namespace std::placeholders;
std::string generateRandomString(size_t length) std::string generateRandomString(size_t length)
{ {
const char charset[] = const char charset[] =
@ -50,15 +55,29 @@ Graph<std::string> generateRandomGraph(size_t number_of_vertices,
return graph; return graph;
} }
std::function<void(int)> callback_function;
void CallCb( int value )
{
callback_function(value);
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Graph<std::string> g = generateRandomGraph(10, 200, 5, 20); Graph<std::string> g = generateRandomGraph(10, 200, 5, 20);
GraphBrowser::init(); GraphBrowser::init();
GraphBrowser ni(g); GraphBrowser gb(g);
ni.setStartVertex(*g.begin()); gb.setStartVertex(*g.begin());
ni.mainLoop();
callback_function = std::bind1st(std::mem_fun(&GraphBrowser::terminalResizedEvent), &gb);
struct sigaction act;
act.sa_handler = CallCb;
sigaction(SIGWINCH, &act, NULL);
gb.mainLoop();
GraphBrowser::destroy(); GraphBrowser::destroy();
return EXIT_SUCCESS; return EXIT_SUCCESS;

Loading…
Cancel
Save