forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmenu.c
132 lines (120 loc) · 3.26 KB
/
gmenu.c
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
/**
* gmenu.c -- goaccess menus
* Copyright (C) 2009-2014 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gmenu.h"
#include "error.h"
#include "xmalloc.h"
#include "settings.h"
#include "util.h"
#include "ui.h"
/* allocate memory for a new GMenu instance */
GMenu *
new_gmenu (WINDOW * parent, int h, int w, int y, int x)
{
GMenu *menu = xmalloc (sizeof (GMenu));
memset (menu, 0, sizeof *menu);
menu->count = 0;
menu->idx = 0;
menu->multiple = 0;
menu->selectable = 0;
menu->start = 0;
menu->status = 0;
menu->h = h;
menu->w = w;
menu->x = x;
menu->y = y;
menu->win = derwin (parent, menu->h, menu->w, menu->y, menu->x);
return menu;
}
/* render an actual menu item */
static void
draw_menu_item (GMenu * menu, char *s, int x, int y, int w, int color,
int checked)
{
char check, *lbl = NULL;
if (menu->selectable) {
check = checked ? 'x' : ' ';
lbl = xmalloc (snprintf (NULL, 0, "[%c] %s", check, s) + 1);
sprintf (lbl, "[%c] %s", check, s);
draw_header (menu->win, lbl, "%s", y, x, w, color, 0);
free (lbl);
} else {
draw_header (menu->win, s, "%s", y, x, w, color, 0);
}
}
/* displays a menu to its associated window */
int
post_gmenu (GMenu * menu)
{
int i = 0, j = 0, k = 0, start, end, height, total, checked = 0;
if (menu == NULL)
return 1;
werase (menu->win);
height = menu->h;
start = menu->start;
total = menu->size;
end = height < total ? start + height : total;
for (i = start; i < end; i++, j++) {
k = i == menu->idx ? 1 : 0;
checked = menu->items[i].checked ? 1 : 0;
draw_menu_item (menu, menu->items[i].name, 0, j, menu->w, k, checked);
}
wrefresh (menu->win);
return 0;
}
/* main work horse of the menu system processing input events */
void
gmenu_driver (GMenu * menu, int c)
{
int i;
switch (c) {
case REQ_DOWN:
if (menu->idx >= menu->size - 1)
break;
++menu->idx;
if (menu->idx >= menu->h && menu->idx >= menu->start + menu->h)
menu->start++;
post_gmenu (menu);
break;
case REQ_UP:
if (menu->idx <= 0)
break;
--menu->idx;
if (menu->idx < menu->start)
--menu->start;
post_gmenu (menu);
break;
case REQ_SEL:
if (!menu->multiple) {
for (i = 0; i < menu->size; i++)
menu->items[i].checked = 0;
}
if (menu->items[menu->idx].checked)
menu->items[menu->idx].checked = 0;
else
menu->items[menu->idx].checked = 1;
post_gmenu (menu);
break;
}
}