f26088da482d1cda8a48475bc3a90abe155e9f58
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 <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 extern void screen_bell(void);
34 struct list_window *
35 list_window_init(WINDOW *w, unsigned width, unsigned height)
36 {
37 struct list_window *lw;
39 lw = g_malloc0(sizeof(list_window_t));
40 lw->w = w;
41 lw->cols = width;
42 lw->rows = height;
43 lw->clear = 1;
44 return lw;
45 }
47 struct list_window *
48 list_window_free(struct list_window *lw)
49 {
50 if (lw) {
51 memset(lw, 0, sizeof(list_window_t));
52 g_free(lw);
53 }
55 return NULL;
56 }
58 void
59 list_window_reset(struct list_window *lw)
60 {
61 lw->selected = 0;
62 lw->xoffset = 0;
63 lw->start = 0;
64 lw->clear = 1;
65 }
67 void
68 list_window_check_selected(struct list_window *lw, unsigned length)
69 {
70 if (lw->start + lw->rows > length) {
71 if (length > lw->rows)
72 lw->start = length - lw->rows;
73 else
74 lw->start = 0;
75 }
77 if (lw->selected < lw->start)
78 lw->selected = lw->start;
80 if (length > 0 && lw->selected >= length)
81 lw->selected = length - 1;
82 }
84 void
85 list_window_set_selected(struct list_window *lw, unsigned n)
86 {
87 lw->selected = n;
88 }
90 void
91 list_window_next(struct list_window *lw, unsigned length)
92 {
93 if (lw->selected + 1 < length)
94 lw->selected++;
95 else if (options.list_wrap)
96 lw->selected = 0;
97 }
99 void
100 list_window_previous(struct list_window *lw, unsigned length)
101 {
102 if (lw->selected > 0)
103 lw->selected--;
104 else if (options.list_wrap)
105 lw->selected = length - 1;
106 }
108 void
109 list_window_first(struct list_window *lw)
110 {
111 lw->xoffset = 0;
112 lw->selected = 0;
113 }
115 void
116 list_window_last(struct list_window *lw, unsigned length)
117 {
118 lw->xoffset = 0;
119 if (length > 0)
120 lw->selected = length - 1;
121 else
122 lw->selected = 0;
123 }
125 void
126 list_window_next_page(struct list_window *lw, unsigned length)
127 {
128 if (lw->rows < 2)
129 return;
130 if (lw->selected + lw->rows < length)
131 lw->selected += lw->rows - 1;
132 else
133 return list_window_last(lw, length);
134 }
136 void
137 list_window_previous_page(struct list_window *lw)
138 {
139 if (lw->rows < 2)
140 return;
141 if (lw->selected > lw->rows - 1)
142 lw->selected -= lw->rows - 1;
143 else
144 list_window_first(lw);
145 }
148 void
149 list_window_paint(struct list_window *lw,
150 list_window_callback_fn_t callback,
151 void *callback_data)
152 {
153 unsigned i;
154 int fill = options.wide_cursor;
155 int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
157 if (show_cursor) {
158 if (lw->selected < lw->start) {
159 lw->start = lw->selected;
160 lw->clear=1;
161 }
163 if (lw->selected >= lw->start + lw->rows) {
164 lw->start = lw->selected - lw->rows + 1;
165 lw->clear=1;
166 }
167 }
169 for (i = 0; i < lw->rows; i++) {
170 int highlight = 0;
171 const char *label;
173 label = callback(lw->start + i, &highlight, callback_data);
174 wmove(lw->w, i, 0);
175 if( lw->clear && (!fill || !label) )
176 wclrtoeol(lw->w);
178 if (label) {
179 int selected = lw->start + i == lw->selected;
180 size_t len = my_strlen(label);
182 if( highlight )
183 colors_use(lw->w, COLOR_LIST_BOLD);
184 else
185 colors_use(lw->w, COLOR_LIST);
187 if( show_cursor && selected )
188 wattron(lw->w, A_REVERSE);
190 //waddnstr(lw->w, label, lw->cols);
191 waddstr(lw->w, label);
192 if( fill && len<lw->cols )
193 whline(lw->w, ' ', lw->cols-len);
195 if( selected )
196 wattroff(lw->w, A_REVERSE);
197 }
198 }
200 lw->clear=0;
201 }
203 int
204 list_window_find(struct list_window *lw,
205 list_window_callback_fn_t callback,
206 void *callback_data,
207 const char *str,
208 int wrap)
209 {
210 int h;
211 unsigned i = lw->selected + 1;
212 const char *label;
214 while (wrap || i == lw->selected + 1) {
215 while ((label = callback(i,&h,callback_data))) {
216 if (str && label && strcasestr(label, str)) {
217 lw->selected = i;
218 return 0;
219 }
220 if (wrap && i == lw->selected)
221 return 1;
222 i++;
223 }
224 if (wrap) {
225 if (i == 0) /* empty list */
226 return 1;
227 i=0; /* first item */
228 screen_bell();
229 }
230 }
232 return 1;
233 }
235 int
236 list_window_rfind(struct list_window *lw,
237 list_window_callback_fn_t callback,
238 void *callback_data,
239 const char *str,
240 int wrap,
241 unsigned rows)
242 {
243 int h;
244 int i = lw->selected - 1;
245 const char *label;
247 if (rows == 0)
248 return 1;
250 while (wrap || i == (int)lw->selected - 1) {
251 while (i >= 0 && (label = callback(i,&h,callback_data))) {
252 if( str && label && strcasestr(label, str) ) {
253 lw->selected = i;
254 return 0;
255 }
256 if (wrap && i == (int)lw->selected)
257 return 1;
258 i--;
259 }
260 if (wrap) {
261 i = rows - 1; /* last item */
262 screen_bell();
263 }
264 }
265 return 1;
266 }
268 /* perform basic list window commands (movement) */
269 int
270 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
271 {
272 switch (cmd) {
273 case CMD_LIST_PREVIOUS:
274 list_window_previous(lw, rows);
275 break;
276 case CMD_LIST_NEXT:
277 list_window_next(lw, rows);
278 break;
279 case CMD_LIST_FIRST:
280 list_window_first(lw);
281 break;
282 case CMD_LIST_LAST:
283 list_window_last(lw, rows);
284 break;
285 case CMD_LIST_NEXT_PAGE:
286 list_window_next_page(lw, rows);
287 break;
288 case CMD_LIST_PREVIOUS_PAGE:
289 list_window_previous_page(lw);
290 break;
291 default:
292 return 0;
293 }
295 lw->repaint = 1;
296 return 1;
297 }
299 int
300 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
301 {
302 switch (cmd) {
303 case CMD_LIST_PREVIOUS:
304 if (lw->start > 0)
305 lw->start--;
306 break;
308 case CMD_LIST_NEXT:
309 if (lw->start + lw->rows < rows)
310 lw->start++;
311 break;
313 case CMD_LIST_FIRST:
314 lw->start = 0;
315 break;
317 case CMD_LIST_LAST:
318 if (rows > lw->rows)
319 lw->start = rows - lw->rows;
320 else
321 lw->start = 0;
322 break;
324 case CMD_LIST_NEXT_PAGE:
325 lw->start += lw->rows - 1;
326 if (lw->start + lw->rows > rows) {
327 if (rows > lw->rows)
328 lw->start = rows - lw->rows;
329 else
330 lw->start = 0;
331 }
332 break;
334 case CMD_LIST_PREVIOUS_PAGE:
335 if (lw->start > lw->rows)
336 lw->start -= lw->rows;
337 else
338 lw->start = 0;
339 break;
341 default:
342 return 0;
343 }
345 lw->repaint = lw->clear = 1;
346 return 1;
347 }
349 list_window_state_t *
350 list_window_init_state(void)
351 {
352 return g_malloc0(sizeof(list_window_state_t));
353 }
355 list_window_state_t *
356 list_window_free_state(list_window_state_t *state)
357 {
358 if (state) {
359 if (state->list) {
360 GList *list = state->list;
362 while (list) {
363 g_free(list->data);
364 list->data = NULL;
365 list = list->next;
366 }
368 g_list_free(state->list);
369 state->list = NULL;
370 }
372 g_free(state);
373 }
375 return NULL;
376 }
378 void
379 list_window_push_state(list_window_state_t *state, struct list_window *lw)
380 {
381 if (state) {
382 struct list_window *tmp = g_malloc(sizeof(list_window_t));
383 memcpy(tmp, lw, sizeof(list_window_t));
384 state->list = g_list_prepend(state->list, (gpointer) tmp);
385 list_window_reset(lw);
386 }
387 }
389 bool
390 list_window_pop_state(list_window_state_t *state, struct list_window *lw)
391 {
392 if (state && state->list) {
393 struct list_window *tmp = state->list->data;
395 memcpy(lw, tmp, sizeof(list_window_t));
396 g_free(tmp);
397 state->list->data = NULL;
398 state->list = g_list_delete_link(state->list, state->list);
399 }
401 // return TRUE if there are still states in the list
402 return (state && state->list) ? TRUE : FALSE;
403 }