1 /*
2 * (c) 2004 by Kalle Wallin (kaw@linux.se)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <ncurses.h>
25 #include "config.h"
26 #include "ncmpc.h"
27 #include "libmpdclient.h"
28 #include "mpc.h"
29 #include "support.h"
30 #include "command.h"
31 #include "options.h"
32 #include "list_window.h"
33 #include "colors.h"
34 #include "screen.h"
36 #define FIND_PROMPT _("Find: ")
37 #define RFIND_PROMPT _("Find backward: ")
39 int
40 screen_getch(WINDOW *w, char *prompt)
41 {
42 int key = -1;
43 int prompt_len = strlen(prompt);
45 colors_use(w, COLOR_STATUS_ALERT);
46 wclear(w);
47 wmove(w, 0, 0);
48 waddstr(w, prompt);
49 wmove(w, 0, prompt_len);
51 echo();
52 curs_set(1);
53 timeout(-1);
55 key = wgetch(w);
56 if( key==KEY_RESIZE )
57 screen_resize();
59 noecho();
60 curs_set(0);
61 timeout(SCREEN_TIMEOUT);
63 return key;
64 }
67 char *
68 screen_getstr(WINDOW *w, char *prompt)
69 {
70 char buf[256], *line = NULL;
71 int prompt_len = strlen(prompt);
73 colors_use(w, COLOR_STATUS_ALERT);
74 wclear(w);
75 wmove(w, 0, 0);
76 waddstr(w, prompt);
77 wmove(w, 0, prompt_len);
79 echo();
80 curs_set(1);
82 if( wgetnstr(w, buf, 256) == OK )
83 line = g_strdup(buf);
85 noecho();
86 curs_set(0);
88 return line;
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_getstr(screen->status_window.w, prompt);
125 if( reversed )
126 retval = list_window_rfind(lw,
127 callback_fn,
128 c,
129 screen->findbuf,
130 options.find_wrap,
131 rows);
132 else
133 retval = list_window_find(lw,
134 callback_fn,
135 c,
136 screen->findbuf,
137 options.find_wrap);
138 if( retval == 0 )
139 {
140 lw->repaint = 1;
141 }
142 else
143 {
144 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
145 beep();
146 }
147 return 1;
148 default:
149 break;
150 }
151 return 0;
152 }