Code

Use my_wgetch() instead of wgetch(), added --[no-]mouse option
[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)
115   int reversed = 0;
116   int retval   = 0;
117   char *prompt = FIND_PROMPT;
119   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
120     {
121       prompt = RFIND_PROMPT;
122       reversed = 1;
123     }
125   switch(findcmd)
126     {
127     case CMD_LIST_FIND:
128     case CMD_LIST_RFIND:
129       if( screen->findbuf )
130         {
131           g_free(screen->findbuf);
132           screen->findbuf=NULL;
133         }
134       /* continue... */
135     case CMD_LIST_FIND_NEXT:
136     case CMD_LIST_RFIND_NEXT:
137       if( !screen->findbuf )
138         screen->findbuf=screen_readln(screen->status_window.w,
139                                       prompt,
140                                       (char *) -1, //NULL,
141                                       &screen->find_history,
142                                       NULL);
143       if( !screen->findbuf || !screen->findbuf[0] )
144         return 1; 
145       if( reversed )
146         retval = list_window_rfind(lw, 
147                                    callback_fn,
148                                    c, 
149                                    screen->findbuf,
150                                    options.find_wrap,
151                                    rows);
152       else
153         retval = list_window_find(lw,
154                                   callback_fn,
155                                   c,
156                                   screen->findbuf,
157                                   options.find_wrap);
158       if( retval == 0 )
159         {
160           lw->repaint  = 1;
161         }
162       else
163         {
164           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
165           screen_bell();
166         }
167       return 1;
168     default:
169       break;
170     }
171   return 0;
174 void
175 screen_display_completion_list(screen_t *screen, GList *list)
177   static GList *prev_list = NULL;
178   static gint prev_length = 0;
179   static gint offset = 0;
180   WINDOW *w = screen->main_window.w;
181   gint length, y=0;
183   length = g_list_length(list);
184   if( list==prev_list && length==prev_length )
185     {
186       offset += screen->main_window.rows;
187       if( offset>=length )
188         offset=0;
189     }
190   else
191     {
192       prev_list = list;
193       prev_length = length;
194       offset = 0;
195     }
197   colors_use(w, COLOR_STATUS_ALERT);
198   while( y<screen->main_window.rows )
199     {
200       GList *item = g_list_nth(list, y+offset);
202       wmove(w, y++, 0);
203       wclrtoeol(w);
204       if( item )
205         {
206           gchar *tmp = g_strdup(item->data);
207           waddstr(w, basename(tmp));
208           g_free(tmp);
209         }
210     }
211   wrefresh(w);
212   doupdate();
213   colors_use(w, COLOR_LIST);
216 void
217 set_xterm_title(char *format, ...)
219   /* the current xterm title exists under the WM_NAME property */
220   /* and can be retreived with xprop -id $WINDOWID */
222   if( options.enable_xterm_title )
223     {
224       if( g_getenv("WINDOWID") )
225         {
226           char buffer[512];
227           va_list ap;
228           
229           va_start(ap,format);
230           vsnprintf(buffer,sizeof(buffer),format,ap);
231           va_end(ap);
232           printf("%c]0;%s%c", '\033', buffer, '\007'); 
233         }
234       else
235         options.enable_xterm_title = FALSE;
236     }