1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "list_window.h"
22 #include "config.h"
23 #include "options.h"
24 #include "support.h"
25 #include "command.h"
26 #include "colors.h"
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
33 extern void screen_bell(void);
35 struct list_window *
36 list_window_init(WINDOW *w, unsigned width, unsigned height)
37 {
38 struct list_window *lw;
40 lw = g_malloc0(sizeof(list_window_t));
41 lw->w = w;
42 lw->cols = width;
43 lw->rows = height;
44 lw->clear = 1;
45 return lw;
46 }
48 void
49 list_window_free(struct list_window *lw)
50 {
51 if (lw) {
52 memset(lw, 0, sizeof(list_window_t));
53 g_free(lw);
54 }
55 }
57 void
58 list_window_reset(struct list_window *lw)
59 {
60 lw->selected = 0;
61 lw->xoffset = 0;
62 lw->start = 0;
63 lw->clear = 1;
64 }
66 void
67 list_window_check_selected(struct list_window *lw, unsigned length)
68 {
69 if (lw->start + lw->rows > length) {
70 if (length > lw->rows)
71 lw->start = length - lw->rows;
72 else
73 lw->start = 0;
74 }
76 if (lw->selected < lw->start)
77 lw->selected = lw->start;
79 if (length > 0 && lw->selected >= length)
80 lw->selected = length - 1;
81 }
83 void
84 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
85 {
86 if (n > lw->rows / 2)
87 lw->start = n - lw->rows / 2;
88 else
89 lw->start = 0;
91 if (lw->start + lw->rows > rows) {
92 if (lw->rows < rows)
93 lw->start = rows - lw->rows;
94 else
95 lw->start = 0;
96 }
98 lw->repaint = lw->clear = 1;
99 }
101 void
102 list_window_set_selected(struct list_window *lw, unsigned n)
103 {
104 lw->selected = n;
105 }
107 static void
108 list_window_next(struct list_window *lw, unsigned length)
109 {
110 if (lw->selected + 1 < length)
111 lw->selected++;
112 else if (options.list_wrap)
113 lw->selected = 0;
114 }
116 static void
117 list_window_previous(struct list_window *lw, unsigned length)
118 {
119 if (lw->selected > 0)
120 lw->selected--;
121 else if (options.list_wrap)
122 lw->selected = length - 1;
123 }
125 static void
126 list_window_first(struct list_window *lw)
127 {
128 lw->xoffset = 0;
129 lw->selected = 0;
130 }
132 static void
133 list_window_last(struct list_window *lw, unsigned length)
134 {
135 lw->xoffset = 0;
136 if (length > 0)
137 lw->selected = length - 1;
138 else
139 lw->selected = 0;
140 }
142 static void
143 list_window_next_page(struct list_window *lw, unsigned length)
144 {
145 if (lw->rows < 2)
146 return;
147 if (lw->selected + lw->rows < length)
148 lw->selected += lw->rows - 1;
149 else
150 list_window_last(lw, length);
151 }
153 static void
154 list_window_previous_page(struct list_window *lw)
155 {
156 if (lw->rows < 2)
157 return;
158 if (lw->selected > lw->rows - 1)
159 lw->selected -= lw->rows - 1;
160 else
161 list_window_first(lw);
162 }
165 void
166 list_window_paint(struct list_window *lw,
167 list_window_callback_fn_t callback,
168 void *callback_data)
169 {
170 unsigned i;
171 int fill = options.wide_cursor;
172 int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
174 if (show_cursor) {
175 if (lw->selected < lw->start) {
176 lw->start = lw->selected;
177 lw->clear=1;
178 }
180 if (lw->selected >= lw->start + lw->rows) {
181 lw->start = lw->selected - lw->rows + 1;
182 lw->clear=1;
183 }
184 }
186 for (i = 0; i < lw->rows; i++) {
187 int highlight = 0;
188 const char *label;
190 label = callback(lw->start + i, &highlight, callback_data);
191 wmove(lw->w, i, 0);
192 if( lw->clear && (!fill || !label) )
193 wclrtoeol(lw->w);
195 if (label) {
196 int selected = lw->start + i == lw->selected;
197 size_t len = my_strlen(label);
199 if( highlight )
200 colors_use(lw->w, COLOR_LIST_BOLD);
201 else
202 colors_use(lw->w, COLOR_LIST);
204 if( show_cursor && selected )
205 wattron(lw->w, A_REVERSE);
207 //waddnstr(lw->w, label, lw->cols);
208 waddstr(lw->w, label);
209 if( fill && len<lw->cols )
210 whline(lw->w, ' ', lw->cols-len);
212 if( selected )
213 wattroff(lw->w, A_REVERSE);
214 }
215 }
217 lw->clear=0;
218 }
220 int
221 list_window_find(struct list_window *lw,
222 list_window_callback_fn_t callback,
223 void *callback_data,
224 const char *str,
225 int wrap)
226 {
227 int h;
228 unsigned i = lw->selected + 1;
229 const char *label;
231 while (wrap || i == lw->selected + 1) {
232 while ((label = callback(i,&h,callback_data))) {
233 if (str && label && strcasestr(label, str)) {
234 lw->selected = i;
235 return 0;
236 }
237 if (wrap && i == lw->selected)
238 return 1;
239 i++;
240 }
241 if (wrap) {
242 if (i == 0) /* empty list */
243 return 1;
244 i=0; /* first item */
245 screen_bell();
246 }
247 }
249 return 1;
250 }
252 int
253 list_window_rfind(struct list_window *lw,
254 list_window_callback_fn_t callback,
255 void *callback_data,
256 const char *str,
257 int wrap,
258 unsigned rows)
259 {
260 int h;
261 int i = lw->selected - 1;
262 const char *label;
264 if (rows == 0)
265 return 1;
267 while (wrap || i == (int)lw->selected - 1) {
268 while (i >= 0 && (label = callback(i,&h,callback_data))) {
269 if( str && label && strcasestr(label, str) ) {
270 lw->selected = i;
271 return 0;
272 }
273 if (wrap && i == (int)lw->selected)
274 return 1;
275 i--;
276 }
277 if (wrap) {
278 i = rows - 1; /* last item */
279 screen_bell();
280 }
281 }
282 return 1;
283 }
285 /* perform basic list window commands (movement) */
286 int
287 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
288 {
289 switch (cmd) {
290 case CMD_LIST_PREVIOUS:
291 list_window_previous(lw, rows);
292 break;
293 case CMD_LIST_NEXT:
294 list_window_next(lw, rows);
295 break;
296 case CMD_LIST_FIRST:
297 list_window_first(lw);
298 break;
299 case CMD_LIST_LAST:
300 list_window_last(lw, rows);
301 break;
302 case CMD_LIST_NEXT_PAGE:
303 list_window_next_page(lw, rows);
304 break;
305 case CMD_LIST_PREVIOUS_PAGE:
306 list_window_previous_page(lw);
307 break;
308 default:
309 return 0;
310 }
312 lw->repaint = 1;
313 return 1;
314 }
316 int
317 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
318 {
319 switch (cmd) {
320 case CMD_LIST_PREVIOUS:
321 if (lw->start > 0)
322 lw->start--;
323 break;
325 case CMD_LIST_NEXT:
326 if (lw->start + lw->rows < rows)
327 lw->start++;
328 break;
330 case CMD_LIST_FIRST:
331 lw->start = 0;
332 break;
334 case CMD_LIST_LAST:
335 if (rows > lw->rows)
336 lw->start = rows - lw->rows;
337 else
338 lw->start = 0;
339 break;
341 case CMD_LIST_NEXT_PAGE:
342 lw->start += lw->rows - 1;
343 if (lw->start + lw->rows > rows) {
344 if (rows > lw->rows)
345 lw->start = rows - lw->rows;
346 else
347 lw->start = 0;
348 }
349 break;
351 case CMD_LIST_PREVIOUS_PAGE:
352 if (lw->start > lw->rows)
353 lw->start -= lw->rows;
354 else
355 lw->start = 0;
356 break;
358 default:
359 return 0;
360 }
362 lw->repaint = lw->clear = 1;
363 return 1;
364 }
366 #ifdef HAVE_GETMOUSE
367 int
368 list_window_mouse(struct list_window *lw, unsigned rows,
369 unsigned long bstate, int y)
370 {
371 assert(lw != NULL);
373 /* if the even occured above the list window move up */
374 if (y < 0) {
375 if (bstate & BUTTON3_CLICKED)
376 list_window_first(lw);
377 else
378 list_window_previous_page(lw);
379 return 1;
380 }
382 /* if the even occured below the list window move down */
383 if ((unsigned)y >= rows) {
384 if (bstate & BUTTON3_CLICKED)
385 list_window_last(lw, rows);
386 else
387 list_window_next_page(lw, rows);
388 return 1;
389 }
391 return 0;
392 }
393 #endif
395 list_window_state_t *
396 list_window_init_state(void)
397 {
398 return g_malloc0(sizeof(list_window_state_t));
399 }
401 void
402 list_window_free_state(list_window_state_t *state)
403 {
404 if (state) {
405 if (state->list) {
406 GList *list = state->list;
408 while (list) {
409 g_free(list->data);
410 list->data = NULL;
411 list = list->next;
412 }
414 g_list_free(state->list);
415 state->list = NULL;
416 }
418 g_free(state);
419 }
420 }
422 void
423 list_window_push_state(list_window_state_t *state, struct list_window *lw)
424 {
425 if (state) {
426 struct list_window *tmp = g_malloc(sizeof(list_window_t));
427 memcpy(tmp, lw, sizeof(list_window_t));
428 state->list = g_list_prepend(state->list, (gpointer) tmp);
429 list_window_reset(lw);
430 }
431 }
433 bool
434 list_window_pop_state(list_window_state_t *state, struct list_window *lw)
435 {
436 if (state && state->list) {
437 struct list_window *tmp = state->list->data;
439 memcpy(lw, tmp, sizeof(list_window_t));
440 g_free(tmp);
441 state->list->data = NULL;
442 state->list = g_list_delete_link(state->list, state->list);
443 }
445 // return TRUE if there are still states in the list
446 return (state && state->list) ? TRUE : FALSE;
447 }