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 width, bool selected,
326 const char *text)
327 {
328 row_paint_text(w, width, COLOR_LIST,
329 selected, text);
330 }
332 void
333 list_window_paint(const struct list_window *lw,
334 list_window_callback_fn_t callback,
335 void *callback_data)
336 {
337 bool show_cursor = !lw->hide_cursor &&
338 (!options.hardware_cursor || lw->range_selection);
339 struct list_window_range range;
341 if (show_cursor)
342 list_window_get_range(lw, &range);
344 for (unsigned i = 0; i < lw->rows; i++) {
345 const char *label;
347 wmove(lw->w, i, 0);
349 if (lw->start + i >= lw->length) {
350 wclrtobot(lw->w);
351 break;
352 }
354 label = callback(lw->start + i, callback_data);
355 assert(label != NULL);
357 list_window_paint_row(lw->w, lw->cols,
358 show_cursor &&
359 lw->start + i >= range.start &&
360 lw->start + i < range.end,
361 label);
362 }
364 row_color_end(lw->w);
366 if (options.hardware_cursor && lw->selected >= lw->start &&
367 lw->selected < lw->start + lw->rows) {
368 curs_set(1);
369 wmove(lw->w, lw->selected - lw->start, 0);
370 }
371 }
373 void
374 list_window_paint2(const struct list_window *lw,
375 list_window_paint_callback_t paint_callback,
376 void *callback_data)
377 {
378 bool show_cursor = !lw->hide_cursor &&
379 (!options.hardware_cursor || lw->range_selection);
380 struct list_window_range range;
382 if (show_cursor)
383 list_window_get_range(lw, &range);
385 for (unsigned i = 0; i < lw->rows; i++) {
386 bool selected;
388 wmove(lw->w, i, 0);
390 if (lw->start + i >= lw->length) {
391 wclrtobot(lw->w);
392 break;
393 }
395 selected = show_cursor &&
396 lw->start + i >= range.start &&
397 lw->start + i < range.end;
399 paint_callback(lw->w, lw->start + i, i, lw->cols,
400 selected, callback_data);
402 if (selected)
403 wattroff(lw->w, A_REVERSE);
404 }
406 if (options.hardware_cursor && lw->selected >= lw->start &&
407 lw->selected < lw->start + lw->rows) {
408 curs_set(1);
409 wmove(lw->w, lw->selected - lw->start, 0);
410 }
411 }
413 bool
414 list_window_find(struct list_window *lw,
415 list_window_callback_fn_t callback,
416 void *callback_data,
417 const char *str,
418 bool wrap,
419 bool bell_on_wrap)
420 {
421 unsigned i = lw->selected + 1;
422 const char *label;
424 assert(str != NULL);
426 do {
427 while (i < lw->length) {
428 label = callback(i, callback_data);
429 assert(label != NULL);
431 if (match_line(label, str)) {
432 list_window_move_cursor(lw, i);
433 return true;
434 }
435 if (wrap && i == lw->selected)
436 return false;
437 i++;
438 }
439 if (wrap) {
440 if (i == 0) /* empty list */
441 return 1;
442 i=0; /* first item */
443 if (bell_on_wrap) {
444 screen_bell();
445 }
446 }
447 } while (wrap);
449 return false;
450 }
452 bool
453 list_window_rfind(struct list_window *lw,
454 list_window_callback_fn_t callback,
455 void *callback_data,
456 const char *str,
457 bool wrap,
458 bool bell_on_wrap)
459 {
460 int i = lw->selected - 1;
461 const char *label;
463 assert(str != NULL);
465 if (lw->length == 0)
466 return false;
468 do {
469 while (i >= 0) {
470 label = callback(i, callback_data);
471 assert(label != NULL);
473 if (match_line(label, str)) {
474 list_window_move_cursor(lw, i);
475 return true;
476 }
477 if (wrap && i == (int)lw->selected)
478 return false;
479 i--;
480 }
481 if (wrap) {
482 i = lw->length - 1; /* last item */
483 if (bell_on_wrap) {
484 screen_bell();
485 }
486 }
487 } while (wrap);
489 return false;
490 }
492 static bool
493 jump_match(const char *haystack, const char *needle)
494 {
495 #ifdef NCMPC_MINI
496 bool jump_prefix_only = true;
497 #else
498 bool jump_prefix_only = options.jump_prefix_only;
499 #endif
501 assert(haystack != NULL);
502 assert(needle != NULL);
504 return jump_prefix_only
505 ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
506 : match_line(haystack, needle);
507 }
509 bool
510 list_window_jump(struct list_window *lw,
511 list_window_callback_fn_t callback,
512 void *callback_data,
513 const char *str)
514 {
515 const char *label;
517 assert(str != NULL);
519 for (unsigned i = 0; i < lw->length; ++i) {
520 label = callback(i, callback_data);
521 assert(label != NULL);
523 if (jump_match(label, str)) {
524 list_window_move_cursor(lw, i);
525 return true;
526 }
527 }
528 return false;
529 }
531 /* perform basic list window commands (movement) */
532 bool
533 list_window_cmd(struct list_window *lw, command_t cmd)
534 {
535 switch (cmd) {
536 case CMD_LIST_PREVIOUS:
537 list_window_previous(lw);
538 break;
539 case CMD_LIST_NEXT:
540 list_window_next(lw);
541 break;
542 case CMD_LIST_TOP:
543 list_window_top(lw);
544 break;
545 case CMD_LIST_MIDDLE:
546 list_window_middle(lw);
547 break;
548 case CMD_LIST_BOTTOM:
549 list_window_bottom(lw);
550 break;
551 case CMD_LIST_FIRST:
552 list_window_first(lw);
553 break;
554 case CMD_LIST_LAST:
555 list_window_last(lw);
556 break;
557 case CMD_LIST_NEXT_PAGE:
558 list_window_next_page(lw);
559 break;
560 case CMD_LIST_PREVIOUS_PAGE:
561 list_window_previous_page(lw);
562 break;
563 case CMD_LIST_RANGE_SELECT:
564 if(lw->range_selection)
565 {
566 screen_status_printf(_("Range selection disabled"));
567 list_window_set_cursor(lw, lw->selected);
568 }
569 else
570 {
571 screen_status_printf(_("Range selection enabled"));
572 lw->range_base = lw->selected;
573 lw->range_selection = true;
574 }
575 break;
576 case CMD_LIST_SCROLL_UP_LINE:
577 list_window_scroll_up(lw, 1);
578 break;
579 case CMD_LIST_SCROLL_DOWN_LINE:
580 list_window_scroll_down(lw, 1);
581 break;
582 case CMD_LIST_SCROLL_UP_HALF:
583 list_window_scroll_up(lw, (lw->rows - 1) / 2);
584 break;
585 case CMD_LIST_SCROLL_DOWN_HALF:
586 list_window_scroll_down(lw, (lw->rows - 1) / 2);
587 break;
588 default:
589 return false;
590 }
592 return true;
593 }
595 bool
596 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
597 {
598 switch (cmd) {
599 case CMD_LIST_SCROLL_UP_LINE:
600 case CMD_LIST_PREVIOUS:
601 if (lw->start > 0)
602 lw->start--;
603 break;
605 case CMD_LIST_SCROLL_DOWN_LINE:
606 case CMD_LIST_NEXT:
607 if (lw->start + lw->rows < lw->length)
608 lw->start++;
609 break;
611 case CMD_LIST_FIRST:
612 lw->start = 0;
613 break;
615 case CMD_LIST_LAST:
616 if (lw->length > lw->rows)
617 lw->start = lw->length - lw->rows;
618 else
619 lw->start = 0;
620 break;
622 case CMD_LIST_NEXT_PAGE:
623 lw->start += lw->rows - 1;
624 if (lw->start + lw->rows > lw->length) {
625 if (lw->length > lw->rows)
626 lw->start = lw->length - lw->rows;
627 else
628 lw->start = 0;
629 }
630 break;
632 case CMD_LIST_PREVIOUS_PAGE:
633 if (lw->start > lw->rows)
634 lw->start -= lw->rows;
635 else
636 lw->start = 0;
637 break;
639 case CMD_LIST_SCROLL_UP_HALF:
640 if (lw->start > (lw->rows - 1) / 2)
641 lw->start -= (lw->rows - 1) / 2;
642 else
643 lw->start = 0;
644 break;
646 case CMD_LIST_SCROLL_DOWN_HALF:
647 lw->start += (lw->rows - 1) / 2;
648 if (lw->start + lw->rows > lw->length) {
649 if (lw->length > lw->rows)
650 lw->start = lw->length - lw->rows;
651 else
652 lw->start = 0;
653 }
654 break;
656 default:
657 return false;
658 }
660 return true;
661 }
663 #ifdef HAVE_GETMOUSE
664 bool
665 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
666 {
667 assert(lw != NULL);
669 /* if the even occurred above the list window move up */
670 if (y < 0) {
671 if (bstate & BUTTON3_CLICKED)
672 list_window_first(lw);
673 else
674 list_window_previous_page(lw);
675 return true;
676 }
678 /* if the even occurred below the list window move down */
679 if ((unsigned)y >= lw->length) {
680 if (bstate & BUTTON3_CLICKED)
681 list_window_last(lw);
682 else
683 list_window_next_page(lw);
684 return true;
685 }
687 return false;
688 }
689 #endif