Code

d44fd29329b427965abedef04a113f3445187015
[ncmpc.git] / src / list_window.c
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         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;
118 void
119 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
121         lw->cols = width;
122         lw->rows = height;
124         list_window_check_origin(lw);
127 void
128 list_window_set_length(struct list_window *lw, unsigned length)
130         if (length == lw->length)
131                 return;
133         lw->length = length;
135         list_window_check_selected(lw);
136         list_window_check_origin(lw);
139 void
140 list_window_center(struct list_window *lw, unsigned n)
142         if (n > lw->rows / 2)
143                 lw->start = n - lw->rows / 2;
144         else
145                 lw->start = 0;
147         if (lw->start + lw->rows > lw->length) {
148                 if (lw->rows < lw->length)
149                         lw->start = lw->length - lw->rows;
150                 else
151                         lw->start = 0;
152         }
155 void
156 list_window_set_cursor(struct list_window *lw, unsigned i)
158         lw->range_selection = false;
159         lw->selected = i;
161         list_window_check_selected(lw);
162         list_window_check_origin(lw);
165 void
166 list_window_move_cursor(struct list_window *lw, unsigned n)
168         lw->selected = n;
170         list_window_check_selected(lw);
171         list_window_check_origin(lw);
174 void
175 list_window_fetch_cursor(struct list_window *lw)
177         if (lw->start > 0 &&
178             lw->selected < lw->start + options.scroll_offset)
179                 list_window_move_cursor(lw, lw->start + options.scroll_offset);
180         else if (lw->start + lw->rows < lw->length &&
181                  lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
182                 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
185 void
186 list_window_get_range(const struct list_window *lw,
187                       struct list_window_range *range)
189         if (lw->length == 0) {
190                 /* empty list - no selection */
191                 range->start = 0;
192                 range->end = 0;
193         } else if (lw->range_selection) {
194                 /* a range selection */
195                 if (lw->range_base < lw->selected) {
196                         range->start = lw->range_base;
197                         range->end = lw->selected + 1;
198                 } else {
199                         range->start = lw->selected;
200                         range->end = lw->range_base + 1;
201                 }
202         } else {
203                 /* no range, just the cursor */
204                 range->start = lw->selected;
205                 range->end = lw->selected + 1;
206         }
209 static void
210 list_window_next(struct list_window *lw)
212         if (lw->selected + 1 < lw->length)
213                 list_window_move_cursor(lw, lw->selected + 1);
214         else if (options.list_wrap)
215                 list_window_move_cursor(lw, 0);
218 static void
219 list_window_previous(struct list_window *lw)
221         if (lw->selected > 0)
222                 list_window_move_cursor(lw, lw->selected - 1);
223         else if (options.list_wrap)
224                 list_window_move_cursor(lw, lw->length - 1);
227 static void
228 list_window_top(struct list_window *lw)
230         if (lw->start == 0)
231                 list_window_move_cursor(lw, lw->start);
232         else
233                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
234                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
235                 else
236                         list_window_move_cursor(lw, lw->start + options.scroll_offset);
239 static void
240 list_window_middle(struct list_window *lw)
242         if (lw->length >= lw->rows)
243                 list_window_move_cursor(lw, lw->start + lw->rows / 2);
244         else
245                 list_window_move_cursor(lw, lw->length / 2);
248 static void
249 list_window_bottom(struct list_window *lw)
251         if (lw->length >= lw->rows)
252                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
253                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
254                 else
255                         if (lw->start + lw->rows == lw->length)
256                                 list_window_move_cursor(lw, lw->length - 1);
257                         else
258                                 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
259         else
260                 list_window_move_cursor(lw, lw->length - 1);
263 static void
264 list_window_first(struct list_window *lw)
266         list_window_move_cursor(lw, 0);
269 static void
270 list_window_last(struct list_window *lw)
272         if (lw->length > 0)
273                 list_window_move_cursor(lw, lw->length - 1);
274         else
275                 list_window_move_cursor(lw, 0);
278 static void
279 list_window_next_page(struct list_window *lw)
281         if (lw->rows < 2)
282                 return;
283         if (lw->selected + lw->rows < lw->length)
284                 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
285         else
286                 list_window_last(lw);
289 static void
290 list_window_previous_page(struct list_window *lw)
292         if (lw->rows < 2)
293                 return;
294         if (lw->selected > lw->rows - 1)
295                 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
296         else
297                 list_window_first(lw);
300 static void
301 list_window_scroll_up(struct list_window *lw, unsigned n)
303         if (lw->start > 0) {
304                 if (n > lw->start)
305                         lw->start = 0;
306                 else
307                         lw->start -= n;
309                 list_window_fetch_cursor(lw);
310         }
313 static void
314 list_window_scroll_down(struct list_window *lw, unsigned n)
316         if (lw->start + lw->rows < lw->length)
317         {
318                 if ( lw->start + lw->rows + n > lw->length - 1)
319                         lw->start = lw->length - lw->rows;
320                 else
321                         lw->start += n;
323                 list_window_fetch_cursor(lw);
324         }
327 static void
328 list_window_paint_row(WINDOW *w, unsigned width, bool selected,
329                       const char *text)
331         row_paint_text(w, width, COLOR_LIST,
332                        selected, text);
335 void
336 list_window_paint(const struct list_window *lw,
337                   list_window_callback_fn_t callback,
338                   void *callback_data)
340         bool show_cursor = !lw->hide_cursor &&
341                 (!options.hardware_cursor || lw->range_selection);
342         struct list_window_range range;
344         if (show_cursor)
345                 list_window_get_range(lw, &range);
347         for (unsigned i = 0; i < lw->rows; i++) {
348                 const char *label;
350                 wmove(lw->w, i, 0);
352                 if (lw->start + i >= lw->length) {
353                         wclrtobot(lw->w);
354                         break;
355                 }
357                 label = callback(lw->start + i, callback_data);
358                 assert(label != NULL);
360                 list_window_paint_row(lw->w, lw->cols,
361                                       show_cursor &&
362                                       lw->start + i >= range.start &&
363                                       lw->start + i < range.end,
364                                       label);
365         }
367         row_color_end(lw->w);
369         if (options.hardware_cursor && lw->selected >= lw->start &&
370             lw->selected < lw->start + lw->rows) {
371                 curs_set(1);
372                 wmove(lw->w, lw->selected - lw->start, 0);
373         }
376 void
377 list_window_paint2(const struct list_window *lw,
378                    list_window_paint_callback_t paint_callback,
379                    void *callback_data)
381         bool show_cursor = !lw->hide_cursor &&
382                 (!options.hardware_cursor || lw->range_selection);
383         struct list_window_range range;
385         if (show_cursor)
386                 list_window_get_range(lw, &range);
388         for (unsigned i = 0; i < lw->rows; i++) {
389                 bool selected;
391                 wmove(lw->w, i, 0);
393                 if (lw->start + i >= lw->length) {
394                         wclrtobot(lw->w);
395                         break;
396                 }
398                 selected = show_cursor &&
399                         lw->start + i >= range.start &&
400                         lw->start + i < range.end;
402                 paint_callback(lw->w, lw->start + i, i, lw->cols,
403                                selected, callback_data);
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         }
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)
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;
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)
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;
492 static bool
493 jump_match(const char *haystack, const char *needle)
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);
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)
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;
531 /* perform basic list window commands (movement) */
532 bool
533 list_window_cmd(struct list_window *lw, command_t cmd)
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;
595 bool
596 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
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;
663 #ifdef HAVE_GETMOUSE
664 bool
665 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
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;
689 #endif