Code

Merge branch 'master' of git://git.musicpd.org/patrick/ncmpc
[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->xoffset = 0;
68         lw->start = 0;
69 }
71 void
72 list_window_check_selected(struct list_window *lw, unsigned length)
73 {
74         if (lw->start + lw->rows > length) {
75                 if (length > lw->rows)
76                         lw->start = length - lw->rows;
77                 else
78                         lw->start = 0;
79         }
81         if (lw->selected < lw->start)
82                 lw->selected = lw->start;
84         if (length == 0)
85                 lw->selected = 0;
86         else if (lw->selected >= length)
87                 lw->selected = length - 1;
89         if(lw->range_selection)
90         {
91                 if(lw->range_base > lw->selected_end)
92                           lw->selected_end = lw->selected;
93                 if(lw->range_base < lw->selected_start)
94                           lw->selected_start = lw->selected;
95         }
96         else
97         {
98                 lw->selected_start = lw->selected;
99                 lw->selected_end = lw->selected;
100         }
103 void
104 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
106         if (n > lw->rows / 2)
107                 lw->start = n - lw->rows / 2;
108         else
109                 lw->start = 0;
111         if (lw->start + lw->rows > rows) {
112                 if (lw->rows < rows)
113                         lw->start = rows - lw->rows;
114                 else
115                         lw->start = 0;
116         }
119 void
120 list_window_set_selected(struct list_window *lw, unsigned n)
122         lw->selected = n;
123         if(lw->range_selection)
124         {
125                 if(n >= lw->range_base)
126                 {
127                         lw->selected_end = n;
128                         lw->selected_start = lw->range_base;
129                 }
130                 if(n <= lw->range_base)
131                 {
132                         lw->selected_start = n;
133                         lw->selected_end = lw->range_base;
134                 }
135         }
136         else
137         {
138                 lw->selected_start = n;
139                 lw->selected_end = n;
140         }
143 static void
144 list_window_next(struct list_window *lw, unsigned length)
146         if (lw->selected + 1 < length)
147                 list_window_set_selected(lw, lw->selected + 1);
148         else if (options.list_wrap)
149                 list_window_set_selected(lw, 0);
152 static void
153 list_window_previous(struct list_window *lw, unsigned length)
155         if (lw->selected > 0)
156                 list_window_set_selected(lw, lw->selected - 1);
157         else if (options.list_wrap)
158                 list_window_set_selected(lw, length-1);
161 static void
162 list_window_top(struct list_window *lw)
164         list_window_set_selected(lw, lw->start);
167 static void
168 list_window_middle(struct list_window *lw, unsigned length)
170         if (length >= lw->rows)
171                 list_window_set_selected(lw, lw->start + lw->rows / 2);
172         else
173                 list_window_set_selected(lw, length / 2);
176 static void
177 list_window_bottom(struct list_window *lw, unsigned length)
179         if (length >= lw->rows)
180                 list_window_set_selected(lw, lw->start + lw->rows - 1);
181         else
182                 list_window_set_selected(lw, length - 1);
185 static void
186 list_window_first(struct list_window *lw)
188         lw->xoffset = 0;
189         list_window_set_selected(lw, 0);
192 static void
193 list_window_last(struct list_window *lw, unsigned length)
195         lw->xoffset = 0;
196         if (length > 0)
197                 list_window_set_selected(lw, length - 1);
198         else
199                 list_window_set_selected(lw, 0);
202 static void
203 list_window_next_page(struct list_window *lw, unsigned length)
205         if (lw->rows < 2)
206                 return;
207         if (lw->selected + lw->rows < length)
208                 list_window_set_selected(lw, lw->selected + lw->rows - 1);
209         else
210                 list_window_last(lw, length);
213 static void
214 list_window_previous_page(struct list_window *lw)
216         if (lw->rows < 2)
217                 return;
218         if (lw->selected > lw->rows - 1)
219                 list_window_set_selected(lw, lw->selected - lw->rows + 1);
220         else
221                 list_window_first(lw);
224 static void
225 list_window_scroll_up(struct list_window *lw, unsigned n)
227         if (lw->start > 0) {
228                 if (n > lw->start)
229                         lw->start = 0;
230                 else
231                         lw->start -= n;
232                 if (lw->selected > lw->start + lw->rows - 1) {
233                         lw->selected = lw->start + lw->rows - 1;
234                         if (lw->range_selection) {
235                                 if (lw->selected < lw->range_base) {
236                                         lw->selected_start = lw->selected;
237                                         lw->selected_end = lw->range_base;
238                                 } else {
239                                         lw->selected_end = lw->selected;
240                                 }
241                         } else {
242                                 lw->selected_start = lw->selected;
243                                 lw->selected_end = lw->selected;
244                         }
245                 }
246         }
249 static void
250 list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
252         if (lw->start + lw->rows < length - 1)
253         {
254                 if ( lw->start + lw->rows + n > length - 1)
255                         lw->start = length-1;
256                 else
257                         lw->start += n;
258                 if (lw->selected < lw->start) {
259                         lw->selected = lw->start;
260                         if (lw->range_selection) {
261                                 if (lw->selected > lw->range_base) {
262                                         lw->selected_end = lw->selected;
263                                         lw->selected_start = lw->range_base;
264                                 } else {
265                                         lw->selected_start = lw->selected;
266                                 }
267                         } else {
268                                 lw->selected_start = lw->selected;
269                                 lw->selected_end = lw->selected;
270                         }
271                 }
272         }
275 void
276 list_window_paint(struct list_window *lw,
277                   list_window_callback_fn_t callback,
278                   void *callback_data)
280         unsigned i;
281         bool fill = options.wide_cursor;
282         bool show_cursor = !lw->hide_cursor;
284         if (show_cursor) {
285                 if (lw->selected < lw->start)
286                         lw->start = lw->selected;
288                 if (lw->selected >= lw->start + lw->rows)
289                         lw->start = lw->selected - lw->rows + 1;
290         }
292         for (i = 0; i < lw->rows; i++) {
293                 bool highlight = false;
294                 const char *label;
296                 label = callback(lw->start + i, &highlight, callback_data);
297                 wmove(lw->w, i, 0);
299                 if (label) {
300                         bool selected = (lw->start + i >= lw->selected_start && lw->start + i <= lw->selected_end);
301                         unsigned len = utf8_width(label);
303                         if (highlight)
304                                 colors_use(lw->w, COLOR_LIST_BOLD);
305                         else
306                                 colors_use(lw->w, COLOR_LIST);
308                         if (show_cursor && selected)
309                                 wattron(lw->w, A_REVERSE);
311                         //waddnstr(lw->w, label, lw->cols);
312                         waddstr(lw->w, label);
313                         if (fill && len < lw->cols)
314                                 whline(lw->w,  ' ', lw->cols-len);
316                         if (selected)
317                                 wattroff(lw->w, A_REVERSE);
319                         if (!fill && len < lw->cols)
320                                 wclrtoeol(lw->w);
321                 } else
322                         wclrtoeol(lw->w);
323         }
326 bool
327 list_window_find(struct list_window *lw,
328                  list_window_callback_fn_t callback,
329                  void *callback_data,
330                  const char *str,
331                  bool wrap,
332                  bool bell_on_wrap)
334         bool h;
335         unsigned i = lw->selected + 1;
336         const char *label;
338         do {
339                 while ((label = callback(i,&h,callback_data))) {
340                         if (str && label && match_line(label, str)) {
341                                 lw->selected = i;
342                                 if(!lw->range_selection || i > lw->selected_end)
343                                           lw->selected_end = i;
344                                 if(!lw->range_selection || i < lw->selected_start)
345                                           lw->selected_start = i;
346                                 return true;
347                         }
348                         if (wrap && i == lw->selected)
349                                 return false;
350                         i++;
351                 }
352                 if (wrap) {
353                         if (i == 0) /* empty list */
354                                 return 1;
355                         i=0; /* first item */
356                         if (bell_on_wrap) {
357                                 screen_bell();
358                         }
359                 }
360         } while (wrap);
362         return false;
365 bool
366 list_window_rfind(struct list_window *lw,
367                   list_window_callback_fn_t callback,
368                   void *callback_data,
369                   const char *str,
370                   bool wrap,
371                   bool bell_on_wrap,
372                   unsigned rows)
374         bool h;
375         int i = lw->selected - 1;
376         const char *label;
378         if (rows == 0)
379                 return false;
381         do {
382                 while (i >= 0 && (label = callback(i,&h,callback_data))) {
383                         if( str && label && match_line(label, str) ) {
384                                 lw->selected = i;
385                                 if(!lw->range_selection || i > (int)lw->selected_end)
386                                           lw->selected_end = i;
387                                 if(!lw->range_selection || i < (int)lw->selected_start)
388                                           lw->selected_start = i;
389                                 return true;
390                         }
391                         if (wrap && i == (int)lw->selected)
392                                 return false;
393                         i--;
394                 }
395                 if (wrap) {
396                         i = rows - 1; /* last item */
397                         if (bell_on_wrap) {
398                                 screen_bell();
399                         }
400                 }
401         } while (wrap);
403         return false;
406 bool
407 list_window_jump(struct list_window *lw,
408                  list_window_callback_fn_t callback,
409                  void *callback_data,
410                  const char *str)
412         bool h;
413         unsigned i = 0;
414         const char *label;
416         while ((label = callback(i,&h,callback_data))) {
417                 if (label && label[0] == '[')
418                         label++;
419 #ifndef NCMPC_MINI
420                 if (str && label &&
421                                 ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
422                                  (!options.jump_prefix_only && match_line(label, str))) )
423 #else
424                 if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
425 #endif
426                 {
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                 i++;
435         }
436         return false;
439 /* perform basic list window commands (movement) */
440 bool
441 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
443         switch (cmd) {
444         case CMD_LIST_PREVIOUS:
445                 list_window_previous(lw, rows);
446                 break;
447         case CMD_LIST_NEXT:
448                 list_window_next(lw, rows);
449                 break;
450         case CMD_LIST_TOP:
451                 list_window_top(lw);
452                 break;
453         case CMD_LIST_MIDDLE:
454                 list_window_middle(lw,rows);
455                 break;
456         case CMD_LIST_BOTTOM:
457                 list_window_bottom(lw,rows);
458                 break;
459         case CMD_LIST_FIRST:
460                 list_window_first(lw);
461                 break;
462         case CMD_LIST_LAST:
463                 list_window_last(lw, rows);
464                 break;
465         case CMD_LIST_NEXT_PAGE:
466                 list_window_next_page(lw, rows);
467                 break;
468         case CMD_LIST_PREVIOUS_PAGE:
469                 list_window_previous_page(lw);
470                 break;
471         case CMD_LIST_RANGE_SELECT:
472                 if(lw->range_selection)
473                 {
474                         screen_status_printf(_("Range selection disabled"));
475                         lw->range_selection = false;
476                         list_window_set_selected(lw, lw->selected);
477                 }
478                 else
479                 {
480                         screen_status_printf(_("Range selection enabled"));
481                         lw->range_base = lw->selected;
482                         lw->range_selection = true;
483                 }
484                 break;
485         case CMD_LIST_SCROLL_UP_LINE:
486                 list_window_scroll_up(lw, 1);
487                 break;
488         case CMD_LIST_SCROLL_DOWN_LINE:
489                 list_window_scroll_down(lw, rows, 1);
490                 break;
491         case CMD_LIST_SCROLL_UP_HALF:
492                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
493                 break;
494         case CMD_LIST_SCROLL_DOWN_HALF:
495                 list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
496                 break;
497         default:
498                 return false;
499         }
501         return true;
504 bool
505 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
507         switch (cmd) {
508         case CMD_LIST_SCROLL_UP_LINE:
509         case CMD_LIST_PREVIOUS:
510                 if (lw->start > 0)
511                         lw->start--;
512                 break;
514         case CMD_LIST_SCROLL_DOWN_LINE:
515         case CMD_LIST_NEXT:
516                 if (lw->start + lw->rows < rows)
517                         lw->start++;
518                 break;
520         case CMD_LIST_FIRST:
521                 lw->start = 0;
522                 break;
524         case CMD_LIST_LAST:
525                 if (rows > lw->rows)
526                         lw->start = rows - lw->rows;
527                 else
528                         lw->start = 0;
529                 break;
531         case CMD_LIST_NEXT_PAGE:
532                 lw->start += lw->rows - 1;
533                 if (lw->start + lw->rows > rows) {
534                         if (rows > lw->rows)
535                                 lw->start = rows - lw->rows;
536                         else
537                                 lw->start = 0;
538                 }
539                 break;
541         case CMD_LIST_PREVIOUS_PAGE:
542                 if (lw->start > lw->rows)
543                         lw->start -= lw->rows;
544                 else
545                         lw->start = 0;
546                 break;
548         case CMD_LIST_SCROLL_UP_HALF:
549                 if (lw->start > (lw->rows - 1) / 2)
550                         lw->start -= (lw->rows - 1) / 2;
551                 else
552                         lw->start = 0;
553                 break;
555         case CMD_LIST_SCROLL_DOWN_HALF:
556                 lw->start += (lw->rows - 1) / 2;
557                 if (lw->start + lw->rows > rows) {
558                         if (rows > lw->rows)
559                                 lw->start = rows - lw->rows;
560                         else
561                                 lw->start = 0;
562                 }
563                 break;
565         default:
566                 return false;
567         }
569         return true;
572 #ifdef HAVE_GETMOUSE
573 bool
574 list_window_mouse(struct list_window *lw, unsigned rows,
575                   unsigned long bstate, int y)
577         assert(lw != NULL);
579         /* if the even occurred above the list window move up */
580         if (y < 0) {
581                 if (bstate & BUTTON3_CLICKED)
582                         list_window_first(lw);
583                 else
584                         list_window_previous_page(lw);
585                 return true;
586         }
588         /* if the even occurred below the list window move down */
589         if ((unsigned)y >= rows) {
590                 if (bstate & BUTTON3_CLICKED)
591                         list_window_last(lw, rows);
592                 else
593                         list_window_next_page(lw, rows);
594                 return true;
595         }
597         return false;
599 #endif