f8ab8e9a708ccd0d996c26443d7a7f126f64ecdc
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 "paint.h"
28 #include "screen_message.h"
29 #include "i18n.h"
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
36 extern void screen_bell(void);
38 struct list_window *
39 list_window_init(WINDOW *w, unsigned width, unsigned height)
40 {
41 struct list_window *lw;
43 lw = g_malloc0(sizeof(*lw));
44 lw->w = w;
45 lw->cols = width;
46 lw->rows = height;
47 lw->range_selection = false;
48 return lw;
49 }
51 void
52 list_window_free(struct list_window *lw)
53 {
54 assert(lw != NULL);
56 g_free(lw);
57 }
59 void
60 list_window_reset(struct list_window *lw)
61 {
62 lw->selected = 0;
63 lw->range_selection = false;
64 lw->range_base = 0;
65 lw->start = 0;
66 }
68 static unsigned
69 list_window_validate_index(const struct list_window *lw, unsigned i)
70 {
71 if (lw->length == 0)
72 return 0;
73 else if (i >= lw->length)
74 return lw->length - 1;
75 else
76 return i;
77 }
79 static void
80 list_window_check_selected(struct list_window *lw)
81 {
82 lw->selected = list_window_validate_index(lw, lw->selected);
84 if(lw->range_selection)
85 lw->range_base =
86 list_window_validate_index(lw, lw->range_base);
87 }
89 /**
90 * Scroll after the cursor was moved, the list was changed or the
91 * window was resized.
92 */
93 static void
94 list_window_check_origin(struct list_window *lw)
95 {
96 int start = lw->start;
98 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
99 // Center if the offset is more than half the screen
100 start = lw->selected - lw->rows / 2;
101 else {
102 if (lw->selected < lw->start + options.scroll_offset)
103 start = lw->selected - options.scroll_offset;
105 if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
106 start = lw->selected - lw->rows + 1 + options.scroll_offset;
107 }
109 if (start + lw->rows > lw->length)
110 start = lw->length - lw->rows;
112 if (start < 0 || lw->length == 0)
113 start = 0;
115 lw->start = start;
116 }
118 void
119 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
120 {
121 lw->cols = width;
122 lw->rows = height;
124 list_window_check_origin(lw);
125 }
127 void
128 list_window_set_length(struct list_window *lw, unsigned length)
129 {
130 lw->length = length;
132 list_window_check_selected(lw);
133 list_window_check_origin(lw);
134 }
136 void
137 list_window_center(struct list_window *lw, unsigned n)
138 {
139 if (n > lw->rows / 2)
140 lw->start = n - lw->rows / 2;
141 else
142 lw->start = 0;
144 if (lw->start + lw->rows > lw->length) {
145 if (lw->rows < lw->length)
146 lw->start = lw->length - lw->rows;
147 else
148 lw->start = 0;
149 }
150 }
152 void
153 list_window_set_cursor(struct list_window *lw, unsigned i)
154 {
155 lw->range_selection = false;
156 lw->selected = i;
158 list_window_check_selected(lw);
159 list_window_check_origin(lw);
160 }
162 void
163 list_window_move_cursor(struct list_window *lw, unsigned n)
164 {
165 lw->selected = n;
167 list_window_check_selected(lw);
168 list_window_check_origin(lw);
169 }
171 void
172 list_window_fetch_cursor(struct list_window *lw)
173 {
174 if (lw->start > 0 &&
175 lw->selected < lw->start + options.scroll_offset)
176 list_window_move_cursor(lw, lw->start + options.scroll_offset);
177 else if (lw->start + lw->rows < lw->length &&
178 lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
179 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
180 }
182 void
183 list_window_get_range(const struct list_window *lw,
184 struct list_window_range *range)
185 {
186 if (lw->length == 0) {
187 /* empty list - no selection */
188 range->start = 0;
189 range->end = 0;
190 } else if (lw->range_selection) {
191 /* a range selection */
192 if (lw->range_base < lw->selected) {
193 range->start = lw->range_base;
194 range->end = lw->selected + 1;
195 } else {
196 range->start = lw->selected;
197 range->end = lw->range_base + 1;
198 }
199 } else {
200 /* no range, just the cursor */
201 range->start = lw->selected;
202 range->end = lw->selected + 1;
203 }
204 }
206 static void
207 list_window_next(struct list_window *lw)
208 {
209 if (lw->selected + 1 < lw->length)
210 list_window_move_cursor(lw, lw->selected + 1);
211 else if (options.list_wrap)
212 list_window_move_cursor(lw, 0);
213 }
215 static void
216 list_window_previous(struct list_window *lw)
217 {
218 if (lw->selected > 0)
219 list_window_move_cursor(lw, lw->selected - 1);
220 else if (options.list_wrap)
221 list_window_move_cursor(lw, lw->length - 1);
222 }
224 static void
225 list_window_top(struct list_window *lw)
226 {
227 if (lw->start == 0)
228 list_window_move_cursor(lw, lw->start);
229 else
230 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
231 list_window_move_cursor(lw, lw->start + lw->rows / 2);
232 else
233 list_window_move_cursor(lw, lw->start + options.scroll_offset);
234 }
236 static void
237 list_window_middle(struct list_window *lw)
238 {
239 if (lw->length >= lw->rows)
240 list_window_move_cursor(lw, lw->start + lw->rows / 2);
241 else
242 list_window_move_cursor(lw, lw->length / 2);
243 }
245 static void
246 list_window_bottom(struct list_window *lw)
247 {
248 if (lw->length >= lw->rows)
249 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
250 list_window_move_cursor(lw, lw->start + lw->rows / 2);
251 else
252 if (lw->start + lw->rows == lw->length)
253 list_window_move_cursor(lw, lw->length - 1);
254 else
255 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
256 else
257 list_window_move_cursor(lw, lw->length - 1);
258 }
260 static void
261 list_window_first(struct list_window *lw)
262 {
263 list_window_move_cursor(lw, 0);
264 }
266 static void
267 list_window_last(struct list_window *lw)
268 {
269 if (lw->length > 0)
270 list_window_move_cursor(lw, lw->length - 1);
271 else
272 list_window_move_cursor(lw, 0);
273 }
275 static void
276 list_window_next_page(struct list_window *lw)
277 {
278 if (lw->rows < 2)
279 return;
280 if (lw->selected + lw->rows < lw->length)
281 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
282 else
283 list_window_last(lw);
284 }
286 static void
287 list_window_previous_page(struct list_window *lw)
288 {
289 if (lw->rows < 2)
290 return;
291 if (lw->selected > lw->rows - 1)
292 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
293 else
294 list_window_first(lw);
295 }
297 static void
298 list_window_scroll_up(struct list_window *lw, unsigned n)
299 {
300 if (lw->start > 0) {
301 if (n > lw->start)
302 lw->start = 0;
303 else
304 lw->start -= n;
306 list_window_fetch_cursor(lw);
307 }
308 }
310 static void
311 list_window_scroll_down(struct list_window *lw, unsigned n)
312 {
313 if (lw->start + lw->rows < lw->length)
314 {
315 if ( lw->start + lw->rows + n > lw->length - 1)
316 lw->start = lw->length - lw->rows;
317 else
318 lw->start += n;
320 list_window_fetch_cursor(lw);
321 }
322 }
324 static void
325 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
326 bool selected, bool highlight,
327 const char *text, const char *second_column)
328 {
329 unsigned second_column_width;
331 #ifdef NCMPC_MINI
332 second_column = NULL;
333 highlight = false;
334 #endif /* NCMPC_MINI */
336 if (second_column != NULL) {
337 second_column_width = utf8_width(second_column) + 1;
338 if (second_column_width < width)
339 width -= second_column_width;
340 else
341 second_column_width = 0;
342 } else
343 second_column_width = 0;
345 row_color(w, highlight ? COLOR_LIST_BOLD : COLOR_LIST, selected);
347 waddstr(w, text);
349 /* erase the unused space after the text */
350 row_clear_to_eol(w, width, selected);
352 if (second_column_width > 0) {
353 wmove(w, y, width);
354 waddch(w, ' ');
355 waddstr(w, second_column);
356 }
357 }
359 void
360 list_window_paint(const struct list_window *lw,
361 list_window_callback_fn_t callback,
362 void *callback_data)
363 {
364 bool show_cursor = !lw->hide_cursor &&
365 (!options.hardware_cursor || lw->range_selection);
366 struct list_window_range range;
368 if (show_cursor)
369 list_window_get_range(lw, &range);
371 for (unsigned i = 0; i < lw->rows; i++) {
372 const char *label;
373 bool highlight = false;
374 char *second_column = NULL;
376 wmove(lw->w, i, 0);
378 if (lw->start + i >= lw->length) {
379 wclrtobot(lw->w);
380 break;
381 }
383 label = callback(lw->start + i, &highlight, &second_column, callback_data);
384 assert(label != NULL);
386 #ifdef NCMPC_MINI
387 highlight = false;
388 second_column = NULL;
389 #endif /* NCMPC_MINI */
391 list_window_paint_row(lw->w, i, lw->cols,
392 show_cursor &&
393 lw->start + i >= range.start &&
394 lw->start + i < range.end,
395 highlight,
396 label, second_column);
398 if (second_column != NULL)
399 g_free(second_column);
400 }
402 row_color_end(lw->w);
404 if (options.hardware_cursor && lw->selected >= lw->start &&
405 lw->selected < lw->start + lw->rows) {
406 curs_set(1);
407 wmove(lw->w, lw->selected - lw->start, 0);
408 }
409 }
411 void
412 list_window_paint2(const struct list_window *lw,
413 list_window_paint_callback_t paint_callback,
414 void *callback_data)
415 {
416 bool show_cursor = !lw->hide_cursor &&
417 (!options.hardware_cursor || lw->range_selection);
418 struct list_window_range range;
420 if (show_cursor)
421 list_window_get_range(lw, &range);
423 for (unsigned i = 0; i < lw->rows; i++) {
424 bool selected;
426 wmove(lw->w, i, 0);
428 if (lw->start + i >= lw->length) {
429 wclrtobot(lw->w);
430 break;
431 }
433 selected = show_cursor &&
434 lw->start + i >= range.start &&
435 lw->start + i < range.end;
437 paint_callback(lw->w, lw->start + i, i, lw->cols,
438 selected, callback_data);
440 if (selected)
441 wattroff(lw->w, A_REVERSE);
442 }
444 if (options.hardware_cursor && lw->selected >= lw->start &&
445 lw->selected < lw->start + lw->rows) {
446 curs_set(1);
447 wmove(lw->w, lw->selected - lw->start, 0);
448 }
449 }
451 bool
452 list_window_find(struct list_window *lw,
453 list_window_callback_fn_t callback,
454 void *callback_data,
455 const char *str,
456 bool wrap,
457 bool bell_on_wrap)
458 {
459 bool h;
460 unsigned i = lw->selected + 1;
461 const char *label;
463 assert(str != NULL);
465 do {
466 while (i < lw->length) {
467 label = callback(i, &h, NULL, callback_data);
468 assert(label != NULL);
470 if (match_line(label, str)) {
471 list_window_move_cursor(lw, i);
472 return true;
473 }
474 if (wrap && i == lw->selected)
475 return false;
476 i++;
477 }
478 if (wrap) {
479 if (i == 0) /* empty list */
480 return 1;
481 i=0; /* first item */
482 if (bell_on_wrap) {
483 screen_bell();
484 }
485 }
486 } while (wrap);
488 return false;
489 }
491 bool
492 list_window_rfind(struct list_window *lw,
493 list_window_callback_fn_t callback,
494 void *callback_data,
495 const char *str,
496 bool wrap,
497 bool bell_on_wrap)
498 {
499 bool h;
500 int i = lw->selected - 1;
501 const char *label;
503 assert(str != NULL);
505 if (lw->length == 0)
506 return false;
508 do {
509 while (i >= 0) {
510 label = callback(i, &h, NULL, callback_data);
511 assert(label != NULL);
513 if (match_line(label, str)) {
514 list_window_move_cursor(lw, i);
515 return true;
516 }
517 if (wrap && i == (int)lw->selected)
518 return false;
519 i--;
520 }
521 if (wrap) {
522 i = lw->length - 1; /* last item */
523 if (bell_on_wrap) {
524 screen_bell();
525 }
526 }
527 } while (wrap);
529 return false;
530 }
532 static bool
533 jump_match(const char *haystack, const char *needle)
534 {
535 #ifdef NCMPC_MINI
536 bool jump_prefix_only = true;
537 #else
538 bool jump_prefix_only = options.jump_prefix_only;
539 #endif
541 assert(haystack != NULL);
542 assert(needle != NULL);
544 return jump_prefix_only
545 ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
546 : match_line(haystack, needle);
547 }
549 bool
550 list_window_jump(struct list_window *lw,
551 list_window_callback_fn_t callback,
552 void *callback_data,
553 const char *str)
554 {
555 bool h;
556 const char *label;
558 assert(str != NULL);
560 for (unsigned i = 0; i < lw->length; ++i) {
561 label = callback(i, &h, NULL, callback_data);
562 assert(label != NULL);
564 if (label[0] == '[')
565 label++;
567 if (jump_match(label, str)) {
568 list_window_move_cursor(lw, i);
569 return true;
570 }
571 }
572 return false;
573 }
575 /* perform basic list window commands (movement) */
576 bool
577 list_window_cmd(struct list_window *lw, command_t cmd)
578 {
579 switch (cmd) {
580 case CMD_LIST_PREVIOUS:
581 list_window_previous(lw);
582 break;
583 case CMD_LIST_NEXT:
584 list_window_next(lw);
585 break;
586 case CMD_LIST_TOP:
587 list_window_top(lw);
588 break;
589 case CMD_LIST_MIDDLE:
590 list_window_middle(lw);
591 break;
592 case CMD_LIST_BOTTOM:
593 list_window_bottom(lw);
594 break;
595 case CMD_LIST_FIRST:
596 list_window_first(lw);
597 break;
598 case CMD_LIST_LAST:
599 list_window_last(lw);
600 break;
601 case CMD_LIST_NEXT_PAGE:
602 list_window_next_page(lw);
603 break;
604 case CMD_LIST_PREVIOUS_PAGE:
605 list_window_previous_page(lw);
606 break;
607 case CMD_LIST_RANGE_SELECT:
608 if(lw->range_selection)
609 {
610 screen_status_printf(_("Range selection disabled"));
611 list_window_set_cursor(lw, lw->selected);
612 }
613 else
614 {
615 screen_status_printf(_("Range selection enabled"));
616 lw->range_base = lw->selected;
617 lw->range_selection = true;
618 }
619 break;
620 case CMD_LIST_SCROLL_UP_LINE:
621 list_window_scroll_up(lw, 1);
622 break;
623 case CMD_LIST_SCROLL_DOWN_LINE:
624 list_window_scroll_down(lw, 1);
625 break;
626 case CMD_LIST_SCROLL_UP_HALF:
627 list_window_scroll_up(lw, (lw->rows - 1) / 2);
628 break;
629 case CMD_LIST_SCROLL_DOWN_HALF:
630 list_window_scroll_down(lw, (lw->rows - 1) / 2);
631 break;
632 default:
633 return false;
634 }
636 return true;
637 }
639 bool
640 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
641 {
642 switch (cmd) {
643 case CMD_LIST_SCROLL_UP_LINE:
644 case CMD_LIST_PREVIOUS:
645 if (lw->start > 0)
646 lw->start--;
647 break;
649 case CMD_LIST_SCROLL_DOWN_LINE:
650 case CMD_LIST_NEXT:
651 if (lw->start + lw->rows < lw->length)
652 lw->start++;
653 break;
655 case CMD_LIST_FIRST:
656 lw->start = 0;
657 break;
659 case CMD_LIST_LAST:
660 if (lw->length > lw->rows)
661 lw->start = lw->length - lw->rows;
662 else
663 lw->start = 0;
664 break;
666 case CMD_LIST_NEXT_PAGE:
667 lw->start += lw->rows - 1;
668 if (lw->start + lw->rows > lw->length) {
669 if (lw->length > lw->rows)
670 lw->start = lw->length - lw->rows;
671 else
672 lw->start = 0;
673 }
674 break;
676 case CMD_LIST_PREVIOUS_PAGE:
677 if (lw->start > lw->rows)
678 lw->start -= lw->rows;
679 else
680 lw->start = 0;
681 break;
683 case CMD_LIST_SCROLL_UP_HALF:
684 if (lw->start > (lw->rows - 1) / 2)
685 lw->start -= (lw->rows - 1) / 2;
686 else
687 lw->start = 0;
688 break;
690 case CMD_LIST_SCROLL_DOWN_HALF:
691 lw->start += (lw->rows - 1) / 2;
692 if (lw->start + lw->rows > lw->length) {
693 if (lw->length > lw->rows)
694 lw->start = lw->length - lw->rows;
695 else
696 lw->start = 0;
697 }
698 break;
700 default:
701 return false;
702 }
704 return true;
705 }
707 #ifdef HAVE_GETMOUSE
708 bool
709 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
710 {
711 assert(lw != NULL);
713 /* if the even occurred above the list window move up */
714 if (y < 0) {
715 if (bstate & BUTTON3_CLICKED)
716 list_window_first(lw);
717 else
718 list_window_previous_page(lw);
719 return true;
720 }
722 /* if the even occurred below the list window move down */
723 if ((unsigned)y >= lw->length) {
724 if (bstate & BUTTON3_CLICKED)
725 list_window_last(lw);
726 else
727 list_window_next_page(lw);
728 return true;
729 }
731 return false;
732 }
733 #endif