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