From 9b5f74da2119dae7358135d2237676b93664b6ab Mon Sep 17 00:00:00 2001 From: denes Date: Thu, 16 Feb 2017 17:22:28 +0100 Subject: [PATCH] reading screen size and handling resize signal --- graph_browser.cpp | 46 ++++++++++++++++++++++++++++++++++++++++------ graph_browser.hpp | 17 ++++++++++------- main.cpp | 25 ++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/graph_browser.cpp b/graph_browser.cpp index 81b8b11..2f93d83 100644 --- a/graph_browser.cpp +++ b/graph_browser.cpp @@ -1,7 +1,8 @@ #include -#include #include +#include + #include "graph_browser.hpp" namespace { @@ -47,6 +48,7 @@ namespace { return retval; } + } @@ -77,6 +79,12 @@ GraphBrowser::GraphBrowser(Graph& g) , n_of_n_win_(0) , graph_(g) , history_() +{ + updateDimensions(); + initLayout(); +} + +void GraphBrowser::initLayout() { // window of the current node menu_ = new_menu((ITEM **)0); @@ -103,7 +111,7 @@ GraphBrowser::GraphBrowser(Graph& g) wrefresh(n_win); // 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); wborder(n_of_n_win_, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, ACS_TTEE, ACS_URCORNER, ACS_BTEE, ACS_LRCORNER); @@ -111,10 +119,11 @@ GraphBrowser::GraphBrowser(Graph& g) wrefresh(n_of_n_win_); // 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(); } + GraphBrowser::~GraphBrowser() { unpost_menu(menu_); @@ -238,8 +247,8 @@ void GraphBrowser::updateCurrent(const std::string& s) addItems(n); updateNeighbours(); - mvprintw(0, 0, "%s",std::string(TERM_MAX_X,' ').c_str()); - mvprintw(0, 0, historyToString(history_, 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()); refresh(); } @@ -247,7 +256,7 @@ void GraphBrowser::updateNeighbours() { 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) { 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()); @@ -304,3 +313,28 @@ void GraphBrowser::addItems(const std::vector& stringVector) for (int i = 0; i < old_items_count; ++i) 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()); +} diff --git a/graph_browser.hpp b/graph_browser.hpp index 46a5dba..b10428e 100644 --- a/graph_browser.hpp +++ b/graph_browser.hpp @@ -9,10 +9,6 @@ #include 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_LOWER_CASE_D = 100; @@ -20,9 +16,13 @@ public: static constexpr int KEY_UPPER_CASE_D = 68; static constexpr int KEY_UPPER_CASE_I = 73; - static constexpr size_t window_height = TERM_MAX_Y-2; - static constexpr size_t current_window_width = TERM_MAX_X/4; - static constexpr size_t n_window_width = TERM_MAX_X/4; + size_t term_max_x_ = 80; + size_t term_max_y_ = 24; + 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 destroy(); @@ -32,13 +32,16 @@ public: void mainLoop(); void setStartVertex(const std::string& s); + void terminalResizedEvent(int); private: + void initLayout(); void updateCurrent(const std::string& s); void updateNeighbours(); void addItems(const std::vector& stringVector); void deleteElement(bool delete_node); + void updateDimensions(); MENU *menu_; WINDOW *current_win_, *n_win, * n_of_n_win_; diff --git a/main.cpp b/main.cpp index 5623169..9c24563 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,14 @@ #include #include +#include + +#include #include "graph_browser.hpp" +using namespace std::placeholders; + std::string generateRandomString(size_t length) { const char charset[] = @@ -50,15 +55,29 @@ Graph generateRandomGraph(size_t number_of_vertices, return graph; } +std::function callback_function; + +void CallCb( int value ) +{ + callback_function(value); +} + int main(int argc, char* argv[]) { Graph g = generateRandomGraph(10, 200, 5, 20); GraphBrowser::init(); - GraphBrowser ni(g); - ni.setStartVertex(*g.begin()); - ni.mainLoop(); + GraphBrowser gb(g); + gb.setStartVertex(*g.begin()); + + 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(); return EXIT_SUCCESS;