1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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 list_window_scroll_to(lw, lw->selected);
97 }
99 void
100 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
101 {
102 lw->cols = width;
103 lw->rows = height;
105 list_window_check_origin(lw);
106 }
108 void
109 list_window_set_length(struct list_window *lw, unsigned length)
110 {
111 if (length == lw->length)
112 return;
114 lw->length = length;
116 list_window_check_selected(lw);
117 list_window_check_origin(lw);
118 }
120 void
121 list_window_center(struct list_window *lw, unsigned n)
122 {
123 if (n > lw->rows / 2)
124 lw->start = n - lw->rows / 2;
125 else
126 lw->start = 0;
128 if (lw->start + lw->rows > lw->length) {
129 if (lw->rows < lw->length)
130 lw->start = lw->length - lw->rows;
131 else
132 lw->start = 0;
133 }
134 }
136 void
137 list_window_scroll_to(struct list_window *lw, unsigned n)
138 {
139 int start = lw->start;
141 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
142 // Center if the offset is more than half the screen
143 start = n - lw->rows / 2;
144 else {
145 if (n < lw->start + options.scroll_offset)
146 start = n - options.scroll_offset;
148 if (n >= lw->start + lw->rows - options.scroll_offset)
149 start = n - lw->rows + 1 + options.scroll_offset;
150 }
152 if (start + lw->rows > lw->length)
153 start = lw->length - lw->rows;
155 if (start < 0 || lw->length == 0)
156 start = 0;
158 lw->start = start;
159 }
161 void
162 list_window_set_cursor(struct list_window *lw, unsigned i)
163 {
164 lw->range_selection = false;
165 lw->selected = i;
167 list_window_check_selected(lw);
168 list_window_check_origin(lw);
169 }
171 void
172 list_window_move_cursor(struct list_window *lw, unsigned n)
173 {
174 lw->selected = n;
176 list_window_check_selected(lw);
177 list_window_check_origin(lw);
178 }
180 void
181 list_window_fetch_cursor(struct list_window *lw)
182 {
183 if (lw->start > 0 &&
184 lw->selected < lw->start + options.scroll_offset)
185 list_window_move_cursor(lw, lw->start + options.scroll_offset);
186 else if (lw->start + lw->rows < lw->length &&
187 lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
188 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
189 }
191 void
192 list_window_get_range(const struct list_window *lw,
193 struct list_window_range *range)
194 {
195 if (lw->length == 0) {
196 /* empty list - no selection */
197 range->start = 0;
198 range->end = 0;
199 } else if (lw->range_selection) {
200 /* a range selection */
201 if (lw->range_base < lw->selected) {
202 range->start = lw->range_base;
203 range->end = lw->selected + 1;
204 } else {
205 range->start = lw->selected;
206 range->end = lw->range_base + 1;
207 }
208 } else {
209 /* no range, just the cursor */
210 range->start = lw->selected;
211 range->end = lw->selected + 1;
212 }
213 }
215 static void
216 list_window_next(struct list_window *lw)
217 {
218 if (lw->selected + 1 < lw->length)
219 list_window_move_cursor(lw, lw->selected + 1);
220 else if (options.list_wrap)
221 list_window_move_cursor(lw, 0);
222 }
224 static void
225 list_window_previous(struct list_window *lw)
226 {
227 if (lw->selected > 0)
228 list_window_move_cursor(lw, lw->selected - 1);
229 else if (options.list_wrap)
230 list_window_move_cursor(lw, lw->length - 1);
231 }
233 static void
234 list_window_top(struct list_window *lw)
235 {
236 if (lw->start == 0)
237 list_window_move_cursor(lw, lw->start);
238 else
239 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
240 list_window_move_cursor(lw, lw->start + lw->rows / 2);
241 else
242 list_window_move_cursor(lw, lw->start + options.scroll_offset);
243 }
245 static void
246 list_window_middle(struct list_window *lw)
247 {
248 if (lw->length >= lw->rows)
249 list_window_move_cursor(lw, lw->start + lw->rows / 2);
250 else
251 list_window_move_cursor(lw, lw->length / 2);
252 }
254 static void
255 list_window_bottom(struct list_window *lw)
256 {
257 if (lw->length >= lw->rows)
258 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
259 list_window_move_cursor(lw, lw->start + lw->rows / 2);
260 else
261 if (lw->start + lw->rows == lw->length)
262 list_window_move_cursor(lw, lw->length - 1);
263 else
264 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
265 else
266 list_window_move_cursor(lw, lw->length - 1);
267 }
269 static void
270 list_window_first(struct list_window *lw)
271 {
272 list_window_move_cursor(lw, 0);
273 }
275 static void
276 list_window_last(struct list_window *lw)
277 {
278 if (lw->length > 0)
279 list_window_move_cursor(lw, lw->length - 1);
280 else
281 list_window_move_cursor(lw, 0);
282 }
284 static void
285 list_window_next_page(struct list_window *lw)
286 {
287 if (lw->rows < 2)
288 return;
289 if (lw->selected + lw->rows < lw->length)
290 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
291 else
292 list_window_last(lw);
293 }
295 static void
296 list_window_previous_page(struct list_window *lw)
297 {
298 if (lw->rows < 2)
299 return;
300 if (lw->selected > lw->rows - 1)
301 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
302 else
303 list_window_first(lw);
304 }
306 static void
307 list_window_scroll_up(struct list_window *lw, unsigned n)
308 {
309 if (lw->start > 0) {
310 if (n > lw->start)
311 lw->start = 0;
312 else
313 lw->start -= n;
315 list_window_fetch_cursor(lw);
316 }
317 }
319 static void
320 list_window_scroll_down(struct list_window *lw, unsigned n)
321 {
322 if (lw->start + lw->rows < lw->length)
323 {
324 if (lw->start + lw->rows + n > lw->length - 1)
325 lw->start = lw->length - lw->rows;
326 else
327 lw->start += n;
329 list_window_fetch_cursor(lw);
330 }
331 }
333 static void
334 list_window_paint_row(WINDOW *w, unsigned width, bool selected,
335 const char *text)
336 {
337 row_paint_text(w, width, COLOR_LIST,
338 selected, text);
339 }
341 void
342 list_window_paint(const struct list_window *lw,
343 list_window_callback_fn_t callback,
344 void *callback_data)
345 {
346 bool show_cursor = !lw->hide_cursor &&
347 (!options.hardware_cursor || lw->range_selection);
348 struct list_window_range range;
350 if (show_cursor)
351 list_window_get_range(lw, &range);
353 for (unsigned i = 0; i < lw->rows; i++) {
354 const char *label;
356 wmove(lw->w, i, 0);
358 if (lw->start + i >= lw->length) {
359 wclrtobot(lw->w);
360 break;
361 }
363 label = callback(lw->start + i, callback_data);
364 assert(label != NULL);
366 list_window_paint_row(lw->w, lw->cols,
367 show_cursor &&
368 lw->start + i >= range.start &&
369 lw->start + i < range.end,
370 label);
371 }
373 row_color_end(lw->w);
375 if (options.hardware_cursor && lw->selected >= lw->start &&
376 lw->selected < lw->start + lw->rows) {
377 curs_set(1);
378 wmove(lw->w, lw->selected - lw->start, 0);
379 }
380 }
382 void
383 list_window_paint2(const struct list_window *lw,
384 list_window_paint_callback_t paint_callback,
385 void *callback_data)
386 {
387 bool show_cursor = !lw->hide_cursor &&
388 (!options.hardware_cursor || lw->range_selection);
389 struct list_window_range range;
391 if (show_cursor)
392 list_window_get_range(lw, &range);
394 for (unsigned i = 0; i < lw->rows; i++) {
395 bool selected;
397 wmove(lw->w, i, 0);
399 if (lw->start + i >= lw->length) {
400 wclrtobot(lw->w);
401 break;
402 }
404 selected = show_cursor &&
405 lw->start + i >= range.start &&
406 lw->start + i < range.end;
408 paint_callback(lw->w, lw->start + i, i, lw->cols,
409 selected, callback_data);
410 }
412 if (options.hardware_cursor && lw->selected >= lw->start &&
413 lw->selected < lw->start + lw->rows) {
414 curs_set(1);
415 wmove(lw->w, lw->selected - lw->start, 0);
416 }
417 }
419 bool
420 list_window_find(struct list_window *lw,
421 list_window_callback_fn_t callback,
422 void *callback_data,
423 const char *str,
424 bool wrap,
425 bool bell_on_wrap)
426 {
427 unsigned i = lw->selected + 1;
428 const char *label;
430 assert(str != NULL);
432 do {
433 while (i < lw->length) {
434 label = callback(i, callback_data);
435 assert(label != NULL);
437 if (match_line(label, str)) {
438 list_window_move_cursor(lw, i);
439 return true;
440 }
441 if (wrap && i == lw->selected)
442 return false;
443 i++;
444 }
445 if (wrap) {
446 if (i == 0) /* empty list */
447 return 1;
448 i=0; /* first item */
449 if (bell_on_wrap) {
450 screen_bell();
451 }
452 }
453 } while (wrap);
455 return false;
456 }
458 bool
459 list_window_rfind(struct list_window *lw,
460 list_window_callback_fn_t callback,
461 void *callback_data,
462 const char *str,
463 bool wrap,
464 bool bell_on_wrap)
465 {
466 int i = lw->selected - 1;
467 const char *label;
469 assert(str != NULL);
471 if (lw->length == 0)
472 return false;
474 do {
475 while (i >= 0) {
476 label = callback(i, callback_data);
477 assert(label != NULL);
479 if (match_line(label, str)) {
480 list_window_move_cursor(lw, i);
481 return true;
482 }
483 if (wrap && i == (int)lw->selected)
484 return false;
485 i--;
486 }
487 if (wrap) {
488 i = lw->length - 1; /* last item */
489 if (bell_on_wrap) {
490 screen_bell();
491 }
492 }
493 } while (wrap);
495 return false;
496 }
498 #ifdef NCMPC_MINI
499 bool
500 list_window_jump(struct list_window *lw,
501 list_window_callback_fn_t callback,
502 void *callback_data,
503 const char *str)
504 {
505 unsigned i;
506 const char *label;
508 assert(str != NULL);
510 for (i = 0; i < lw->length; i++) {
511 label = callback(i, callback_data);
512 assert(label != NULL);
514 if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
515 list_window_move_cursor(lw, i);
516 return true;
517 }
518 }
519 return false;
520 }
521 #else
522 bool
523 list_window_jump(struct list_window *lw,
524 list_window_callback_fn_t callback,
525 void *callback_data,
526 const char *str)
527 {
528 unsigned i;
529 const char *label;
530 GRegex *regex;
532 assert(str != NULL);
534 regex = compile_regex(str, options.jump_prefix_only);
535 if (regex == NULL)
536 return false;
538 for (i = 0; i < lw->length; i++) {
539 label = callback(i, callback_data);
540 assert(label != NULL);
542 if (match_regex(regex, label)) {
543 g_regex_unref(regex);
544 list_window_move_cursor(lw, i);
545 return true;
546 }
547 }
548 g_regex_unref(regex);
549 return false;
550 }
551 #endif
553 /* perform basic list window commands (movement) */
554 bool
555 list_window_cmd(struct list_window *lw, command_t cmd)
556 {
557 switch (cmd) {
558 case CMD_LIST_PREVIOUS:
559 list_window_previous(lw);
560 break;
561 case CMD_LIST_NEXT:
562 list_window_next(lw);
563 break;
564 case CMD_LIST_TOP:
565 list_window_top(lw);
566 break;
567 case CMD_LIST_MIDDLE:
568 list_window_middle(lw);
569 break;
570 case CMD_LIST_BOTTOM:
571 list_window_bottom(lw);
572 break;
573 case CMD_LIST_FIRST:
574 list_window_first(lw);
575 break;
576 case CMD_LIST_LAST:
577 list_window_last(lw);
578 break;
579 case CMD_LIST_NEXT_PAGE:
580 list_window_next_page(lw);
581 break;
582 case CMD_LIST_PREVIOUS_PAGE:
583 list_window_previous_page(lw);
584 break;
585 case CMD_LIST_RANGE_SELECT:
586 if(lw->range_selection)
587 {
588 screen_status_printf(_("Range selection disabled"));
589 list_window_set_cursor(lw, lw->selected);
590 }
591 else
592 {
593 screen_status_printf(_("Range selection enabled"));
594 lw->range_base = lw->selected;
595 lw->range_selection = true;
596 }
597 break;
598 case CMD_LIST_SCROLL_UP_LINE:
599 list_window_scroll_up(lw, 1);
600 break;
601 case CMD_LIST_SCROLL_DOWN_LINE:
602 list_window_scroll_down(lw, 1);
603 break;
604 case CMD_LIST_SCROLL_UP_HALF:
605 list_window_scroll_up(lw, (lw->rows - 1) / 2);
606 break;
607 case CMD_LIST_SCROLL_DOWN_HALF:
608 list_window_scroll_down(lw, (lw->rows - 1) / 2);
609 break;
610 default:
611 return false;
612 }
614 return true;
615 }
617 bool
618 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
619 {
620 switch (cmd) {
621 case CMD_LIST_SCROLL_UP_LINE:
622 case CMD_LIST_PREVIOUS:
623 if (lw->start > 0)
624 lw->start--;
625 break;
627 case CMD_LIST_SCROLL_DOWN_LINE:
628 case CMD_LIST_NEXT:
629 if (lw->start + lw->rows < lw->length)
630 lw->start++;
631 break;
633 case CMD_LIST_FIRST:
634 lw->start = 0;
635 break;
637 case CMD_LIST_LAST:
638 if (lw->length > lw->rows)
639 lw->start = lw->length - lw->rows;
640 else
641 lw->start = 0;
642 break;
644 case CMD_LIST_NEXT_PAGE:
645 lw->start += lw->rows - 1;
646 if (lw->start + lw->rows > lw->length) {
647 if (lw->length > lw->rows)
648 lw->start = lw->length - lw->rows;
649 else
650 lw->start = 0;
651 }
652 break;
654 case CMD_LIST_PREVIOUS_PAGE:
655 if (lw->start > lw->rows)
656 lw->start -= lw->rows;
657 else
658 lw->start = 0;
659 break;
661 case CMD_LIST_SCROLL_UP_HALF:
662 if (lw->start > (lw->rows - 1) / 2)
663 lw->start -= (lw->rows - 1) / 2;
664 else
665 lw->start = 0;
666 break;
668 case CMD_LIST_SCROLL_DOWN_HALF:
669 lw->start += (lw->rows - 1) / 2;
670 if (lw->start + lw->rows > lw->length) {
671 if (lw->length > lw->rows)
672 lw->start = lw->length - lw->rows;
673 else
674 lw->start = 0;
675 }
676 break;
678 default:
679 return false;
680 }
682 return true;
683 }
685 #ifdef HAVE_GETMOUSE
686 bool
687 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
688 {
689 assert(lw != NULL);
691 /* if the even occurred above the list window move up */
692 if (y < 0) {
693 if (bstate & BUTTON3_CLICKED)
694 list_window_first(lw);
695 else
696 list_window_previous_page(lw);
697 return true;
698 }
700 /* if the even occurred below the list window move down */
701 if ((unsigned)y >= lw->length) {
702 if (bstate & BUTTON3_CLICKED)
703 list_window_last(lw);
704 else
705 list_window_next_page(lw);
706 return true;
707 }
709 return false;
710 }
711 #endif