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 "libmpdclient.h"
30 #include "mpc.h"
31 #include "support.h"
32 #include "command.h"
33 #include "options.h"
34 #include "list_window.h"
35 #include "colors.h"
36 #include "wreadln.h"
37 #include "screen.h"
39 #define FIND_PROMPT _("Find: ")
40 #define RFIND_PROMPT _("Find backward: ")
42 int
43 screen_getch(WINDOW *w, char *prompt)
44 {
45 int key = -1;
46 int prompt_len = strlen(prompt);
48 colors_use(w, COLOR_STATUS_ALERT);
49 wclear(w);
50 wmove(w, 0, 0);
51 waddstr(w, prompt);
52 wmove(w, 0, prompt_len);
54 echo();
55 curs_set(1);
56 timeout(-1);
58 key = wgetch(w);
59 if( key==KEY_RESIZE )
60 screen_resize();
62 noecho();
63 curs_set(0);
64 timeout(SCREEN_TIMEOUT);
66 return key;
67 }
69 char *
70 screen_readln(WINDOW *w,
71 char *prompt,
72 char *value,
73 GList **history,
74 GCompletion *gcmp)
75 {
76 char *line = NULL;
78 wmove(w, 0,0);
79 curs_set(1);
80 line = wreadln(w, prompt, value, COLS, history, gcmp);
81 curs_set(0);
82 return line;
83 }
85 char *
86 screen_getstr(WINDOW *w, char *prompt)
87 {
88 return screen_readln(w, prompt, NULL, NULL, NULL);
89 }
92 /* query user for a string and find it in a list window */
93 int
94 screen_find(screen_t *screen,
95 mpd_client_t *c,
96 list_window_t *lw,
97 int rows,
98 command_t findcmd,
99 list_window_callback_fn_t callback_fn)
100 {
101 int reversed = 0;
102 int retval = 0;
103 char *prompt = FIND_PROMPT;
105 if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT )
106 {
107 prompt = RFIND_PROMPT;
108 reversed = 1;
109 }
111 switch(findcmd)
112 {
113 case CMD_LIST_FIND:
114 case CMD_LIST_RFIND:
115 if( screen->findbuf )
116 {
117 g_free(screen->findbuf);
118 screen->findbuf=NULL;
119 }
120 /* continue... */
121 case CMD_LIST_FIND_NEXT:
122 case CMD_LIST_RFIND_NEXT:
123 if( !screen->findbuf )
124 screen->findbuf=screen_readln(screen->status_window.w,
125 prompt,
126 (char *) -1, //NULL,
127 &screen->find_history,
128 NULL);
129 if( !screen->findbuf || !screen->findbuf[0] )
130 return 1;
131 if( reversed )
132 retval = list_window_rfind(lw,
133 callback_fn,
134 c,
135 screen->findbuf,
136 options.find_wrap,
137 rows);
138 else
139 retval = list_window_find(lw,
140 callback_fn,
141 c,
142 screen->findbuf,
143 options.find_wrap);
144 if( retval == 0 )
145 {
146 lw->repaint = 1;
147 }
148 else
149 {
150 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
151 beep();
152 }
153 return 1;
154 default:
155 break;
156 }
157 return 0;
158 }