88f0a690c739f74316872331cb536785f568cda2
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 list_window_state_t *
300 list_window_init_state(void)
301 {
302 return g_malloc0(sizeof(list_window_state_t));
303 }
305 list_window_state_t *
306 list_window_free_state(list_window_state_t *state)
307 {
308 if (state) {
309 if (state->list) {
310 GList *list = state->list;
312 while (list) {
313 g_free(list->data);
314 list->data = NULL;
315 list = list->next;
316 }
318 g_list_free(state->list);
319 state->list = NULL;
320 }
322 g_free(state);
323 }
325 return NULL;
326 }
328 void
329 list_window_push_state(list_window_state_t *state, struct list_window *lw)
330 {
331 if (state) {
332 struct list_window *tmp = g_malloc(sizeof(list_window_t));
333 memcpy(tmp, lw, sizeof(list_window_t));
334 state->list = g_list_prepend(state->list, (gpointer) tmp);
335 list_window_reset(lw);
336 }
337 }
339 bool
340 list_window_pop_state(list_window_state_t *state, struct list_window *lw)
341 {
342 if (state && state->list) {
343 struct list_window *tmp = state->list->data;
345 memcpy(lw, tmp, sizeof(list_window_t));
346 g_free(tmp);
347 state->list->data = NULL;
348 state->list = g_list_delete_link(state->list, state->list);
349 }
351 // return TRUE if there are still states in the list
352 return (state && state->list) ? TRUE : FALSE;
353 }