Code

Added completion stuff
[ncmpc.git] / src / screen_utils.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 "ncmpc.h"
29 #include "mpdclient.h"
30 #include "support.h"
31 #include "command.h"
32 #include "options.h"
33 #include "list_window.h"
34 #include "colors.h"
35 #include "wreadln.h"
36 #include "screen.h"
38 #define FIND_PROMPT  _("Find: ")
39 #define RFIND_PROMPT _("Find backward: ")
41 int
42 screen_getch(WINDOW *w, char *prompt)
43 {
44   int key = -1;
45   int prompt_len = strlen(prompt);
47   colors_use(w, COLOR_STATUS_ALERT);
48   wclear(w);  
49   wmove(w, 0, 0);
50   waddstr(w, prompt);
51   wmove(w, 0, prompt_len);
52   
53   echo();
54   curs_set(1);
55   timeout(-1);
57   while( (key=wgetch(w)) == ERR )
58     ;
59   
60   if( key==KEY_RESIZE )
61     screen_resize();
63   noecho();
64   curs_set(0);
65   timeout(SCREEN_TIMEOUT);
67   return key;
68 }
70 char *
71 screen_readln(WINDOW *w, 
72               char *prompt, 
73               char *value,
74               GList **history,
75               GCompletion *gcmp)
76 {
77   char *line = NULL;
79   wmove(w, 0,0);
80   curs_set(1);
81   colors_use(w, COLOR_STATUS_ALERT);
82   line = wreadln(w, prompt, value, COLS, history, gcmp);
83   curs_set(0);
84   return line;
85 }
87 char *
88 screen_getstr(WINDOW *w, char *prompt)
89 {
90   return screen_readln(w, prompt, NULL, NULL, NULL);
91 }
94 /* query user for a string and find it in a list window */
95 int 
96 screen_find(screen_t *screen,
97             mpdclient_t *c,
98             list_window_t *lw, 
99             int rows,
100             command_t findcmd,
101             list_window_callback_fn_t callback_fn)
103   int reversed = 0;
104   int retval   = 0;
105   char *prompt = FIND_PROMPT;
107   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
108     {
109       prompt = RFIND_PROMPT;
110       reversed = 1;
111     }
113   switch(findcmd)
114     {
115     case CMD_LIST_FIND:
116     case CMD_LIST_RFIND:
117       if( screen->findbuf )
118         {
119           g_free(screen->findbuf);
120           screen->findbuf=NULL;
121         }
122       /* continue... */
123     case CMD_LIST_FIND_NEXT:
124     case CMD_LIST_RFIND_NEXT:
125       if( !screen->findbuf )
126         screen->findbuf=screen_readln(screen->status_window.w,
127                                       prompt,
128                                       (char *) -1, //NULL,
129                                       &screen->find_history,
130                                       NULL);
131       if( !screen->findbuf || !screen->findbuf[0] )
132         return 1; 
133       if( reversed )
134         retval = list_window_rfind(lw, 
135                                    callback_fn,
136                                    c, 
137                                    screen->findbuf,
138                                    options.find_wrap,
139                                    rows);
140       else
141         retval = list_window_find(lw,
142                                   callback_fn,
143                                   c,
144                                   screen->findbuf,
145                                   options.find_wrap);
146       if( retval == 0 )
147         {
148           lw->repaint  = 1;
149         }
150       else
151         {
152           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
153           beep();
154         }
155       return 1;
156     default:
157       break;
158     }
159   return 0;
162 void
163 screen_display_completion_list(screen_t *screen, GList *list)
165   WINDOW *w = screen->main_window.w;
166   gint y=0;
168   colors_use(w, COLOR_STATUS_ALERT);
169   while( y<screen->main_window.rows )
170     {
171       wmove(w, y++, 0);
172       wclrtoeol(w);
173       if( list )
174         {
175           gchar *tmp = g_strdup(list->data);
176           waddstr(w, basename(tmp));
177           g_free(tmp);
178           list = list->next;
179         }
180     }
181   wrefresh(w);
182   doupdate();
183   colors_use(w, COLOR_LIST);