Code

2a7db368ced999192e91fd62de6790eaf0965884
[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         assert(str != NULL);
484         do {
485                 while (i < lw->length) {
486                         label = callback(i, &h, NULL, callback_data);
487                         assert(label != NULL);
489                         if (match_line(label, str)) {
490                                 list_window_move_cursor(lw, i);
491                                 return true;
492                         }
493                         if (wrap && i == lw->selected)
494                                 return false;
495                         i++;
496                 }
497                 if (wrap) {
498                         if (i == 0) /* empty list */
499                                 return 1;
500                         i=0; /* first item */
501                         if (bell_on_wrap) {
502                                 screen_bell();
503                         }
504                 }
505         } while (wrap);
507         return false;
510 bool
511 list_window_rfind(struct list_window *lw,
512                   list_window_callback_fn_t callback,
513                   void *callback_data,
514                   const char *str,
515                   bool wrap,
516                   bool bell_on_wrap)
518         bool h;
519         int i = lw->selected - 1;
520         const char *label;
522         assert(str != NULL);
524         if (lw->length == 0)
525                 return false;
527         do {
528                 while (i >= 0) {
529                         label = callback(i, &h, NULL, callback_data);
530                         assert(label != NULL);
532                         if (match_line(label, str)) {
533                                 list_window_move_cursor(lw, i);
534                                 return true;
535                         }
536                         if (wrap && i == (int)lw->selected)
537                                 return false;
538                         i--;
539                 }
540                 if (wrap) {
541                         i = lw->length - 1; /* last item */
542                         if (bell_on_wrap) {
543                                 screen_bell();
544                         }
545                 }
546         } while (wrap);
548         return false;
551 static bool
552 jump_match(const char *haystack, const char *needle)
554 #ifdef NCMPC_MINI
555         bool jump_prefix_only = true;
556 #else
557         bool jump_prefix_only = options.jump_prefix_only;
558 #endif
560         assert(haystack != NULL);
561         assert(needle != NULL);
563         return jump_prefix_only
564                 ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
565                 : match_line(haystack, needle);
568 bool
569 list_window_jump(struct list_window *lw,
570                  list_window_callback_fn_t callback,
571                  void *callback_data,
572                  const char *str)
574         bool h;
575         const char *label;
577         assert(str != NULL);
579         for (unsigned i = 0; i < lw->length; ++i) {
580                 label = callback(i, &h, NULL, callback_data);
581                 assert(label != NULL);
583                 if (label[0] == '[')
584                         label++;
586                 if (jump_match(label, str)) {
587                         list_window_move_cursor(lw, i);
588                         return true;
589                 }
590         }
591         return false;
594 /* perform basic list window commands (movement) */
595 bool
596 list_window_cmd(struct list_window *lw, command_t cmd)
598         switch (cmd) {
599         case CMD_LIST_PREVIOUS:
600                 list_window_previous(lw);
601                 break;
602         case CMD_LIST_NEXT:
603                 list_window_next(lw);
604                 break;
605         case CMD_LIST_TOP:
606                 list_window_top(lw);
607                 break;
608         case CMD_LIST_MIDDLE:
609                 list_window_middle(lw);
610                 break;
611         case CMD_LIST_BOTTOM:
612                 list_window_bottom(lw);
613                 break;
614         case CMD_LIST_FIRST:
615                 list_window_first(lw);
616                 break;
617         case CMD_LIST_LAST:
618                 list_window_last(lw);
619                 break;
620         case CMD_LIST_NEXT_PAGE:
621                 list_window_next_page(lw);
622                 break;
623         case CMD_LIST_PREVIOUS_PAGE:
624                 list_window_previous_page(lw);
625                 break;
626         case CMD_LIST_RANGE_SELECT:
627                 if(lw->range_selection)
628                 {
629                         screen_status_printf(_("Range selection disabled"));
630                         list_window_set_cursor(lw, lw->selected);
631                 }
632                 else
633                 {
634                         screen_status_printf(_("Range selection enabled"));
635                         lw->range_base = lw->selected;
636                         lw->range_selection = true;
637                 }
638                 break;
639         case CMD_LIST_SCROLL_UP_LINE:
640                 list_window_scroll_up(lw, 1);
641                 break;
642         case CMD_LIST_SCROLL_DOWN_LINE:
643                 list_window_scroll_down(lw, 1);
644                 break;
645         case CMD_LIST_SCROLL_UP_HALF:
646                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
647                 break;
648         case CMD_LIST_SCROLL_DOWN_HALF:
649                 list_window_scroll_down(lw, (lw->rows - 1) / 2);
650                 break;
651         default:
652                 return false;
653         }
655         return true;
658 bool
659 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
661         switch (cmd) {
662         case CMD_LIST_SCROLL_UP_LINE:
663         case CMD_LIST_PREVIOUS:
664                 if (lw->start > 0)
665                         lw->start--;
666                 break;
668         case CMD_LIST_SCROLL_DOWN_LINE:
669         case CMD_LIST_NEXT:
670                 if (lw->start + lw->rows < lw->length)
671                         lw->start++;
672                 break;
674         case CMD_LIST_FIRST:
675                 lw->start = 0;
676                 break;
678         case CMD_LIST_LAST:
679                 if (lw->length > lw->rows)
680                         lw->start = lw->length - lw->rows;
681                 else
682                         lw->start = 0;
683                 break;
685         case CMD_LIST_NEXT_PAGE:
686                 lw->start += lw->rows - 1;
687                 if (lw->start + lw->rows > lw->length) {
688                         if (lw->length > lw->rows)
689                                 lw->start = lw->length - lw->rows;
690                         else
691                                 lw->start = 0;
692                 }
693                 break;
695         case CMD_LIST_PREVIOUS_PAGE:
696                 if (lw->start > lw->rows)
697                         lw->start -= lw->rows;
698                 else
699                         lw->start = 0;
700                 break;
702         case CMD_LIST_SCROLL_UP_HALF:
703                 if (lw->start > (lw->rows - 1) / 2)
704                         lw->start -= (lw->rows - 1) / 2;
705                 else
706                         lw->start = 0;
707                 break;
709         case CMD_LIST_SCROLL_DOWN_HALF:
710                 lw->start += (lw->rows - 1) / 2;
711                 if (lw->start + lw->rows > lw->length) {
712                         if (lw->length > lw->rows)
713                                 lw->start = lw->length - lw->rows;
714                         else
715                                 lw->start = 0;
716                 }
717                 break;
719         default:
720                 return false;
721         }
723         return true;
726 #ifdef HAVE_GETMOUSE
727 bool
728 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
730         assert(lw != NULL);
732         /* if the even occurred above the list window move up */
733         if (y < 0) {
734                 if (bstate & BUTTON3_CLICKED)
735                         list_window_first(lw);
736                 else
737                         list_window_previous_page(lw);
738                 return true;
739         }
741         /* if the even occurred below the list window move down */
742         if ((unsigned)y >= lw->length) {
743                 if (bstate & BUTTON3_CLICKED)
744                         list_window_last(lw);
745                 else
746                         list_window_next_page(lw);
747                 return true;
748         }
750         return false;
752 #endif