-
Notifications
You must be signed in to change notification settings - Fork 0
/
window-util.h
156 lines (145 loc) · 4.86 KB
/
window-util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef WINDOW_UTIL_H
#define WINDOW_UTIL_H
#include <curses.h>
#include <menu.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
void print_in_middle_para(WINDOW*, int, int, int, char*, chtype);
void print_in_middle(WINDOW*, int, int, int, char*, chtype);
void renew_win(WINDOW*, int, int, char*, chtype);
WINDOW* create_window(int, int, int, int, char*, chtype);
void destroy_win(WINDOW*);
/**
* \brief Function to print a paragraph in the middle of a window, breaking lines/word wrapping automatically
* \param win pointer to ncurses window
* \param sy starting y coordinate
* \param sx starting x coordinate
* \param width required maximum width of a line
* \param msg message string (paragraph to be printed)
* \param color color setting for printing
*/
void print_in_middle_para(WINDOW* win, int sy, int sx, int width, char* msg, chtype color) {
int len, x, y;
float temp;
int i, c = 0;
int printLen = 0;
int nextStart = 0;
if (win == NULL) {
win = stdscr;
}
getyx(win, y, x);
if (sx) x = sx;
if (sy) y = sy;
if (!width) width = 80;
len = strlen(msg);
wattron(win, color);
for (int i = 0; i < len; i++) {
c++;
if (msg[i] == ' ') {
printLen = i - nextStart;
}
if (c == (width - 4)) {
// on this line, print only till prev_space
temp = (width - printLen)/2;
x = sx + (int)temp;
mvwprintw(win, y++, x, "%.*s", printLen, msg + nextStart);
nextStart += printLen + 1;
c = 0;
}
}
temp = (width - (len - nextStart))/2;
x = sx + (int)temp;
mvwprintw(win, y, x, "%s", msg + nextStart);
wattroff(win, color);
wrefresh(win);
}
/**
* \brief Function to print a line in the middle of a window
* \param win pointer to ncurses window
* \param starty starting y coordinate
* \param startx starting x coordinate
* \param width required maximum width of a line
* \param msg message string (line to be printed)
* \param color color setting for printing
*/
void print_in_middle(WINDOW* win, int starty, int startx, int width, char* msg, chtype color) {
int length, x, y;
float temp;
if (win == NULL)
win = stdscr;
getyx(win, y, x);
if (startx)
x = startx;
if (starty)
y = starty;
if (width == 0)
width = 80;
length = strlen(msg);
temp = (width - length)/2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", msg);
wattroff(win, color);
refresh();
}
/**
* \brief Function to redraw border and reprint title of a window after it has been cleared
* \param win ncurses window pointer
* \param rows window height
* \param cols window length
* \param title heading to be displayed at the top of window
* \param color color setting for title
*/
void renew_win(WINDOW* win, int rows, int cols, char *title, chtype color) {
box(win, ACS_VLINE, ACS_HLINE);
wattron(win, A_BOLD);
print_in_middle(win, 1, 0, cols, title, color);
wattroff(win, A_BOLD);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, cols - 2);
mvwaddch(win, 2, cols - 1, ACS_RTEE);
wrefresh(win);
}
/**
* \brief Function to create a window and print its borders and heading
* \param startx x-coordinate of window top left corner
* \param starty y-coordinate of window top left corner
* \param rows window height
* \param cols window length
* \param title heading to be displayed at the top of window
* \param color color setting for title
* \return pointer to the window created
*/
WINDOW* create_window(int startx, int starty, int rows, int cols, char *title, chtype color) {
WINDOW *win;
win = newwin(rows, cols, starty, startx);
renew_win(win, rows, cols, title, color);
return win;
}
/**
* \brief Function to delete a window
* \param local_win pointer to window to be deleted
*/
void destroy_win(WINDOW *local_win) {
wclear(local_win);
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window.
*/
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}
#endif