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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* $Id$
*
* (c) 2004 by Kalle Wallin <kaw@linux.se>
*
* 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.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "list_window.h"
#include "config.h"
#include "options.h"
#include "charset.h"
#include "support.h"
#include "command.h"
#include "colors.h"
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
extern void screen_bell(void);
struct list_window *
list_window_init(WINDOW *w, unsigned width, unsigned height)
{
struct list_window *lw;
lw = g_malloc0(sizeof(list_window_t));
lw->w = w;
lw->cols = width;
lw->rows = height;
return lw;
}
void
list_window_free(struct list_window *lw)
{
if (lw) {
memset(lw, 0, sizeof(list_window_t));
g_free(lw);
}
}
void
list_window_reset(struct list_window *lw)
{
lw->selected = 0;
lw->xoffset = 0;
lw->start = 0;
}
void
list_window_check_selected(struct list_window *lw, unsigned length)
{
if (lw->start + lw->rows > length) {
if (length > lw->rows)
lw->start = length - lw->rows;
else
lw->start = 0;
}
if (lw->selected < lw->start)
lw->selected = lw->start;
if (length > 0 && lw->selected >= length)
lw->selected = length - 1;
}
void
list_window_center(struct list_window *lw, unsigned rows, unsigned n)
{
if (n > lw->rows / 2)
lw->start = n - lw->rows / 2;
else
lw->start = 0;
if (lw->start + lw->rows > rows) {
if (lw->rows < rows)
lw->start = rows - lw->rows;
else
lw->start = 0;
}
}
void
list_window_set_selected(struct list_window *lw, unsigned n)
{
lw->selected = n;
}
static void
list_window_next(struct list_window *lw, unsigned length)
{
if (lw->selected + 1 < length)
lw->selected++;
else if (options.list_wrap)
lw->selected = 0;
}
static void
list_window_previous(struct list_window *lw, unsigned length)
{
if (lw->selected > 0)
lw->selected--;
else if (options.list_wrap)
lw->selected = length - 1;
}
static void
list_window_first(struct list_window *lw)
{
lw->xoffset = 0;
lw->selected = 0;
}
static void
list_window_last(struct list_window *lw, unsigned length)
{
lw->xoffset = 0;
if (length > 0)
lw->selected = length - 1;
else
lw->selected = 0;
}
static void
list_window_next_page(struct list_window *lw, unsigned length)
{
if (lw->rows < 2)
return;
if (lw->selected + lw->rows < length)
lw->selected += lw->rows - 1;
else
list_window_last(lw, length);
}
static void
list_window_previous_page(struct list_window *lw)
{
if (lw->rows < 2)
return;
if (lw->selected > lw->rows - 1)
lw->selected -= lw->rows - 1;
else
list_window_first(lw);
}
void
list_window_paint(struct list_window *lw,
list_window_callback_fn_t callback,
void *callback_data)
{
unsigned i;
int fill = options.wide_cursor;
int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
if (show_cursor) {
if (lw->selected < lw->start)
lw->start = lw->selected;
if (lw->selected >= lw->start + lw->rows)
lw->start = lw->selected - lw->rows + 1;
}
for (i = 0; i < lw->rows; i++) {
int highlight = 0;
const char *label;
label = callback(lw->start + i, &highlight, callback_data);
wmove(lw->w, i, 0);
if (label) {
int selected = lw->start + i == lw->selected;
size_t len = my_strlen(label);
if (highlight)
colors_use(lw->w, COLOR_LIST_BOLD);
else
colors_use(lw->w, COLOR_LIST);
if (show_cursor && selected)
wattron(lw->w, A_REVERSE);
//waddnstr(lw->w, label, lw->cols);
waddstr(lw->w, label);
if (fill && len < lw->cols)
whline(lw->w, ' ', lw->cols-len);
if (selected)
wattroff(lw->w, A_REVERSE);
if (!fill && len < lw->cols)
wclrtoeol(lw->w);
} else
wclrtoeol(lw->w);
}
}
int
list_window_find(struct list_window *lw,
list_window_callback_fn_t callback,
void *callback_data,
const char *str,
int wrap)
{
int h;
unsigned i = lw->selected + 1;
const char *label;
while (wrap || i == lw->selected + 1) {
while ((label = callback(i,&h,callback_data))) {
if (str && label && strcasestr(label, str)) {
lw->selected = i;
return 0;
}
if (wrap && i == lw->selected)
return 1;
i++;
}
if (wrap) {
if (i == 0) /* empty list */
return 1;
i=0; /* first item */
screen_bell();
}
}
return 1;
}
int
list_window_rfind(struct list_window *lw,
list_window_callback_fn_t callback,
void *callback_data,
const char *str,
int wrap,
unsigned rows)
{
int h;
int i = lw->selected - 1;
const char *label;
if (rows == 0)
return 1;
while (wrap || i == (int)lw->selected - 1) {
while (i >= 0 && (label = callback(i,&h,callback_data))) {
if( str && label && strcasestr(label, str) ) {
lw->selected = i;
return 0;
}
if (wrap && i == (int)lw->selected)
return 1;
i--;
}
if (wrap) {
i = rows - 1; /* last item */
screen_bell();
}
}
return 1;
}
/* perform basic list window commands (movement) */
int
list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
{
switch (cmd) {
case CMD_LIST_PREVIOUS:
list_window_previous(lw, rows);
break;
case CMD_LIST_NEXT:
list_window_next(lw, rows);
break;
case CMD_LIST_FIRST:
list_window_first(lw);
break;
case CMD_LIST_LAST:
list_window_last(lw, rows);
break;
case CMD_LIST_NEXT_PAGE:
list_window_next_page(lw, rows);
break;
case CMD_LIST_PREVIOUS_PAGE:
list_window_previous_page(lw);
break;
default:
return 0;
}
return 1;
}
int
list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
{
switch (cmd) {
case CMD_LIST_PREVIOUS:
if (lw->start > 0)
lw->start--;
break;
case CMD_LIST_NEXT:
if (lw->start + lw->rows < rows)
lw->start++;
break;
case CMD_LIST_FIRST:
lw->start = 0;
break;
case CMD_LIST_LAST:
if (rows > lw->rows)
lw->start = rows - lw->rows;
else
lw->start = 0;
break;
case CMD_LIST_NEXT_PAGE:
lw->start += lw->rows - 1;
if (lw->start + lw->rows > rows) {
if (rows > lw->rows)
lw->start = rows - lw->rows;
else
lw->start = 0;
}
break;
case CMD_LIST_PREVIOUS_PAGE:
if (lw->start > lw->rows)
lw->start -= lw->rows;
else
lw->start = 0;
break;
default:
return 0;
}
return 1;
}
#ifdef HAVE_GETMOUSE
int
list_window_mouse(struct list_window *lw, unsigned rows,
unsigned long bstate, int y)
{
assert(lw != NULL);
/* if the even occured above the list window move up */
if (y < 0) {
if (bstate & BUTTON3_CLICKED)
list_window_first(lw);
else
list_window_previous_page(lw);
return 1;
}
/* if the even occured below the list window move down */
if ((unsigned)y >= rows) {
if (bstate & BUTTON3_CLICKED)
list_window_last(lw, rows);
else
list_window_next_page(lw, rows);
return 1;
}
return 0;
}
#endif
list_window_state_t *
list_window_init_state(void)
{
return g_malloc0(sizeof(list_window_state_t));
}
void
list_window_free_state(list_window_state_t *state)
{
if (state) {
if (state->list) {
GList *list = state->list;
while (list) {
g_free(list->data);
list->data = NULL;
list = list->next;
}
g_list_free(state->list);
state->list = NULL;
}
g_free(state);
}
}
void
list_window_push_state(list_window_state_t *state, struct list_window *lw)
{
if (state) {
struct list_window *tmp = g_malloc(sizeof(list_window_t));
memcpy(tmp, lw, sizeof(list_window_t));
state->list = g_list_prepend(state->list, (gpointer) tmp);
list_window_reset(lw);
}
}
bool
list_window_pop_state(list_window_state_t *state, struct list_window *lw)
{
if (state && state->list) {
struct list_window *tmp = state->list->data;
memcpy(lw, tmp, sizeof(list_window_t));
g_free(tmp);
state->list->data = NULL;
state->list = g_list_delete_link(state->list, state->list);
}
// return TRUE if there are still states in the list
return (state && state->list) ? TRUE : FALSE;
}
|