Code

5ab8f9c9c125c5ecc628fd050a6de932de327fb8
[ncmpc.git] / src / list_window.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "list_window.h"
22 #include "config.h"
23 #include "options.h"
24 #include "support.h"
25 #include "command.h"
26 #include "colors.h"
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 extern void screen_bell(void);
34 struct list_window *
35 list_window_init(WINDOW *w, unsigned width, unsigned height)
36 {
37         struct list_window *lw;
39         lw = g_malloc0(sizeof(list_window_t));
40         lw->w = w;
41         lw->cols = width;
42         lw->rows = height;
43         lw->clear = 1;
44         return lw;
45 }
47 void
48 list_window_free(struct list_window *lw)
49 {
50         if (lw) {
51                 memset(lw, 0, sizeof(list_window_t));
52                 g_free(lw);
53         }
54 }
56 void
57 list_window_reset(struct list_window *lw)
58 {
59         lw->selected = 0;
60         lw->xoffset = 0;
61         lw->start = 0;
62         lw->clear = 1;
63 }
65 void
66 list_window_check_selected(struct list_window *lw, unsigned length)
67 {
68         if (lw->start + lw->rows > length) {
69                 if (length > lw->rows)
70                         lw->start = length - lw->rows;
71                 else
72                         lw->start = 0;
73         }
75         if (lw->selected < lw->start)
76                 lw->selected = lw->start;
78         if (length > 0 && lw->selected >= length)
79                 lw->selected = length - 1;
80 }
82 void
83 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
84 {
85         if (n > lw->rows / 2)
86                 lw->start = n - lw->rows / 2;
87         else
88                 lw->start = 0;
90         if (lw->start + lw->rows > rows) {
91                 if (lw->rows < rows)
92                         lw->start = rows - lw->rows;
93                 else
94                         lw->start = 0;
95         }
97         lw->repaint = lw->clear = 1;
98 }
100 void
101 list_window_set_selected(struct list_window *lw, unsigned n)
103         lw->selected = n;
106 void
107 list_window_next(struct list_window *lw, unsigned length)
109         if (lw->selected + 1 < length)
110                 lw->selected++;
111         else if (options.list_wrap)
112                 lw->selected = 0;
115 void
116 list_window_previous(struct list_window *lw, unsigned length)
118         if (lw->selected > 0)
119                 lw->selected--;
120         else if (options.list_wrap)
121                 lw->selected = length - 1;
124 void
125 list_window_first(struct list_window *lw)
127         lw->xoffset = 0;
128         lw->selected = 0;
131 void
132 list_window_last(struct list_window *lw, unsigned length)
134         lw->xoffset = 0;
135         if (length > 0)
136                 lw->selected = length - 1;
137         else
138                 lw->selected = 0;
141 void
142 list_window_next_page(struct list_window *lw, unsigned length)
144         if (lw->rows < 2)
145                 return;
146         if (lw->selected + lw->rows < length)
147                 lw->selected += lw->rows - 1;
148         else
149                 list_window_last(lw, length);
152 void
153 list_window_previous_page(struct list_window *lw)
155         if (lw->rows < 2)
156                 return;
157         if (lw->selected > lw->rows - 1)
158                 lw->selected -= lw->rows - 1;
159         else
160                 list_window_first(lw);
164 void
165 list_window_paint(struct list_window *lw,
166                   list_window_callback_fn_t callback,
167                   void *callback_data)
169         unsigned i;
170         int fill = options.wide_cursor;
171         int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
173         if (show_cursor) {
174                 if (lw->selected < lw->start) {
175                         lw->start = lw->selected;
176                         lw->clear=1;
177                 }
179                 if (lw->selected >= lw->start + lw->rows) {
180                         lw->start = lw->selected - lw->rows + 1;
181                         lw->clear=1;
182                 }
183         }
185         for (i = 0; i < lw->rows; i++) {
186                 int highlight = 0;
187                 const char *label;
189                 label = callback(lw->start + i, &highlight, callback_data);
190                 wmove(lw->w, i, 0);
191                 if( lw->clear && (!fill || !label) )
192                         wclrtoeol(lw->w);
194                 if (label) {
195                         int selected = lw->start + i == lw->selected;
196                         size_t len = my_strlen(label);
198                         if( highlight )
199                                 colors_use(lw->w, COLOR_LIST_BOLD);
200                         else
201                                 colors_use(lw->w, COLOR_LIST);
203                         if( show_cursor && selected )
204                                 wattron(lw->w, A_REVERSE);
206                         //waddnstr(lw->w, label, lw->cols);
207                         waddstr(lw->w, label);
208                         if( fill && len<lw->cols )
209                                 whline(lw->w,  ' ', lw->cols-len);
211                         if( selected )
212                                 wattroff(lw->w, A_REVERSE);
213                 }
214         }
216         lw->clear=0;
219 int
220 list_window_find(struct list_window *lw,
221                  list_window_callback_fn_t callback,
222                  void *callback_data,
223                  const char *str,
224                  int wrap)
226         int h;
227         unsigned i = lw->selected + 1;
228         const char *label;
230         while (wrap || i == lw->selected + 1) {
231                 while ((label = callback(i,&h,callback_data))) {
232                         if (str && label && strcasestr(label, str)) {
233                                 lw->selected = i;
234                                 return 0;
235                         }
236                         if (wrap && i == lw->selected)
237                                 return 1;
238                         i++;
239                 }
240                 if (wrap) {
241                         if (i == 0) /* empty list */
242                                 return 1;
243                         i=0; /* first item */
244                         screen_bell();
245                 }
246         }
248         return 1;
251 int
252 list_window_rfind(struct list_window *lw,
253                   list_window_callback_fn_t callback,
254                   void *callback_data,
255                   const char *str,
256                   int wrap,
257                   unsigned rows)
259         int h;
260         int i = lw->selected - 1;
261         const char *label;
263         if (rows == 0)
264                 return 1;
266         while (wrap || i == (int)lw->selected - 1) {
267                 while (i >= 0 && (label = callback(i,&h,callback_data))) {
268                         if( str && label && strcasestr(label, str) ) {
269                                 lw->selected = i;
270                                 return 0;
271                         }
272                         if (wrap && i == (int)lw->selected)
273                                 return 1;
274                         i--;
275                 }
276                 if (wrap) {
277                         i = rows - 1; /* last item */
278                         screen_bell();
279                 }
280         }
281         return 1;
284 /* perform basic list window commands (movement) */
285 int
286 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
288         switch (cmd) {
289         case CMD_LIST_PREVIOUS:
290                 list_window_previous(lw, rows);
291                 break;
292         case CMD_LIST_NEXT:
293                 list_window_next(lw, rows);
294                 break;
295         case CMD_LIST_FIRST:
296                 list_window_first(lw);
297                 break;
298         case CMD_LIST_LAST:
299                 list_window_last(lw, rows);
300                 break;
301         case CMD_LIST_NEXT_PAGE:
302                 list_window_next_page(lw, rows);
303                 break;
304         case CMD_LIST_PREVIOUS_PAGE:
305                 list_window_previous_page(lw);
306                 break;
307         default:
308                 return 0;
309         }
311         lw->repaint  = 1;
312         return 1;
315 int
316 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
318         switch (cmd) {
319         case CMD_LIST_PREVIOUS:
320                 if (lw->start > 0)
321                         lw->start--;
322                 break;
324         case CMD_LIST_NEXT:
325                 if (lw->start + lw->rows < rows)
326                         lw->start++;
327                 break;
329         case CMD_LIST_FIRST:
330                 lw->start = 0;
331                 break;
333         case CMD_LIST_LAST:
334                 if (rows > lw->rows)
335                         lw->start = rows - lw->rows;
336                 else
337                         lw->start = 0;
338                 break;
340         case CMD_LIST_NEXT_PAGE:
341                 lw->start += lw->rows - 1;
342                 if (lw->start + lw->rows > rows) {
343                         if (rows > lw->rows)
344                                 lw->start = rows - lw->rows;
345                         else
346                                 lw->start = 0;
347                 }
348                 break;
350         case CMD_LIST_PREVIOUS_PAGE:
351                 if (lw->start > lw->rows)
352                         lw->start -= lw->rows;
353                 else
354                         lw->start = 0;
355                 break;
357         default:
358                 return 0;
359         }
361         lw->repaint = lw->clear = 1;
362         return 1;
365 list_window_state_t *
366 list_window_init_state(void)
368         return g_malloc0(sizeof(list_window_state_t));
371 void
372 list_window_free_state(list_window_state_t *state)
374         if (state) {
375                 if (state->list) {
376                         GList *list = state->list;
378                         while (list) {
379                                 g_free(list->data);
380                                 list->data = NULL;
381                                 list = list->next;
382                         }
384                         g_list_free(state->list);
385                         state->list = NULL;
386                 }
388                 g_free(state);
389         }
392 void
393 list_window_push_state(list_window_state_t *state, struct list_window *lw)
395         if (state) {
396                 struct list_window *tmp = g_malloc(sizeof(list_window_t));
397                 memcpy(tmp, lw, sizeof(list_window_t));
398                 state->list = g_list_prepend(state->list, (gpointer) tmp);
399                 list_window_reset(lw);
400         }
403 bool
404 list_window_pop_state(list_window_state_t *state, struct list_window *lw)
406         if (state && state->list) {
407                 struct list_window *tmp = state->list->data;
409                 memcpy(lw, tmp, sizeof(list_window_t));
410                 g_free(tmp);
411                 state->list->data = NULL;
412                 state->list = g_list_delete_link(state->list, state->list);
413         }
415         // return TRUE if there are still states in the list
416         return (state && state->list) ? TRUE : FALSE;