Code

Major cleanup of the mpd client code (mpc->mpdclient)
[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   line = wreadln(w, prompt, value, COLS, history, gcmp);
82   curs_set(0);
83   return line;
84 }
86 char *
87 screen_getstr(WINDOW *w, char *prompt)
88 {
89   return screen_readln(w, prompt, NULL, NULL, NULL);
90 }
93 /* query user for a string and find it in a list window */
94 int 
95 screen_find(screen_t *screen,
96             mpdclient_t *c,
97             list_window_t *lw, 
98             int rows,
99             command_t findcmd,
100             list_window_callback_fn_t callback_fn)
102   int reversed = 0;
103   int retval   = 0;
104   char *prompt = FIND_PROMPT;
106   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
107     {
108       prompt = RFIND_PROMPT;
109       reversed = 1;
110     }
112   switch(findcmd)
113     {
114     case CMD_LIST_FIND:
115     case CMD_LIST_RFIND:
116       if( screen->findbuf )
117         {
118           g_free(screen->findbuf);
119           screen->findbuf=NULL;
120         }
121       /* continue... */
122     case CMD_LIST_FIND_NEXT:
123     case CMD_LIST_RFIND_NEXT:
124       if( !screen->findbuf )
125         screen->findbuf=screen_readln(screen->status_window.w,
126                                       prompt,
127                                       (char *) -1, //NULL,
128                                       &screen->find_history,
129                                       NULL);
130       if( !screen->findbuf || !screen->findbuf[0] )
131         return 1; 
132       if( reversed )
133         retval = list_window_rfind(lw, 
134                                    callback_fn,
135                                    c, 
136                                    screen->findbuf,
137                                    options.find_wrap,
138                                    rows);
139       else
140         retval = list_window_find(lw,
141                                   callback_fn,
142                                   c,
143                                   screen->findbuf,
144                                   options.find_wrap);
145       if( retval == 0 )
146         {
147           lw->repaint  = 1;
148         }
149       else
150         {
151           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
152           beep();
153         }
154       return 1;
155     default:
156       break;
157     }
158   return 0;