Skip to content
This repository has been archived by the owner on Jul 23, 2022. It is now read-only.

Commit

Permalink
new curses window members
Browse files Browse the repository at this point in the history
  • Loading branch information
guimauveb committed Dec 25, 2020
1 parent ea64a89 commit 3f57653
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 129 deletions.
9 changes: 5 additions & 4 deletions Curses/include/CursesWindow.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../source/helper/WinSize.cxx"
#include "../source/helper/point.cxx"
#include "../../Log/include/Log.hxx"

#include <ncurses.h>

Expand All @@ -21,7 +22,7 @@ class CursesWindow
void refreshWin();
void resetWin();

// TODO
// TODO - Store multiple sub/der windows
void setDerwin(int nl, int nc, int by, int bx);
WINDOW * getDerwin();

Expand All @@ -36,8 +37,8 @@ class CursesWindow
void drawBox(int y, int x);

/* Pass row / col etc */
void printInMiddleWithBackground(int y, int x, int str_begin, const std::string& str, chtype color);
void printInMiddle(int starty, int startx, int width, std::string str, chtype color);
void printAtTop(int starty, int startx, int width, std::string str, chtype color);
void wMove(int y, int x);
void clearToEOL();

Expand All @@ -50,9 +51,9 @@ class CursesWindow
WINDOW *win = nullptr;
// TODO - Use a vector of derwins if needed
/* Derwin that can be used for a menu */
WINDOW * der = nullptr;
WINDOW *der = nullptr;
/* Save all parameters we got from the initialisation so we can resize it */
int row, col, begy, begx;
std::string winName;
std::string win_name;
};

73 changes: 35 additions & 38 deletions Curses/source/CursesWindow.cxx
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
#include "../include/CursesWindow.hxx"
//#include <fstream>

CursesWindow::CursesWindow(int r, int c, int by, int bx, std::string name)
:row{r}, col{c}, begy{by}, begx{bx}
{
winName = name;
// std::ofstream ofstr;
// std::string e = "Allocating memory for window " + winName;
// ofstr.open("newwin.txt", std::ios::app);
// ofstr << e << '\n';
// ofstr.close();
:row{r}, col{c}, begy{by}, begx{bx}, win_name{name}
{
if ((win = newwin(row, col, begy, begx)) == NULL) {
// std::ofstream ofstr;
// std::string e = "Error while allocating memory for window: " + winName;
// ofstr.open("newwin_log.txt", std::ios::out);
// ofstr << e;
// ofstr.close();
Log() << "Could not allocate memory for window " << win_name;
exit(1);
}
wrefresh(win);
}

CursesWindow::~CursesWindow()
{
// std::ofstream ofstr;
// std::string e = "Freeing memory of window " + winName;
// ofstr.open("del_win_log.txt", std::ios::out);
// ofstr << e << '\n';
// ofstr.close();
if (win != nullptr) {
delwin(win);
}
delwin(win);
}

WINDOW *CursesWindow::getRawPtr()
Expand Down Expand Up @@ -72,21 +55,17 @@ void CursesWindow::resetWin()
/* Couldn't get wresize() to work properly. Calling delwin() then newwin() instead */
void CursesWindow::resizeWin(WinSize new_size)
{
//werase(win);
//wresize(win, new_size.row, new_size.col);

if (win != NULL) {
delwin(win);
// int d = delwin(win);
// if (d == ERR) {
// std::ofstream ofstr;
// std::string e = "Error while freeing memory of window " + winName;
// ofstr.open("del_win_log.txt", std::ios::out);
// ofstr << e << '\n';
// ofstr.close();
// }
if (win != nullptr) {
if (delwin(win) == ERR) {
Log() << "Error while freeing memory of window " << win_name;
}
}

row = new_size.row;
col = new_size.col;
begy = new_size.begy;
begx = new_size.begx;

win = newwin(new_size.row, new_size.col, new_size.begy, new_size.begx);
}

Expand All @@ -112,20 +91,38 @@ void CursesWindow::printInMiddle(int starty, int startx, int width, std::string
refresh();
}

void CursesWindow::printAtTop(int starty, int startx, int width, std::string str, chtype color)
// Pass text coordinates, text, color pair, row size
void CursesWindow::printInMiddleWithBackground(int y, int x, int str_begin, const std::string& str, chtype color)
{
char *banner = (char*)malloc((col + 1)*sizeof(char));
int i = 0;

// First for loop = print background before str begining
for (; i < str_begin; ++i) {
banner[i] = ' ';
}
// Second for loop - print str starting at str_begin
for (; i < (int)str.length(); ++i) {
banner[i] = str.at(i);
}
// Third for loop - print background color after str
for (; i < col; ++i) {
banner[i] = ' ';
}
banner[i] = '\0';

wattron(win, color);
mvwprintw(win, y, x, "%s", banner);
wattroff(win, color);
}

/* TODO - pass pointers */
point CursesWindow::getBegyx()
{
int by, bx;
getbegyx(win, by, bx);
return point {.y = by, .x = bx};
}

/* TODO - pass pointers */
point CursesWindow::getMaxyx()
{
int my, mx;
Expand Down
6 changes: 0 additions & 6 deletions UI/include/UI.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,11 @@ class UI
/* All windows are initialized by initWin() */
std::unique_ptr<CursesWindow> initWin(WinSize& size, const std::string& name);

void paintTopWindow(std::unique_ptr<CursesWindow>& win);
void paintLabelsWindow(std::unique_ptr<CursesWindow>& win);
//void paintMainWindow(std::unique_ptr<CursesWindow>& win);
void paintShowHelpWindow(std::unique_ptr<CursesWindow>& win);
void paintSettingsWindow(std::unique_ptr<CursesWindow>& win);
//void paintDownloadsInfosWindow(std::unique_ptr<CursesWindow>& win);

std::unique_ptr<CursesMenu> initMenu(std::vector<std::string> items_data);
void setDownloadsMenu();
//void paintDownloadsStatusWin(std::unique_ptr<CursesWindow>& win);
void populateStatusWin(const std::vector<DownloadWinInfo>& vec);
/* Update status window in a designated thread */
void updateDownloadsStatusWindow();
Expand All @@ -122,7 +117,6 @@ class UI
* protected by a mutex. */
bool update_status = false;


/* Program's subwindows - 'Add a download' window / 'Details' window - 'Progress bar' window */
std::unique_ptr<CursesForm> initForm(size_t num_fields);

Expand Down
101 changes: 22 additions & 79 deletions UI/source/UI.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void UI::resizeUI()
setWindowsSize();

main_windows.at(top_bar_window_index)->resizeWin(window_size_map["topBarSz"]);
paintTopWindow(main_windows.at(top_bar_window_index));
main_windows.at(top_bar_window_index)->printInMiddleWithBackground(0, 0, 0, tinyDLM_label, COLOR_PAIR(8));

main_windows.at(labels_window_index)->resizeWin(window_size_map["labelsSz"]);
paintLabelsWindow(main_windows.at(labels_window_index));
Expand All @@ -262,11 +262,10 @@ void UI::resizeUI()
menu->clearMenu();
menu->clearItems();
main_windows.at(main_window_index)->resizeWin(window_size_map["mainWinSz"]);

main_windows.at(status_window_index)->resizeWin(window_size_map["statusSz"]);

main_windows.at(show_help_window_index)->resizeWin(window_size_map["pHelpSz"]);
paintShowHelpWindow(main_windows.at(show_help_window_index));
main_windows.at(show_help_window_index)->printInMiddle(0, 0, col / 4, msgHelp, COLOR_PAIR(7));

main_windows.at(infos_window_index)->resizeWin(window_size_map["infosSz"]);

Expand All @@ -279,7 +278,7 @@ void UI::initMainWindows()

/* Window containing top bar title */
main_windows.emplace_back(initWin(window_size_map["topBarSz"], "topBar"));
paintTopWindow(main_windows.at(top_bar_window_index));
main_windows.at(top_bar_window_index)->printInMiddleWithBackground(0, 0, 0, tinyDLM_label, COLOR_PAIR(8));

/* Top window containing labels such as "name", "url","speed" etc */
main_windows.emplace_back(initWin(window_size_map["labelsSz"], "labels"));
Expand All @@ -293,7 +292,7 @@ void UI::initMainWindows()

/* Winodow displaying help message */
main_windows.emplace_back(initWin(window_size_map["pHelpSz"], "help"));
paintShowHelpWindow(main_windows.at(show_help_window_index));
main_windows.at(show_help_window_index)->printInMiddle(0, 0, col / 4, msgHelp, COLOR_PAIR(7));

/* Not used for now */
main_windows.emplace_back(initWin(window_size_map["infosSz"], "infos"));
Expand All @@ -308,18 +307,6 @@ void UI::paintLabelsWindow(std::unique_ptr<CursesWindow>& win)
win->printInMiddle(0, 7 * col / 8, col / 8, labelStatus, COLOR_PAIR(7));
}

//void UI::paintMainWindow(std::unique_ptr<CursesWindow>& win) {}
//void UI::paintDownloadsStatusWin(std::unique_ptr<CursesWindow>& win) {}
//void UI::paintDownloadsInfosWindow(std::unique_ptr<CursesWindow>& win) {}

void UI::paintShowHelpWindow(std::unique_ptr<CursesWindow>& win)
{
/* Display keys and their associated functions at the bottom of the window */
win->printInMiddle(0, 0, col / 4, msgHelp, COLOR_PAIR(7));
}

// TODO - printInMiddleWithBackground() for the 5 following functions
/* TODO - Abstract away printInMiddleWithBackground() */
std::string UI::initDownloadDetailsTitle(const std::string& itemName)
{
std::string it = itemName;
Expand All @@ -346,71 +333,25 @@ std::string UI::initDownloadDetailsTitle(const std::string& itemName)
/* TODO - Abstract away printInMiddleWithBackground() */
void UI::paintHelpWindow(std::unique_ptr<CursesWindow>& win)
{
const int begy = 1;
const point maxyx = win->getMaxyx();
char *title_help = (char*)malloc(((col / 2) + 1)*sizeof(char));

title_help[col / 2] = '\0';

std::string title_nl = msgHelpMenu;

int i = 0;
// col / 2 - strlen char*
for (i = 0; i < (col / 2 - 10) / 2; ++i)
title_help[i] = ' ';

if (title_nl.length() >= strlen(title_help)) {
;
}
else {

for (size_t j = 0; j < title_nl.length(); ++j) {
title_help[i++] = title_nl[j];
}
for (; i < col / 2 ; ++i) {
title_help[i] = ' ';
}
win->printInMiddle(1, 0, maxyx.x , title_help, COLOR_PAIR(8));

}

win->addStr(begy + 2, 0, msgHelpAdd);
win->addStr(begy + 3, 0, msgHelpArrowKeys);
win->addStr(begy + 4, 0, msgHelpReturn);
win->addStr(begy + 5, 0, msgHelpPause);
win->addStr(begy + 6, 0, msgHelpPauseAll);
win->addStr(begy + 7, 0, msgHelpResume);
win->addStr(begy + 8, 0, msgHelpResumeAll);
win->addStr(begy + 9, 0, msgHelpClear);
win->addStr(begy + 10, 0, msgHelpKill);
win->addStr(begy + 12, 0, msgHelpKillAll);
win->addStr(begy + 11, 0, msgHelpSettings);
win->addStr(begy + 13, 0, msgHelpExit);
win->printInMiddle(begy + 15, 0, maxyx.x, msgHelpCloseWin, COLOR_PAIR(7));
win->printInMiddleWithBackground(0, 0, 0, msgHelpMenu, COLOR_PAIR(8));

win->addStr(3, 0, msgHelpAdd);
win->addStr(4, 0, msgHelpArrowKeys);
win->addStr(5, 0, msgHelpReturn);
win->addStr(6, 0, msgHelpPause);
win->addStr(7, 0, msgHelpPauseAll);
win->addStr(8, 0, msgHelpResume);
win->addStr(9, 0, msgHelpResumeAll);
win->addStr(10, 0, msgHelpClear);
win->addStr(11, 0, msgHelpKill);
win->addStr(13, 0, msgHelpKillAll);
win->addStr(12, 0, msgHelpSettings);
win->addStr(14, 0, msgHelpExit);
win->printInMiddle(16, 0, maxyx.x, msgHelpCloseWin, COLOR_PAIR(7));

win->drawBox(0, 0);
free(title_help);
}

/* TODO - Abstract away printInMiddleWithBackground() */
void UI::paintTopWindow(std::unique_ptr<CursesWindow>& topWin)
{
/* Print a whole black on white row at the top of the main window that will be the size of the terminal
* window width */
char *titleMain = (char*)malloc((col + 1)*sizeof(char));
int i = 0;

for (i = 0; i < (int)liteDL_label.length(); ++i) {
titleMain[i] = liteDL_label.at(i);
}
for (; i < col; ++i) {
titleMain[i] = ' ';
}
titleMain[i] = '\0';

topWin->printInMiddle(0, 0, col, titleMain, COLOR_PAIR(8));

free(titleMain);
}

/* TODO - Abstract away printInMiddleWithBackground() */
Expand Down Expand Up @@ -1468,7 +1409,9 @@ void UI::paintDetailsWin(const std::string& itemName)
det_win->printInMiddle(maxyx.y - 2, maxyx.x / 4, maxyx.x / 4, msgPause, COLOR_PAIR(8));
det_win->printInMiddle(maxyx.y - 2, 2 * maxyx.x / 4, maxyx.x / 4, msgResume, COLOR_PAIR(8));
det_win->printInMiddle(maxyx.y - 2, 3 * maxyx.x / 4, maxyx.x / 4, msgKill, COLOR_PAIR(8));
det_win->printInMiddle(1, 0, maxyx.x, initDownloadDetailsTitle(itemName), COLOR_PAIR(8));


det_win->printInMiddleWithBackground(1, 0, maxyx.x, initDownloadDetailsTitle(itemName), COLOR_PAIR(8));
}

void UI::startProgressBarThread(const std::string& filename)
Expand Down
2 changes: 1 addition & 1 deletion dialogs/include/dialogs.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <string>

extern const std::string liteDL_label;
extern const std::string tinyDLM_label;
extern const std::string workInProg;

extern const std::string tinyDLMWelcome;
Expand Down
2 changes: 1 addition & 1 deletion dialogs/source/dialogs.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/dialogs.hxx"

const std::string liteDL_label = "tinyDLM v1.1";
const std::string tinyDLM_label = "tinyDLM";
const std::string workInProg = "Work in progress";

const std::string tinyDLMWelcome = " tinyDLM - A tiny download manager ";
Expand Down

0 comments on commit 3f57653

Please sign in to comment.