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 <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <ncurses.h>
25 #include "config.h"
26 #include "options.h"
27 #include "support.h"
28 #include "command.h"
29 #include "list_window.h"
31 list_window_t *
32 list_window_init(WINDOW *w, int width, int height)
33 {
34 list_window_t *lw;
36 lw = g_malloc(sizeof(list_window_t));
37 memset(lw, 0, sizeof(list_window_t));
38 lw->w = w;
39 lw->cols = width;
40 lw->rows = height;
41 lw->clear = 1;
42 return lw;
43 }
45 list_window_t *
46 list_window_free(list_window_t *lw)
47 {
48 if( lw )
49 {
50 memset(lw, 0, sizeof(list_window_t));
51 g_free(lw);
52 }
53 return NULL;
54 }
56 void
57 list_window_reset(list_window_t *lw)
58 {
59 lw->selected = 0;
60 lw->start = 0;
61 lw->clear = 1;
62 }
64 void
65 list_window_check_selected(list_window_t *lw, int length)
66 {
67 while( lw->start && lw->start+lw->rows>length)
68 lw->start--;
70 if( lw->selected<0 )
71 lw->selected=0;
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, int n)
82 {
83 lw->selected=n;
84 }
86 void
87 list_window_next(list_window_t *lw, int length)
88 {
89 if( lw->selected < length-1 )
90 lw->selected++;
91 }
93 void
94 list_window_previous(list_window_t *lw)
95 {
96 if( lw->selected > 0 )
97 lw->selected--;
98 }
100 void
101 list_window_first(list_window_t *lw)
102 {
103 lw->selected = 0;
104 }
106 void
107 list_window_last(list_window_t *lw, int length)
108 {
109 lw->selected = length-1;
110 }
112 void
113 list_window_next_page(list_window_t *lw, int length)
114 {
115 int step = lw->rows-1;
116 if( step<= 0 )
117 return;
118 if( lw->selected+step < length-1 )
119 lw->selected+=step;
120 else
121 return list_window_last(lw,length);
122 }
124 void
125 list_window_previous_page(list_window_t *lw)
126 {
127 int step = lw->rows-1;
128 if( step<= 0 )
129 return;
130 if( lw->selected-step > 0 )
131 lw->selected-=step;
132 else
133 list_window_first(lw);
134 }
137 void
138 list_window_paint(list_window_t *lw,
139 list_window_callback_fn_t callback,
140 void *callback_data)
141 {
142 int i;
143 int fill = options.wide_cursor;
145 while( lw->selected < lw->start )
146 {
147 lw->start--;
148 lw->clear=1;
149 }
150 while( lw->selected >= lw->start+lw->rows )
151 {
152 lw->start++;
153 lw->clear=1;
154 }
156 for(i=0; i<lw->rows; i++)
157 {
158 int highlight = 0;
159 char *label;
161 label = (callback) (lw->start+i, &highlight, callback_data);
162 wmove(lw->w, i, 0);
163 if( lw->clear && (!fill || !label) )
164 wclrtoeol(lw->w);
165 if( label )
166 {
167 if( highlight )
168 wattron(lw->w, A_BOLD);
169 if( lw->start+i == lw->selected )
170 wattron(lw->w, A_REVERSE);
172 waddnstr(lw->w, label, lw->cols-1);
173 if( fill )
174 mvwhline(lw->w, i, strlen(label), ' ', lw->cols-1);
176 if( highlight )
177 wattroff(lw->w, A_BOLD);
178 if( lw->start+i == lw->selected )
179 wattroff(lw->w, A_REVERSE);
180 }
182 }
183 lw->clear=0;
184 }
187 int
188 list_window_find(list_window_t *lw,
189 list_window_callback_fn_t callback,
190 void *callback_data,
191 char *str,
192 int wrap)
193 {
194 int h;
195 int i = lw->selected+1;
196 char *label;
198 while( wrap || i==lw->selected+1 )
199 {
200 while( (label=(callback) (i,&h,callback_data)) )
201 {
202 if( str && label && strcasestr(label, str) )
203 {
204 lw->selected = i;
205 return 0;
206 }
207 if( wrap && i==lw->selected )
208 return 1;
209 i++;
210 }
211 if( wrap )
212 {
213 i=0; /* first item */
214 beep();
215 }
216 }
217 return 1;
218 }
221 int
222 list_window_rfind(list_window_t *lw,
223 list_window_callback_fn_t callback,
224 void *callback_data,
225 char *str,
226 int wrap,
227 int rows)
228 {
229 int h;
230 int i = lw->selected-1;
231 char *label;
233 while( wrap || i==lw->selected-1 )
234 {
235 while( i>=0 && (label=(callback) (i,&h,callback_data)) )
236 {
237 if( str && label && strcasestr(label, str) )
238 {
239 lw->selected = i;
240 return 0;
241 }
242 if( wrap && i==lw->selected )
243 return 1;
244 i--;
245 }
246 if( wrap )
247 {
248 i=rows-1; /* last item */
249 beep();
250 }
251 }
252 return 1;
253 }
256 /* perform basic list window commands (movement) */
257 int
258 list_window_cmd(list_window_t *lw, int rows, command_t cmd)
259 {
260 switch(cmd)
261 {
262 case CMD_LIST_PREVIOUS:
263 list_window_previous(lw);
264 lw->repaint=1;
265 break;
266 case CMD_LIST_NEXT:
267 list_window_next(lw, rows);
268 lw->repaint=1;
269 break;
270 case CMD_LIST_FIRST:
271 list_window_first(lw);
272 lw->repaint = 1;
273 break;
274 case CMD_LIST_LAST:
275 list_window_last(lw, rows);
276 lw->repaint = 1;
277 break;
278 case CMD_LIST_NEXT_PAGE:
279 list_window_next_page(lw, rows);
280 lw->repaint = 1;
281 break;
282 case CMD_LIST_PREVIOUS_PAGE:
283 list_window_previous_page(lw);
284 lw->repaint = 1;
285 break;
286 default:
287 return 0;
288 }
289 return 1;
290 }