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 <algorithm>
#include <cassert>
#include <algorithm>
#include "graph_browser.hpp"
namespace {
@ -47,6 +48,7 @@ namespace {
return retval;
}
}
@ -77,6 +79,12 @@ GraphBrowser::GraphBrowser(Graph<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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());
}

@ -9,10 +9,6 @@
#include <graph/graph.hpp>
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<std::string>& stringVector);
void deleteElement(bool delete_node);
void updateDimensions();
MENU *menu_;
WINDOW *current_win_, *n_win, * n_of_n_win_;

@ -1,9 +1,14 @@
#include <graph/graph.hpp>
#include <string>
#include <signal.h>
#include <functional>
#include "graph_browser.hpp"
using namespace std::placeholders;
std::string generateRandomString(size_t length)
{
const char charset[] =
@ -50,15 +55,29 @@ Graph<std::string> generateRandomGraph(size_t number_of_vertices,
return graph;
}
std::function<void(int)> callback_function;
void CallCb( int value )
{
callback_function(value);
}
int main(int argc, char* argv[])
{
Graph<std::string> 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;

Loading…
Cancel
Save