Code

Added a search screen
[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 void
42 screen_bell(void)
43 {
44   if( options.audible_bell )
45     beep();
46   if( options.visible_bell )
47     flash();
48 }
50 int
51 screen_getch(WINDOW *w, char *prompt)
52 {
53   int key = -1;
54   int prompt_len = strlen(prompt);
56   colors_use(w, COLOR_STATUS_ALERT);
57   wclear(w);  
58   wmove(w, 0, 0);
59   waddstr(w, prompt);
60   wmove(w, 0, prompt_len);
61   
62   echo();
63   curs_set(1);
64   timeout(-1);
66   while( (key=my_wgetch(w)) == ERR )
67     ;
69 #ifdef HAVE_GETMOUSE
70   /* ignore mouse events */
71   if( key==KEY_MOUSE )
72     return screen_getch(w, prompt);
73 #endif
75   noecho();
76   curs_set(0);
77   timeout(SCREEN_TIMEOUT);
79   return key;
80 }
82 char *
83 screen_readln(WINDOW *w, 
84               char *prompt, 
85               char *value,
86               GList **history,
87               GCompletion *gcmp)
88 {
89   char *line = NULL;
91   wmove(w, 0,0);
92   curs_set(1);
93   colors_use(w, COLOR_STATUS_ALERT);
94   line = wreadln(w, prompt, value, COLS, history, gcmp);
95   curs_set(0);
96   return line;
97 }
99 char *
100 screen_getstr(WINDOW *w, char *prompt)
102   return screen_readln(w, prompt, NULL, NULL, NULL);
106 /* query user for a string and find it in a list window */
107 int 
108 screen_find(screen_t *screen,
109             mpdclient_t *c,
110             list_window_t *lw, 
111             int rows,
112             command_t findcmd,
113             list_window_callback_fn_t callback_fn,
114             void *callback_data)
116   int reversed = 0;
117   int retval   = 0;
118   char *prompt = FIND_PROMPT;
120   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
121     {
122       prompt = RFIND_PROMPT;
123       reversed = 1;
124     }
126   switch(findcmd)
127     {
128     case CMD_LIST_FIND:
129     case CMD_LIST_RFIND:
130       if( screen->findbuf )
131         {
132           g_free(screen->findbuf);
133           screen->findbuf=NULL;
134         }
135       /* continue... */
136     case CMD_LIST_FIND_NEXT:
137     case CMD_LIST_RFIND_NEXT:
138       if( !screen->findbuf )
139         screen->findbuf=screen_readln(screen->status_window.w,
140                                       prompt,
141                                       (char *) -1, //NULL,
142                                       &screen->find_history,
143                                       NULL);
144       if( !screen->findbuf || !screen->findbuf[0] )
145         return 1; 
146       if( reversed )
147         retval = list_window_rfind(lw, 
148                                    callback_fn,
149                                    callback_data, 
150                                    screen->findbuf,
151                                    options.find_wrap,
152                                    rows);
153       else
154         retval = list_window_find(lw,
155                                   callback_fn,
156                                   callback_data,
157                                   screen->findbuf,
158                                   options.find_wrap);
159       if( retval == 0 )
160         {
161           lw->repaint  = 1;
162         }
163       else
164         {
165           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
166           screen_bell();
167         }
168       return 1;
169     default:
170       break;
171     }
172   return 0;
175 void
176 screen_display_completion_list(screen_t *screen, GList *list)
178   static GList *prev_list = NULL;
179   static gint prev_length = 0;
180   static gint offset = 0;
181   WINDOW *w = screen->main_window.w;
182   gint length, y=0;
184   length = g_list_length(list);
185   if( list==prev_list && length==prev_length )
186     {
187       offset += screen->main_window.rows;
188       if( offset>=length )
189         offset=0;
190     }
191   else
192     {
193       prev_list = list;
194       prev_length = length;
195       offset = 0;
196     }
198   colors_use(w, COLOR_STATUS_ALERT);
199   while( y<screen->main_window.rows )
200     {
201       GList *item = g_list_nth(list, y+offset);
203       wmove(w, y++, 0);
204       wclrtoeol(w);
205       if( item )
206         {
207           gchar *tmp = g_strdup(item->data);
208           waddstr(w, basename(tmp));
209           g_free(tmp);
210         }
211     }
212   wrefresh(w);
213   doupdate();
214   colors_use(w, COLOR_LIST);
217 void
218 set_xterm_title(char *format, ...)
220   /* the current xterm title exists under the WM_NAME property */
221   /* and can be retreived with xprop -id $WINDOWID */
223   if( options.enable_xterm_title )
224     {
225       if( g_getenv("WINDOWID") )
226         {
227           char *msg;
228           va_list ap;
229           
230           va_start(ap,format);
231           msg = g_strdup_vprintf(format,ap);
232           va_end(ap);
233           printf("%c]0;%s%c", '\033', msg, '\007'); 
234           g_free(msg);
235         }
236       else
237         options.enable_xterm_title = FALSE;
238     }