1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "list_window.h"
21 #include "config.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "match.h"
25 #include "command.h"
26 #include "colors.h"
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
33 extern void screen_bell(void);
35 struct list_window *
36 list_window_init(WINDOW *w, unsigned width, unsigned height)
37 {
38 struct list_window *lw;
40 lw = g_malloc0(sizeof(list_window_t));
41 lw->w = w;
42 lw->cols = width;
43 lw->rows = height;
44 return lw;
45 }
47 void
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 }
54 }
56 void
57 list_window_reset(struct list_window *lw)
58 {
59 lw->selected = 0;
60 lw->xoffset = 0;
61 lw->start = 0;
62 }
64 void
65 list_window_check_selected(struct list_window *lw, unsigned length)
66 {
67 if (lw->start + lw->rows > length) {
68 if (length > lw->rows)
69 lw->start = length - lw->rows;
70 else
71 lw->start = 0;
72 }
74 if (lw->selected < lw->start)
75 lw->selected = lw->start;
77 if (length == 0)
78 lw->selected = 0;
79 else if (lw->selected >= length)
80 lw->selected = length - 1;
81 }
83 void
84 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
85 {
86 if (n > lw->rows / 2)
87 lw->start = n - lw->rows / 2;
88 else
89 lw->start = 0;
91 if (lw->start + lw->rows > rows) {
92 if (lw->rows < rows)
93 lw->start = rows - lw->rows;
94 else
95 lw->start = 0;
96 }
97 }
99 void
100 list_window_set_selected(struct list_window *lw, unsigned n)
101 {
102 lw->selected = n;
103 }
105 static void
106 list_window_next(struct list_window *lw, unsigned length)
107 {
108 if (lw->selected + 1 < length)
109 lw->selected++;
110 else if (options.list_wrap)
111 lw->selected = 0;
112 }
114 static void
115 list_window_previous(struct list_window *lw, unsigned length)
116 {
117 if (lw->selected > 0)
118 lw->selected--;
119 else if (options.list_wrap)
120 lw->selected = length - 1;
121 }
123 static void
124 list_window_first(struct list_window *lw)
125 {
126 lw->xoffset = 0;
127 lw->selected = 0;
128 }
130 static void
131 list_window_last(struct list_window *lw, unsigned length)
132 {
133 lw->xoffset = 0;
134 if (length > 0)
135 lw->selected = length - 1;
136 else
137 lw->selected = 0;
138 }
140 static void
141 list_window_next_page(struct list_window *lw, unsigned length)
142 {
143 if (lw->rows < 2)
144 return;
145 if (lw->selected + lw->rows < length)
146 lw->selected += lw->rows - 1;
147 else
148 list_window_last(lw, length);
149 }
151 static void
152 list_window_previous_page(struct list_window *lw)
153 {
154 if (lw->rows < 2)
155 return;
156 if (lw->selected > lw->rows - 1)
157 lw->selected -= lw->rows - 1;
158 else
159 list_window_first(lw);
160 }
163 void
164 list_window_paint(struct list_window *lw,
165 list_window_callback_fn_t callback,
166 void *callback_data)
167 {
168 unsigned i;
169 bool fill = options.wide_cursor;
170 bool show_cursor = !lw->hide_cursor;
172 if (show_cursor) {
173 if (lw->selected < lw->start)
174 lw->start = lw->selected;
176 if (lw->selected >= lw->start + lw->rows)
177 lw->start = lw->selected - lw->rows + 1;
178 }
180 for (i = 0; i < lw->rows; i++) {
181 bool highlight = false;
182 const char *label;
184 label = callback(lw->start + i, &highlight, callback_data);
185 wmove(lw->w, i, 0);
187 if (label) {
188 bool selected = lw->start + i == lw->selected;
189 unsigned len = utf8_width(label);
191 if (highlight)
192 colors_use(lw->w, COLOR_LIST_BOLD);
193 else
194 colors_use(lw->w, COLOR_LIST);
196 if (show_cursor && selected)
197 wattron(lw->w, A_REVERSE);
199 //waddnstr(lw->w, label, lw->cols);
200 waddstr(lw->w, label);
201 if (fill && len < lw->cols)
202 whline(lw->w, ' ', lw->cols-len);
204 if (selected)
205 wattroff(lw->w, A_REVERSE);
207 if (!fill && len < lw->cols)
208 wclrtoeol(lw->w);
209 } else
210 wclrtoeol(lw->w);
211 }
212 }
214 bool
215 list_window_find(struct list_window *lw,
216 list_window_callback_fn_t callback,
217 void *callback_data,
218 const char *str,
219 bool wrap,
220 bool bell_on_wrap)
221 {
222 bool h;
223 unsigned i = lw->selected + 1;
224 const char *label;
226 do {
227 while ((label = callback(i,&h,callback_data))) {
228 if (str && label && match_line(label, str)) {
229 lw->selected = i;
230 return true;
231 }
232 if (wrap && i == lw->selected)
233 return false;
234 i++;
235 }
236 if (wrap) {
237 if (i == 0) /* empty list */
238 return 1;
239 i=0; /* first item */
240 if (bell_on_wrap) {
241 screen_bell();
242 }
243 }
244 } while (wrap);
246 return false;
247 }
249 bool
250 list_window_rfind(struct list_window *lw,
251 list_window_callback_fn_t callback,
252 void *callback_data,
253 const char *str,
254 bool wrap,
255 bool bell_on_wrap,
256 unsigned rows)
257 {
258 bool h;
259 int i = lw->selected - 1;
260 const char *label;
262 if (rows == 0)
263 return false;
265 do {
266 while (i >= 0 && (label = callback(i,&h,callback_data))) {
267 if( str && label && match_line(label, str) ) {
268 lw->selected = i;
269 return true;
270 }
271 if (wrap && i == (int)lw->selected)
272 return false;
273 i--;
274 }
275 if (wrap) {
276 i = rows - 1; /* last item */
277 if (bell_on_wrap) {
278 screen_bell();
279 }
280 }
281 } while (wrap);
283 return false;
284 }
286 /* perform basic list window commands (movement) */
287 bool
288 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
289 {
290 switch (cmd) {
291 case CMD_LIST_PREVIOUS:
292 list_window_previous(lw, rows);
293 break;
294 case CMD_LIST_NEXT:
295 list_window_next(lw, rows);
296 break;
297 case CMD_LIST_FIRST:
298 list_window_first(lw);
299 break;
300 case CMD_LIST_LAST:
301 list_window_last(lw, rows);
302 break;
303 case CMD_LIST_NEXT_PAGE:
304 list_window_next_page(lw, rows);
305 break;
306 case CMD_LIST_PREVIOUS_PAGE:
307 list_window_previous_page(lw);
308 break;
309 default:
310 return false;
311 }
313 return true;
314 }
316 bool
317 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
318 {
319 switch (cmd) {
320 case CMD_LIST_PREVIOUS:
321 if (lw->start > 0)
322 lw->start--;
323 break;
325 case CMD_LIST_NEXT:
326 if (lw->start + lw->rows < rows)
327 lw->start++;
328 break;
330 case CMD_LIST_FIRST:
331 lw->start = 0;
332 break;
334 case CMD_LIST_LAST:
335 if (rows > lw->rows)
336 lw->start = rows - lw->rows;
337 else
338 lw->start = 0;
339 break;
341 case CMD_LIST_NEXT_PAGE:
342 lw->start += lw->rows - 1;
343 if (lw->start + lw->rows > rows) {
344 if (rows > lw->rows)
345 lw->start = rows - lw->rows;
346 else
347 lw->start = 0;
348 }
349 break;
351 case CMD_LIST_PREVIOUS_PAGE:
352 if (lw->start > lw->rows)
353 lw->start -= lw->rows;
354 else
355 lw->start = 0;
356 break;
358 default:
359 return false;
360 }
362 return true;
363 }
365 #ifdef HAVE_GETMOUSE
366 bool
367 list_window_mouse(struct list_window *lw, unsigned rows,
368 unsigned long bstate, int y)
369 {
370 assert(lw != NULL);
372 /* if the even occured above the list window move up */
373 if (y < 0) {
374 if (bstate & BUTTON3_CLICKED)
375 list_window_first(lw);
376 else
377 list_window_previous_page(lw);
378 return true;
379 }
381 /* if the even occured below the list window move down */
382 if ((unsigned)y >= rows) {
383 if (bstate & BUTTON3_CLICKED)
384 list_window_last(lw, rows);
385 else
386 list_window_next_page(lw, rows);
387 return true;
388 }
390 return false;
391 }
392 #endif