6f6c34cbd625b8571deeabe236263348be165bcd
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"
27 #include "screen_message.h"
28 #include "i18n.h"
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
35 extern void screen_bell(void);
37 struct list_window *
38 list_window_init(WINDOW *w, unsigned width, unsigned height)
39 {
40 struct list_window *lw;
42 lw = g_malloc0(sizeof(list_window_t));
43 lw->w = w;
44 lw->cols = width;
45 lw->rows = height;
46 lw->range_selection = false;
47 return lw;
48 }
50 void
51 list_window_free(struct list_window *lw)
52 {
53 assert(lw != NULL);
55 g_free(lw);
56 }
58 void
59 list_window_reset(struct list_window *lw)
60 {
61 lw->selected = 0;
62 lw->selected_start = 0;
63 lw->selected_end = 0;
64 lw->range_selection = false;
65 lw->range_base = 0;
66 lw->start = 0;
67 }
69 void
70 list_window_check_selected(struct list_window *lw, unsigned length)
71 {
72 if (lw->start + lw->rows > length) {
73 if (length > lw->rows)
74 lw->start = length - lw->rows;
75 else
76 lw->start = 0;
77 }
79 if (lw->selected < lw->start)
80 lw->selected = lw->start;
82 if (length == 0)
83 lw->selected = 0;
84 else if (lw->selected >= length)
85 lw->selected = length - 1;
87 if(lw->range_selection)
88 {
89 if (length == 0) {
90 lw->selected_start = 0;
91 lw->selected_end = 0;
92 lw->range_base = 0;
93 } else {
94 if (lw->selected_start >= length)
95 lw->selected_start = length - 1;
96 if (lw->selected_end >= length)
97 lw->selected_end = length - 1;
98 if (lw->range_base >= length)
99 lw->range_base = length - 1;
100 }
102 if(lw->range_base > lw->selected_end)
103 lw->selected_end = lw->selected;
104 if(lw->range_base < lw->selected_start)
105 lw->selected_start = lw->selected;
106 }
107 else
108 {
109 lw->selected_start = lw->selected;
110 lw->selected_end = lw->selected;
111 }
112 }
114 void
115 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
116 {
117 if (n > lw->rows / 2)
118 lw->start = n - lw->rows / 2;
119 else
120 lw->start = 0;
122 if (lw->start + lw->rows > rows) {
123 if (lw->rows < rows)
124 lw->start = rows - lw->rows;
125 else
126 lw->start = 0;
127 }
128 }
130 void
131 list_window_set_selected(struct list_window *lw, unsigned n)
132 {
133 lw->selected = n;
134 if(lw->range_selection)
135 {
136 if(n >= lw->range_base)
137 {
138 lw->selected_end = n;
139 lw->selected_start = lw->range_base;
140 }
141 if(n <= lw->range_base)
142 {
143 lw->selected_start = n;
144 lw->selected_end = lw->range_base;
145 }
146 }
147 else
148 {
149 lw->selected_start = n;
150 lw->selected_end = n;
151 }
152 }
154 static void
155 list_window_next(struct list_window *lw, unsigned length)
156 {
157 if (lw->selected + 1 < length)
158 list_window_set_selected(lw, lw->selected + 1);
159 else if (options.list_wrap)
160 list_window_set_selected(lw, 0);
161 }
163 static void
164 list_window_previous(struct list_window *lw, unsigned length)
165 {
166 if (lw->selected > 0)
167 list_window_set_selected(lw, lw->selected - 1);
168 else if (options.list_wrap)
169 list_window_set_selected(lw, length-1);
170 }
172 static void
173 list_window_top(struct list_window *lw)
174 {
175 if (lw->start == 0)
176 list_window_set_selected(lw, lw->start);
177 else
178 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
179 list_window_set_selected(lw, lw->start + lw->rows / 2);
180 else
181 list_window_set_selected(lw, lw->start + options.scroll_offset);
182 }
184 static void
185 list_window_middle(struct list_window *lw, unsigned length)
186 {
187 if (length >= lw->rows)
188 list_window_set_selected(lw, lw->start + lw->rows / 2);
189 else
190 list_window_set_selected(lw, length / 2);
191 }
193 static void
194 list_window_bottom(struct list_window *lw, unsigned length)
195 {
196 if (length >= lw->rows)
197 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
198 list_window_set_selected(lw, lw->start + lw->rows / 2);
199 else
200 if (lw->start + lw->rows == length)
201 list_window_set_selected(lw, length - 1);
202 else
203 list_window_set_selected(lw, lw->start + lw->rows - 1 - options.scroll_offset);
204 else
205 list_window_set_selected(lw, length - 1);
206 }
208 static void
209 list_window_first(struct list_window *lw)
210 {
211 list_window_set_selected(lw, 0);
212 }
214 static void
215 list_window_last(struct list_window *lw, unsigned length)
216 {
217 if (length > 0)
218 list_window_set_selected(lw, length - 1);
219 else
220 list_window_set_selected(lw, 0);
221 }
223 static void
224 list_window_next_page(struct list_window *lw, unsigned length)
225 {
226 if (lw->rows < 2)
227 return;
228 if (lw->selected + lw->rows < length)
229 list_window_set_selected(lw, lw->selected + lw->rows - 1);
230 else
231 list_window_last(lw, length);
232 }
234 static void
235 list_window_previous_page(struct list_window *lw)
236 {
237 if (lw->rows < 2)
238 return;
239 if (lw->selected > lw->rows - 1)
240 list_window_set_selected(lw, lw->selected - lw->rows + 1);
241 else
242 list_window_first(lw);
243 }
245 static void
246 list_window_scroll_up(struct list_window *lw, unsigned n)
247 {
248 if (lw->start > 0) {
249 if (n > lw->start)
250 lw->start = 0;
251 else
252 lw->start -= n;
253 if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
254 lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
255 if (lw->range_selection) {
256 if (lw->selected < lw->range_base) {
257 lw->selected_start = lw->selected;
258 lw->selected_end = lw->range_base;
259 } else {
260 lw->selected_end = lw->selected;
261 }
262 } else {
263 lw->selected_start = lw->selected;
264 lw->selected_end = lw->selected;
265 }
266 }
267 }
268 }
270 static void
271 list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
272 {
273 if (lw->start + lw->rows < length)
274 {
275 if ( lw->start + lw->rows + n > length - 1)
276 lw->start = length - lw->rows;
277 else
278 lw->start += n;
279 if (lw->selected < lw->start + options.scroll_offset) {
280 lw->selected = lw->start + options.scroll_offset;
281 if (lw->range_selection) {
282 if (lw->selected > lw->range_base) {
283 lw->selected_end = lw->selected;
284 lw->selected_start = lw->range_base;
285 } else {
286 lw->selected_start = lw->selected;
287 }
288 } else {
289 lw->selected_start = lw->selected;
290 lw->selected_end = lw->selected;
291 }
292 }
293 }
294 }
296 void
297 list_window_paint(struct list_window *lw,
298 list_window_callback_fn_t callback,
299 void *callback_data)
300 {
301 unsigned i;
302 bool fill = options.wide_cursor;
303 bool show_cursor = !lw->hide_cursor;
304 bool highlight = false;
306 if (show_cursor) {
307 int start = lw->start;
308 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
309 // Center if the offset is more than half the screen
310 start = lw->selected - lw->rows / 2;
311 else
312 {
313 if (lw->selected < lw->start + options.scroll_offset)
314 start = lw->selected - options.scroll_offset;
316 if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
317 {
318 start = lw->selected - lw->rows + 1 + options.scroll_offset;
319 }
320 }
321 if (start < 0)
322 lw->start = 0;
323 else
324 {
325 while ( start > 0 && callback(start + lw->rows - 1, &highlight, NULL, callback_data) == NULL)
326 start--;
327 lw->start = start;
328 }
329 }
331 for (i = 0; i < lw->rows; i++) {
332 const char *label;
333 char *second_column = NULL;
334 highlight = false;
336 label = callback(lw->start + i, &highlight, &second_column, callback_data);
337 wmove(lw->w, i, 0);
339 if (label) {
340 bool selected = (lw->start + i >= lw->selected_start && lw->start + i <= lw->selected_end);
341 unsigned len = utf8_width(label);
343 if (highlight)
344 colors_use(lw->w, COLOR_LIST_BOLD);
345 else
346 colors_use(lw->w, COLOR_LIST);
348 if (show_cursor && selected &&
349 (!options.hardware_cursor || lw->range_selection))
350 wattron(lw->w, A_REVERSE);
352 //waddnstr(lw->w, label, lw->cols);
353 waddstr(lw->w, label);
354 if (fill && len < lw->cols)
355 whline(lw->w, ' ', lw->cols-len);
357 if(second_column)
358 {
359 unsigned sc_len = utf8_width(second_column) + 1;
360 if(lw->cols > sc_len)
361 {
362 wmove(lw->w, i, lw->cols - sc_len);
363 waddstr(lw->w, " ");
364 wmove(lw->w, i, lw->cols - sc_len + 1);
365 waddstr(lw->w, second_column);
366 }
367 g_free(second_column);
368 }
370 if (selected)
371 wattroff(lw->w, A_REVERSE);
373 if (!fill && len < lw->cols)
374 wclrtoeol(lw->w);
375 } else
376 wclrtoeol(lw->w);
377 }
379 if (options.hardware_cursor && lw->selected >= lw->start &&
380 lw->selected < lw->start + lw->rows) {
381 curs_set(1);
382 wmove(lw->w, lw->selected - lw->start, 0);
383 }
384 }
386 bool
387 list_window_find(struct list_window *lw,
388 list_window_callback_fn_t callback,
389 void *callback_data,
390 const char *str,
391 bool wrap,
392 bool bell_on_wrap)
393 {
394 bool h;
395 unsigned i = lw->selected + 1;
396 const char *label;
398 do {
399 while ((label = callback(i,&h,NULL,callback_data))) {
400 if (str && label && match_line(label, str)) {
401 lw->selected = i;
402 if(!lw->range_selection || i > lw->selected_end)
403 lw->selected_end = i;
404 if(!lw->range_selection || i < lw->selected_start)
405 lw->selected_start = i;
406 return true;
407 }
408 if (wrap && i == lw->selected)
409 return false;
410 i++;
411 }
412 if (wrap) {
413 if (i == 0) /* empty list */
414 return 1;
415 i=0; /* first item */
416 if (bell_on_wrap) {
417 screen_bell();
418 }
419 }
420 } while (wrap);
422 return false;
423 }
425 bool
426 list_window_rfind(struct list_window *lw,
427 list_window_callback_fn_t callback,
428 void *callback_data,
429 const char *str,
430 bool wrap,
431 bool bell_on_wrap,
432 unsigned rows)
433 {
434 bool h;
435 int i = lw->selected - 1;
436 const char *label;
438 if (rows == 0)
439 return false;
441 do {
442 while (i >= 0 && (label = callback(i,&h,NULL,callback_data))) {
443 if( str && label && match_line(label, str) ) {
444 lw->selected = i;
445 if(!lw->range_selection || i > (int)lw->selected_end)
446 lw->selected_end = i;
447 if(!lw->range_selection || i < (int)lw->selected_start)
448 lw->selected_start = i;
449 return true;
450 }
451 if (wrap && i == (int)lw->selected)
452 return false;
453 i--;
454 }
455 if (wrap) {
456 i = rows - 1; /* last item */
457 if (bell_on_wrap) {
458 screen_bell();
459 }
460 }
461 } while (wrap);
463 return false;
464 }
466 bool
467 list_window_jump(struct list_window *lw,
468 list_window_callback_fn_t callback,
469 void *callback_data,
470 const char *str)
471 {
472 bool h;
473 unsigned i = 0;
474 const char *label;
476 while ((label = callback(i,&h,NULL,callback_data))) {
477 if (label && label[0] == '[')
478 label++;
479 #ifndef NCMPC_MINI
480 if (str && label &&
481 ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
482 (!options.jump_prefix_only && match_line(label, str))) )
483 #else
484 if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
485 #endif
486 {
487 lw->selected = i;
488 if(!lw->range_selection || i > lw->selected_end)
489 lw->selected_end = i;
490 if(!lw->range_selection || i < lw->selected_start)
491 lw->selected_start = i;
492 return true;
493 }
494 i++;
495 }
496 return false;
497 }
499 /* perform basic list window commands (movement) */
500 bool
501 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
502 {
503 switch (cmd) {
504 case CMD_LIST_PREVIOUS:
505 list_window_previous(lw, rows);
506 break;
507 case CMD_LIST_NEXT:
508 list_window_next(lw, rows);
509 break;
510 case CMD_LIST_TOP:
511 list_window_top(lw);
512 break;
513 case CMD_LIST_MIDDLE:
514 list_window_middle(lw,rows);
515 break;
516 case CMD_LIST_BOTTOM:
517 list_window_bottom(lw,rows);
518 break;
519 case CMD_LIST_FIRST:
520 list_window_first(lw);
521 break;
522 case CMD_LIST_LAST:
523 list_window_last(lw, rows);
524 break;
525 case CMD_LIST_NEXT_PAGE:
526 list_window_next_page(lw, rows);
527 break;
528 case CMD_LIST_PREVIOUS_PAGE:
529 list_window_previous_page(lw);
530 break;
531 case CMD_LIST_RANGE_SELECT:
532 if(lw->range_selection)
533 {
534 screen_status_printf(_("Range selection disabled"));
535 lw->range_selection = false;
536 list_window_set_selected(lw, lw->selected);
537 }
538 else
539 {
540 screen_status_printf(_("Range selection enabled"));
541 lw->range_base = lw->selected;
542 lw->range_selection = true;
543 }
544 break;
545 case CMD_LIST_SCROLL_UP_LINE:
546 list_window_scroll_up(lw, 1);
547 break;
548 case CMD_LIST_SCROLL_DOWN_LINE:
549 list_window_scroll_down(lw, rows, 1);
550 break;
551 case CMD_LIST_SCROLL_UP_HALF:
552 list_window_scroll_up(lw, (lw->rows - 1) / 2);
553 break;
554 case CMD_LIST_SCROLL_DOWN_HALF:
555 list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
556 break;
557 default:
558 return false;
559 }
561 return true;
562 }
564 bool
565 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
566 {
567 switch (cmd) {
568 case CMD_LIST_SCROLL_UP_LINE:
569 case CMD_LIST_PREVIOUS:
570 if (lw->start > 0)
571 lw->start--;
572 break;
574 case CMD_LIST_SCROLL_DOWN_LINE:
575 case CMD_LIST_NEXT:
576 if (lw->start + lw->rows < rows)
577 lw->start++;
578 break;
580 case CMD_LIST_FIRST:
581 lw->start = 0;
582 break;
584 case CMD_LIST_LAST:
585 if (rows > lw->rows)
586 lw->start = rows - lw->rows;
587 else
588 lw->start = 0;
589 break;
591 case CMD_LIST_NEXT_PAGE:
592 lw->start += lw->rows - 1;
593 if (lw->start + lw->rows > rows) {
594 if (rows > lw->rows)
595 lw->start = rows - lw->rows;
596 else
597 lw->start = 0;
598 }
599 break;
601 case CMD_LIST_PREVIOUS_PAGE:
602 if (lw->start > lw->rows)
603 lw->start -= lw->rows;
604 else
605 lw->start = 0;
606 break;
608 case CMD_LIST_SCROLL_UP_HALF:
609 if (lw->start > (lw->rows - 1) / 2)
610 lw->start -= (lw->rows - 1) / 2;
611 else
612 lw->start = 0;
613 break;
615 case CMD_LIST_SCROLL_DOWN_HALF:
616 lw->start += (lw->rows - 1) / 2;
617 if (lw->start + lw->rows > rows) {
618 if (rows > lw->rows)
619 lw->start = rows - lw->rows;
620 else
621 lw->start = 0;
622 }
623 break;
625 default:
626 return false;
627 }
629 return true;
630 }
632 #ifdef HAVE_GETMOUSE
633 bool
634 list_window_mouse(struct list_window *lw, unsigned rows,
635 unsigned long bstate, int y)
636 {
637 assert(lw != NULL);
639 /* if the even occurred above the list window move up */
640 if (y < 0) {
641 if (bstate & BUTTON3_CLICKED)
642 list_window_first(lw);
643 else
644 list_window_previous_page(lw);
645 return true;
646 }
648 /* if the even occurred below the list window move down */
649 if ((unsigned)y >= rows) {
650 if (bstate & BUTTON3_CLICKED)
651 list_window_last(lw, rows);
652 else
653 list_window_next_page(lw, rows);
654 return true;
655 }
657 return false;
658 }
659 #endif