Code

screen_search.c: Fix typo and tweak spacing of search help.
[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.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(list_window_t));
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         if (lw) {
54                 memset(lw, 0, sizeof(list_window_t));
55                 g_free(lw);
56         }
57 }
59 void
60 list_window_reset(struct list_window *lw)
61 {
62         lw->selected = 0;
63         lw->selected_start = 0;
64         lw->selected_end = 0;
65         lw->range_selection = false;
66         lw->range_base = 0;
67         lw->start = 0;
68 }
70 void
71 list_window_check_selected(struct list_window *lw, unsigned length)
72 {
73         if (lw->start + lw->rows > length) {
74                 if (length > lw->rows)
75                         lw->start = length - lw->rows;
76                 else
77                         lw->start = 0;
78         }
80         if (lw->selected < lw->start)
81                 lw->selected = lw->start;
83         if (length == 0)
84                 lw->selected = 0;
85         else if (lw->selected >= length)
86                 lw->selected = length - 1;
88         if(lw->range_selection)
89         {
90                 if(lw->range_base > lw->selected_end)
91                           lw->selected_end = lw->selected;
92                 if(lw->range_base < lw->selected_start)
93                           lw->selected_start = lw->selected;
94         }
95         else
96         {
97                 lw->selected_start = lw->selected;
98                 lw->selected_end = lw->selected;
99         }
102 void
103 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
105         if (n > lw->rows / 2)
106                 lw->start = n - lw->rows / 2;
107         else
108                 lw->start = 0;
110         if (lw->start + lw->rows > rows) {
111                 if (lw->rows < rows)
112                         lw->start = rows - lw->rows;
113                 else
114                         lw->start = 0;
115         }
118 void
119 list_window_set_selected(struct list_window *lw, unsigned n)
121         lw->selected = n;
122         if(lw->range_selection)
123         {
124                 if(n >= lw->range_base)
125                 {
126                         lw->selected_end = n;
127                         lw->selected_start = lw->range_base;
128                 }
129                 if(n <= lw->range_base)
130                 {
131                         lw->selected_start = n;
132                         lw->selected_end = lw->range_base;
133                 }
134         }
135         else
136         {
137                 lw->selected_start = n;
138                 lw->selected_end = n;
139         }
142 static void
143 list_window_next(struct list_window *lw, unsigned length)
145         if (lw->selected + 1 < length)
146                 list_window_set_selected(lw, lw->selected + 1);
147         else if (options.list_wrap)
148                 list_window_set_selected(lw, 0);
151 static void
152 list_window_previous(struct list_window *lw, unsigned length)
154         if (lw->selected > 0)
155                 list_window_set_selected(lw, lw->selected - 1);
156         else if (options.list_wrap)
157                 list_window_set_selected(lw, length-1);
160 static void
161 list_window_top(struct list_window *lw)
163         if (lw->start == 0)
164                 list_window_set_selected(lw, lw->start);
165         else
166                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
167                         list_window_set_selected(lw, lw->start + lw->rows / 2);
168                 else
169                         list_window_set_selected(lw, lw->start + options.scroll_offset);
172 static void
173 list_window_middle(struct list_window *lw, unsigned length)
175         if (length >= lw->rows)
176                 list_window_set_selected(lw, lw->start + lw->rows / 2);
177         else
178                 list_window_set_selected(lw, length / 2);
181 static void
182 list_window_bottom(struct list_window *lw, unsigned length)
184         if (length >= lw->rows)
185                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
186                         list_window_set_selected(lw, lw->start + lw->rows / 2);
187                 else
188                         if (lw->start + lw->rows == length)
189                                 list_window_set_selected(lw, length - 1);
190                         else
191                                 list_window_set_selected(lw, lw->start + lw->rows - 1 - options.scroll_offset);
192         else
193                 list_window_set_selected(lw, length - 1);
196 static void
197 list_window_first(struct list_window *lw)
199         list_window_set_selected(lw, 0);
202 static void
203 list_window_last(struct list_window *lw, unsigned length)
205         if (length > 0)
206                 list_window_set_selected(lw, length - 1);
207         else
208                 list_window_set_selected(lw, 0);
211 static void
212 list_window_next_page(struct list_window *lw, unsigned length)
214         if (lw->rows < 2)
215                 return;
216         if (lw->selected + lw->rows < length)
217                 list_window_set_selected(lw, lw->selected + lw->rows - 1);
218         else
219                 list_window_last(lw, length);
222 static void
223 list_window_previous_page(struct list_window *lw)
225         if (lw->rows < 2)
226                 return;
227         if (lw->selected > lw->rows - 1)
228                 list_window_set_selected(lw, lw->selected - lw->rows + 1);
229         else
230                 list_window_first(lw);
233 static void
234 list_window_scroll_up(struct list_window *lw, unsigned n)
236         if (lw->start > 0) {
237                 if (n > lw->start)
238                         lw->start = 0;
239                 else
240                         lw->start -= n;
241                 if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
242                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
243                         if (lw->range_selection) {
244                                 if (lw->selected < lw->range_base) {
245                                         lw->selected_start = lw->selected;
246                                         lw->selected_end = lw->range_base;
247                                 } else {
248                                         lw->selected_end = lw->selected;
249                                 }
250                         } else {
251                                 lw->selected_start = lw->selected;
252                                 lw->selected_end = lw->selected;
253                         }
254                 }
255         }
258 static void
259 list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
261         if (lw->start + lw->rows < length)
262         {
263                 if ( lw->start + lw->rows + n > length - 1)
264                         lw->start = length - lw->rows;
265                 else
266                         lw->start += n;
267                 if (lw->selected < lw->start + options.scroll_offset) {
268                         lw->selected = lw->start + options.scroll_offset;
269                         if (lw->range_selection) {
270                                 if (lw->selected > lw->range_base) {
271                                         lw->selected_end = lw->selected;
272                                         lw->selected_start = lw->range_base;
273                                 } else {
274                                         lw->selected_start = lw->selected;
275                                 }
276                         } else {
277                                 lw->selected_start = lw->selected;
278                                 lw->selected_end = lw->selected;
279                         }
280                 }
281         }
284 void
285 list_window_paint(struct list_window *lw,
286                   list_window_callback_fn_t callback,
287                   void *callback_data)
289         unsigned i;
290         bool fill = options.wide_cursor;
291         bool show_cursor = !lw->hide_cursor;
292         bool highlight = false;
294         if (show_cursor) {
295                 int start = lw->start;
296                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
297                         // Center if the offset is more than half the screen
298                         start = lw->selected - lw->rows / 2;
299                 else
300                 {
301                         if (lw->selected < lw->start + options.scroll_offset)
302                                 start = lw->selected - options.scroll_offset;
304                         if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
305                         {
306                                 start = lw->selected - lw->rows + 1 + options.scroll_offset;
307                         }
308                 }
309                 if (start < 0)
310                         lw->start = 0;
311                 else
312                 {
313                         while ( start > 0 && callback(start + lw->rows - 1, &highlight, callback_data) == NULL)
314                                 start--;
315                         lw->start = start;
316                 }
317         }
319         for (i = 0; i < lw->rows; i++) {
320                 const char *label;
321                 highlight = false;
323                 label = callback(lw->start + i, &highlight, callback_data);
324                 wmove(lw->w, i, 0);
326                 if (label) {
327                         bool selected = (lw->start + i >= lw->selected_start && lw->start + i <= lw->selected_end);
328                         unsigned len = utf8_width(label);
330                         if (highlight)
331                                 colors_use(lw->w, COLOR_LIST_BOLD);
332                         else
333                                 colors_use(lw->w, COLOR_LIST);
335                         if (show_cursor && selected)
336                                 wattron(lw->w, A_REVERSE);
338                         //waddnstr(lw->w, label, lw->cols);
339                         waddstr(lw->w, label);
340                         if (fill && len < lw->cols)
341                                 whline(lw->w,  ' ', lw->cols-len);
343                         if (selected)
344                                 wattroff(lw->w, A_REVERSE);
346                         if (!fill && len < lw->cols)
347                                 wclrtoeol(lw->w);
348                 } else
349                         wclrtoeol(lw->w);
350         }
353 bool
354 list_window_find(struct list_window *lw,
355                  list_window_callback_fn_t callback,
356                  void *callback_data,
357                  const char *str,
358                  bool wrap,
359                  bool bell_on_wrap)
361         bool h;
362         unsigned i = lw->selected + 1;
363         const char *label;
365         do {
366                 while ((label = callback(i,&h,callback_data))) {
367                         if (str && label && match_line(label, str)) {
368                                 lw->selected = i;
369                                 if(!lw->range_selection || i > lw->selected_end)
370                                           lw->selected_end = i;
371                                 if(!lw->range_selection || i < lw->selected_start)
372                                           lw->selected_start = i;
373                                 return true;
374                         }
375                         if (wrap && i == lw->selected)
376                                 return false;
377                         i++;
378                 }
379                 if (wrap) {
380                         if (i == 0) /* empty list */
381                                 return 1;
382                         i=0; /* first item */
383                         if (bell_on_wrap) {
384                                 screen_bell();
385                         }
386                 }
387         } while (wrap);
389         return false;
392 bool
393 list_window_rfind(struct list_window *lw,
394                   list_window_callback_fn_t callback,
395                   void *callback_data,
396                   const char *str,
397                   bool wrap,
398                   bool bell_on_wrap,
399                   unsigned rows)
401         bool h;
402         int i = lw->selected - 1;
403         const char *label;
405         if (rows == 0)
406                 return false;
408         do {
409                 while (i >= 0 && (label = callback(i,&h,callback_data))) {
410                         if( str && label && match_line(label, str) ) {
411                                 lw->selected = i;
412                                 if(!lw->range_selection || i > (int)lw->selected_end)
413                                           lw->selected_end = i;
414                                 if(!lw->range_selection || i < (int)lw->selected_start)
415                                           lw->selected_start = i;
416                                 return true;
417                         }
418                         if (wrap && i == (int)lw->selected)
419                                 return false;
420                         i--;
421                 }
422                 if (wrap) {
423                         i = rows - 1; /* last item */
424                         if (bell_on_wrap) {
425                                 screen_bell();
426                         }
427                 }
428         } while (wrap);
430         return false;
433 bool
434 list_window_jump(struct list_window *lw,
435                  list_window_callback_fn_t callback,
436                  void *callback_data,
437                  const char *str)
439         bool h;
440         unsigned i = 0;
441         const char *label;
443         while ((label = callback(i,&h,callback_data))) {
444                 if (label && label[0] == '[')
445                         label++;
446 #ifndef NCMPC_MINI
447                 if (str && label &&
448                                 ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
449                                  (!options.jump_prefix_only && match_line(label, str))) )
450 #else
451                 if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
452 #endif
453                 {
454                         lw->selected = i;
455                         if(!lw->range_selection || i > lw->selected_end)
456                                 lw->selected_end = i;
457                         if(!lw->range_selection || i < lw->selected_start)
458                                 lw->selected_start = i;
459                         return true;
460                 }
461                 i++;
462         }
463         return false;
466 /* perform basic list window commands (movement) */
467 bool
468 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
470         switch (cmd) {
471         case CMD_LIST_PREVIOUS:
472                 list_window_previous(lw, rows);
473                 break;
474         case CMD_LIST_NEXT:
475                 list_window_next(lw, rows);
476                 break;
477         case CMD_LIST_TOP:
478                 list_window_top(lw);
479                 break;
480         case CMD_LIST_MIDDLE:
481                 list_window_middle(lw,rows);
482                 break;
483         case CMD_LIST_BOTTOM:
484                 list_window_bottom(lw,rows);
485                 break;
486         case CMD_LIST_FIRST:
487                 list_window_first(lw);
488                 break;
489         case CMD_LIST_LAST:
490                 list_window_last(lw, rows);
491                 break;
492         case CMD_LIST_NEXT_PAGE:
493                 list_window_next_page(lw, rows);
494                 break;
495         case CMD_LIST_PREVIOUS_PAGE:
496                 list_window_previous_page(lw);
497                 break;
498         case CMD_LIST_RANGE_SELECT:
499                 if(lw->range_selection)
500                 {
501                         screen_status_printf(_("Range selection disabled"));
502                         lw->range_selection = false;
503                         list_window_set_selected(lw, lw->selected);
504                 }
505                 else
506                 {
507                         screen_status_printf(_("Range selection enabled"));
508                         lw->range_base = lw->selected;
509                         lw->range_selection = true;
510                 }
511                 break;
512         case CMD_LIST_SCROLL_UP_LINE:
513                 list_window_scroll_up(lw, 1);
514                 break;
515         case CMD_LIST_SCROLL_DOWN_LINE:
516                 list_window_scroll_down(lw, rows, 1);
517                 break;
518         case CMD_LIST_SCROLL_UP_HALF:
519                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
520                 break;
521         case CMD_LIST_SCROLL_DOWN_HALF:
522                 list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
523                 break;
524         default:
525                 return false;
526         }
528         return true;
531 bool
532 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
534         switch (cmd) {
535         case CMD_LIST_SCROLL_UP_LINE:
536         case CMD_LIST_PREVIOUS:
537                 if (lw->start > 0)
538                         lw->start--;
539                 break;
541         case CMD_LIST_SCROLL_DOWN_LINE:
542         case CMD_LIST_NEXT:
543                 if (lw->start + lw->rows < rows)
544                         lw->start++;
545                 break;
547         case CMD_LIST_FIRST:
548                 lw->start = 0;
549                 break;
551         case CMD_LIST_LAST:
552                 if (rows > lw->rows)
553                         lw->start = rows - lw->rows;
554                 else
555                         lw->start = 0;
556                 break;
558         case CMD_LIST_NEXT_PAGE:
559                 lw->start += lw->rows - 1;
560                 if (lw->start + lw->rows > rows) {
561                         if (rows > lw->rows)
562                                 lw->start = rows - lw->rows;
563                         else
564                                 lw->start = 0;
565                 }
566                 break;
568         case CMD_LIST_PREVIOUS_PAGE:
569                 if (lw->start > lw->rows)
570                         lw->start -= lw->rows;
571                 else
572                         lw->start = 0;
573                 break;
575         case CMD_LIST_SCROLL_UP_HALF:
576                 if (lw->start > (lw->rows - 1) / 2)
577                         lw->start -= (lw->rows - 1) / 2;
578                 else
579                         lw->start = 0;
580                 break;
582         case CMD_LIST_SCROLL_DOWN_HALF:
583                 lw->start += (lw->rows - 1) / 2;
584                 if (lw->start + lw->rows > rows) {
585                         if (rows > lw->rows)
586                                 lw->start = rows - lw->rows;
587                         else
588                                 lw->start = 0;
589                 }
590                 break;
592         default:
593                 return false;
594         }
596         return true;
599 #ifdef HAVE_GETMOUSE
600 bool
601 list_window_mouse(struct list_window *lw, unsigned rows,
602                   unsigned long bstate, int y)
604         assert(lw != NULL);
606         /* if the even occurred above the list window move up */
607         if (y < 0) {
608                 if (bstate & BUTTON3_CLICKED)
609                         list_window_first(lw);
610                 else
611                         list_window_previous_page(lw);
612                 return true;
613         }
615         /* if the even occurred below the list window move down */
616         if ((unsigned)y >= rows) {
617                 if (bstate & BUTTON3_CLICKED)
618                         list_window_last(lw, rows);
619                 else
620                         list_window_next_page(lw, rows);
621                 return true;
622         }
624         return false;
626 #endif