d0fcaa58e1cd078470b945c0df0af41a24b4d965
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 if( lw->selected<0 )
68 lw->selected=0;
70 while( lw->selected<lw->start )
71 lw->selected++;
73 while( lw->selected>0 && length>0 && lw->selected>=length )
74 lw->selected--;
75 }
77 void
78 list_window_set_selected(list_window_t *lw, int n)
79 {
80 lw->selected=n;
81 }
83 void
84 list_window_next(list_window_t *lw, int length)
85 {
86 if( lw->selected < length-1 )
87 lw->selected++;
88 }
90 void
91 list_window_previous(list_window_t *lw)
92 {
93 if( lw->selected > 0 )
94 lw->selected--;
95 }
97 void
98 list_window_first(list_window_t *lw)
99 {
100 lw->selected = 0;
101 }
103 void
104 list_window_last(list_window_t *lw, int length)
105 {
106 lw->selected = length-1;
107 }
109 void
110 list_window_next_page(list_window_t *lw, int length)
111 {
112 int step = lw->rows-1;
113 if( step<= 0 )
114 return;
115 if( lw->selected+step < length-1 )
116 lw->selected+=step;
117 else
118 return list_window_last(lw,length);
119 }
121 void
122 list_window_previous_page(list_window_t *lw)
123 {
124 int step = lw->rows-1;
125 if( step<= 0 )
126 return;
127 if( lw->selected-step > 0 )
128 lw->selected-=step;
129 else
130 list_window_first(lw);
131 }
134 void
135 list_window_paint(list_window_t *lw,
136 list_window_callback_fn_t callback,
137 void *callback_data)
138 {
139 int i;
140 int fill = options.wide_cursor;
142 while( lw->selected < lw->start )
143 {
144 lw->start--;
145 lw->clear=1;
146 }
147 while( lw->selected >= lw->start+lw->rows )
148 {
149 lw->start++;
150 lw->clear=1;
151 }
153 for(i=0; i<lw->rows; i++)
154 {
155 int highlight = 0;
156 char *label;
158 label = (callback) (lw->start+i, &highlight, callback_data);
159 wmove(lw->w, i, 0);
160 if( lw->clear && (!fill || !label) )
161 wclrtoeol(lw->w);
162 if( label )
163 {
164 if( highlight )
165 wattron(lw->w, A_BOLD);
166 if( lw->start+i == lw->selected )
167 wattron(lw->w, A_REVERSE);
169 waddnstr(lw->w, label, lw->cols-1);
170 if( fill )
171 mvwhline(lw->w, i, strlen(label), ' ', lw->cols-1);
173 if( highlight )
174 wattroff(lw->w, A_BOLD);
175 if( lw->start+i == lw->selected )
176 wattroff(lw->w, A_REVERSE);
177 }
178 }
179 lw->clear=0;
180 }
183 int
184 list_window_find(list_window_t *lw,
185 list_window_callback_fn_t callback,
186 void *callback_data,
187 char *str,
188 int wrap)
189 {
190 int h;
191 int i = lw->selected+1;
192 char *label;
194 while( wrap || i==lw->selected+1 )
195 {
196 while( (label=(callback) (i,&h,callback_data)) )
197 {
198 if( str && label && strcasestr(label, str) )
199 {
200 lw->selected = i;
201 return 0;
202 }
203 if( wrap && i==lw->selected )
204 return 1;
205 i++;
206 }
207 if( wrap )
208 {
209 i=0; /* first item */
210 beep();
211 }
212 }
213 return 1;
214 }
217 int
218 list_window_rfind(list_window_t *lw,
219 list_window_callback_fn_t callback,
220 void *callback_data,
221 char *str,
222 int wrap,
223 int rows)
224 {
225 int h;
226 int i = lw->selected-1;
227 char *label;
229 while( wrap || i==lw->selected-1 )
230 {
231 while( i>=0 && (label=(callback) (i,&h,callback_data)) )
232 {
233 if( str && label && strcasestr(label, str) )
234 {
235 lw->selected = i;
236 return 0;
237 }
238 if( wrap && i==lw->selected )
239 return 1;
240 i--;
241 }
242 if( wrap )
243 {
244 i=rows-1; /* last item */
245 beep();
246 }
247 }
248 return 1;
249 }
252 /* perform basic list window commands (movement) */
253 int
254 list_window_cmd(list_window_t *lw, int rows, command_t cmd)
255 {
256 switch(cmd)
257 {
258 case CMD_LIST_PREVIOUS:
259 list_window_previous(lw);
260 lw->repaint=1;
261 break;
262 case CMD_LIST_NEXT:
263 list_window_next(lw, rows);
264 lw->repaint=1;
265 break;
266 case CMD_LIST_FIRST:
267 list_window_first(lw);
268 lw->repaint = 1;
269 break;
270 case CMD_LIST_LAST:
271 list_window_last(lw, rows);
272 lw->repaint = 1;
273 break;
274 case CMD_LIST_NEXT_PAGE:
275 list_window_next_page(lw, rows);
276 lw->repaint = 1;
277 break;
278 case CMD_LIST_PREVIOUS_PAGE:
279 list_window_previous_page(lw);
280 lw->repaint = 1;
281 break;
282 default:
283 return 0;
284 }
285 return 1;
286 }