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 void
70 list_window_check_selected(struct list_window *lw)
71 {
72 if (lw->start + lw->rows > lw->length) {
73 if (lw->length > lw->rows)
74 lw->start = lw->length - lw->rows;
75 else
76 lw->start = 0;
77 }
79 if (lw->selected < lw->start)
80 lw->selected = lw->start;
82 if (lw->length == 0)
83 lw->selected = 0;
84 else if (lw->selected >= lw->length)
85 lw->selected = lw->length - 1;
87 if(lw->range_selection)
88 {
89 if (lw->length == 0) {
90 lw->selected_start = 0;
91 lw->selected_end = 0;
92 lw->range_base = 0;
93 } else {
94 if (lw->selected_start >= lw->length)
95 lw->selected_start = lw->length - 1;
96 if (lw->selected_end >= lw->length)
97 lw->selected_end = lw->length - 1;
98 if (lw->range_base >= lw->length)
99 lw->range_base = lw->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 /**
115 * Scroll after the cursor was moved, the list was changed or the
116 * window was resized.
117 */
118 static void
119 list_window_check_origin(struct list_window *lw)
120 {
121 int start = lw->start;
123 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
124 // Center if the offset is more than half the screen
125 start = lw->selected - lw->rows / 2;
126 else
127 {
128 if (lw->selected < lw->start + options.scroll_offset)
129 start = lw->selected - options.scroll_offset;
131 if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
132 {
133 start = lw->selected - lw->rows + 1 + options.scroll_offset;
134 }
135 }
137 if (start + lw->rows > lw->length)
138 start = lw->length - lw->rows;
140 if (start < 0 || lw->length == 0)
141 start = 0;
143 lw->start = start;
144 }
146 void
147 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
148 {
149 lw->cols = width;
150 lw->rows = height;
152 list_window_check_origin(lw);
153 }
155 void
156 list_window_set_length(struct list_window *lw, unsigned length)
157 {
158 lw->length = length;
160 list_window_check_selected(lw);
161 list_window_check_origin(lw);
162 }
164 void
165 list_window_center(struct list_window *lw, unsigned n)
166 {
167 if (n > lw->rows / 2)
168 lw->start = n - lw->rows / 2;
169 else
170 lw->start = 0;
172 if (lw->start + lw->rows > lw->length) {
173 if (lw->rows < lw->length)
174 lw->start = lw->length - lw->rows;
175 else
176 lw->start = 0;
177 }
178 }
180 void
181 list_window_set_cursor(struct list_window *lw, unsigned i)
182 {
183 lw->range_selection = false;
184 lw->selected = i;
185 lw->selected_start = i;
186 lw->selected_end = i;
188 list_window_check_selected(lw);
189 list_window_check_origin(lw);
190 }
192 void
193 list_window_move_cursor(struct list_window *lw, unsigned n)
194 {
195 lw->selected = n;
196 if(lw->range_selection)
197 {
198 if(n >= lw->range_base)
199 {
200 lw->selected_end = n;
201 lw->selected_start = lw->range_base;
202 }
203 if(n <= lw->range_base)
204 {
205 lw->selected_start = n;
206 lw->selected_end = lw->range_base;
207 }
208 }
209 else
210 {
211 lw->selected_start = n;
212 lw->selected_end = n;
213 }
215 list_window_check_selected(lw);
216 list_window_check_origin(lw);
217 }
219 void
220 list_window_fetch_cursor(struct list_window *lw)
221 {
222 if (lw->selected < lw->start + options.scroll_offset) {
223 if (lw->start > 0)
224 lw->selected = lw->start + options.scroll_offset;
225 if (lw->range_selection) {
226 if (lw->selected > lw->range_base) {
227 lw->selected_end = lw->selected;
228 lw->selected_start = lw->range_base;
229 } else {
230 lw->selected_start = lw->selected;
231 }
232 } else {
233 lw->selected_start = lw->selected;
234 lw->selected_end = lw->selected;
235 }
236 } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
237 if (lw->start + lw->rows < lw->length)
238 lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
239 if (lw->range_selection) {
240 if (lw->selected < lw->range_base) {
241 lw->selected_start = lw->selected;
242 lw->selected_end = lw->range_base;
243 } else {
244 lw->selected_end = lw->selected;
245 }
246 } else {
247 lw->selected_start = lw->selected;
248 lw->selected_end = lw->selected;
249 }
250 }
251 }
253 static void
254 list_window_next(struct list_window *lw)
255 {
256 if (lw->selected + 1 < lw->length)
257 list_window_move_cursor(lw, lw->selected + 1);
258 else if (options.list_wrap)
259 list_window_move_cursor(lw, 0);
260 }
262 static void
263 list_window_previous(struct list_window *lw)
264 {
265 if (lw->selected > 0)
266 list_window_move_cursor(lw, lw->selected - 1);
267 else if (options.list_wrap)
268 list_window_move_cursor(lw, lw->length - 1);
269 }
271 static void
272 list_window_top(struct list_window *lw)
273 {
274 if (lw->start == 0)
275 list_window_move_cursor(lw, lw->start);
276 else
277 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
278 list_window_move_cursor(lw, lw->start + lw->rows / 2);
279 else
280 list_window_move_cursor(lw, lw->start + options.scroll_offset);
281 }
283 static void
284 list_window_middle(struct list_window *lw)
285 {
286 if (lw->length >= lw->rows)
287 list_window_move_cursor(lw, lw->start + lw->rows / 2);
288 else
289 list_window_move_cursor(lw, lw->length / 2);
290 }
292 static void
293 list_window_bottom(struct list_window *lw)
294 {
295 if (lw->length >= lw->rows)
296 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
297 list_window_move_cursor(lw, lw->start + lw->rows / 2);
298 else
299 if (lw->start + lw->rows == lw->length)
300 list_window_move_cursor(lw, lw->length - 1);
301 else
302 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
303 else
304 list_window_move_cursor(lw, lw->length - 1);
305 }
307 static void
308 list_window_first(struct list_window *lw)
309 {
310 list_window_move_cursor(lw, 0);
311 }
313 static void
314 list_window_last(struct list_window *lw)
315 {
316 if (lw->length > 0)
317 list_window_move_cursor(lw, lw->length - 1);
318 else
319 list_window_move_cursor(lw, 0);
320 }
322 static void
323 list_window_next_page(struct list_window *lw)
324 {
325 if (lw->rows < 2)
326 return;
327 if (lw->selected + lw->rows < lw->length)
328 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
329 else
330 list_window_last(lw);
331 }
333 static void
334 list_window_previous_page(struct list_window *lw)
335 {
336 if (lw->rows < 2)
337 return;
338 if (lw->selected > lw->rows - 1)
339 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
340 else
341 list_window_first(lw);
342 }
344 static void
345 list_window_scroll_up(struct list_window *lw, unsigned n)
346 {
347 if (lw->start > 0) {
348 if (n > lw->start)
349 lw->start = 0;
350 else
351 lw->start -= n;
353 list_window_fetch_cursor(lw);
354 }
355 }
357 static void
358 list_window_scroll_down(struct list_window *lw, unsigned n)
359 {
360 if (lw->start + lw->rows < lw->length)
361 {
362 if ( lw->start + lw->rows + n > lw->length - 1)
363 lw->start = lw->length - lw->rows;
364 else
365 lw->start += n;
367 list_window_fetch_cursor(lw);
368 }
369 }
371 static void
372 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
373 bool selected, bool highlight,
374 const char *text, const char *second_column)
375 {
376 unsigned text_width = utf8_width(text);
377 unsigned second_column_width;
379 #ifdef NCMPC_MINI
380 second_column = NULL;
381 #endif /* NCMPC_MINI */
383 if (second_column != NULL) {
384 second_column_width = utf8_width(second_column) + 1;
385 if (second_column_width < width)
386 width -= second_column_width;
387 else
388 second_column_width = 0;
389 } else
390 second_column_width = 0;
392 if (highlight)
393 colors_use(w, COLOR_LIST_BOLD);
394 else
395 colors_use(w, COLOR_LIST);
397 if (selected)
398 wattron(w, A_REVERSE);
400 waddstr(w, text);
401 if (options.wide_cursor && text_width < width)
402 whline(w, ' ', width - text_width);
404 if (second_column_width > 0) {
405 wmove(w, y, width);
406 waddch(w, ' ');
407 waddstr(w, second_column);
408 }
410 if (selected)
411 wattroff(w, A_REVERSE);
413 if (!options.wide_cursor && text_width < width) {
414 if (second_column_width == 0)
415 /* the cursor is at the end of the text; clear
416 the rest of this row */
417 wclrtoeol(w);
418 else
419 /* there's a second column: clear the space
420 between the first and the second column */
421 mvwhline(w, y, text_width, ' ', width - text_width);
422 }
423 }
425 void
426 list_window_paint(const struct list_window *lw,
427 list_window_callback_fn_t callback,
428 void *callback_data)
429 {
430 unsigned i;
431 bool show_cursor = !lw->hide_cursor;
432 bool highlight = false;
434 show_cursor = show_cursor &&
435 (!options.hardware_cursor || lw->range_selection);
437 for (i = 0; i < lw->rows; i++) {
438 const char *label;
439 char *second_column = NULL;
440 highlight = false;
442 wmove(lw->w, i, 0);
444 if (lw->start + i >= lw->length) {
445 wclrtobot(lw->w);
446 break;
447 }
449 label = callback(lw->start + i, &highlight, &second_column, callback_data);
450 assert(label != NULL);
452 list_window_paint_row(lw->w, i, lw->cols,
453 show_cursor &&
454 lw->start + i >= lw->selected_start &&
455 lw->start + i <= lw->selected_end,
456 highlight,
457 label, second_column);
459 if (second_column != NULL)
460 g_free(second_column);
461 }
463 if (options.hardware_cursor && lw->selected >= lw->start &&
464 lw->selected < lw->start + lw->rows) {
465 curs_set(1);
466 wmove(lw->w, lw->selected - lw->start, 0);
467 }
468 }
470 bool
471 list_window_find(struct list_window *lw,
472 list_window_callback_fn_t callback,
473 void *callback_data,
474 const char *str,
475 bool wrap,
476 bool bell_on_wrap)
477 {
478 bool h;
479 unsigned i = lw->selected + 1;
480 const char *label;
482 assert(str != NULL);
484 do {
485 while (i < lw->length) {
486 label = callback(i, &h, NULL, callback_data);
487 assert(label != NULL);
489 if (match_line(label, str)) {
490 list_window_move_cursor(lw, i);
491 return true;
492 }
493 if (wrap && i == lw->selected)
494 return false;
495 i++;
496 }
497 if (wrap) {
498 if (i == 0) /* empty list */
499 return 1;
500 i=0; /* first item */
501 if (bell_on_wrap) {
502 screen_bell();
503 }
504 }
505 } while (wrap);
507 return false;
508 }
510 bool
511 list_window_rfind(struct list_window *lw,
512 list_window_callback_fn_t callback,
513 void *callback_data,
514 const char *str,
515 bool wrap,
516 bool bell_on_wrap)
517 {
518 bool h;
519 int i = lw->selected - 1;
520 const char *label;
522 assert(str != NULL);
524 if (lw->length == 0)
525 return false;
527 do {
528 while (i >= 0) {
529 label = callback(i, &h, NULL, callback_data);
530 assert(label != NULL);
532 if (match_line(label, str)) {
533 list_window_move_cursor(lw, i);
534 return true;
535 }
536 if (wrap && i == (int)lw->selected)
537 return false;
538 i--;
539 }
540 if (wrap) {
541 i = lw->length - 1; /* last item */
542 if (bell_on_wrap) {
543 screen_bell();
544 }
545 }
546 } while (wrap);
548 return false;
549 }
551 bool
552 list_window_jump(struct list_window *lw,
553 list_window_callback_fn_t callback,
554 void *callback_data,
555 const char *str)
556 {
557 bool h;
558 const char *label;
560 assert(str != NULL);
562 for (unsigned i = 0; i < lw->length; ++i) {
563 label = callback(i, &h, NULL, callback_data);
564 assert(label != NULL);
566 if (label[0] == '[')
567 label++;
568 #ifndef NCMPC_MINI
569 if ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
570 (!options.jump_prefix_only && match_line(label, str)))
571 #else
572 if (g_ascii_strncasecmp(label, str, strlen(str)) == 0)
573 #endif
574 {
575 list_window_move_cursor(lw, i);
576 return true;
577 }
578 }
579 return false;
580 }
582 /* perform basic list window commands (movement) */
583 bool
584 list_window_cmd(struct list_window *lw, command_t cmd)
585 {
586 switch (cmd) {
587 case CMD_LIST_PREVIOUS:
588 list_window_previous(lw);
589 break;
590 case CMD_LIST_NEXT:
591 list_window_next(lw);
592 break;
593 case CMD_LIST_TOP:
594 list_window_top(lw);
595 break;
596 case CMD_LIST_MIDDLE:
597 list_window_middle(lw);
598 break;
599 case CMD_LIST_BOTTOM:
600 list_window_bottom(lw);
601 break;
602 case CMD_LIST_FIRST:
603 list_window_first(lw);
604 break;
605 case CMD_LIST_LAST:
606 list_window_last(lw);
607 break;
608 case CMD_LIST_NEXT_PAGE:
609 list_window_next_page(lw);
610 break;
611 case CMD_LIST_PREVIOUS_PAGE:
612 list_window_previous_page(lw);
613 break;
614 case CMD_LIST_RANGE_SELECT:
615 if(lw->range_selection)
616 {
617 screen_status_printf(_("Range selection disabled"));
618 list_window_set_cursor(lw, lw->selected);
619 }
620 else
621 {
622 screen_status_printf(_("Range selection enabled"));
623 lw->range_base = lw->selected;
624 lw->range_selection = true;
625 }
626 break;
627 case CMD_LIST_SCROLL_UP_LINE:
628 list_window_scroll_up(lw, 1);
629 break;
630 case CMD_LIST_SCROLL_DOWN_LINE:
631 list_window_scroll_down(lw, 1);
632 break;
633 case CMD_LIST_SCROLL_UP_HALF:
634 list_window_scroll_up(lw, (lw->rows - 1) / 2);
635 break;
636 case CMD_LIST_SCROLL_DOWN_HALF:
637 list_window_scroll_down(lw, (lw->rows - 1) / 2);
638 break;
639 default:
640 return false;
641 }
643 return true;
644 }
646 bool
647 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
648 {
649 switch (cmd) {
650 case CMD_LIST_SCROLL_UP_LINE:
651 case CMD_LIST_PREVIOUS:
652 if (lw->start > 0)
653 lw->start--;
654 break;
656 case CMD_LIST_SCROLL_DOWN_LINE:
657 case CMD_LIST_NEXT:
658 if (lw->start + lw->rows < lw->length)
659 lw->start++;
660 break;
662 case CMD_LIST_FIRST:
663 lw->start = 0;
664 break;
666 case CMD_LIST_LAST:
667 if (lw->length > lw->rows)
668 lw->start = lw->length - lw->rows;
669 else
670 lw->start = 0;
671 break;
673 case CMD_LIST_NEXT_PAGE:
674 lw->start += lw->rows - 1;
675 if (lw->start + lw->rows > lw->length) {
676 if (lw->length > lw->rows)
677 lw->start = lw->length - lw->rows;
678 else
679 lw->start = 0;
680 }
681 break;
683 case CMD_LIST_PREVIOUS_PAGE:
684 if (lw->start > lw->rows)
685 lw->start -= lw->rows;
686 else
687 lw->start = 0;
688 break;
690 case CMD_LIST_SCROLL_UP_HALF:
691 if (lw->start > (lw->rows - 1) / 2)
692 lw->start -= (lw->rows - 1) / 2;
693 else
694 lw->start = 0;
695 break;
697 case CMD_LIST_SCROLL_DOWN_HALF:
698 lw->start += (lw->rows - 1) / 2;
699 if (lw->start + lw->rows > lw->length) {
700 if (lw->length > lw->rows)
701 lw->start = lw->length - lw->rows;
702 else
703 lw->start = 0;
704 }
705 break;
707 default:
708 return false;
709 }
711 return true;
712 }
714 #ifdef HAVE_GETMOUSE
715 bool
716 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
717 {
718 assert(lw != NULL);
720 /* if the even occurred above the list window move up */
721 if (y < 0) {
722 if (bstate & BUTTON3_CLICKED)
723 list_window_first(lw);
724 else
725 list_window_previous_page(lw);
726 return true;
727 }
729 /* if the even occurred below the list window move down */
730 if ((unsigned)y >= lw->length) {
731 if (bstate & BUTTON3_CLICKED)
732 list_window_last(lw);
733 else
734 list_window_next_page(lw);
735 return true;
736 }
738 return false;
739 }
740 #endif