-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.c
More file actions
340 lines (328 loc) · 15.1 KB
/
loop.c
File metadata and controls
340 lines (328 loc) · 15.1 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Created by roberto on 30/4/21.
IT will not process properly UTF8
export LC_ALL=C
Function in this file assume a windows setup as:
+-------------------------------+ <-- main_win
|+-----------------------------+|
|| win_menu ||
|+-----------------------------+|
|+--------++-------------------+|
|| || ||
|| || ||
||win_form|| win_out ||
|| || ||
|| || ||
|+--------++-------------------+|
|+-----------------------------+|
|| win_messages ||
|+-----------------------------+|
+-------------------------------+
*/
#include "loop.h"
#include <string.h>
#include <stdio.h>
void loop(_Windows *windows, _Menus *menus,
_Forms *forms, _Panels *panels) {
/** get keys pressed by user and process it.
* - If left/right arrow is pressed move to
* the next/previous menu item
* - If up/down key is pressed check on focus
* variable if focus == FOCUS_LEFT move to the
* previous/next form item in the left window
* (aka form_window). Otherwise do the same
* in the win_out.
* - If enter is pressed the action depend on the
* item selected in the menu bar and the value of
* "focus":
* a) focus = FOCUS_LEFT
* quit -> quit the application
* search -> execute results_search
* and fill win_out
* bpass -> execute bpas_search
* and fill win_out
* b) focus == FOCUS_RIGHT
* show the highlighted line in
* win_out in the win_message window
* - tab key toggle focus between
* win_form and win_out windows
*/
int focus = FOCUS_LEFT; /* focus is in win_form/win_out window */
int ch=0; /* typed character */
bool enterKey = FALSE; /* has enter been presswed ? */
char buffer[128]; /* auxiliary buffer to compose messages */
ITEM *auxItem = (ITEM *) NULL; /* item selected in the menu */
int choice = -1; /* index of the item selected in menu*/
MENU *menu = NULL; /* pointer to menu in menu_win*/
WINDOW *menu_win= NULL; /* pointer to menu_win */
WINDOW *out_win= NULL; /* pointer to out_win */
WINDOW *msg_win= NULL; /* pointer to msg_win */
char * tmpStr1= NULL; /* used to read values typed in forms */
char * tmpStr2= NULL; /* used to read values typed in forms */
char * tmpStr3= NULL; /* used to read values typed in forms */
int n_out_choices=0; /* number of printed lines in win_out window */
int out_highlight = 0; /* line highlighted in win_out window */
int rows_out_window = 0; /* size of win_out window */
int i = 0; /* dummy variable for loops */
int page=0;
int des=0;
int max_pages=0;
int rows_last_page=1;
int max_rows;
(void) curs_set(1); /* show cursor */
menu = menus->menu;
menu_win = windows->menu_win;
out_win = windows->out_win;
msg_win = windows->msg_win;
rows_out_window = windows->terminal_nrows - 2 * windows->height_menu_win - 1;
/* windows->rows_out_win=7;esta linea es pa probar que va lo de las paginas*/
max_rows=windows->rows_out_win-2;
while ((bool) TRUE) {
ch = getch(); /* get char typed by user */
if ((bool)DEBUG) {
/*(void)snprintf(buffer, 128, "key pressed %d %c (%d)", ch, ch, item_index(auxItem)); */
(void)snprintf(buffer, 128, "hilighted:%d, max_pages:%d, page:%d, n_out_choices:%d, max_rows:%d, max_rowlastpage:%d", out_highlight,max_pages, page, n_out_choices, max_rows, rows_last_page);
write_msg(msg_win, buffer, -1, -1, windows->msg_title);
}
switch (ch) {
case KEY_LEFT:
case 0x3C: /* < */
focus = FOCUS_LEFT;
/* auxiliary function provided by ncurses see
* https://docs.oracle.com/cd/E88353_01/html/E37849/menu-driver-3curses.html
*/
(void) menu_driver(menu, REQ_LEFT_ITEM);
/* refresh window menu_win */
(void) wrefresh(menu_win);
/* get selected item from menu */
auxItem = current_item(menu);
/* draw the corresponding form in win_form */
if (item_index(auxItem) == SEARCH)
(void) top_panel(panels->search_panel);
else if (item_index(auxItem) == BPASS)
(void) top_panel(panels->bpass_panel);
/* refresh window using the panel system.
* we need panels because search and bpass
* are attached to two ovelapping windows
* */
(void) update_panels();
(void) doupdate();
break;
case KEY_RIGHT:
case 0x3E: /* > */
focus = FOCUS_LEFT;
(void) menu_driver(menu, REQ_RIGHT_ITEM);
(void) wrefresh(menu_win);
auxItem = current_item(menu);
if (item_index(auxItem) == SEARCH)
(void) top_panel(panels->search_panel);
else if (item_index(auxItem) == BPASS)
(void) top_panel(panels->bpass_panel);
(void) update_panels();
(void) doupdate();
break;
case KEY_UP:
case 0x2B: /* + */
/* form_driver is the equivalent to menu_driver for forms */
if (item_index(auxItem) == SEARCH && focus == FOCUS_LEFT) {
(void) form_driver(forms->search_form, REQ_PREV_FIELD);
(void) form_driver(forms->search_form, REQ_END_LINE);
(void) wrefresh(windows->form_search_win);
} else if (item_index(auxItem) == BPASS && focus == FOCUS_LEFT) {
(void) form_driver(forms->bpass_form, REQ_PREV_FIELD);
(void) form_driver(forms->bpass_form, REQ_END_LINE);
(void) wrefresh(windows->form_bpass_win);
} else if (focus == FOCUS_RIGHT){
out_highlight = MAX(out_highlight - 1, 0+des);
print_out(out_win, menus->out_win_choices,1+des,MIN(max_rows, n_out_choices)+des,
out_highlight, windows->out_title);
}
break;
case KEY_DOWN:
case 0x2D: /* - */
if (item_index(auxItem) == SEARCH && focus == FOCUS_LEFT) {
(void) form_driver(forms->search_form, REQ_NEXT_FIELD);
(void) form_driver(forms->search_form, REQ_END_LINE);
(void) wrefresh(windows->form_search_win);
} else if (item_index(auxItem) == BPASS && focus == FOCUS_LEFT) {
(void) form_driver(forms->bpass_form, REQ_NEXT_FIELD);
(void) form_driver(forms->bpass_form, REQ_END_LINE);
(void) wrefresh(windows->form_bpass_win);
} else if (focus == FOCUS_RIGHT){
if(page==max_pages){
out_highlight = MIN(out_highlight + 1, rows_last_page+des-1);
}
else{
out_highlight = MIN(out_highlight + 1, MIN(max_rows, n_out_choices)-1+des);
}
print_out(out_win, menus->out_win_choices,1+des,MIN(max_rows, n_out_choices) +des,
out_highlight, windows->out_title);
}
break;
case KEY_STAB: /* tab key */
case 9:
/* toggle focus between win_form and win_out*/
if (focus == FOCUS_RIGHT && n_out_choices!=0){
focus = FOCUS_LEFT;
(void) snprintf(buffer, 128, "focus in window %d", focus);
}
else if(focus == FOCUS_LEFT && n_out_choices!=0){
focus = FOCUS_RIGHT;
(void) snprintf(buffer, 128, "focus in window %d", focus);
}
else{
(void) snprintf(buffer, 128, "No hay resultados que inspeccionar");
}
write_msg(msg_win, buffer, -1, -1, windows->msg_title);
/* If win_form is selected place the cursor in the right place */
if (item_index(auxItem) == SEARCH && focus == FOCUS_LEFT) {
(void)form_driver(forms->search_form, REQ_END_LINE);
(void)wrefresh(windows->form_search_win);
}
else if (item_index(auxItem) == BPASS && focus == FOCUS_LEFT) {
(void)form_driver(forms->bpass_form, REQ_END_LINE);
(void)wrefresh(windows->form_bpass_win);
}
break;
case KEY_BACKSPACE: /* delete last key */
case 127:
if (item_index(auxItem) == SEARCH) {
(void)form_driver(forms->search_form, REQ_DEL_PREV);
(void)wrefresh(windows->form_search_win);
} else if (item_index(auxItem) == BPASS) {
(void)form_driver(forms->bpass_form, REQ_DEL_PREV);
(void)wrefresh(windows->form_bpass_win);
}
break;
case 10: /* enter has been pressed*/
auxItem = current_item(menu);
choice = item_index(auxItem);
enterKey = (bool) TRUE; /* mark enter pressed */
break;
case 338: /* Av Página has been pressed*/
if (focus == FOCUS_RIGHT && n_out_choices!=0){
des+=max_rows;
page=MIN(page+1,max_pages);
if(des>=n_out_choices){
des-=max_rows;
}
(void) wclear(out_win);
out_highlight = 0+des;
print_out(out_win, menus->out_win_choices,1+des,MIN(max_rows, n_out_choices) +des,
out_highlight, windows->out_title);
}
break;
case 339: /* Re Página has been pressed*/
if(focus == FOCUS_RIGHT && n_out_choices!=0){
des-=max_rows;
page=MAX(page-1,1);
if(des<=0){
des=0;
}
(void) wclear(out_win);
out_highlight = 0+des;
print_out(out_win, menus->out_win_choices,1+des, MIN(max_rows, n_out_choices)+des,
out_highlight, windows->out_title);
}
break;
default: /* echo pressed key */
auxItem = current_item(menu);
if (item_index(auxItem) == SEARCH) {
(void)form_driver(forms->search_form, ch);
(void)wrefresh(windows->form_search_win);
}
else if (item_index(auxItem) == BPASS) {
(void)form_driver(forms->bpass_form, ch);
(void)wrefresh(windows->form_bpass_win);
}
break;
}
if (choice != -1 && enterKey) /* User did a choice process it */
{
if (choice == QUIT)
break; /* quit */
else if ((choice == SEARCH) && (focus == FOCUS_LEFT)) {
out_highlight = 0;
for(i=0; i< rows_out_window ; i++)
(menus->out_win_choices)[i][0] = '\0';
(void)wclear(out_win);
(void)form_driver(forms->search_form, REQ_VALIDATION);
tmpStr1 = field_buffer((forms->search_form_items)[1], 0);
tmpStr2 = field_buffer((forms->search_form_items)[3], 0);
tmpStr3 = field_buffer((forms->search_form_items)[5], 0);
results_search(tmpStr1, tmpStr2, tmpStr3, &n_out_choices, & (menus->out_win_choices), & (menus->out_win_choices_info),
windows->cols_out_win-4, windows->rows_out_win-2);
if (n_out_choices==0)
{
page=0;
}
else{
page=1;
}
des=0;
if(n_out_choices%max_rows==0){
max_pages=n_out_choices/max_rows;
}
else{
max_pages=(n_out_choices/max_rows)+1;
}
rows_last_page=n_out_choices%max_rows;
if(rows_last_page==0 ){
rows_last_page=max_rows;
}
print_out(out_win, menus->out_win_choices,1, MIN(max_rows, n_out_choices),
out_highlight, windows->out_title);
if ((bool)DEBUG) {
if(n_out_choices == 0){
snprintf(buffer, 128, "No se encontraron vuelos con los datos introducidos. Reviselos para realizar su búsqueda");
}
else{
(void)snprintf(buffer, 128, "Busqueda encontrada .arg1=%s, arg2=%s, arg3= %s", tmpStr1, tmpStr2, tmpStr3);
}
write_msg(msg_win, buffer, -1, -1, windows->msg_title);
}
}
else if ((choice == SEARCH) && (focus == FOCUS_RIGHT)) {
(void)snprintf(buffer, 128, "msg=%s", (menus->out_win_choices_info)[out_highlight] );
write_msg(msg_win,buffer,
-1, -1, windows->msg_title);
}
else if ((choice == BPASS) && (focus == FOCUS_LEFT)) {
out_highlight = 0;
for(i=0; i< rows_out_window ; i++)
(menus->out_win_choices)[i][0] = '\0';
(void) wclear(out_win);
(void) form_driver(forms->bpass_form, REQ_VALIDATION);
tmpStr1 = field_buffer((forms->bpass_form_items)[1], 0);
results_bpass(tmpStr1, &n_out_choices, & (menus->out_win_choices),
windows->cols_out_win-4, windows->rows_out_win-2);
if (n_out_choices==0)
{
page=0;
}
else{
page=1;
}
des=0;
if(n_out_choices%max_rows==0){
max_pages=n_out_choices/max_rows;
}
else{
max_pages=(n_out_choices/max_rows)+1;
}
rows_last_page=n_out_choices%max_rows;
if(rows_last_page==0){
rows_last_page=max_rows;
}
print_out(out_win, menus->out_win_choices,1, MIN(max_rows, n_out_choices),
out_highlight, windows->out_title);
}
else if ((choice == BPASS) && focus == (FOCUS_RIGHT)) {
write_msg(msg_win, (menus->out_win_choices)[out_highlight],
-1, -1, windows->msg_title);
}
}
choice = -1;
enterKey = (bool) FALSE;
}
}