1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "list_window.h"
20 #include "config.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "support.h"
24 #include "command.h"
25 #include "colors.h"
27 #include <assert.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 return lw;
44 }
46 void
47 list_window_free(struct list_window *lw)
48 {
49 if (lw) {
50 memset(lw, 0, sizeof(list_window_t));
51 g_free(lw);
52 }
53 }
55 void
56 list_window_reset(struct list_window *lw)
57 {
58 lw->selected = 0;
59 lw->xoffset = 0;
60 lw->start = 0;
61 }
63 void
64 list_window_check_selected(struct list_window *lw, unsigned length)
65 {
66 if (lw->start + lw->rows > length) {
67 if (length > lw->rows)
68 lw->start = length - lw->rows;
69 else
70 lw->start = 0;
71 }
73 if (lw->selected < lw->start)
74 lw->selected = lw->start;
76 if (length > 0 && lw->selected >= length)
77 lw->selected = length - 1;
78 }
80 void
81 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
82 {
83 if (n > lw->rows / 2)
84 lw->start = n - lw->rows / 2;
85 else
86 lw->start = 0;
88 if (lw->start + lw->rows > rows) {
89 if (lw->rows < rows)
90 lw->start = rows - lw->rows;
91 else
92 lw->start = 0;
93 }
94 }
96 void
97 list_window_set_selected(struct list_window *lw, unsigned n)
98 {
99 lw->selected = n;
100 }
102 static void
103 list_window_next(struct list_window *lw, unsigned length)
104 {
105 if (lw->selected + 1 < length)
106 lw->selected++;
107 else if (options.list_wrap)
108 lw->selected = 0;
109 }
111 static void
112 list_window_previous(struct list_window *lw, unsigned length)
113 {
114 if (lw->selected > 0)
115 lw->selected--;
116 else if (options.list_wrap)
117 lw->selected = length - 1;
118 }
120 static void
121 list_window_first(struct list_window *lw)
122 {
123 lw->xoffset = 0;
124 lw->selected = 0;
125 }
127 static void
128 list_window_last(struct list_window *lw, unsigned length)
129 {
130 lw->xoffset = 0;
131 if (length > 0)
132 lw->selected = length - 1;
133 else
134 lw->selected = 0;
135 }
137 static void
138 list_window_next_page(struct list_window *lw, unsigned length)
139 {
140 if (lw->rows < 2)
141 return;
142 if (lw->selected + lw->rows < length)
143 lw->selected += lw->rows - 1;
144 else
145 list_window_last(lw, length);
146 }
148 static void
149 list_window_previous_page(struct list_window *lw)
150 {
151 if (lw->rows < 2)
152 return;
153 if (lw->selected > lw->rows - 1)
154 lw->selected -= lw->rows - 1;
155 else
156 list_window_first(lw);
157 }
160 void
161 list_window_paint(struct list_window *lw,
162 list_window_callback_fn_t callback,
163 void *callback_data)
164 {
165 unsigned i;
166 int fill = options.wide_cursor;
167 int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
169 if (show_cursor) {
170 if (lw->selected < lw->start)
171 lw->start = lw->selected;
173 if (lw->selected >= lw->start + lw->rows)
174 lw->start = lw->selected - lw->rows + 1;
175 }
177 for (i = 0; i < lw->rows; i++) {
178 int highlight = 0;
179 const char *label;
181 label = callback(lw->start + i, &highlight, callback_data);
182 wmove(lw->w, i, 0);
184 if (label) {
185 int selected = lw->start + i == lw->selected;
186 unsigned len = utf8_width(label);
188 if (highlight)
189 colors_use(lw->w, COLOR_LIST_BOLD);
190 else
191 colors_use(lw->w, COLOR_LIST);
193 if (show_cursor && selected)
194 wattron(lw->w, A_REVERSE);
196 //waddnstr(lw->w, label, lw->cols);
197 waddstr(lw->w, label);
198 if (fill && len < lw->cols)
199 whline(lw->w, ' ', lw->cols-len);
201 if (selected)
202 wattroff(lw->w, A_REVERSE);
204 if (!fill && len < lw->cols)
205 wclrtoeol(lw->w);
206 } else
207 wclrtoeol(lw->w);
208 }
209 }
211 int
212 list_window_find(struct list_window *lw,
213 list_window_callback_fn_t callback,
214 void *callback_data,
215 const char *str,
216 int wrap)
217 {
218 int h;
219 unsigned i = lw->selected + 1;
220 const char *label;
222 while (wrap || i == lw->selected + 1) {
223 while ((label = callback(i,&h,callback_data))) {
224 if (str && label && strcasestr(label, str)) {
225 lw->selected = i;
226 return 0;
227 }
228 if (wrap && i == lw->selected)
229 return 1;
230 i++;
231 }
232 if (wrap) {
233 if (i == 0) /* empty list */
234 return 1;
235 i=0; /* first item */
236 screen_bell();
237 }
238 }
240 return 1;
241 }
243 int
244 list_window_rfind(struct list_window *lw,
245 list_window_callback_fn_t callback,
246 void *callback_data,
247 const char *str,
248 int wrap,
249 unsigned rows)
250 {
251 int h;
252 int i = lw->selected - 1;
253 const char *label;
255 if (rows == 0)
256 return 1;
258 while (wrap || i == (int)lw->selected - 1) {
259 while (i >= 0 && (label = callback(i,&h,callback_data))) {
260 if( str && label && strcasestr(label, str) ) {
261 lw->selected = i;
262 return 0;
263 }
264 if (wrap && i == (int)lw->selected)
265 return 1;
266 i--;
267 }
268 if (wrap) {
269 i = rows - 1; /* last item */
270 screen_bell();
271 }
272 }
273 return 1;
274 }
276 /* perform basic list window commands (movement) */
277 int
278 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
279 {
280 switch (cmd) {
281 case CMD_LIST_PREVIOUS:
282 list_window_previous(lw, rows);
283 break;
284 case CMD_LIST_NEXT:
285 list_window_next(lw, rows);
286 break;
287 case CMD_LIST_FIRST:
288 list_window_first(lw);
289 break;
290 case CMD_LIST_LAST:
291 list_window_last(lw, rows);
292 break;
293 case CMD_LIST_NEXT_PAGE:
294 list_window_next_page(lw, rows);
295 break;
296 case CMD_LIST_PREVIOUS_PAGE:
297 list_window_previous_page(lw);
298 break;
299 default:
300 return 0;
301 }
303 return 1;
304 }
306 int
307 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
308 {
309 switch (cmd) {
310 case CMD_LIST_PREVIOUS:
311 if (lw->start > 0)
312 lw->start--;
313 break;
315 case CMD_LIST_NEXT:
316 if (lw->start + lw->rows < rows)
317 lw->start++;
318 break;
320 case CMD_LIST_FIRST:
321 lw->start = 0;
322 break;
324 case CMD_LIST_LAST:
325 if (rows > lw->rows)
326 lw->start = rows - lw->rows;
327 else
328 lw->start = 0;
329 break;
331 case CMD_LIST_NEXT_PAGE:
332 lw->start += lw->rows - 1;
333 if (lw->start + lw->rows > rows) {
334 if (rows > lw->rows)
335 lw->start = rows - lw->rows;
336 else
337 lw->start = 0;
338 }
339 break;
341 case CMD_LIST_PREVIOUS_PAGE:
342 if (lw->start > lw->rows)
343 lw->start -= lw->rows;
344 else
345 lw->start = 0;
346 break;
348 default:
349 return 0;
350 }
352 return 1;
353 }
355 #ifdef HAVE_GETMOUSE
356 int
357 list_window_mouse(struct list_window *lw, unsigned rows,
358 unsigned long bstate, int y)
359 {
360 assert(lw != NULL);
362 /* if the even occured above the list window move up */
363 if (y < 0) {
364 if (bstate & BUTTON3_CLICKED)
365 list_window_first(lw);
366 else
367 list_window_previous_page(lw);
368 return 1;
369 }
371 /* if the even occured below the list window move down */
372 if ((unsigned)y >= rows) {
373 if (bstate & BUTTON3_CLICKED)
374 list_window_last(lw, rows);
375 else
376 list_window_next_page(lw, rows);
377 return 1;
378 }
380 return 0;
381 }
382 #endif