Code

list_window: added list_window_center()
[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 struct list_window *
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         }
55         return NULL;
56 }
58 void
59 list_window_reset(struct list_window *lw)
60 {
61         lw->selected = 0;
62         lw->xoffset = 0;
63         lw->start = 0;
64         lw->clear = 1;
65 }
67 void
68 list_window_check_selected(struct list_window *lw, unsigned length)
69 {
70         if (lw->start + lw->rows > length) {
71                 if (length > lw->rows)
72                         lw->start = length - lw->rows;
73                 else
74                         lw->start = 0;
75         }
77         if (lw->selected < lw->start)
78                 lw->selected = lw->start;
80         if (length > 0 && lw->selected >= length)
81                 lw->selected = length - 1;
82 }
84 void
85 list_window_center(struct list_window *lw, unsigned rows, unsigned n)
86 {
87         if (n > lw->rows / 2)
88                 lw->start = n - lw->rows / 2;
89         else
90                 lw->start = 0;
92         if (lw->start + lw->rows > rows) {
93                 if (lw->rows < rows)
94                         lw->start = rows - lw->rows;
95                 else
96                         lw->start = 0;
97         }
99         lw->repaint = lw->clear = 1;
102 void
103 list_window_set_selected(struct list_window *lw, unsigned n)
105         lw->selected = n;
108 void
109 list_window_next(struct list_window *lw, unsigned length)
111         if (lw->selected + 1 < length)
112                 lw->selected++;
113         else if (options.list_wrap)
114                 lw->selected = 0;
117 void
118 list_window_previous(struct list_window *lw, unsigned length)
120         if (lw->selected > 0)
121                 lw->selected--;
122         else if (options.list_wrap)
123                 lw->selected = length - 1;
126 void
127 list_window_first(struct list_window *lw)
129         lw->xoffset = 0;
130         lw->selected = 0;
133 void
134 list_window_last(struct list_window *lw, unsigned length)
136         lw->xoffset = 0;
137         if (length > 0)
138                 lw->selected = length - 1;
139         else
140                 lw->selected = 0;
143 void
144 list_window_next_page(struct list_window *lw, unsigned length)
146         if (lw->rows < 2)
147                 return;
148         if (lw->selected + lw->rows < length)
149                 lw->selected += lw->rows - 1;
150         else
151                 return list_window_last(lw, length);
154 void
155 list_window_previous_page(struct list_window *lw)
157         if (lw->rows < 2)
158                 return;
159         if (lw->selected > lw->rows - 1)
160                 lw->selected -= lw->rows - 1;
161         else
162                 list_window_first(lw);
166 void
167 list_window_paint(struct list_window *lw,
168                   list_window_callback_fn_t callback,
169                   void *callback_data)
171         unsigned i;
172         int fill = options.wide_cursor;
173         int show_cursor = !(lw->flags & LW_HIDE_CURSOR);
175         if (show_cursor) {
176                 if (lw->selected < lw->start) {
177                         lw->start = lw->selected;
178                         lw->clear=1;
179                 }
181                 if (lw->selected >= lw->start + lw->rows) {
182                         lw->start = lw->selected - lw->rows + 1;
183                         lw->clear=1;
184                 }
185         }
187         for (i = 0; i < lw->rows; i++) {
188                 int highlight = 0;
189                 const char *label;
191                 label = callback(lw->start + i, &highlight, callback_data);
192                 wmove(lw->w, i, 0);
193                 if( lw->clear && (!fill || !label) )
194                         wclrtoeol(lw->w);
196                 if (label) {
197                         int selected = lw->start + i == lw->selected;
198                         size_t len = my_strlen(label);
200                         if( highlight )
201                                 colors_use(lw->w, COLOR_LIST_BOLD);
202                         else
203                                 colors_use(lw->w, COLOR_LIST);
205                         if( show_cursor && selected )
206                                 wattron(lw->w, A_REVERSE);
208                         //waddnstr(lw->w, label, lw->cols);
209                         waddstr(lw->w, label);
210                         if( fill && len<lw->cols )
211                                 whline(lw->w,  ' ', lw->cols-len);
213                         if( selected )
214                                 wattroff(lw->w, A_REVERSE);
215                 }
216         }
218         lw->clear=0;
221 int
222 list_window_find(struct list_window *lw,
223                  list_window_callback_fn_t callback,
224                  void *callback_data,
225                  const char *str,
226                  int wrap)
228         int h;
229         unsigned i = lw->selected + 1;
230         const char *label;
232         while (wrap || i == lw->selected + 1) {
233                 while ((label = callback(i,&h,callback_data))) {
234                         if (str && label && strcasestr(label, str)) {
235                                 lw->selected = i;
236                                 return 0;
237                         }
238                         if (wrap && i == lw->selected)
239                                 return 1;
240                         i++;
241                 }
242                 if (wrap) {
243                         if (i == 0) /* empty list */
244                                 return 1;
245                         i=0; /* first item */
246                         screen_bell();
247                 }
248         }
250         return 1;
253 int
254 list_window_rfind(struct list_window *lw,
255                   list_window_callback_fn_t callback,
256                   void *callback_data,
257                   const char *str,
258                   int wrap,
259                   unsigned rows)
261         int h;
262         int i = lw->selected - 1;
263         const char *label;
265         if (rows == 0)
266                 return 1;
268         while (wrap || i == (int)lw->selected - 1) {
269                 while (i >= 0 && (label = callback(i,&h,callback_data))) {
270                         if( str && label && strcasestr(label, str) ) {
271                                 lw->selected = i;
272                                 return 0;
273                         }
274                         if (wrap && i == (int)lw->selected)
275                                 return 1;
276                         i--;
277                 }
278                 if (wrap) {
279                         i = rows - 1; /* last item */
280                         screen_bell();
281                 }
282         }
283         return 1;
286 /* perform basic list window commands (movement) */
287 int
288 list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
290         switch (cmd) {
291         case CMD_LIST_PREVIOUS:
292                 list_window_previous(lw, rows);
293                 break;
294         case CMD_LIST_NEXT:
295                 list_window_next(lw, rows);
296                 break;
297         case CMD_LIST_FIRST:
298                 list_window_first(lw);
299                 break;
300         case CMD_LIST_LAST:
301                 list_window_last(lw, rows);
302                 break;
303         case CMD_LIST_NEXT_PAGE:
304                 list_window_next_page(lw, rows);
305                 break;
306         case CMD_LIST_PREVIOUS_PAGE:
307                 list_window_previous_page(lw);
308                 break;
309         default:
310                 return 0;
311         }
313         lw->repaint  = 1;
314         return 1;
317 int
318 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
320         switch (cmd) {
321         case CMD_LIST_PREVIOUS:
322                 if (lw->start > 0)
323                         lw->start--;
324                 break;
326         case CMD_LIST_NEXT:
327                 if (lw->start + lw->rows < rows)
328                         lw->start++;
329                 break;
331         case CMD_LIST_FIRST:
332                 lw->start = 0;
333                 break;
335         case CMD_LIST_LAST:
336                 if (rows > lw->rows)
337                         lw->start = rows - lw->rows;
338                 else
339                         lw->start = 0;
340                 break;
342         case CMD_LIST_NEXT_PAGE:
343                 lw->start += lw->rows - 1;
344                 if (lw->start + lw->rows > rows) {
345                         if (rows > lw->rows)
346                                 lw->start = rows - lw->rows;
347                         else
348                                 lw->start = 0;
349                 }
350                 break;
352         case CMD_LIST_PREVIOUS_PAGE:
353                 if (lw->start > lw->rows)
354                         lw->start -= lw->rows;
355                 else
356                         lw->start = 0;
357                 break;
359         default:
360                 return 0;
361         }
363         lw->repaint = lw->clear = 1;
364         return 1;
367 list_window_state_t *
368 list_window_init_state(void)
370         return g_malloc0(sizeof(list_window_state_t));
373 list_window_state_t *
374 list_window_free_state(list_window_state_t *state)
376         if (state) {
377                 if (state->list) {
378                         GList *list = state->list;
380                         while (list) {
381                                 g_free(list->data);
382                                 list->data = NULL;
383                                 list = list->next;
384                         }
386                         g_list_free(state->list);
387                         state->list = NULL;
388                 }
390                 g_free(state);
391         }
393         return NULL;
396 void
397 list_window_push_state(list_window_state_t *state, struct list_window *lw)
399         if (state) {
400                 struct list_window *tmp = g_malloc(sizeof(list_window_t));
401                 memcpy(tmp, lw, sizeof(list_window_t));
402                 state->list = g_list_prepend(state->list, (gpointer) tmp);
403                 list_window_reset(lw);
404         }
407 bool
408 list_window_pop_state(list_window_state_t *state, struct list_window *lw)
410         if (state && state->list) {
411                 struct list_window *tmp = state->list->data;
413                 memcpy(lw, tmp, sizeof(list_window_t));
414                 g_free(tmp);
415                 state->list->data = NULL;
416                 state->list = g_list_delete_link(state->list, state->list);
417         }
419         // return TRUE if there are still states in the list
420         return (state && state->list) ? TRUE : FALSE;