Code

Changed directory layout (for future use of gettext)
[ncmpc.git] / src / screen_utils.c
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 "colors.h"
33 #include "screen.h"
35 #define FIND_PROMPT  "Find: "
36 #define RFIND_PROMPT "Find backward: "
38 int
39 screen_getch(WINDOW *w, char *prompt)
40 {
41   int key = -1;
42   int prompt_len = strlen(prompt);
44   colors_use(w, COLOR_STATUS_ALERT);
45   wclear(w);  
46   wmove(w, 0, 0);
47   waddstr(w, prompt);
48   wmove(w, 0, prompt_len);
49   
50   echo();
51   curs_set(1);
52   timeout(-1);
54   key = wgetch(w);
55   if( key==KEY_RESIZE )
56     screen_resize();
58   noecho();
59   curs_set(0);
60   timeout(SCREEN_TIMEOUT);
62   return key;
63 }
66 char *
67 screen_getstr(WINDOW *w, char *prompt)
68 {
69   char buf[256], *line = NULL;
70   int prompt_len = strlen(prompt);
72   colors_use(w, COLOR_STATUS_ALERT);
73   wclear(w);  
74   wmove(w, 0, 0);
75   waddstr(w, prompt);
76   wmove(w, 0, prompt_len);
77   
78   echo();
79   curs_set(1);
81   if( wgetnstr(w, buf, 256) == OK )
82     line = g_strdup(buf);
84   noecho();
85   curs_set(0);
87   return line;
88 }
91 /* query user for a string and find it in a list window */
92 int 
93 screen_find(screen_t *screen,
94             mpd_client_t *c,
95             list_window_t *lw, 
96             int rows,
97             command_t findcmd,
98             list_window_callback_fn_t callback_fn)
99 {
100   int reversed = 0;
101   int retval   = 0;
102   char *prompt = FIND_PROMPT;
104   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
105     {
106       prompt = RFIND_PROMPT;
107       reversed = 1;
108     }
110   switch(findcmd)
111     {
112     case CMD_LIST_FIND:
113     case CMD_LIST_RFIND:
114       if( screen->findbuf )
115         {
116           g_free(screen->findbuf);
117           screen->findbuf=NULL;
118         }
119       /* continue... */
120     case CMD_LIST_FIND_NEXT:
121     case CMD_LIST_RFIND_NEXT:
122       if( !screen->findbuf )
123         screen->findbuf=screen_getstr(screen->status_window.w, prompt);
124       if( reversed )
125         retval = list_window_rfind(lw, 
126                                    callback_fn,
127                                    c, 
128                                    screen->findbuf,
129                                    options.find_wrap,
130                                    rows);
131       else
132         retval = list_window_find(lw,
133                                   callback_fn,
134                                   c,
135                                   screen->findbuf,
136                                   options.find_wrap);
137       if( retval == 0 )
138         {
139           lw->repaint  = 1;
140         }
141       else
142         {
143           screen_status_printf("Unable to find \'%s\'", screen->findbuf);
144           beep();
145         }
146       return 1;
147     default:
148       break;
149     }
150   return 0;