Code

Hide the cursor on the help screen (#247)
[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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #include "options.h"
29 #include "support.h"
30 #include "command.h"
31 #include "colors.h"
32 #include "list_window.h"
34 extern void screen_bell(void);
36 list_window_t *
37 list_window_init(WINDOW *w, int width, int height)
38 {
39   list_window_t *lw;
41   lw = g_malloc0(sizeof(list_window_t));
42   lw->w = w;
43   lw->cols = width;
44   lw->rows = height;
45   lw->clear = 1;
46   return lw;
47 }
49 list_window_t *
50 list_window_free(list_window_t *lw)
51 {
52   if( lw )
53     {
54       memset(lw, 0, sizeof(list_window_t));
55       g_free(lw);
56     }
57   return NULL;
58 }
60 void
61 list_window_reset(list_window_t *lw)
62 {
63   lw->selected = 0;
64   lw->xoffset = 0;
65   lw->start = 0;
66   lw->clear = 1;
67 }
69 void
70 list_window_check_selected(list_window_t *lw, int length)
71 {
72   while( lw->start && lw->start+lw->rows>length)
73     lw->start--;
75   if( lw->selected<0 )
76     lw->selected=0;
78   while( lw->selected<lw->start )
79     lw->selected++;
81   while( lw->selected>0 && length>0 && lw->selected>=length )
82     lw->selected--;
83 }
85 void 
86 list_window_set_selected(list_window_t *lw, int n)
87 {
88   lw->selected=n;
89 }
91 void
92 list_window_next(list_window_t *lw, int length)
93 {
94   if( lw->selected < length-1 )
95     lw->selected++;
96   else if ( options.list_wrap )
97     lw->selected = 0;
98 }
100 void
101 list_window_previous(list_window_t *lw, int length)
103   if( lw->selected > 0 )
104     lw->selected--;
105   else if( options.list_wrap )
106     lw->selected = length-1;
109 void
110 list_window_right(list_window_t *lw, int length)
112   lw->xoffset++;
115 void
116 list_window_left(list_window_t *lw)
118   if( lw->xoffset > 0 )
119     lw->xoffset--;
122 void
123 list_window_first(list_window_t *lw)
125   lw->xoffset = 0;
126   lw->selected = 0;
129 void
130 list_window_last(list_window_t *lw, int length)
132   lw->xoffset = 0;
133   lw->selected = length-1;
136 void
137 list_window_next_page(list_window_t *lw, int length)
139   int step = lw->rows-1;
140   if( step<= 0 )
141     return;
142   if( lw->selected+step < length-1 )
143     lw->selected+=step;
144   else
145     return list_window_last(lw,length);
148 void
149 list_window_previous_page(list_window_t *lw)
151   int step = lw->rows-1;
152   if( step<= 0 )
153     return;
154   if( lw->selected-step > 0 )
155     lw->selected-=step;
156   else
157     list_window_first(lw);
161 void 
162 list_window_paint(list_window_t *lw,
163                   list_window_callback_fn_t callback,
164                   void *callback_data)
166   int i;
167   int fill = options.wide_cursor;
169   if( lw->flags & LW_HIDE_CURSOR )
170     {
171       lw->selected = -1;
172     }
173   else
174     {
175       while( lw->selected < lw->start )
176         {
177           lw->start--;
178           lw->clear=1;
179         }
180       while( lw->selected >= lw->start+lw->rows )
181         {
182           lw->start++;
183           lw->clear=1;
184         }
185     }
187   for(i=0; i<lw->rows; i++)
188     {
189       int highlight = 0;
190       char *label;
192       label = (callback) (lw->start+i, &highlight, callback_data);
193       wmove(lw->w, i, 0);
194       if( lw->clear && (!fill || !label) )
195         wclrtoeol(lw->w);
196       if( label )
197         {
198           int selected = lw->start+i == lw->selected;
199           size_t len = strlen(label);
201           if( highlight )
202             colors_use(lw->w, COLOR_LIST_BOLD);
203           else
204             colors_use(lw->w, COLOR_LIST);
206           if( selected )
207             wattron(lw->w, A_REVERSE);
208           
209           waddnstr(lw->w, label, lw->cols);
210           if( fill && len<lw->cols )
211             mvwhline(lw->w, i, len, ' ', lw->cols-len);
213           if( selected )
214             wattroff(lw->w, A_REVERSE);   
215         }
216         
217     }
218   lw->clear=0;
222 int
223 list_window_find(list_window_t *lw, 
224                  list_window_callback_fn_t callback,
225                  void *callback_data,
226                  char *str,
227                  int wrap)
229   int h;
230   int i = lw->selected+1;
231   char *label;
232   
233   while( wrap || i==lw->selected+1 )
234     {
235       while( (label=(callback) (i,&h,callback_data)) )
236         {
237           if( str && label && strcasestr(label, str) )
238             {
239               lw->selected = i;
240               return 0;
241             }
242           if( wrap && i==lw->selected )
243             return 1;
244           i++;
245         }
246       if( wrap )
247         {
248           i=0; /* first item */
249           screen_bell();
250         }
251     }
252   return 1;
256 int
257 list_window_rfind(list_window_t *lw, 
258                   list_window_callback_fn_t callback,
259                   void *callback_data,
260                   char *str,
261                   int wrap,
262                   int rows)
264   int h;
265   int i = lw->selected-1;
266   char *label;
268   while( wrap || i==lw->selected-1 )
269     {
270       while( i>=0 && (label=(callback) (i,&h,callback_data)) )
271         {
272           if( str && label && strcasestr(label, str) )
273             {
274               lw->selected = i;
275               return 0;
276             }
277           if( wrap && i==lw->selected )
278             return 1;
279           i--;
280         }
281       if( wrap )
282         {
283           i=rows-1; /* last item */
284           screen_bell();
285         }
286     }
287   return 1;
291 /* perform basic list window commands (movement) */
292 int 
293 list_window_cmd(list_window_t *lw, int rows, command_t cmd)
295   switch(cmd)
296     {
297     case CMD_LIST_PREVIOUS:
298       list_window_previous(lw, rows);
299       lw->repaint=1;
300       break;
301     case CMD_LIST_NEXT:
302       list_window_next(lw, rows);
303       lw->repaint=1;
304       break;
305     case CMD_LIST_FIRST:
306       list_window_first(lw);
307       lw->repaint  = 1;
308       break;
309     case CMD_LIST_LAST:
310       list_window_last(lw, rows);
311       lw->repaint = 1;
312       break;
313     case CMD_LIST_NEXT_PAGE:
314       list_window_next_page(lw, rows);
315       lw->repaint  = 1;
316       break;
317     case CMD_LIST_PREVIOUS_PAGE:
318       list_window_previous_page(lw);
319       lw->repaint  = 1;
320       break;
321     default:
322       return 0;
323     }
324   return 1;