Code

list_window: moved painting utilities to paint.h
[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 "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         lw->length = length;
132         list_window_check_selected(lw);
133         list_window_check_origin(lw);
136 void
137 list_window_center(struct list_window *lw, unsigned n)
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         }
152 void
153 list_window_set_cursor(struct list_window *lw, unsigned i)
155         lw->range_selection = false;
156         lw->selected = i;
158         list_window_check_selected(lw);
159         list_window_check_origin(lw);
162 void
163 list_window_move_cursor(struct list_window *lw, unsigned n)
165         lw->selected = n;
167         list_window_check_selected(lw);
168         list_window_check_origin(lw);
171 void
172 list_window_fetch_cursor(struct list_window *lw)
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);
182 void
183 list_window_get_range(const struct list_window *lw,
184                       struct list_window_range *range)
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         }
206 static void
207 list_window_next(struct list_window *lw)
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);
215 static void
216 list_window_previous(struct list_window *lw)
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);
224 static void
225 list_window_top(struct list_window *lw)
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);
236 static void
237 list_window_middle(struct list_window *lw)
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);
245 static void
246 list_window_bottom(struct list_window *lw)
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);
260 static void
261 list_window_first(struct list_window *lw)
263         list_window_move_cursor(lw, 0);
266 static void
267 list_window_last(struct list_window *lw)
269         if (lw->length > 0)
270                 list_window_move_cursor(lw, lw->length - 1);
271         else
272                 list_window_move_cursor(lw, 0);
275 static void
276 list_window_next_page(struct list_window *lw)
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);
286 static void
287 list_window_previous_page(struct list_window *lw)
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);
297 static void
298 list_window_scroll_up(struct list_window *lw, unsigned n)
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         }
310 static void
311 list_window_scroll_down(struct list_window *lw, unsigned n)
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         }
324 static void
325 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
326                       bool selected, bool highlight,
327                       const char *text, const char *second_column)
329         unsigned second_column_width;
331 #ifdef NCMPC_MINI
332         second_column = NULL;
333         highlight = false;
334 #endif /* NCMPC_MINI */
336         if (second_column != NULL) {
337                 second_column_width = utf8_width(second_column) + 1;
338                 if (second_column_width < width)
339                         width -= second_column_width;
340                 else
341                         second_column_width = 0;
342         } else
343                 second_column_width = 0;
345         row_color(w, highlight ? COLOR_LIST_BOLD : COLOR_LIST, selected);
347         waddstr(w, text);
349         /* erase the unused space after the text */
350         row_clear_to_eol(w, width, selected);
352         if (second_column_width > 0) {
353                 wmove(w, y, width);
354                 waddch(w, ' ');
355                 waddstr(w, second_column);
356         }
359 void
360 list_window_paint(const struct list_window *lw,
361                   list_window_callback_fn_t callback,
362                   void *callback_data)
364         bool show_cursor = !lw->hide_cursor &&
365                 (!options.hardware_cursor || lw->range_selection);
366         struct list_window_range range;
368         if (show_cursor)
369                 list_window_get_range(lw, &range);
371         for (unsigned i = 0; i < lw->rows; i++) {
372                 const char *label;
373                 bool highlight = false;
374                 char *second_column = NULL;
376                 wmove(lw->w, i, 0);
378                 if (lw->start + i >= lw->length) {
379                         wclrtobot(lw->w);
380                         break;
381                 }
383                 label = callback(lw->start + i, &highlight, &second_column, callback_data);
384                 assert(label != NULL);
386 #ifdef NCMPC_MINI
387                 highlight = false;
388                 second_column = NULL;
389 #endif /* NCMPC_MINI */
391                 list_window_paint_row(lw->w, i, lw->cols,
392                                       show_cursor &&
393                                       lw->start + i >= range.start &&
394                                       lw->start + i < range.end,
395                                       highlight,
396                                       label, second_column);
398                 if (second_column != NULL)
399                         g_free(second_column);
400         }
402         row_color_end(lw->w);
404         if (options.hardware_cursor && lw->selected >= lw->start &&
405             lw->selected < lw->start + lw->rows) {
406                 curs_set(1);
407                 wmove(lw->w, lw->selected - lw->start, 0);
408         }
411 bool
412 list_window_find(struct list_window *lw,
413                  list_window_callback_fn_t callback,
414                  void *callback_data,
415                  const char *str,
416                  bool wrap,
417                  bool bell_on_wrap)
419         bool h;
420         unsigned i = lw->selected + 1;
421         const char *label;
423         assert(str != NULL);
425         do {
426                 while (i < lw->length) {
427                         label = callback(i, &h, NULL, callback_data);
428                         assert(label != NULL);
430                         if (match_line(label, str)) {
431                                 list_window_move_cursor(lw, i);
432                                 return true;
433                         }
434                         if (wrap && i == lw->selected)
435                                 return false;
436                         i++;
437                 }
438                 if (wrap) {
439                         if (i == 0) /* empty list */
440                                 return 1;
441                         i=0; /* first item */
442                         if (bell_on_wrap) {
443                                 screen_bell();
444                         }
445                 }
446         } while (wrap);
448         return false;
451 bool
452 list_window_rfind(struct list_window *lw,
453                   list_window_callback_fn_t callback,
454                   void *callback_data,
455                   const char *str,
456                   bool wrap,
457                   bool bell_on_wrap)
459         bool h;
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, &h, NULL, 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         bool h;
516         const char *label;
518         assert(str != NULL);
520         for (unsigned i = 0; i < lw->length; ++i) {
521                 label = callback(i, &h, NULL, callback_data);
522                 assert(label != NULL);
524                 if (label[0] == '[')
525                         label++;
527                 if (jump_match(label, str)) {
528                         list_window_move_cursor(lw, i);
529                         return true;
530                 }
531         }
532         return false;
535 /* perform basic list window commands (movement) */
536 bool
537 list_window_cmd(struct list_window *lw, command_t cmd)
539         switch (cmd) {
540         case CMD_LIST_PREVIOUS:
541                 list_window_previous(lw);
542                 break;
543         case CMD_LIST_NEXT:
544                 list_window_next(lw);
545                 break;
546         case CMD_LIST_TOP:
547                 list_window_top(lw);
548                 break;
549         case CMD_LIST_MIDDLE:
550                 list_window_middle(lw);
551                 break;
552         case CMD_LIST_BOTTOM:
553                 list_window_bottom(lw);
554                 break;
555         case CMD_LIST_FIRST:
556                 list_window_first(lw);
557                 break;
558         case CMD_LIST_LAST:
559                 list_window_last(lw);
560                 break;
561         case CMD_LIST_NEXT_PAGE:
562                 list_window_next_page(lw);
563                 break;
564         case CMD_LIST_PREVIOUS_PAGE:
565                 list_window_previous_page(lw);
566                 break;
567         case CMD_LIST_RANGE_SELECT:
568                 if(lw->range_selection)
569                 {
570                         screen_status_printf(_("Range selection disabled"));
571                         list_window_set_cursor(lw, lw->selected);
572                 }
573                 else
574                 {
575                         screen_status_printf(_("Range selection enabled"));
576                         lw->range_base = lw->selected;
577                         lw->range_selection = true;
578                 }
579                 break;
580         case CMD_LIST_SCROLL_UP_LINE:
581                 list_window_scroll_up(lw, 1);
582                 break;
583         case CMD_LIST_SCROLL_DOWN_LINE:
584                 list_window_scroll_down(lw, 1);
585                 break;
586         case CMD_LIST_SCROLL_UP_HALF:
587                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
588                 break;
589         case CMD_LIST_SCROLL_DOWN_HALF:
590                 list_window_scroll_down(lw, (lw->rows - 1) / 2);
591                 break;
592         default:
593                 return false;
594         }
596         return true;
599 bool
600 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
602         switch (cmd) {
603         case CMD_LIST_SCROLL_UP_LINE:
604         case CMD_LIST_PREVIOUS:
605                 if (lw->start > 0)
606                         lw->start--;
607                 break;
609         case CMD_LIST_SCROLL_DOWN_LINE:
610         case CMD_LIST_NEXT:
611                 if (lw->start + lw->rows < lw->length)
612                         lw->start++;
613                 break;
615         case CMD_LIST_FIRST:
616                 lw->start = 0;
617                 break;
619         case CMD_LIST_LAST:
620                 if (lw->length > lw->rows)
621                         lw->start = lw->length - lw->rows;
622                 else
623                         lw->start = 0;
624                 break;
626         case CMD_LIST_NEXT_PAGE:
627                 lw->start += lw->rows - 1;
628                 if (lw->start + lw->rows > lw->length) {
629                         if (lw->length > lw->rows)
630                                 lw->start = lw->length - lw->rows;
631                         else
632                                 lw->start = 0;
633                 }
634                 break;
636         case CMD_LIST_PREVIOUS_PAGE:
637                 if (lw->start > lw->rows)
638                         lw->start -= lw->rows;
639                 else
640                         lw->start = 0;
641                 break;
643         case CMD_LIST_SCROLL_UP_HALF:
644                 if (lw->start > (lw->rows - 1) / 2)
645                         lw->start -= (lw->rows - 1) / 2;
646                 else
647                         lw->start = 0;
648                 break;
650         case CMD_LIST_SCROLL_DOWN_HALF:
651                 lw->start += (lw->rows - 1) / 2;
652                 if (lw->start + lw->rows > lw->length) {
653                         if (lw->length > lw->rows)
654                                 lw->start = lw->length - lw->rows;
655                         else
656                                 lw->start = 0;
657                 }
658                 break;
660         default:
661                 return false;
662         }
664         return true;
667 #ifdef HAVE_GETMOUSE
668 bool
669 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
671         assert(lw != NULL);
673         /* if the even occurred above the list window move up */
674         if (y < 0) {
675                 if (bstate & BUTTON3_CLICKED)
676                         list_window_first(lw);
677                 else
678                         list_window_previous_page(lw);
679                 return true;
680         }
682         /* if the even occurred below the list window move down */
683         if ((unsigned)y >= lw->length) {
684                 if (bstate & BUTTON3_CLICKED)
685                         list_window_last(lw);
686                 else
687                         list_window_next_page(lw);
688                 return true;
689         }
691         return false;
693 #endif