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 list_window_t *
35 list_window_init(WINDOW *w, unsigned width, unsigned height)
36 {
37 list_window_t *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 list_window_t *
48 list_window_free(list_window_t *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(list_window_t *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(list_window_t *lw, unsigned length)
69 {
70 while (lw->start > 0 && lw->start + lw->rows > length)
71 lw->start--;
73 while (lw->selected < lw->start)
74 lw->selected++;
76 while (lw->selected > 0 && length > 0 && lw->selected >= length)
77 lw->selected--;
78 }
80 void
81 list_window_set_selected(list_window_t *lw, unsigned n)
82 {
83 lw->selected = n;
84 }
86 void
87 list_window_next(list_window_t *lw, unsigned length)
88 {
89 if (lw->selected + 1 < length)
90 lw->selected++;
91 else if (options.list_wrap)
92 lw->selected = 0;
93 }
95 void
96 list_window_previous(list_window_t *lw, unsigned length)
97 {
98 if (lw->selected > 0)
99 lw->selected--;
100 else if (options.list_wrap)
101 lw->selected = length - 1;
102 }
104 void
105 list_window_first(list_window_t *lw)
106 {
107 lw->xoffset = 0;
108 lw->selected = 0;
109 }
111 void
112 list_window_last(list_window_t *lw, unsigned length)
113 {
114 lw->xoffset = 0;
115 if (length > 0)
116 lw->selected = length - 1;
117 else
118 lw->selected = 0;
119 }
121 void
122 list_window_next_page(list_window_t *lw, unsigned length)
123 {
124 if (lw->rows < 2)
125 return;
126 if (lw->selected + lw->rows < length)
127 lw->selected += lw->rows - 1;
128 else
129 return list_window_last(lw, length);
130 }
132 void
133 list_window_previous_page(list_window_t *lw)
134 {
135 if (lw->rows < 2)
136 return;
137 if (lw->selected > lw->rows - 1)
138 lw->selected -= lw->rows - 1;
139 else
140 list_window_first(lw);
141 }
144 void
145 list_window_paint(list_window_t *lw,
146 list_window_callback_fn_t callback,
147 void *callback_data)
148 {
149 unsigned i;
150 int fill = options.wide_cursor;
151 int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
153 if (show_cursor) {
154 while (lw->selected < lw->start) {
155 lw->start--;
156 lw->clear=1;
157 }
159 while (lw->selected >= lw->start+lw->rows) {
160 lw->start++;
161 lw->clear=1;
162 }
163 }
165 for (i = 0; i < lw->rows; i++) {
166 int highlight = 0;
167 const char *label;
169 label = callback(lw->start + i, &highlight, callback_data);
170 wmove(lw->w, i, 0);
171 if( lw->clear && (!fill || !label) )
172 wclrtoeol(lw->w);
174 if (label) {
175 int selected = lw->start + i == lw->selected;
176 size_t len = my_strlen(label);
178 if( highlight )
179 colors_use(lw->w, COLOR_LIST_BOLD);
180 else
181 colors_use(lw->w, COLOR_LIST);
183 if( show_cursor && selected )
184 wattron(lw->w, A_REVERSE);
186 //waddnstr(lw->w, label, lw->cols);
187 waddstr(lw->w, label);
188 if( fill && len<lw->cols )
189 whline(lw->w, ' ', lw->cols-len);
191 if( selected )
192 wattroff(lw->w, A_REVERSE);
193 }
194 }
196 lw->clear=0;
197 }
199 int
200 list_window_find(list_window_t *lw,
201 list_window_callback_fn_t callback,
202 void *callback_data,
203 const char *str,
204 int wrap)
205 {
206 int h;
207 unsigned i = lw->selected + 1;
208 const char *label;
210 while (wrap || i == lw->selected + 1) {
211 while ((label = callback(i,&h,callback_data))) {
212 if (str && label && strcasestr(label, str)) {
213 lw->selected = i;
214 return 0;
215 }
216 if (wrap && i == lw->selected)
217 return 1;
218 i++;
219 }
220 if (wrap) {
221 if (i == 0) /* empty list */
222 return 1;
223 i=0; /* first item */
224 screen_bell();
225 }
226 }
228 return 1;
229 }
231 int
232 list_window_rfind(list_window_t *lw,
233 list_window_callback_fn_t callback,
234 void *callback_data,
235 const char *str,
236 int wrap,
237 unsigned rows)
238 {
239 int h;
240 int i = lw->selected - 1;
241 const char *label;
243 if (rows == 0)
244 return 1;
246 while (wrap || i == (int)lw->selected - 1) {
247 while (i >= 0 && (label = callback(i,&h,callback_data))) {
248 if( str && label && strcasestr(label, str) ) {
249 lw->selected = i;
250 return 0;
251 }
252 if (wrap && i == (int)lw->selected)
253 return 1;
254 i--;
255 }
256 if (wrap) {
257 i = rows - 1; /* last item */
258 screen_bell();
259 }
260 }
261 return 1;
262 }
264 /* perform basic list window commands (movement) */
265 int
266 list_window_cmd(list_window_t *lw, unsigned rows, command_t cmd)
267 {
268 switch (cmd) {
269 case CMD_LIST_PREVIOUS:
270 list_window_previous(lw, rows);
271 lw->repaint=1;
272 break;
273 case CMD_LIST_NEXT:
274 list_window_next(lw, rows);
275 lw->repaint=1;
276 break;
277 case CMD_LIST_FIRST:
278 list_window_first(lw);
279 lw->repaint = 1;
280 break;
281 case CMD_LIST_LAST:
282 list_window_last(lw, rows);
283 lw->repaint = 1;
284 break;
285 case CMD_LIST_NEXT_PAGE:
286 list_window_next_page(lw, rows);
287 lw->repaint = 1;
288 break;
289 case CMD_LIST_PREVIOUS_PAGE:
290 list_window_previous_page(lw);
291 lw->repaint = 1;
292 break;
293 default:
294 return 0;
295 }
297 return 1;
298 }
300 list_window_state_t *
301 list_window_init_state(void)
302 {
303 return g_malloc0(sizeof(list_window_state_t));
304 }
306 list_window_state_t *
307 list_window_free_state(list_window_state_t *state)
308 {
309 if( state )
310 {
311 if( state->list )
312 {
313 GList *list = state->list;
314 while( list )
315 {
316 g_free(list->data);
317 list->data = NULL;
318 list = list->next;
319 }
320 g_list_free(state->list);
321 state->list = NULL;
322 }
323 g_free(state);
324 }
325 return NULL;
326 }
328 void
329 list_window_push_state(list_window_state_t *state, list_window_t *lw)
330 {
331 if( state )
332 {
333 list_window_t *tmp = g_malloc(sizeof(list_window_t));
334 memcpy(tmp, lw, sizeof(list_window_t));
335 state->list = g_list_prepend(state->list, (gpointer) tmp);
336 list_window_reset(lw);
337 }
338 }
340 bool
341 list_window_pop_state(list_window_state_t *state, list_window_t *lw)
342 {
343 if( state && state->list )
344 {
345 list_window_t *tmp = state->list->data;
347 memcpy(lw, tmp, sizeof(list_window_t));
348 g_free(tmp);
349 state->list->data = NULL;
350 state->list = g_list_delete_link(state->list, state->list);
351 }
353 // return TRUE if there are still states in the list
354 return (state && state->list) ? TRUE : FALSE;
355 }