Code

release v0.29
[ncmpc.git] / src / list_window.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_status.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)
102         lw->cols = width;
103         lw->rows = height;
105         list_window_check_origin(lw);
108 void
109 list_window_set_length(struct list_window *lw, unsigned length)
111         if (length == lw->length)
112                 return;
114         lw->length = length;
116         list_window_check_selected(lw);
117         list_window_check_origin(lw);
120 void
121 list_window_center(struct list_window *lw, unsigned n)
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         }
136 void
137 list_window_scroll_to(struct list_window *lw, unsigned n)
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;
161 void
162 list_window_set_cursor(struct list_window *lw, unsigned i)
164         lw->range_selection = false;
165         lw->selected = i;
167         list_window_check_selected(lw);
168         list_window_check_origin(lw);
171 void
172 list_window_move_cursor(struct list_window *lw, unsigned n)
174         lw->selected = n;
176         list_window_check_selected(lw);
177         list_window_check_origin(lw);
180 void
181 list_window_fetch_cursor(struct list_window *lw)
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);
191 void
192 list_window_get_range(const struct list_window *lw,
193                       struct list_window_range *range)
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         }
215 static void
216 list_window_next(struct list_window *lw)
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);
224 static void
225 list_window_previous(struct list_window *lw)
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);
233 static void
234 list_window_top(struct list_window *lw)
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);
245 static void
246 list_window_middle(struct list_window *lw)
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);
254 static void
255 list_window_bottom(struct list_window *lw)
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);
269 static void
270 list_window_first(struct list_window *lw)
272         list_window_move_cursor(lw, 0);
275 static void
276 list_window_last(struct list_window *lw)
278         if (lw->length > 0)
279                 list_window_move_cursor(lw, lw->length - 1);
280         else
281                 list_window_move_cursor(lw, 0);
284 static void
285 list_window_next_page(struct list_window *lw)
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);
295 static void
296 list_window_previous_page(struct list_window *lw)
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);
306 static void
307 list_window_scroll_up(struct list_window *lw, unsigned n)
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         }
319 static void
320 list_window_scroll_down(struct list_window *lw, unsigned n)
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         }
333 static void
334 list_window_paint_row(WINDOW *w, unsigned width, bool selected,
335                       const char *text)
337         row_paint_text(w, width, COLOR_LIST,
338                        selected, text);
341 void
342 list_window_paint(const struct list_window *lw,
343                   list_window_callback_fn_t callback,
344                   void *callback_data)
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                 wmove(lw->w, i, 0);
356                 if (lw->start + i >= lw->length) {
357                         wclrtobot(lw->w);
358                         break;
359                 }
361                 const char *label = callback(lw->start + i, callback_data);
362                 assert(label != NULL);
364                 list_window_paint_row(lw->w, lw->cols,
365                                       show_cursor &&
366                                       lw->start + i >= range.start &&
367                                       lw->start + i < range.end,
368                                       label);
369         }
371         row_color_end(lw->w);
373         if (options.hardware_cursor && lw->selected >= lw->start &&
374             lw->selected < lw->start + lw->rows) {
375                 curs_set(1);
376                 wmove(lw->w, lw->selected - lw->start, 0);
377         }
380 void
381 list_window_paint2(const struct list_window *lw,
382                    list_window_paint_callback_t paint_callback,
383                    const void *callback_data)
385         bool show_cursor = !lw->hide_cursor &&
386                 (!options.hardware_cursor || lw->range_selection);
387         struct list_window_range range;
389         if (show_cursor)
390                 list_window_get_range(lw, &range);
392         for (unsigned i = 0; i < lw->rows; i++) {
393                 wmove(lw->w, i, 0);
395                 if (lw->start + i >= lw->length) {
396                         wclrtobot(lw->w);
397                         break;
398                 }
400                 bool selected = show_cursor &&
401                         lw->start + i >= range.start &&
402                         lw->start + i < range.end;
404                 paint_callback(lw->w, lw->start + i, i, lw->cols,
405                                selected, callback_data);
406         }
408         if (options.hardware_cursor && lw->selected >= lw->start &&
409             lw->selected < lw->start + lw->rows) {
410                 curs_set(1);
411                 wmove(lw->w, lw->selected - lw->start, 0);
412         }
415 bool
416 list_window_find(struct list_window *lw,
417                  list_window_callback_fn_t callback,
418                  void *callback_data,
419                  const char *str,
420                  bool wrap,
421                  bool bell_on_wrap)
423         unsigned i = lw->selected + 1;
425         assert(str != NULL);
427         do {
428                 while (i < lw->length) {
429                         const char *label = callback(i, callback_data);
430                         assert(label != NULL);
432                         if (match_line(label, str)) {
433                                 list_window_move_cursor(lw, i);
434                                 return true;
435                         }
436                         if (wrap && i == lw->selected)
437                                 return false;
438                         i++;
439                 }
440                 if (wrap) {
441                         if (i == 0) /* empty list */
442                                 return 1;
443                         i=0; /* first item */
444                         if (bell_on_wrap) {
445                                 screen_bell();
446                         }
447                 }
448         } while (wrap);
450         return false;
453 bool
454 list_window_rfind(struct list_window *lw,
455                   list_window_callback_fn_t callback,
456                   void *callback_data,
457                   const char *str,
458                   bool wrap,
459                   bool bell_on_wrap)
461         int i = lw->selected - 1;
463         assert(str != NULL);
465         if (lw->length == 0)
466                 return false;
468         do {
469                 while (i >= 0) {
470                         const char *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;
492 #ifdef NCMPC_MINI
493 bool
494 list_window_jump(struct list_window *lw,
495                  list_window_callback_fn_t callback,
496                  void *callback_data,
497                  const char *str)
499         assert(str != NULL);
501         for (unsigned i = 0; i < lw->length; i++) {
502                 const char *label = callback(i, callback_data);
503                 assert(label != NULL);
505                 if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
506                         list_window_move_cursor(lw, i);
507                         return true;
508                 }
509         }
510         return false;
512 #else
513 bool
514 list_window_jump(struct list_window *lw,
515                  list_window_callback_fn_t callback,
516                  void *callback_data,
517                  const char *str)
519         assert(str != NULL);
521         GRegex *regex = compile_regex(str, options.jump_prefix_only);
522         if (regex == NULL)
523                 return false;
525         for (unsigned i = 0; i < lw->length; i++) {
526                 const char *label = callback(i, callback_data);
527                 assert(label != NULL);
529                 if (match_regex(regex, label)) {
530                         g_regex_unref(regex);
531                         list_window_move_cursor(lw, i);
532                         return true;
533                 }
534         }
535         g_regex_unref(regex);
536         return false;
538 #endif
540 /* perform basic list window commands (movement) */
541 bool
542 list_window_cmd(struct list_window *lw, command_t cmd)
544         switch (cmd) {
545         case CMD_LIST_PREVIOUS:
546                 list_window_previous(lw);
547                 break;
548         case CMD_LIST_NEXT:
549                 list_window_next(lw);
550                 break;
551         case CMD_LIST_TOP:
552                 list_window_top(lw);
553                 break;
554         case CMD_LIST_MIDDLE:
555                 list_window_middle(lw);
556                 break;
557         case CMD_LIST_BOTTOM:
558                 list_window_bottom(lw);
559                 break;
560         case CMD_LIST_FIRST:
561                 list_window_first(lw);
562                 break;
563         case CMD_LIST_LAST:
564                 list_window_last(lw);
565                 break;
566         case CMD_LIST_NEXT_PAGE:
567                 list_window_next_page(lw);
568                 break;
569         case CMD_LIST_PREVIOUS_PAGE:
570                 list_window_previous_page(lw);
571                 break;
572         case CMD_LIST_RANGE_SELECT:
573                 if(lw->range_selection)
574                 {
575                         screen_status_printf(_("Range selection disabled"));
576                         list_window_set_cursor(lw, lw->selected);
577                 }
578                 else
579                 {
580                         screen_status_printf(_("Range selection enabled"));
581                         lw->range_base = lw->selected;
582                         lw->range_selection = true;
583                 }
584                 break;
585         case CMD_LIST_SCROLL_UP_LINE:
586                 list_window_scroll_up(lw, 1);
587                 break;
588         case CMD_LIST_SCROLL_DOWN_LINE:
589                 list_window_scroll_down(lw, 1);
590                 break;
591         case CMD_LIST_SCROLL_UP_HALF:
592                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
593                 break;
594         case CMD_LIST_SCROLL_DOWN_HALF:
595                 list_window_scroll_down(lw, (lw->rows - 1) / 2);
596                 break;
597         default:
598                 return false;
599         }
601         return true;
604 bool
605 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
607         switch (cmd) {
608         case CMD_LIST_SCROLL_UP_LINE:
609         case CMD_LIST_PREVIOUS:
610                 if (lw->start > 0)
611                         lw->start--;
612                 break;
614         case CMD_LIST_SCROLL_DOWN_LINE:
615         case CMD_LIST_NEXT:
616                 if (lw->start + lw->rows < lw->length)
617                         lw->start++;
618                 break;
620         case CMD_LIST_FIRST:
621                 lw->start = 0;
622                 break;
624         case CMD_LIST_LAST:
625                 if (lw->length > lw->rows)
626                         lw->start = lw->length - lw->rows;
627                 else
628                         lw->start = 0;
629                 break;
631         case CMD_LIST_NEXT_PAGE:
632                 lw->start += lw->rows;
633                 if (lw->start + lw->rows > lw->length) {
634                         if (lw->length > lw->rows)
635                                 lw->start = lw->length - lw->rows;
636                         else
637                                 lw->start = 0;
638                 }
639                 break;
641         case CMD_LIST_PREVIOUS_PAGE:
642                 if (lw->start > lw->rows)
643                         lw->start -= lw->rows;
644                 else
645                         lw->start = 0;
646                 break;
648         case CMD_LIST_SCROLL_UP_HALF:
649                 if (lw->start > (lw->rows - 1) / 2)
650                         lw->start -= (lw->rows - 1) / 2;
651                 else
652                         lw->start = 0;
653                 break;
655         case CMD_LIST_SCROLL_DOWN_HALF:
656                 lw->start += (lw->rows - 1) / 2;
657                 if (lw->start + lw->rows > lw->length) {
658                         if (lw->length > lw->rows)
659                                 lw->start = lw->length - lw->rows;
660                         else
661                                 lw->start = 0;
662                 }
663                 break;
665         default:
666                 return false;
667         }
669         return true;
672 #ifdef HAVE_GETMOUSE
673 bool
674 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
676         assert(lw != NULL);
678         /* if the even occurred above the list window move up */
679         if (y < 0) {
680                 if (bstate & BUTTON3_CLICKED)
681                         list_window_first(lw);
682                 else
683                         list_window_previous_page(lw);
684                 return true;
685         }
687         /* if the even occurred below the list window move down */
688         if ((unsigned)y >= lw->length) {
689                 if (bstate & BUTTON3_CLICKED)
690                         list_window_last(lw);
691                 else
692                         list_window_next_page(lw);
693                 return true;
694         }
696         return false;
698 #endif