Code

Spelling corrections
[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->visual_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->visual_selection = false;
66         lw->visual_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->visual_selection)
90         {
91                 if(lw->visual_base > lw->selected_end)
92                           lw->selected_end = lw->selected;
93                 if(lw->visual_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->visual_selection)
124         {
125                 if(n >= lw->visual_base)
126                 {
127                         lw->selected_end = n;
128                         lw->selected_start = lw->visual_base;
129                 }
130                 if(n <= lw->visual_base)
131                 {
132                         lw->selected_start = n;
133                         lw->selected_end = lw->visual_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->visual_selection) {
235                                 if (lw->selected < lw->visual_base) {
236                                         lw->selected_start = lw->selected;
237                                         lw->selected_end = lw->visual_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->visual_selection) {
261                                 if (lw->selected > lw->visual_base) {
262                                         lw->selected_end = lw->selected;
263                                         lw->selected_start = lw->visual_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->visual_selection || i > lw->selected_end)
343                                           lw->selected_end = i;
344                                 if(!lw->visual_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->visual_selection || i > (int)lw->selected_end)
386                                           lw->selected_end = i;
387                                 if(!lw->visual_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                 {
419                         if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
420                                 lw->selected = i;
421                                 if(!lw->visual_selection || i > lw->selected_end)
422                                           lw->selected_end = i;
423                                 if(!lw->visual_selection || i < lw->selected_start)
424                                           lw->selected_start = i;
425                                 return true;
426                         }
427                 }
428                 else if (str && label && g_ascii_strncasecmp(label+1, str, strlen(str)) == 0) {
429                                 lw->selected = i;
430                                 if(!lw->visual_selection || i > lw->selected_end)
431                                           lw->selected_end = i;
432                                 if(!lw->visual_selection || i < lw->selected_start)
433                                           lw->selected_start = i;
434                                 return true;
435                         }
436                 i++;
437         }
438         return false;
441 /* perform basic list window commands (movement) */
442 bool
443 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
445         switch (cmd) {
446         case CMD_LIST_PREVIOUS:
447                 list_window_previous(lw, rows);
448                 break;
449         case CMD_LIST_NEXT:
450                 list_window_next(lw, rows);
451                 break;
452         case CMD_LIST_TOP:
453                 list_window_top(lw);
454                 break;
455         case CMD_LIST_MIDDLE:
456                 list_window_middle(lw,rows);
457                 break;
458         case CMD_LIST_BOTTOM:
459                 list_window_bottom(lw,rows);
460                 break;
461         case CMD_LIST_FIRST:
462                 list_window_first(lw);
463                 break;
464         case CMD_LIST_LAST:
465                 list_window_last(lw, rows);
466                 break;
467         case CMD_LIST_NEXT_PAGE:
468                 list_window_next_page(lw, rows);
469                 break;
470         case CMD_LIST_PREVIOUS_PAGE:
471                 list_window_previous_page(lw);
472                 break;
473         case CMD_LIST_VISUAL_SELECT:
474                 if(lw->visual_selection)
475                 {
476                         screen_status_printf(_("Visual selection disabled"));
477                         lw->visual_selection = false;
478                         list_window_set_selected(lw, lw->selected);
479                 }
480                 else
481                 {
482                         screen_status_printf(_("Visual selection enabled"));
483                         lw->visual_base = lw->selected;
484                         lw->visual_selection = true;
485                 }
486                 break;
487         case CMD_LIST_SCROLL_UP_LINE:
488                 list_window_scroll_up(lw, 1);
489                 break;
490         case CMD_LIST_SCROLL_DOWN_LINE:
491                 list_window_scroll_down(lw, rows, 1);
492                 break;
493         case CMD_LIST_SCROLL_UP_HALF:
494                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
495                 break;
496         case CMD_LIST_SCROLL_DOWN_HALF:
497                 list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
498                 break;
499         default:
500                 return false;
501         }
503         return true;
506 bool
507 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
509         switch (cmd) {
510         case CMD_LIST_SCROLL_UP_LINE:
511         case CMD_LIST_PREVIOUS:
512                 if (lw->start > 0)
513                         lw->start--;
514                 break;
516         case CMD_LIST_SCROLL_DOWN_LINE:
517         case CMD_LIST_NEXT:
518                 if (lw->start + lw->rows < rows)
519                         lw->start++;
520                 break;
522         case CMD_LIST_FIRST:
523                 lw->start = 0;
524                 break;
526         case CMD_LIST_LAST:
527                 if (rows > lw->rows)
528                         lw->start = rows - lw->rows;
529                 else
530                         lw->start = 0;
531                 break;
533         case CMD_LIST_NEXT_PAGE:
534                 lw->start += lw->rows - 1;
535                 if (lw->start + lw->rows > rows) {
536                         if (rows > lw->rows)
537                                 lw->start = rows - lw->rows;
538                         else
539                                 lw->start = 0;
540                 }
541                 break;
543         case CMD_LIST_PREVIOUS_PAGE:
544                 if (lw->start > lw->rows)
545                         lw->start -= lw->rows;
546                 else
547                         lw->start = 0;
548                 break;
550         case CMD_LIST_SCROLL_UP_HALF:
551                 if (lw->start > (lw->rows - 1) / 2)
552                         lw->start -= (lw->rows - 1) / 2;
553                 else
554                         lw->start = 0;
555                 break;
557         case CMD_LIST_SCROLL_DOWN_HALF:
558                 lw->start += (lw->rows - 1) / 2;
559                 if (lw->start + lw->rows > rows) {
560                         if (rows > lw->rows)
561                                 lw->start = rows - lw->rows;
562                         else
563                                 lw->start = 0;
564                 }
565                 break;
567         default:
568                 return false;
569         }
571         return true;
574 #ifdef HAVE_GETMOUSE
575 bool
576 list_window_mouse(struct list_window *lw, unsigned rows,
577                   unsigned long bstate, int y)
579         assert(lw != NULL);
581         /* if the even occurred above the list window move up */
582         if (y < 0) {
583                 if (bstate & BUTTON3_CLICKED)
584                         list_window_first(lw);
585                 else
586                         list_window_previous_page(lw);
587                 return true;
588         }
590         /* if the even occurred below the list window move down */
591         if ((unsigned)y >= rows) {
592                 if (bstate & BUTTON3_CLICKED)
593                         list_window_last(lw, rows);
594                 else
595                         list_window_next_page(lw, rows);
596                 return true;
597         }
599         return false;
601 #endif