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(*lw));
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 static unsigned
70 list_window_validate_index(const struct list_window *lw, unsigned i)
71 {
72 if (lw->length == 0)
73 return 0;
74 else if (i >= lw->length)
75 return lw->length - 1;
76 else
77 return i;
78 }
80 static void
81 list_window_check_selected(struct list_window *lw)
82 {
83 lw->selected = list_window_validate_index(lw, lw->selected);
85 if(lw->range_selection)
86 {
87 lw->selected_start =
88 list_window_validate_index(lw, lw->selected_start);
89 lw->selected_end =
90 list_window_validate_index(lw, lw->selected_end);
91 lw->range_base =
92 list_window_validate_index(lw, lw->range_base);
93 }
94 else
95 {
96 lw->selected_start = lw->selected;
97 lw->selected_end = lw->selected;
98 }
99 }
101 /**
102 * Scroll after the cursor was moved, the list was changed or the
103 * window was resized.
104 */
105 static void
106 list_window_check_origin(struct list_window *lw)
107 {
108 int start = lw->start;
110 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
111 // Center if the offset is more than half the screen
112 start = lw->selected - lw->rows / 2;
113 else {
114 if (lw->selected < lw->start + options.scroll_offset)
115 start = lw->selected - options.scroll_offset;
117 if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
118 start = lw->selected - lw->rows + 1 + options.scroll_offset;
119 }
121 if (start + lw->rows > lw->length)
122 start = lw->length - lw->rows;
124 if (start < 0 || lw->length == 0)
125 start = 0;
127 lw->start = start;
128 }
130 void
131 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
132 {
133 lw->cols = width;
134 lw->rows = height;
136 list_window_check_origin(lw);
137 }
139 void
140 list_window_set_length(struct list_window *lw, unsigned length)
141 {
142 lw->length = length;
144 list_window_check_selected(lw);
145 list_window_check_origin(lw);
146 }
148 void
149 list_window_center(struct list_window *lw, unsigned n)
150 {
151 if (n > lw->rows / 2)
152 lw->start = n - lw->rows / 2;
153 else
154 lw->start = 0;
156 if (lw->start + lw->rows > lw->length) {
157 if (lw->rows < lw->length)
158 lw->start = lw->length - lw->rows;
159 else
160 lw->start = 0;
161 }
162 }
164 void
165 list_window_set_cursor(struct list_window *lw, unsigned i)
166 {
167 lw->range_selection = false;
168 lw->selected = i;
169 lw->selected_start = i;
170 lw->selected_end = i;
172 list_window_check_selected(lw);
173 list_window_check_origin(lw);
174 }
176 void
177 list_window_move_cursor(struct list_window *lw, unsigned n)
178 {
179 lw->selected = n;
180 if(lw->range_selection)
181 {
182 if(n >= lw->range_base)
183 {
184 lw->selected_end = n;
185 lw->selected_start = lw->range_base;
186 }
187 if(n <= lw->range_base)
188 {
189 lw->selected_start = n;
190 lw->selected_end = lw->range_base;
191 }
192 }
193 else
194 {
195 lw->selected_start = n;
196 lw->selected_end = n;
197 }
199 list_window_check_selected(lw);
200 list_window_check_origin(lw);
201 }
203 void
204 list_window_fetch_cursor(struct list_window *lw)
205 {
206 if (lw->start > 0 &&
207 lw->selected < lw->start + options.scroll_offset)
208 list_window_move_cursor(lw, lw->start + options.scroll_offset);
209 else if (lw->start + lw->rows < lw->length &&
210 lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
211 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
212 }
214 static void
215 list_window_next(struct list_window *lw)
216 {
217 if (lw->selected + 1 < lw->length)
218 list_window_move_cursor(lw, lw->selected + 1);
219 else if (options.list_wrap)
220 list_window_move_cursor(lw, 0);
221 }
223 static void
224 list_window_previous(struct list_window *lw)
225 {
226 if (lw->selected > 0)
227 list_window_move_cursor(lw, lw->selected - 1);
228 else if (options.list_wrap)
229 list_window_move_cursor(lw, lw->length - 1);
230 }
232 static void
233 list_window_top(struct list_window *lw)
234 {
235 if (lw->start == 0)
236 list_window_move_cursor(lw, lw->start);
237 else
238 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
239 list_window_move_cursor(lw, lw->start + lw->rows / 2);
240 else
241 list_window_move_cursor(lw, lw->start + options.scroll_offset);
242 }
244 static void
245 list_window_middle(struct list_window *lw)
246 {
247 if (lw->length >= lw->rows)
248 list_window_move_cursor(lw, lw->start + lw->rows / 2);
249 else
250 list_window_move_cursor(lw, lw->length / 2);
251 }
253 static void
254 list_window_bottom(struct list_window *lw)
255 {
256 if (lw->length >= lw->rows)
257 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
258 list_window_move_cursor(lw, lw->start + lw->rows / 2);
259 else
260 if (lw->start + lw->rows == lw->length)
261 list_window_move_cursor(lw, lw->length - 1);
262 else
263 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
264 else
265 list_window_move_cursor(lw, lw->length - 1);
266 }
268 static void
269 list_window_first(struct list_window *lw)
270 {
271 list_window_move_cursor(lw, 0);
272 }
274 static void
275 list_window_last(struct list_window *lw)
276 {
277 if (lw->length > 0)
278 list_window_move_cursor(lw, lw->length - 1);
279 else
280 list_window_move_cursor(lw, 0);
281 }
283 static void
284 list_window_next_page(struct list_window *lw)
285 {
286 if (lw->rows < 2)
287 return;
288 if (lw->selected + lw->rows < lw->length)
289 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
290 else
291 list_window_last(lw);
292 }
294 static void
295 list_window_previous_page(struct list_window *lw)
296 {
297 if (lw->rows < 2)
298 return;
299 if (lw->selected > lw->rows - 1)
300 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
301 else
302 list_window_first(lw);
303 }
305 static void
306 list_window_scroll_up(struct list_window *lw, unsigned n)
307 {
308 if (lw->start > 0) {
309 if (n > lw->start)
310 lw->start = 0;
311 else
312 lw->start -= n;
314 list_window_fetch_cursor(lw);
315 }
316 }
318 static void
319 list_window_scroll_down(struct list_window *lw, unsigned n)
320 {
321 if (lw->start + lw->rows < lw->length)
322 {
323 if ( lw->start + lw->rows + n > lw->length - 1)
324 lw->start = lw->length - lw->rows;
325 else
326 lw->start += n;
328 list_window_fetch_cursor(lw);
329 }
330 }
332 static void
333 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
334 bool selected, bool highlight,
335 const char *text, const char *second_column)
336 {
337 unsigned text_width = utf8_width(text);
338 unsigned second_column_width;
340 #ifdef NCMPC_MINI
341 second_column = NULL;
342 highlight = false;
343 #endif /* NCMPC_MINI */
345 if (second_column != NULL) {
346 second_column_width = utf8_width(second_column) + 1;
347 if (second_column_width < width)
348 width -= second_column_width;
349 else
350 second_column_width = 0;
351 } else
352 second_column_width = 0;
354 if (highlight)
355 colors_use(w, COLOR_LIST_BOLD);
356 else
357 colors_use(w, COLOR_LIST);
359 if (selected)
360 wattron(w, A_REVERSE);
362 waddstr(w, text);
363 if (options.wide_cursor && text_width < width)
364 whline(w, ' ', width - text_width);
366 if (second_column_width > 0) {
367 wmove(w, y, width);
368 waddch(w, ' ');
369 waddstr(w, second_column);
370 }
372 if (selected)
373 wattroff(w, A_REVERSE);
375 if (!options.wide_cursor && text_width < width) {
376 if (second_column_width == 0)
377 /* the cursor is at the end of the text; clear
378 the rest of this row */
379 wclrtoeol(w);
380 else
381 /* there's a second column: clear the space
382 between the first and the second column */
383 mvwhline(w, y, text_width, ' ', width - text_width);
384 }
385 }
387 void
388 list_window_paint(const struct list_window *lw,
389 list_window_callback_fn_t callback,
390 void *callback_data)
391 {
392 unsigned i;
393 bool show_cursor = !lw->hide_cursor;
395 show_cursor = show_cursor &&
396 (!options.hardware_cursor || lw->range_selection);
398 for (i = 0; i < lw->rows; i++) {
399 const char *label;
400 bool highlight = false;
401 char *second_column = NULL;
403 wmove(lw->w, i, 0);
405 if (lw->start + i >= lw->length) {
406 wclrtobot(lw->w);
407 break;
408 }
410 label = callback(lw->start + i, &highlight, &second_column, callback_data);
411 assert(label != NULL);
413 #ifdef NCMPC_MINI
414 highlight = false;
415 second_column = NULL;
416 #endif /* NCMPC_MINI */
418 list_window_paint_row(lw->w, i, lw->cols,
419 show_cursor &&
420 lw->start + i >= lw->selected_start &&
421 lw->start + i <= lw->selected_end,
422 highlight,
423 label, second_column);
425 if (second_column != NULL)
426 g_free(second_column);
427 }
429 if (options.hardware_cursor && lw->selected >= lw->start &&
430 lw->selected < lw->start + lw->rows) {
431 curs_set(1);
432 wmove(lw->w, lw->selected - lw->start, 0);
433 }
434 }
436 bool
437 list_window_find(struct list_window *lw,
438 list_window_callback_fn_t callback,
439 void *callback_data,
440 const char *str,
441 bool wrap,
442 bool bell_on_wrap)
443 {
444 bool h;
445 unsigned i = lw->selected + 1;
446 const char *label;
448 assert(str != NULL);
450 do {
451 while (i < lw->length) {
452 label = callback(i, &h, NULL, callback_data);
453 assert(label != NULL);
455 if (match_line(label, str)) {
456 list_window_move_cursor(lw, i);
457 return true;
458 }
459 if (wrap && i == lw->selected)
460 return false;
461 i++;
462 }
463 if (wrap) {
464 if (i == 0) /* empty list */
465 return 1;
466 i=0; /* first item */
467 if (bell_on_wrap) {
468 screen_bell();
469 }
470 }
471 } while (wrap);
473 return false;
474 }
476 bool
477 list_window_rfind(struct list_window *lw,
478 list_window_callback_fn_t callback,
479 void *callback_data,
480 const char *str,
481 bool wrap,
482 bool bell_on_wrap)
483 {
484 bool h;
485 int i = lw->selected - 1;
486 const char *label;
488 assert(str != NULL);
490 if (lw->length == 0)
491 return false;
493 do {
494 while (i >= 0) {
495 label = callback(i, &h, NULL, callback_data);
496 assert(label != NULL);
498 if (match_line(label, str)) {
499 list_window_move_cursor(lw, i);
500 return true;
501 }
502 if (wrap && i == (int)lw->selected)
503 return false;
504 i--;
505 }
506 if (wrap) {
507 i = lw->length - 1; /* last item */
508 if (bell_on_wrap) {
509 screen_bell();
510 }
511 }
512 } while (wrap);
514 return false;
515 }
517 static bool
518 jump_match(const char *haystack, const char *needle)
519 {
520 #ifdef NCMPC_MINI
521 bool jump_prefix_only = true;
522 #else
523 bool jump_prefix_only = options.jump_prefix_only;
524 #endif
526 assert(haystack != NULL);
527 assert(needle != NULL);
529 return jump_prefix_only
530 ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
531 : match_line(haystack, needle);
532 }
534 bool
535 list_window_jump(struct list_window *lw,
536 list_window_callback_fn_t callback,
537 void *callback_data,
538 const char *str)
539 {
540 bool h;
541 const char *label;
543 assert(str != NULL);
545 for (unsigned i = 0; i < lw->length; ++i) {
546 label = callback(i, &h, NULL, callback_data);
547 assert(label != NULL);
549 if (label[0] == '[')
550 label++;
552 if (jump_match(label, str)) {
553 list_window_move_cursor(lw, i);
554 return true;
555 }
556 }
557 return false;
558 }
560 /* perform basic list window commands (movement) */
561 bool
562 list_window_cmd(struct list_window *lw, command_t cmd)
563 {
564 switch (cmd) {
565 case CMD_LIST_PREVIOUS:
566 list_window_previous(lw);
567 break;
568 case CMD_LIST_NEXT:
569 list_window_next(lw);
570 break;
571 case CMD_LIST_TOP:
572 list_window_top(lw);
573 break;
574 case CMD_LIST_MIDDLE:
575 list_window_middle(lw);
576 break;
577 case CMD_LIST_BOTTOM:
578 list_window_bottom(lw);
579 break;
580 case CMD_LIST_FIRST:
581 list_window_first(lw);
582 break;
583 case CMD_LIST_LAST:
584 list_window_last(lw);
585 break;
586 case CMD_LIST_NEXT_PAGE:
587 list_window_next_page(lw);
588 break;
589 case CMD_LIST_PREVIOUS_PAGE:
590 list_window_previous_page(lw);
591 break;
592 case CMD_LIST_RANGE_SELECT:
593 if(lw->range_selection)
594 {
595 screen_status_printf(_("Range selection disabled"));
596 list_window_set_cursor(lw, lw->selected);
597 }
598 else
599 {
600 screen_status_printf(_("Range selection enabled"));
601 lw->range_base = lw->selected;
602 lw->range_selection = true;
603 }
604 break;
605 case CMD_LIST_SCROLL_UP_LINE:
606 list_window_scroll_up(lw, 1);
607 break;
608 case CMD_LIST_SCROLL_DOWN_LINE:
609 list_window_scroll_down(lw, 1);
610 break;
611 case CMD_LIST_SCROLL_UP_HALF:
612 list_window_scroll_up(lw, (lw->rows - 1) / 2);
613 break;
614 case CMD_LIST_SCROLL_DOWN_HALF:
615 list_window_scroll_down(lw, (lw->rows - 1) / 2);
616 break;
617 default:
618 return false;
619 }
621 return true;
622 }
624 bool
625 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
626 {
627 switch (cmd) {
628 case CMD_LIST_SCROLL_UP_LINE:
629 case CMD_LIST_PREVIOUS:
630 if (lw->start > 0)
631 lw->start--;
632 break;
634 case CMD_LIST_SCROLL_DOWN_LINE:
635 case CMD_LIST_NEXT:
636 if (lw->start + lw->rows < lw->length)
637 lw->start++;
638 break;
640 case CMD_LIST_FIRST:
641 lw->start = 0;
642 break;
644 case CMD_LIST_LAST:
645 if (lw->length > lw->rows)
646 lw->start = lw->length - lw->rows;
647 else
648 lw->start = 0;
649 break;
651 case CMD_LIST_NEXT_PAGE:
652 lw->start += lw->rows - 1;
653 if (lw->start + lw->rows > lw->length) {
654 if (lw->length > lw->rows)
655 lw->start = lw->length - lw->rows;
656 else
657 lw->start = 0;
658 }
659 break;
661 case CMD_LIST_PREVIOUS_PAGE:
662 if (lw->start > lw->rows)
663 lw->start -= lw->rows;
664 else
665 lw->start = 0;
666 break;
668 case CMD_LIST_SCROLL_UP_HALF:
669 if (lw->start > (lw->rows - 1) / 2)
670 lw->start -= (lw->rows - 1) / 2;
671 else
672 lw->start = 0;
673 break;
675 case CMD_LIST_SCROLL_DOWN_HALF:
676 lw->start += (lw->rows - 1) / 2;
677 if (lw->start + lw->rows > lw->length) {
678 if (lw->length > lw->rows)
679 lw->start = lw->length - lw->rows;
680 else
681 lw->start = 0;
682 }
683 break;
685 default:
686 return false;
687 }
689 return true;
690 }
692 #ifdef HAVE_GETMOUSE
693 bool
694 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
695 {
696 assert(lw != NULL);
698 /* if the even occurred above the list window move up */
699 if (y < 0) {
700 if (bstate & BUTTON3_CLICKED)
701 list_window_first(lw);
702 else
703 list_window_previous_page(lw);
704 return true;
705 }
707 /* if the even occurred below the list window move down */
708 if ((unsigned)y >= lw->length) {
709 if (bstate & BUTTON3_CLICKED)
710 list_window_last(lw);
711 else
712 list_window_next_page(lw);
713 return true;
714 }
716 return false;
717 }
718 #endif