Code

Display stream names in the list window.
[ncmpc.git] / src / list_window.c
1 /* 
2  * (c) 2004 by Kalle Wallin (kaw@linux.se)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <ncurses.h>
25 #include "config.h"
26 #include "options.h"
27 #include "support.h"
28 #include "command.h"
29 #include "colors.h"
30 #include "list_window.h"
32 list_window_t *
33 list_window_init(WINDOW *w, int width, int height)
34 {
35   list_window_t *lw;
37   lw = g_malloc(sizeof(list_window_t));
38   memset(lw, 0, sizeof(list_window_t));
39   lw->w = w;
40   lw->cols = width;
41   lw->rows = height;
42   lw->clear = 1;
43   return lw;
44 }
46 list_window_t *
47 list_window_free(list_window_t *lw)
48 {
49   if( lw )
50     {
51       memset(lw, 0, sizeof(list_window_t));
52       g_free(lw);
53     }
54   return NULL;
55 }
57 void
58 list_window_reset(list_window_t *lw)
59 {
60   lw->selected = 0;
61   lw->start = 0;
62   lw->clear = 1;
63 }
65 void
66 list_window_check_selected(list_window_t *lw, int length)
67 {
68   while( lw->start && lw->start+lw->rows>length)
69     lw->start--;
71   if( lw->selected<0 )
72     lw->selected=0;
74   while( lw->selected<lw->start )
75     lw->selected++;
77   while( lw->selected>0 && length>0 && lw->selected>=length )
78     lw->selected--;
79 }
81 void 
82 list_window_set_selected(list_window_t *lw, int n)
83 {
84   lw->selected=n;
85 }
87 void
88 list_window_next(list_window_t *lw, int length)
89 {
90   if( lw->selected < length-1 )
91     lw->selected++;
92 }
94 void
95 list_window_previous(list_window_t *lw)
96 {
97   if( lw->selected > 0 )
98     lw->selected--;
99 }
101 void
102 list_window_first(list_window_t *lw)
104   lw->selected = 0;
107 void
108 list_window_last(list_window_t *lw, int length)
110   lw->selected = length-1;
113 void
114 list_window_next_page(list_window_t *lw, int length)
116   int step = lw->rows-1;
117   if( step<= 0 )
118     return;
119   if( lw->selected+step < length-1 )
120     lw->selected+=step;
121   else
122     return list_window_last(lw,length);
125 void
126 list_window_previous_page(list_window_t *lw)
128   int step = lw->rows-1;
129   if( step<= 0 )
130     return;
131   if( lw->selected-step > 0 )
132     lw->selected-=step;
133   else
134     list_window_first(lw);
138 void 
139 list_window_paint(list_window_t *lw,
140                   list_window_callback_fn_t callback,
141                   void *callback_data)
143   int i;
144   int fill = options.wide_cursor;
146   while( lw->selected < lw->start )
147     {
148       lw->start--;
149       lw->clear=1;
150     }
151   while( lw->selected >= lw->start+lw->rows )
152     {
153       lw->start++;
154       lw->clear=1;
155     }
157   for(i=0; i<lw->rows; i++)
158     {
159       int highlight = 0;
160       char *label;
162       label = (callback) (lw->start+i, &highlight, callback_data);
163       wmove(lw->w, i, 0);
164       if( lw->clear && (!fill || !label) )
165         wclrtoeol(lw->w);
166       if( label )
167         {
168           int selected = lw->start+i == lw->selected;
170           if( highlight )
171             colors_use(lw->w, COLOR_LIST_BOLD);
172           else
173             colors_use(lw->w, COLOR_LIST);
175           if( selected )
176             wattron(lw->w, A_REVERSE);
177           
178           waddnstr(lw->w, label, lw->cols-1);
179           if( fill )
180             mvwhline(lw->w, i, strlen(label), ' ', lw->cols-1);
182           if( selected )
183             wattroff(lw->w, A_REVERSE);   
184         }
185         
186     }
187   lw->clear=0;
191 int
192 list_window_find(list_window_t *lw, 
193                  list_window_callback_fn_t callback,
194                  void *callback_data,
195                  char *str,
196                  int wrap)
198   int h;
199   int i = lw->selected+1;
200   char *label;
201   
202   while( wrap || i==lw->selected+1 )
203     {
204       while( (label=(callback) (i,&h,callback_data)) )
205         {
206           if( str && label && strcasestr(label, str) )
207             {
208               lw->selected = i;
209               return 0;
210             }
211           if( wrap && i==lw->selected )
212             return 1;
213           i++;
214         }
215       if( wrap )
216         {
217           i=0; /* first item */
218           beep(); 
219         }
220     }
221   return 1;
225 int
226 list_window_rfind(list_window_t *lw, 
227                   list_window_callback_fn_t callback,
228                   void *callback_data,
229                   char *str,
230                   int wrap,
231                   int rows)
233   int h;
234   int i = lw->selected-1;
235   char *label;
237   while( wrap || i==lw->selected-1 )
238     {
239       while( i>=0 && (label=(callback) (i,&h,callback_data)) )
240         {
241           if( str && label && strcasestr(label, str) )
242             {
243               lw->selected = i;
244               return 0;
245             }
246           if( wrap && i==lw->selected )
247             return 1;
248           i--;
249         }
250       if( wrap )
251         {
252           i=rows-1; /* last item */
253           beep();
254         }
255     }
256   return 1;
260 /* perform basic list window commands (movement) */
261 int 
262 list_window_cmd(list_window_t *lw, int rows, command_t cmd)
264   switch(cmd)
265     {
266     case CMD_LIST_PREVIOUS:
267       list_window_previous(lw);
268       lw->repaint=1;
269       break;
270     case CMD_LIST_NEXT:
271       list_window_next(lw, rows);
272       lw->repaint=1;
273       break;
274     case CMD_LIST_FIRST:
275       list_window_first(lw);
276       lw->repaint  = 1;
277       break;
278     case CMD_LIST_LAST:
279       list_window_last(lw, rows);
280       lw->repaint = 1;
281       break;
282     case CMD_LIST_NEXT_PAGE:
283       list_window_next_page(lw, rows);
284       lw->repaint  = 1;
285       break;
286     case CMD_LIST_PREVIOUS_PAGE:
287       list_window_previous_page(lw);
288       lw->repaint  = 1;
289       break;
290     default:
291       return 0;
292     }
293   return 1;