Code

3faf437f791a9b305d303c0139a28ec945a4ac5e
[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 void
70 list_window_check_selected(struct list_window *lw, unsigned length)
71 {
72         if (lw->start + lw->rows > length) {
73                 if (length > lw->rows)
74                         lw->start = length - lw->rows;
75                 else
76                         lw->start = 0;
77         }
79         if (lw->selected < lw->start)
80                 lw->selected = lw->start;
82         if (length == 0)
83                 lw->selected = 0;
84         else if (lw->selected >= length)
85                 lw->selected = length - 1;
87         if(lw->range_selection)
88         {
89                 if (length == 0) {
90                         lw->selected_start = 0;
91                         lw->selected_end = 0;
92                         lw->range_base = 0;
93                 } else {
94                         if (lw->selected_start >= length)
95                                 lw->selected_start = length - 1;
96                         if (lw->selected_end >= length)
97                                 lw->selected_end = length - 1;
98                         if (lw->range_base >= length)
99                                 lw->range_base = 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 void
115 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
117         if (n > lw->rows / 2)
118                 lw->start = n - lw->rows / 2;
119         else
120                 lw->start = 0;
122         if (lw->start + lw->rows > rows) {
123                 if (lw->rows < rows)
124                         lw->start = rows - lw->rows;
125                 else
126                         lw->start = 0;
127         }
130 void
131 list_window_set_selected(struct list_window *lw, unsigned n)
133         lw->selected = n;
134         if(lw->range_selection)
135         {
136                 if(n >= lw->range_base)
137                 {
138                         lw->selected_end = n;
139                         lw->selected_start = lw->range_base;
140                 }
141                 if(n <= lw->range_base)
142                 {
143                         lw->selected_start = n;
144                         lw->selected_end = lw->range_base;
145                 }
146         }
147         else
148         {
149                 lw->selected_start = n;
150                 lw->selected_end = n;
151         }
154 static void
155 list_window_next(struct list_window *lw, unsigned length)
157         if (lw->selected + 1 < length)
158                 list_window_set_selected(lw, lw->selected + 1);
159         else if (options.list_wrap)
160                 list_window_set_selected(lw, 0);
163 static void
164 list_window_previous(struct list_window *lw, unsigned length)
166         if (lw->selected > 0)
167                 list_window_set_selected(lw, lw->selected - 1);
168         else if (options.list_wrap)
169                 list_window_set_selected(lw, length-1);
172 static void
173 list_window_top(struct list_window *lw)
175         if (lw->start == 0)
176                 list_window_set_selected(lw, lw->start);
177         else
178                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
179                         list_window_set_selected(lw, lw->start + lw->rows / 2);
180                 else
181                         list_window_set_selected(lw, lw->start + options.scroll_offset);
184 static void
185 list_window_middle(struct list_window *lw, unsigned length)
187         if (length >= lw->rows)
188                 list_window_set_selected(lw, lw->start + lw->rows / 2);
189         else
190                 list_window_set_selected(lw, length / 2);
193 static void
194 list_window_bottom(struct list_window *lw, unsigned length)
196         if (length >= lw->rows)
197                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
198                         list_window_set_selected(lw, lw->start + lw->rows / 2);
199                 else
200                         if (lw->start + lw->rows == length)
201                                 list_window_set_selected(lw, length - 1);
202                         else
203                                 list_window_set_selected(lw, lw->start + lw->rows - 1 - options.scroll_offset);
204         else
205                 list_window_set_selected(lw, length - 1);
208 static void
209 list_window_first(struct list_window *lw)
211         list_window_set_selected(lw, 0);
214 static void
215 list_window_last(struct list_window *lw, unsigned length)
217         if (length > 0)
218                 list_window_set_selected(lw, length - 1);
219         else
220                 list_window_set_selected(lw, 0);
223 static void
224 list_window_next_page(struct list_window *lw, unsigned length)
226         if (lw->rows < 2)
227                 return;
228         if (lw->selected + lw->rows < length)
229                 list_window_set_selected(lw, lw->selected + lw->rows - 1);
230         else
231                 list_window_last(lw, length);
234 static void
235 list_window_previous_page(struct list_window *lw)
237         if (lw->rows < 2)
238                 return;
239         if (lw->selected > lw->rows - 1)
240                 list_window_set_selected(lw, lw->selected - lw->rows + 1);
241         else
242                 list_window_first(lw);
245 static void
246 list_window_scroll_up(struct list_window *lw, unsigned n)
248         if (lw->start > 0) {
249                 if (n > lw->start)
250                         lw->start = 0;
251                 else
252                         lw->start -= n;
253                 if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
254                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
255                         if (lw->range_selection) {
256                                 if (lw->selected < lw->range_base) {
257                                         lw->selected_start = lw->selected;
258                                         lw->selected_end = lw->range_base;
259                                 } else {
260                                         lw->selected_end = lw->selected;
261                                 }
262                         } else {
263                                 lw->selected_start = lw->selected;
264                                 lw->selected_end = lw->selected;
265                         }
266                 }
267         }
270 static void
271 list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
273         if (lw->start + lw->rows < length)
274         {
275                 if ( lw->start + lw->rows + n > length - 1)
276                         lw->start = length - lw->rows;
277                 else
278                         lw->start += n;
279                 if (lw->selected < lw->start + options.scroll_offset) {
280                         lw->selected = lw->start + options.scroll_offset;
281                         if (lw->range_selection) {
282                                 if (lw->selected > lw->range_base) {
283                                         lw->selected_end = lw->selected;
284                                         lw->selected_start = lw->range_base;
285                                 } else {
286                                         lw->selected_start = lw->selected;
287                                 }
288                         } else {
289                                 lw->selected_start = lw->selected;
290                                 lw->selected_end = lw->selected;
291                         }
292                 }
293         }
296 static void
297 list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
298                       bool selected, bool highlight,
299                       const char *text, const char *second_column)
301         unsigned text_width = utf8_width(text);
302         unsigned second_column_width;
304         if (second_column != NULL) {
305                 second_column_width = utf8_width(second_column) + 1;
306                 if (second_column_width < width)
307                         width -= second_column_width;
308                 else
309                         second_column_width = 0;
310         } else
311                 second_column_width = 0;
313         if (highlight)
314                 colors_use(w, COLOR_LIST_BOLD);
315         else
316                 colors_use(w, COLOR_LIST);
318         if (selected)
319                 wattron(w, A_REVERSE);
321         waddstr(w, text);
322         if (options.wide_cursor && text_width < width)
323                 whline(w, ' ', width - text_width);
325         if (second_column_width > 0) {
326                 wmove(w, y, width);
327                 waddch(w, ' ');
328                 waddstr(w, second_column);
329         }
331         if (selected)
332                 wattroff(w, A_REVERSE);
334         if (!options.wide_cursor && text_width < width) {
335                 if (second_column_width == 0)
336                         /* the cursor is at the end of the text; clear
337                            the rest of this row */
338                         wclrtoeol(w);
339                 else
340                         /* there's a second column: clear the space
341                            between the first and the second column */
342                         mvwhline(w, y, text_width, ' ', width - text_width);
343         }
346 void
347 list_window_paint(struct list_window *lw,
348                   list_window_callback_fn_t callback,
349                   void *callback_data)
351         unsigned i;
352         bool show_cursor = !lw->hide_cursor;
353         bool highlight = false;
355         if (show_cursor) {
356                 int start = lw->start;
357                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
358                         // Center if the offset is more than half the screen
359                         start = lw->selected - lw->rows / 2;
360                 else
361                 {
362                         if (lw->selected < lw->start + options.scroll_offset)
363                                 start = lw->selected - options.scroll_offset;
365                         if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
366                         {
367                                 start = lw->selected - lw->rows + 1 + options.scroll_offset;
368                         }
369                 }
370                 if (start < 0)
371                         lw->start = 0;
372                 else
373                 {
374                         while ( start > 0 && callback(start + lw->rows - 1, &highlight, NULL, callback_data) == NULL)
375                                 start--;
376                         lw->start = start;
377                 }
378         }
380         show_cursor = show_cursor &&
381                 (!options.hardware_cursor || lw->range_selection);
383         for (i = 0; i < lw->rows; i++) {
384                 const char *label;
385                 char *second_column = NULL;
386                 highlight = false;
388                 label = callback(lw->start + i, &highlight, &second_column, callback_data);
389                 wmove(lw->w, i, 0);
391                 if (label) {
392                         list_window_paint_row(lw->w, i, lw->cols,
393                                               show_cursor &&
394                                               lw->start + i >= lw->selected_start &&
395                                               lw->start + i <= lw->selected_end,
396                                               highlight,
397                                               label, second_column);
399                         if (second_column != NULL)
400                                 g_free(second_column);
401                 } else
402                         wclrtoeol(lw->w);
403         }
405         if (options.hardware_cursor && lw->selected >= lw->start &&
406             lw->selected < lw->start + lw->rows) {
407                 curs_set(1);
408                 wmove(lw->w, lw->selected - lw->start, 0);
409         }
412 bool
413 list_window_find(struct list_window *lw,
414                  list_window_callback_fn_t callback,
415                  void *callback_data,
416                  const char *str,
417                  bool wrap,
418                  bool bell_on_wrap)
420         bool h;
421         unsigned i = lw->selected + 1;
422         const char *label;
424         do {
425                 while ((label = callback(i,&h,NULL,callback_data))) {
426                         if (str && label && match_line(label, str)) {
427                                 lw->selected = i;
428                                 if(!lw->range_selection || i > lw->selected_end)
429                                           lw->selected_end = i;
430                                 if(!lw->range_selection || i < lw->selected_start)
431                                           lw->selected_start = 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,
458                   unsigned rows)
460         bool h;
461         int i = lw->selected - 1;
462         const char *label;
464         if (rows == 0)
465                 return false;
467         do {
468                 while (i >= 0 && (label = callback(i,&h,NULL,callback_data))) {
469                         if( str && label && match_line(label, str) ) {
470                                 lw->selected = i;
471                                 if(!lw->range_selection || i > (int)lw->selected_end)
472                                           lw->selected_end = i;
473                                 if(!lw->range_selection || i < (int)lw->selected_start)
474                                           lw->selected_start = i;
475                                 return true;
476                         }
477                         if (wrap && i == (int)lw->selected)
478                                 return false;
479                         i--;
480                 }
481                 if (wrap) {
482                         i = rows - 1; /* last item */
483                         if (bell_on_wrap) {
484                                 screen_bell();
485                         }
486                 }
487         } while (wrap);
489         return false;
492 bool
493 list_window_jump(struct list_window *lw,
494                  list_window_callback_fn_t callback,
495                  void *callback_data,
496                  const char *str)
498         bool h;
499         unsigned i = 0;
500         const char *label;
502         while ((label = callback(i,&h,NULL,callback_data))) {
503                 if (label && label[0] == '[')
504                         label++;
505 #ifndef NCMPC_MINI
506                 if (str && label &&
507                                 ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
508                                  (!options.jump_prefix_only && match_line(label, str))) )
509 #else
510                 if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
511 #endif
512                 {
513                         lw->selected = i;
514                         if(!lw->range_selection || i > lw->selected_end)
515                                 lw->selected_end = i;
516                         if(!lw->range_selection || i < lw->selected_start)
517                                 lw->selected_start = i;
518                         return true;
519                 }
520                 i++;
521         }
522         return false;
525 /* perform basic list window commands (movement) */
526 bool
527 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
529         switch (cmd) {
530         case CMD_LIST_PREVIOUS:
531                 list_window_previous(lw, rows);
532                 break;
533         case CMD_LIST_NEXT:
534                 list_window_next(lw, rows);
535                 break;
536         case CMD_LIST_TOP:
537                 list_window_top(lw);
538                 break;
539         case CMD_LIST_MIDDLE:
540                 list_window_middle(lw,rows);
541                 break;
542         case CMD_LIST_BOTTOM:
543                 list_window_bottom(lw,rows);
544                 break;
545         case CMD_LIST_FIRST:
546                 list_window_first(lw);
547                 break;
548         case CMD_LIST_LAST:
549                 list_window_last(lw, rows);
550                 break;
551         case CMD_LIST_NEXT_PAGE:
552                 list_window_next_page(lw, rows);
553                 break;
554         case CMD_LIST_PREVIOUS_PAGE:
555                 list_window_previous_page(lw);
556                 break;
557         case CMD_LIST_RANGE_SELECT:
558                 if(lw->range_selection)
559                 {
560                         screen_status_printf(_("Range selection disabled"));
561                         lw->range_selection = false;
562                         list_window_set_selected(lw, lw->selected);
563                 }
564                 else
565                 {
566                         screen_status_printf(_("Range selection enabled"));
567                         lw->range_base = lw->selected;
568                         lw->range_selection = true;
569                 }
570                 break;
571         case CMD_LIST_SCROLL_UP_LINE:
572                 list_window_scroll_up(lw, 1);
573                 break;
574         case CMD_LIST_SCROLL_DOWN_LINE:
575                 list_window_scroll_down(lw, rows, 1);
576                 break;
577         case CMD_LIST_SCROLL_UP_HALF:
578                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
579                 break;
580         case CMD_LIST_SCROLL_DOWN_HALF:
581                 list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
582                 break;
583         default:
584                 return false;
585         }
587         return true;
590 bool
591 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
593         switch (cmd) {
594         case CMD_LIST_SCROLL_UP_LINE:
595         case CMD_LIST_PREVIOUS:
596                 if (lw->start > 0)
597                         lw->start--;
598                 break;
600         case CMD_LIST_SCROLL_DOWN_LINE:
601         case CMD_LIST_NEXT:
602                 if (lw->start + lw->rows < rows)
603                         lw->start++;
604                 break;
606         case CMD_LIST_FIRST:
607                 lw->start = 0;
608                 break;
610         case CMD_LIST_LAST:
611                 if (rows > lw->rows)
612                         lw->start = rows - lw->rows;
613                 else
614                         lw->start = 0;
615                 break;
617         case CMD_LIST_NEXT_PAGE:
618                 lw->start += lw->rows - 1;
619                 if (lw->start + lw->rows > rows) {
620                         if (rows > lw->rows)
621                                 lw->start = rows - lw->rows;
622                         else
623                                 lw->start = 0;
624                 }
625                 break;
627         case CMD_LIST_PREVIOUS_PAGE:
628                 if (lw->start > lw->rows)
629                         lw->start -= lw->rows;
630                 else
631                         lw->start = 0;
632                 break;
634         case CMD_LIST_SCROLL_UP_HALF:
635                 if (lw->start > (lw->rows - 1) / 2)
636                         lw->start -= (lw->rows - 1) / 2;
637                 else
638                         lw->start = 0;
639                 break;
641         case CMD_LIST_SCROLL_DOWN_HALF:
642                 lw->start += (lw->rows - 1) / 2;
643                 if (lw->start + lw->rows > rows) {
644                         if (rows > lw->rows)
645                                 lw->start = rows - lw->rows;
646                         else
647                                 lw->start = 0;
648                 }
649                 break;
651         default:
652                 return false;
653         }
655         return true;
658 #ifdef HAVE_GETMOUSE
659 bool
660 list_window_mouse(struct list_window *lw, unsigned rows,
661                   unsigned long bstate, int y)
663         assert(lw != NULL);
665         /* if the even occurred above the list window move up */
666         if (y < 0) {
667                 if (bstate & BUTTON3_CLICKED)
668                         list_window_first(lw);
669                 else
670                         list_window_previous_page(lw);
671                 return true;
672         }
674         /* if the even occurred below the list window move down */
675         if ((unsigned)y >= rows) {
676                 if (bstate & BUTTON3_CLICKED)
677                         list_window_last(lw, rows);
678                 else
679                         list_window_next_page(lw, rows);
680                 return true;
681         }
683         return false;
685 #endif