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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #include "options.h"
29 #include "support.h"
30 #include "command.h"
31 #include "colors.h"
32 #include "list_window.h"
34 extern void screen_bell(void);
36 list_window_t *
37 list_window_init(WINDOW *w, int width, int height)
38 {
39 list_window_t *lw;
41 lw = g_malloc0(sizeof(list_window_t));
42 lw->w = w;
43 lw->cols = width;
44 lw->rows = height;
45 lw->clear = 1;
46 return lw;
47 }
49 list_window_t *
50 list_window_free(list_window_t *lw)
51 {
52 if( lw )
53 {
54 memset(lw, 0, sizeof(list_window_t));
55 g_free(lw);
56 }
57 return NULL;
58 }
60 void
61 list_window_reset(list_window_t *lw)
62 {
63 lw->selected = 0;
64 lw->xoffset = 0;
65 lw->start = 0;
66 lw->clear = 1;
67 }
69 void
70 list_window_check_selected(list_window_t *lw, int length)
71 {
72 while( lw->start && lw->start+lw->rows>length)
73 lw->start--;
75 if( lw->selected<0 )
76 lw->selected=0;
78 while( lw->selected<lw->start )
79 lw->selected++;
81 while( lw->selected>0 && length>0 && lw->selected>=length )
82 lw->selected--;
83 }
85 void
86 list_window_set_selected(list_window_t *lw, int n)
87 {
88 lw->selected=n;
89 }
91 void
92 list_window_next(list_window_t *lw, int length)
93 {
94 if( lw->selected < length-1 )
95 lw->selected++;
96 else if ( options.list_wrap )
97 lw->selected = 0;
98 }
100 void
101 list_window_previous(list_window_t *lw, int length)
102 {
103 if( lw->selected > 0 )
104 lw->selected--;
105 else if( options.list_wrap )
106 lw->selected = length-1;
107 }
109 void
110 list_window_right(list_window_t *lw, int length)
111 {
112 lw->xoffset++;
113 }
115 void
116 list_window_left(list_window_t *lw)
117 {
118 if( lw->xoffset > 0 )
119 lw->xoffset--;
120 }
122 void
123 list_window_first(list_window_t *lw)
124 {
125 lw->xoffset = 0;
126 lw->selected = 0;
127 }
129 void
130 list_window_last(list_window_t *lw, int length)
131 {
132 lw->xoffset = 0;
133 lw->selected = length-1;
134 }
136 void
137 list_window_next_page(list_window_t *lw, int length)
138 {
139 int step = lw->rows-1;
140 if( step<= 0 )
141 return;
142 if( lw->selected+step < length-1 )
143 lw->selected+=step;
144 else
145 return list_window_last(lw,length);
146 }
148 void
149 list_window_previous_page(list_window_t *lw)
150 {
151 int step = lw->rows-1;
152 if( step<= 0 )
153 return;
154 if( lw->selected-step > 0 )
155 lw->selected-=step;
156 else
157 list_window_first(lw);
158 }
161 void
162 list_window_paint(list_window_t *lw,
163 list_window_callback_fn_t callback,
164 void *callback_data)
165 {
166 int i;
167 int fill = options.wide_cursor;
168 int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
170 if( show_cursor )
171 {
172 while( lw->selected < lw->start )
173 {
174 lw->start--;
175 lw->clear=1;
176 }
177 while( lw->selected >= lw->start+lw->rows )
178 {
179 lw->start++;
180 lw->clear=1;
181 }
182 }
184 for(i=0; i<lw->rows; i++)
185 {
186 int highlight = 0;
187 char *label;
189 label = (callback) (lw->start+i, &highlight, callback_data);
190 wmove(lw->w, i, 0);
191 if( lw->clear && (!fill || !label) )
192 wclrtoeol(lw->w);
193 if( label )
194 {
195 int selected = lw->start+i == lw->selected;
196 size_t len = my_strlen(label);
198 if( highlight )
199 colors_use(lw->w, COLOR_LIST_BOLD);
200 else
201 colors_use(lw->w, COLOR_LIST);
203 if( show_cursor && selected )
204 wattron(lw->w, A_REVERSE);
206 waddnstr(lw->w, label, lw->cols);
207 if( fill && len<lw->cols )
208 mvwhline(lw->w, i, len, ' ', lw->cols-len);
210 if( selected )
211 wattroff(lw->w, A_REVERSE);
212 }
214 }
215 lw->clear=0;
216 }
219 int
220 list_window_find(list_window_t *lw,
221 list_window_callback_fn_t callback,
222 void *callback_data,
223 char *str,
224 int wrap)
225 {
226 int h;
227 int i = lw->selected+1;
228 char *label;
230 while( wrap || i==lw->selected+1 )
231 {
232 while( (label=(callback) (i,&h,callback_data)) )
233 {
234 if( str && label && strcasestr(label, str) )
235 {
236 lw->selected = i;
237 return 0;
238 }
239 if( wrap && i==lw->selected )
240 return 1;
241 i++;
242 }
243 if( wrap )
244 {
245 if ( i==0 ) /* empty list */
246 return 1;
247 i=0; /* first item */
248 screen_bell();
249 }
250 }
251 return 1;
252 }
255 int
256 list_window_rfind(list_window_t *lw,
257 list_window_callback_fn_t callback,
258 void *callback_data,
259 char *str,
260 int wrap,
261 int rows)
262 {
263 int h;
264 int i = lw->selected-1;
265 char *label;
267 if ( rows == 0 )
268 return 1;
270 while( wrap || i==lw->selected-1 )
271 {
272 while( i>=0 && (label=(callback) (i,&h,callback_data)) )
273 {
274 if( str && label && strcasestr(label, str) )
275 {
276 lw->selected = i;
277 return 0;
278 }
279 if( wrap && i==lw->selected )
280 return 1;
281 i--;
282 }
283 if( wrap )
284 {
285 i=rows-1; /* last item */
286 screen_bell();
287 }
288 }
289 return 1;
290 }
293 /* perform basic list window commands (movement) */
294 int
295 list_window_cmd(list_window_t *lw, int rows, command_t cmd)
296 {
297 switch(cmd)
298 {
299 case CMD_LIST_PREVIOUS:
300 list_window_previous(lw, rows);
301 lw->repaint=1;
302 break;
303 case CMD_LIST_NEXT:
304 list_window_next(lw, rows);
305 lw->repaint=1;
306 break;
307 case CMD_LIST_FIRST:
308 list_window_first(lw);
309 lw->repaint = 1;
310 break;
311 case CMD_LIST_LAST:
312 list_window_last(lw, rows);
313 lw->repaint = 1;
314 break;
315 case CMD_LIST_NEXT_PAGE:
316 list_window_next_page(lw, rows);
317 lw->repaint = 1;
318 break;
319 case CMD_LIST_PREVIOUS_PAGE:
320 list_window_previous_page(lw);
321 lw->repaint = 1;
322 break;
323 default:
324 return 0;
325 }
326 return 1;
327 }