-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.c
More file actions
316 lines (287 loc) · 10.4 KB
/
windows.c
File metadata and controls
316 lines (287 loc) · 10.4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Created by roberto on 30/4/21.
windows related auxiliary functions
*/
#include "windows.h"
static WINDOW *create_newwin(int height, int width, int starty, int startx,
char *title) {
/** Create a window and draw a title
*
* @param height: window height
* @param width: window width
* @param starty: position of left top corner (absolute-y)
* @param startx: position of left top corner (absolute-x)
* @param title: window title
*
* @return pointer to window structure
*/
WINDOW *local_win = NULL;
local_win = newwin(height, width, starty, startx);
assert(local_win != NULL);
keypad(local_win, TRUE);
box(local_win, 0, 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
(void) mvwaddstr(local_win, 0, 2, title);
(void) wrefresh(local_win); /* Show that box */
return local_win;
}
static void make_form(int n_choices,
FIELD ***items,
char **choices,
FORM **form,
WINDOW *win,
_Windows *windows)
/** Auxiliary function to create a form
*
* @param n_choices: number of items in the menu
* @param items: list of items in the menu
* @param choices: labels for the items
* @param form: store the pointer to the form here
* @param win: form will be attached to this window
* @param windows: auxiliary information such a window size
*/
{
int i = 0;
int cury = 0, curx = 1;
FIELD *field;
/* alloc memory fo fiel, each item will be stored in a field */
*items = (FIELD **) calloc(2 * n_choices + 1, sizeof(FIELD * ));
assert(items);
(*items)[n_choices * 2] = NULL;
/* place the fields in the window */
for (i = 0; i < n_choices * 2; i++) {
(*items)[i] = new_field(1, 7, cury, curx, 0, 0);
field = (*items)[i];
assert(field);
if (i % 2 == 1) { /** odd fields are values */
cury = cury + 1;
curx = 1;
set_field_opts(field, O_VISIBLE | O_PUBLIC | O_EDIT | O_ACTIVE);
set_field_back(field, A_UNDERLINE);
} else { /** even field are labels */
set_field_buffer(field, 0, choices[i / 2]);
set_field_opts(field, O_VISIBLE | O_PUBLIC | O_AUTOSKIP);
curx = 9;
}
}
/* create form */
*form = new_form(*items);
/* attach form to window */
set_form_win(*form, win);
set_form_sub(*form,
derwin(win, windows->terminal_nrows -
2 * windows->height_menu_win - 4,
windows->width_form_win - 3, 2, 2));
post_form(*form);
}
static void create_form(_Windows *windows,
__attribute__((unused)) _Menus *menus,
_Forms *forms, _Panels *panels)
/** Create form window that may display either
* a search or a bpass form
*
*/
{
/* TODO: think about saving geometry of each create window */
int n_choices = 0;
/* create windows */
windows->form_bpass_win =
create_newwin(
windows->terminal_nrows -
2 * windows->height_menu_win - 1, windows->width_form_win,
windows->height_menu_win + 0, 0, windows->form_bpass_title);
windows->form_search_win =
create_newwin(
windows->terminal_nrows -
2 * windows->height_menu_win - 1, windows->width_form_win,
windows->height_menu_win + 0, 0, windows->form_search_title);
/* create forms*/
/* 1) search form */
n_choices = forms->no_items_search;
make_form(n_choices,
&(forms->search_form_items),
forms->choices_search_form,
&(forms->search_form),
windows->form_search_win,
windows);
/* 2) bpass form */
n_choices = forms->no_items_bpass;
make_form(n_choices,
&(forms->bpass_form_items),
forms->choices_bpass_form,
&(forms->bpass_form),
windows->form_bpass_win,
windows);
/* create panels and attach window to panels.
* Pannels are used when several windows are
* placed in the same (x,y) position creating a stack.
* Pannel allow to change the top (visible) window easily */
panels->bpass_panel = new_panel(windows->form_bpass_win);
panels->search_panel = new_panel(windows->form_search_win);
update_panels();
doupdate(); /* panel refresh */
(void) refresh();
(void) wrefresh(windows->form_search_win);
(void) wrefresh(windows->menu_win);
}
static void create_out(_Windows *windows, _Menus *menu)
/** Create output window. This window will show the
* result of the different quweies using a hand made menu system
*
* It also allocs the memory (char **) used to save the text
* taht will be displayed.
*
*/
{
int i = 0;
windows->rows_out_win =
windows->terminal_nrows - 2 * windows->height_menu_win - 1;
windows->cols_out_win =
windows->terminal_ncols - windows->width_form_win - 1;
windows->out_win = create_newwin(windows->rows_out_win,
windows->cols_out_win,
windows->height_menu_win + 0,
windows->width_form_win + 1,
windows->out_title);
menu->out_win_choices =
(char **) calloc(99*windows->rows_out_win, sizeof(char *));
for (i = 0; i < 99*windows->rows_out_win; i++)
(menu->out_win_choices)[i] =
(char *) calloc(windows->cols_out_win, sizeof(char *));
menu->out_win_choices_info =
(char **) calloc(99*windows->rows_out_win, sizeof(char *));
for (i = 0; i < 99*windows->rows_out_win; i++)
(menu->out_win_choices_info)[i] =
(char *) calloc(windows->cols_out_win, sizeof(char *));
}
static void create_msg(_Windows *windows)
/** Creates the message window. This is an output only window.
* that does not interact with users.
*/
{
windows->msg_win =
create_newwin(windows->height_menu_win + 1,
windows->terminal_ncols,
windows->terminal_nrows - windows->height_menu_win - 1,
0, windows->msg_title);
}
void write_msg(WINDOW *win, char *msg, int y, int x, char *title)
/** write string msg in window win, at position (y,x)
*
* @param win window
* @param msg message
* @param x message position (X)
* @param y message position (Y)
*/
{
if (x < 0)
x = 2;
if (y < 0)
y = 2;
(void) wclear(win);
/* since we cleared the box we need to repaint it */
(void) box(win, 0, 0);
(void) mvwaddstr(win, 0, 2, title);
/* write message */
(void) mvwaddstr(win, y, x, msg);
(void) wrefresh(win);
}
static void create_menu(_Windows *windows, _Menus *menus)
/** creates the menu window as well as the menu display in it
*/
{
size_t i = 0;
size_t n_choices = 0;
/* create window */
windows->menu_win = create_newwin(windows->height_menu_win,
windows->terminal_ncols, 0, 0,
windows->menu_title);
/* create menu */
n_choices = menus->no_items;
menus->menuitems = (ITEM **) calloc(n_choices + 1, sizeof(ITEM * ));
for (i = 0; i < n_choices; i++) {
(menus->menuitems)[i] = new_item(menus->choices[i], " ");
assert((menus->menuitems)[i] != NULL);
}
(menus->menuitems)[i] = (ITEM *) NULL;
menus->menu = new_menu(menus->menuitems);
assert(menus->menu != NULL && windows->menu_win != NULL);
keypad(windows->menu_win, TRUE);
/* menu shape, made it horizontal */
set_menu_format(menus->menu, 1, n_choices);
set_current_item(menus->menu, (menus->menuitems)[0]);
/* "*" will be used to mark selected item */
set_menu_mark(menus->menu, " * ");
/* attach menu to window */
set_menu_win(menus->menu, windows->menu_win);
/* draw menu in a window overlay */
set_menu_sub(menus->menu, derwin(windows->menu_win, 1,
windows->terminal_ncols - 2, 1, 1));
/* write menu to the subwindow*/
post_menu(menus->menu);
wrefresh(windows->menu_win);
}
void _initsrc(_Windows *windows,
_Menus *menus,
_Forms *forms,
_Panels *panels)
/** initialize windows
*
* @param windows structure with window information
* @param menus
* @param forms
* @param panels
**/
{
windows->main_win = initscr(); /* curses call to initialize window */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
/* cbreak put the terminal into cbreak() mode.
* In this mode, characters typed by the user
* are immediately available to the program,
* and erase/kill character-processing is not performed */
(void) cbreak(); /* no waiting for Enter key */
(void) noecho(); /* set no echoing as you type*/
/* curses call to find size of window */
getmaxyx(windows->main_win, windows->terminal_nrows,
windows->terminal_ncols);
(void) clear(); /* call to clear screen, send cursor to position (0,0) */
(void) refresh(); /* curses call to implement all changes since last refresh */
create_menu(windows, menus);
create_form(windows, menus, forms, panels);
create_out(windows, menus);
create_msg(windows);
}
void print_out(WINDOW *win,
char **choices,
int menuitems_min,
int menuitems_max,
int highlight,
char *title)
/** print array of strings choices window win (usually out_win)
*
* @param win
* @param choices list of fields
* @param menuitems_min first row to print
* @param menuitems_min last row to print
* @param highlight highlight this row (0 -> first row)
* @param title
*/
{
int x=0, y=0, i=0;
x = 2;
y = 1;
(void) box(win, 0, 0);
(void) mvwaddstr(win, 0, 2, title);
for (i = menuitems_min-1; i < menuitems_max; ++i) {
if (highlight == i) /* High light the present choice */
{
(void) wattron(win, A_REVERSE); /** set reverse attribute on */
(void) mvwprintw(win, y, x, "%s", choices[i]);
(void) wattroff(win, A_REVERSE); /** set reverse attribute off */
} else
(void) mvwprintw(win, y, x, "%s", choices[i]);
y += 1;
}
(void) wrefresh(win);
}