Code

list_window: moved auto-scrolling code to list_window_check_origin()
[ncmpc.git] / src / list_window.c
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 static void
70 list_window_check_selected(struct list_window *lw)
71 {
72         if (lw->start + lw->rows > lw->length) {
73                 if (lw->length > lw->rows)
74                         lw->start = lw->length - lw->rows;
75                 else
76                         lw->start = 0;
77         }
79         if (lw->selected < lw->start)
80                 lw->selected = lw->start;
82         if (lw->length == 0)
83                 lw->selected = 0;
84         else if (lw->selected >= lw->length)
85                 lw->selected = lw->length - 1;
87         if(lw->range_selection)
88         {
89                 if (lw->length == 0) {
90                         lw->selected_start = 0;
91                         lw->selected_end = 0;
92                         lw->range_base = 0;
93                 } else {
94                         if (lw->selected_start >= lw->length)
95                                 lw->selected_start = lw->length - 1;
96                         if (lw->selected_end >= lw->length)
97                                 lw->selected_end = lw->length - 1;
98                         if (lw->range_base >= lw->length)
99                                 lw->range_base = lw->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         }
114 /**
115  * Scroll after the cursor was moved, the list was changed or the
116  * window was resized.
117  */
118 static void
119 list_window_check_origin(struct list_window *lw)
121         int start = lw->start;
123         if ((unsigned) options.scroll_offset * 2 >= lw->rows)
124                 // Center if the offset is more than half the screen
125                 start = lw->selected - lw->rows / 2;
126         else
127                 {
128                         if (lw->selected < lw->start + options.scroll_offset)
129                                 start = lw->selected - options.scroll_offset;
131                         if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
132                                 {
133                                         start = lw->selected - lw->rows + 1 + options.scroll_offset;
134                                 }
135                 }
137         if (start + lw->rows > lw->length)
138                 start = lw->length - lw->rows;
140         if (start < 0 || lw->length == 0)
141                 start = 0;
143         lw->start = start;
146 void
147 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
149         lw->cols = width;
150         lw->rows = height;
152         list_window_check_origin(lw);
155 void
156 list_window_set_length(struct list_window *lw, unsigned length)
158         lw->length = length;
160         list_window_check_selected(lw);
161         list_window_check_origin(lw);
164 void
165 list_window_center(struct list_window *lw, unsigned n)
167         if (n > lw->rows / 2)
168                 lw->start = n - lw->rows / 2;
169         else
170                 lw->start = 0;
172         if (lw->start + lw->rows > lw->length) {
173                 if (lw->rows < lw->length)
174                         lw->start = lw->length - lw->rows;
175                 else
176                         lw->start = 0;
177         }
180 void
181 list_window_set_cursor(struct list_window *lw, unsigned i)
183         lw->range_selection = false;
184         lw->selected = i;
185         lw->selected_start = i;
186         lw->selected_end = i;
188         list_window_check_selected(lw);
189         list_window_check_origin(lw);
192 void
193 list_window_move_cursor(struct list_window *lw, unsigned n)
195         lw->selected = n;
196         if(lw->range_selection)
197         {
198                 if(n >= lw->range_base)
199                 {
200                         lw->selected_end = n;
201                         lw->selected_start = lw->range_base;
202                 }
203                 if(n <= lw->range_base)
204                 {
205                         lw->selected_start = n;
206                         lw->selected_end = lw->range_base;
207                 }
208         }
209         else
210         {
211                 lw->selected_start = n;
212                 lw->selected_end = n;
213         }
215         list_window_check_selected(lw);
216         list_window_check_origin(lw);
219 void
220 list_window_fetch_cursor(struct list_window *lw)
222         if (lw->selected < lw->start + options.scroll_offset) {
223                 if (lw->start > 0)
224                         lw->selected = lw->start + options.scroll_offset;
225                 if (lw->range_selection) {
226                         if (lw->selected > lw->range_base) {
227                                 lw->selected_end = lw->selected;
228                                 lw->selected_start = lw->range_base;
229                         } else {
230                                 lw->selected_start = lw->selected;
231                         }
232                 } else {
233                         lw->selected_start = lw->selected;
234                         lw->selected_end = lw->selected;
235                 }
236         } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
237                 if (lw->start + lw->rows < lw->length)
238                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
239                 if (lw->range_selection) {
240                         if (lw->selected < lw->range_base) {
241                                 lw->selected_start = lw->selected;
242                                 lw->selected_end = lw->range_base;
243                         } else {
244                                 lw->selected_end = lw->selected;
245                         }
246                 } else {
247                         lw->selected_start = lw->selected;
248                         lw->selected_end = lw->selected;
249                 }
250         }
253 static void
254 list_window_next(struct list_window *lw)
256         if (lw->selected + 1 < lw->length)
257                 list_window_move_cursor(lw, lw->selected + 1);
258         else if (options.list_wrap)
259                 list_window_move_cursor(lw, 0);
262 static void
263 list_window_previous(struct list_window *lw)
265         if (lw->selected > 0)
266                 list_window_move_cursor(lw, lw->selected - 1);
267         else if (options.list_wrap)
268                 list_window_move_cursor(lw, lw->length - 1);
271 static void
272 list_window_top(struct list_window *lw)
274         if (lw->start == 0)
275                 list_window_move_cursor(lw, lw->start);
276         else
277                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
278                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
279                 else
280                         list_window_move_cursor(lw, lw->start + options.scroll_offset);
283 static void
284 list_window_middle(struct list_window *lw)
286         if (lw->length >= lw->rows)
287                 list_window_move_cursor(lw, lw->start + lw->rows / 2);
288         else
289                 list_window_move_cursor(lw, lw->length / 2);
292 static void
293 list_window_bottom(struct list_window *lw)
295         if (lw->length >= lw->rows)
296                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
297                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
298                 else
299                         if (lw->start + lw->rows == lw->length)
300                                 list_window_move_cursor(lw, lw->length - 1);
301                         else
302                                 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
303         else
304                 list_window_move_cursor(lw, lw->length - 1);
307 static void
308 list_window_first(struct list_window *lw)
310         list_window_move_cursor(lw, 0);
313 static void
314 list_window_last(struct list_window *lw)
316         if (lw->length > 0)
317                 list_window_move_cursor(lw, lw->length - 1);
318         else
319                 list_window_move_cursor(lw, 0);
322 static void
323 list_window_next_page(struct list_window *lw)
325         if (lw->rows < 2)
326                 return;
327         if (lw->selected + lw->rows < lw->length)
328                 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
329         else
330                 list_window_last(lw);
333 static void
334 list_window_previous_page(struct list_window *lw)
336         if (lw->rows < 2)
337                 return;
338         if (lw->selected > lw->rows - 1)
339                 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
340         else
341                 list_window_first(lw);
344 static void
345 list_window_scroll_up(struct list_window *lw, unsigned n)
347         if (lw->start > 0) {
348                 if (n > lw->start)
349                         lw->start = 0;
350                 else
351                         lw->start -= n;
353                 list_window_fetch_cursor(lw);
354         }
357 static void
358 list_window_scroll_down(struct list_window *lw, unsigned n)
360         if (lw->start + lw->rows < lw->length)
361         {
362                 if ( lw->start + lw->rows + n > lw->length - 1)
363                         lw->start = lw->length - lw->rows;
364                 else
365                         lw->start += n;
367                 list_window_fetch_cursor(lw);
368         }
371 static void
372 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
373                       bool selected, bool highlight,
374                       const char *text, const char *second_column)
376         unsigned text_width = utf8_width(text);
377         unsigned second_column_width;
379 #ifdef NCMPC_MINI
380         second_column = NULL;
381 #endif /* NCMPC_MINI */
383         if (second_column != NULL) {
384                 second_column_width = utf8_width(second_column) + 1;
385                 if (second_column_width < width)
386                         width -= second_column_width;
387                 else
388                         second_column_width = 0;
389         } else
390                 second_column_width = 0;
392         if (highlight)
393                 colors_use(w, COLOR_LIST_BOLD);
394         else
395                 colors_use(w, COLOR_LIST);
397         if (selected)
398                 wattron(w, A_REVERSE);
400         waddstr(w, text);
401         if (options.wide_cursor && text_width < width)
402                 whline(w, ' ', width - text_width);
404         if (second_column_width > 0) {
405                 wmove(w, y, width);
406                 waddch(w, ' ');
407                 waddstr(w, second_column);
408         }
410         if (selected)
411                 wattroff(w, A_REVERSE);
413         if (!options.wide_cursor && text_width < width) {
414                 if (second_column_width == 0)
415                         /* the cursor is at the end of the text; clear
416                            the rest of this row */
417                         wclrtoeol(w);
418                 else
419                         /* there's a second column: clear the space
420                            between the first and the second column */
421                         mvwhline(w, y, text_width, ' ', width - text_width);
422         }
425 void
426 list_window_paint(const struct list_window *lw,
427                   list_window_callback_fn_t callback,
428                   void *callback_data)
430         unsigned i;
431         bool show_cursor = !lw->hide_cursor;
432         bool highlight = false;
434         show_cursor = show_cursor &&
435                 (!options.hardware_cursor || lw->range_selection);
437         for (i = 0; i < lw->rows; i++) {
438                 const char *label;
439                 char *second_column = NULL;
440                 highlight = false;
442                 wmove(lw->w, i, 0);
444                 if (lw->start + i >= lw->length) {
445                         wclrtobot(lw->w);
446                         break;
447                 }
449                 label = callback(lw->start + i, &highlight, &second_column, callback_data);
450                 assert(label != NULL);
452                 list_window_paint_row(lw->w, i, lw->cols,
453                                       show_cursor &&
454                                       lw->start + i >= lw->selected_start &&
455                                       lw->start + i <= lw->selected_end,
456                                       highlight,
457                                       label, second_column);
459                 if (second_column != NULL)
460                         g_free(second_column);
461         }
463         if (options.hardware_cursor && lw->selected >= lw->start &&
464             lw->selected < lw->start + lw->rows) {
465                 curs_set(1);
466                 wmove(lw->w, lw->selected - lw->start, 0);
467         }
470 bool
471 list_window_find(struct list_window *lw,
472                  list_window_callback_fn_t callback,
473                  void *callback_data,
474                  const char *str,
475                  bool wrap,
476                  bool bell_on_wrap)
478         bool h;
479         unsigned i = lw->selected + 1;
480         const char *label;
482         do {
483                 while (i < lw->length) {
484                         label = callback(i, &h, NULL, callback_data);
485                         assert(label != NULL);
487                         if (str && label && match_line(label, str)) {
488                                 list_window_move_cursor(lw, i);
489                                 return true;
490                         }
491                         if (wrap && i == lw->selected)
492                                 return false;
493                         i++;
494                 }
495                 if (wrap) {
496                         if (i == 0) /* empty list */
497                                 return 1;
498                         i=0; /* first item */
499                         if (bell_on_wrap) {
500                                 screen_bell();
501                         }
502                 }
503         } while (wrap);
505         return false;
508 bool
509 list_window_rfind(struct list_window *lw,
510                   list_window_callback_fn_t callback,
511                   void *callback_data,
512                   const char *str,
513                   bool wrap,
514                   bool bell_on_wrap)
516         bool h;
517         int i = lw->selected - 1;
518         const char *label;
520         if (lw->length == 0)
521                 return false;
523         do {
524                 while (i >= 0) {
525                         label = callback(i, &h, NULL, callback_data);
526                         assert(label != NULL);
528                         if( str && label && match_line(label, str) ) {
529                                 list_window_move_cursor(lw, i);
530                                 return true;
531                         }
532                         if (wrap && i == (int)lw->selected)
533                                 return false;
534                         i--;
535                 }
536                 if (wrap) {
537                         i = lw->length - 1; /* last item */
538                         if (bell_on_wrap) {
539                                 screen_bell();
540                         }
541                 }
542         } while (wrap);
544         return false;
547 bool
548 list_window_jump(struct list_window *lw,
549                  list_window_callback_fn_t callback,
550                  void *callback_data,
551                  const char *str)
553         bool h;
554         const char *label;
556         for (unsigned i = 0; i < lw->length; ++i) {
557                 label = callback(i, &h, NULL, callback_data);
558                 assert(label != NULL);
560                 if (label[0] == '[')
561                         label++;
562 #ifndef NCMPC_MINI
563                 if (str && label &&
564                                 ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
565                                  (!options.jump_prefix_only && match_line(label, str))) )
566 #else
567                 if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
568 #endif
569                 {
570                         list_window_move_cursor(lw, i);
571                         return true;
572                 }
573         }
574         return false;
577 /* perform basic list window commands (movement) */
578 bool
579 list_window_cmd(struct list_window *lw, command_t cmd)
581         switch (cmd) {
582         case CMD_LIST_PREVIOUS:
583                 list_window_previous(lw);
584                 break;
585         case CMD_LIST_NEXT:
586                 list_window_next(lw);
587                 break;
588         case CMD_LIST_TOP:
589                 list_window_top(lw);
590                 break;
591         case CMD_LIST_MIDDLE:
592                 list_window_middle(lw);
593                 break;
594         case CMD_LIST_BOTTOM:
595                 list_window_bottom(lw);
596                 break;
597         case CMD_LIST_FIRST:
598                 list_window_first(lw);
599                 break;
600         case CMD_LIST_LAST:
601                 list_window_last(lw);
602                 break;
603         case CMD_LIST_NEXT_PAGE:
604                 list_window_next_page(lw);
605                 break;
606         case CMD_LIST_PREVIOUS_PAGE:
607                 list_window_previous_page(lw);
608                 break;
609         case CMD_LIST_RANGE_SELECT:
610                 if(lw->range_selection)
611                 {
612                         screen_status_printf(_("Range selection disabled"));
613                         list_window_set_cursor(lw, lw->selected);
614                 }
615                 else
616                 {
617                         screen_status_printf(_("Range selection enabled"));
618                         lw->range_base = lw->selected;
619                         lw->range_selection = true;
620                 }
621                 break;
622         case CMD_LIST_SCROLL_UP_LINE:
623                 list_window_scroll_up(lw, 1);
624                 break;
625         case CMD_LIST_SCROLL_DOWN_LINE:
626                 list_window_scroll_down(lw, 1);
627                 break;
628         case CMD_LIST_SCROLL_UP_HALF:
629                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
630                 break;
631         case CMD_LIST_SCROLL_DOWN_HALF:
632                 list_window_scroll_down(lw, (lw->rows - 1) / 2);
633                 break;
634         default:
635                 return false;
636         }
638         return true;
641 bool
642 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
644         switch (cmd) {
645         case CMD_LIST_SCROLL_UP_LINE:
646         case CMD_LIST_PREVIOUS:
647                 if (lw->start > 0)
648                         lw->start--;
649                 break;
651         case CMD_LIST_SCROLL_DOWN_LINE:
652         case CMD_LIST_NEXT:
653                 if (lw->start + lw->rows < lw->length)
654                         lw->start++;
655                 break;
657         case CMD_LIST_FIRST:
658                 lw->start = 0;
659                 break;
661         case CMD_LIST_LAST:
662                 if (lw->length > lw->rows)
663                         lw->start = lw->length - lw->rows;
664                 else
665                         lw->start = 0;
666                 break;
668         case CMD_LIST_NEXT_PAGE:
669                 lw->start += lw->rows - 1;
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         case CMD_LIST_PREVIOUS_PAGE:
679                 if (lw->start > lw->rows)
680                         lw->start -= lw->rows;
681                 else
682                         lw->start = 0;
683                 break;
685         case CMD_LIST_SCROLL_UP_HALF:
686                 if (lw->start > (lw->rows - 1) / 2)
687                         lw->start -= (lw->rows - 1) / 2;
688                 else
689                         lw->start = 0;
690                 break;
692         case CMD_LIST_SCROLL_DOWN_HALF:
693                 lw->start += (lw->rows - 1) / 2;
694                 if (lw->start + lw->rows > lw->length) {
695                         if (lw->length > lw->rows)
696                                 lw->start = lw->length - lw->rows;
697                         else
698                                 lw->start = 0;
699                 }
700                 break;
702         default:
703                 return false;
704         }
706         return true;
709 #ifdef HAVE_GETMOUSE
710 bool
711 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
713         assert(lw != NULL);
715         /* if the even occurred above the list window move up */
716         if (y < 0) {
717                 if (bstate & BUTTON3_CLICKED)
718                         list_window_first(lw);
719                 else
720                         list_window_previous_page(lw);
721                 return true;
722         }
724         /* if the even occurred below the list window move down */
725         if ((unsigned)y >= lw->length) {
726                 if (bstate & BUTTON3_CLICKED)
727                         list_window_last(lw);
728                 else
729                         list_window_next_page(lw);
730                 return true;
731         }
733         return false;
735 #endif