Code

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