| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Author: Vitali Adamenka |
| 3 |
// Description: Source for describe ncurses main window. |
| 4 |
// Copyright (c) 2015 EPAM Systems. All Rights Reserved. |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
/* |
| 7 |
This file is part of Nfstrace. |
| 8 |
|
| 9 |
Nfstrace is free software: you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation, version 2 of the License. |
| 12 |
|
| 13 |
Nfstrace is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with Nfstrace. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
//------------------------------------------------------------------------------ |
| 22 |
#include <stdexcept> |
| 23 |
|
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include "main_window.h" |
| 27 |
//------------------------------------------------------------------------------ |
| 28 |
void MainWindow::init() |
| 29 |
{ |
| 30 |
if(_window != nullptr) |
| 31 |
{ |
| 32 |
destroy(); |
| 33 |
} |
| 34 |
_window = initscr(); |
| 35 |
if(_window == nullptr) |
| 36 |
{ |
| 37 |
throw std::runtime_error("Initialization of Main window failed."); |
| 38 |
} |
| 39 |
noecho(); |
| 40 |
cbreak(); |
| 41 |
intrflush(stdscr, false); // flush main window |
| 42 |
curs_set(0); // disable blinking cursore |
| 43 |
keypad(_window, true); // init keyboard |
| 44 |
timeout(200); // set keyboard timeout |
| 45 |
start_color(); // set background color |
| 46 |
if(_window != nullptr) |
| 47 |
{ |
| 48 |
werase(_window); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void MainWindow::destroy() |
| 53 |
{ |
| 54 |
nocbreak(); |
| 55 |
echo(); |
| 56 |
clrtoeol(); |
| 57 |
refresh(); |
| 58 |
endwin(); |
| 59 |
_window = nullptr; |
| 60 |
} |
| 61 |
|
| 62 |
void MainWindow::cleanStdin(int key) |
| 63 |
{ |
| 64 |
while((key != EOF) && (key != '\n') && (key != ' ')) |
| 65 |
{ |
| 66 |
key = getch(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
MainWindow::MainWindow() |
| 71 |
: _window{nullptr} |
| 72 |
{ |
| 73 |
init(); |
| 74 |
} |
| 75 |
|
| 76 |
MainWindow::~MainWindow() |
| 77 |
{ |
| 78 |
destroy(); |
| 79 |
} |
| 80 |
|
| 81 |
int MainWindow::inputKeys() |
| 82 |
{ |
| 83 |
int key = wgetch(_window); |
| 84 |
|
| 85 |
if(key != KEY_UP && key != KEY_DOWN && key != KEY_LEFT && key != KEY_RIGHT) |
| 86 |
{ |
| 87 |
key = 0; |
| 88 |
} |
| 89 |
cleanStdin(key); |
| 90 |
return key; |
| 91 |
} |
| 92 |
|
| 93 |
void MainWindow::resize() |
| 94 |
{ |
| 95 |
if(_window != nullptr) |
| 96 |
{ |
| 97 |
destroy(); |
| 98 |
} |
| 99 |
init(); |
| 100 |
} |
| 101 |
|
| 102 |
void MainWindow::update() const |
| 103 |
{ |
| 104 |
if(_window != nullptr) |
| 105 |
{ |
| 106 |
refresh(); |
| 107 |
} |
| 108 |
} |
| 109 |
//------------------------------------------------------------------------------ |