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 "libmpdclient.h"
27 #include "mpc.h"
28 #include "support.h"
29 #include "command.h"
30 #include "options.h"
31 #include "list_window.h"
32 #include "screen.h"
34 #define FIND_PROMPT "Find: "
35 #define RFIND_PROMPT "Find backward: "
37 int
38 screen_getch(WINDOW *w, char *prompt)
39 {
40 int key = -1;
41 int prompt_len = strlen(prompt);
43 wclear(w);
44 wmove(w, 0, 0);
45 waddstr(w, prompt);
46 wmove(w, 0, prompt_len);
48 echo();
49 curs_set(1);
50 timeout(-1);
52 key = wgetch(w);
53 if( key==KEY_RESIZE )
54 screen_resize();
56 noecho();
57 curs_set(0);
58 timeout(SCREEN_TIMEOUT);
60 return key;
61 }
64 char *
65 screen_getstr(WINDOW *w, char *prompt)
66 {
67 char buf[256], *line = NULL;
68 int prompt_len = strlen(prompt);
70 wclear(w);
71 wmove(w, 0, 0);
72 waddstr(w, prompt);
73 wmove(w, 0, prompt_len);
75 echo();
76 curs_set(1);
78 if( wgetnstr(w, buf, 256) == OK )
79 line = g_strdup(buf);
81 noecho();
82 curs_set(0);
84 return line;
85 }
88 /* query user for a string and find it in a list window */
89 int
90 screen_find(screen_t *screen,
91 mpd_client_t *c,
92 list_window_t *lw,
93 int rows,
94 command_t findcmd,
95 list_window_callback_fn_t callback_fn)
96 {
97 int reversed = 0;
98 int retval = 0;
99 char *prompt = FIND_PROMPT;
101 if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT )
102 {
103 prompt = RFIND_PROMPT;
104 reversed = 1;
105 }
107 switch(findcmd)
108 {
109 case CMD_LIST_FIND:
110 case CMD_LIST_RFIND:
111 if( screen->findbuf )
112 {
113 g_free(screen->findbuf);
114 screen->findbuf=NULL;
115 }
116 /* continue... */
117 case CMD_LIST_FIND_NEXT:
118 case CMD_LIST_RFIND_NEXT:
119 if( !screen->findbuf )
120 screen->findbuf=screen_getstr(screen->status_window.w, prompt);
121 if( reversed )
122 retval = list_window_rfind(lw,
123 callback_fn,
124 c,
125 screen->findbuf,
126 options.find_wrap,
127 rows);
128 else
129 retval = list_window_find(lw,
130 callback_fn,
131 c,
132 screen->findbuf,
133 options.find_wrap);
134 if( retval == 0 )
135 {
136 lw->repaint = 1;
137 }
138 else
139 {
140 screen_status_printf("Unable to find \'%s\'", screen->findbuf);
141 beep();
142 }
143 return 1;
144 default:
145 break;
146 }
147 return 0;
148 }
151 int
152 my_waddstr(WINDOW *w, const char *text, int color)
153 {
154 int ret;
156 if( options.enable_colors )
157 wattron(w, color);
158 ret = waddstr(w, text);
159 if( options.enable_colors )
160 wattroff(w, color);
162 return ret;
163 }
165 int
166 my_mvwaddstr(WINDOW *w, int x, int y, const char *text, int color)
167 {
168 int ret;
170 if( options.enable_colors )
171 wattron(w, color);
172 ret = mvwaddstr(w, x, y, text);
173 if( options.enable_colors )
174 wattroff(w, color);
176 return ret;
177 }