Code

Added a search screen
[ncmpc.git] / src / screen_help.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 <string.h>
23 #include <glib.h>
24 #include <ncurses.h>
26 #include "config.h"
27 #include "ncmpc.h"
28 #include "mpdclient.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "screen_utils.h"
34 typedef struct
35 {
36   signed char highlight;
37   command_t command;
38   char *text;
39 } help_text_row_t;
41 static help_text_row_t help_text[] = 
42 {
43   { 1, CMD_NONE,           N_("Keys - Movement") },
44   { 2, CMD_NONE,           NULL },
45   { 0, CMD_LIST_PREVIOUS,  NULL },
46   { 0, CMD_LIST_NEXT,      NULL },
47   { 0, CMD_LIST_PREVIOUS_PAGE, NULL }, 
48   { 0, CMD_LIST_NEXT_PAGE, NULL },
49   { 0, CMD_LIST_FIRST,     NULL },
50   { 0, CMD_LIST_LAST,      NULL },
51   { 0, CMD_NONE,           NULL },
52   { 0, CMD_SCREEN_NEXT,    NULL },
53   { 0, CMD_SCREEN_HELP,    NULL },
54   { 0, CMD_SCREEN_PLAY,    NULL },
55   { 0, CMD_SCREEN_FILE,    NULL },
56 #ifdef ENABLE_SEARCH_SCREEN
57   { 0, CMD_SCREEN_SEARCH,  NULL },
58 #endif
59 #ifdef ENABLE_CLOCK_SCREEN
60   { 0, CMD_SCREEN_CLOCK,   NULL },
61 #endif
62 #ifdef ENABLE_KEYDEF_SCREEN
63   { 0, CMD_SCREEN_KEYDEF,  NULL },
64 #endif
66   { 0, CMD_NONE,           NULL },
67   { 0, CMD_NONE,           NULL },
68   { 1, CMD_NONE,           N_("Keys - Global") },
69   { 2, CMD_NONE,           NULL },
70   { 0, CMD_STOP,           NULL },
71   { 0, CMD_PAUSE,          NULL },
72   { 0, CMD_TRACK_NEXT,     NULL },
73   { 0, CMD_TRACK_PREVIOUS, NULL },
74   { 0, CMD_SEEK_FORWARD,   NULL },
75   { 0, CMD_SEEK_BACKWARD,  NULL },
76   { 0, CMD_VOLUME_DOWN,    NULL },
77   { 0, CMD_VOLUME_UP,      NULL },
78   { 0, CMD_NONE,           NULL },
79   { 0, CMD_REPEAT,         NULL },
80   { 0, CMD_RANDOM,         NULL },
81   { 0, CMD_CROSSFADE,      NULL },
82   { 0, CMD_SHUFFLE,        NULL },
83   { 0, CMD_DB_UPDATE,      NULL },
84   { 0, CMD_NONE,           NULL },
85   { 0, CMD_LIST_FIND,      NULL },
86   { 0, CMD_LIST_RFIND,     NULL },
87   { 0, CMD_LIST_FIND_NEXT, NULL },
88   { 0, CMD_LIST_RFIND_NEXT,  NULL },
89   { 0, CMD_TOGGLE_FIND_WRAP, NULL },
90   { 0, CMD_NONE,           NULL },
91   { 0, CMD_QUIT,           NULL },
93   { 0, CMD_NONE,           NULL },
94   { 0, CMD_NONE,           NULL },
95   { 1, CMD_NONE,           N_("Keys - Playlist screen") },
96   { 2, CMD_NONE,           NULL },
97   { 0, CMD_PLAY,           N_("Play") },
98   { 0, CMD_DELETE,         NULL },
99   { 0, CMD_CLEAR,          NULL },
100   { 0, CMD_LIST_MOVE_UP,   N_("Move song up") },
101   { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
102   { 0, CMD_ADD,            NULL },
103   { 0, CMD_SAVE_PLAYLIST,  NULL },
104   { 0, CMD_SCREEN_UPDATE,  N_("Center") },
105   { 0, CMD_TOGGLE_AUTOCENTER, NULL },
107   { 0, CMD_NONE,           NULL },
108   { 0, CMD_NONE,           NULL },
109   { 1, CMD_NONE,           N_("Keys - Browse screen") },
110   { 2, CMD_NONE,           NULL },
111   { 0, CMD_PLAY,           N_("Enter directory/Select and play song") },
112   { 0, CMD_SELECT,         NULL },
113   { 0, CMD_DELETE,         N_("Delete playlist") },
114   { 0, CMD_SCREEN_UPDATE,  NULL },
116 #ifdef ENABLE_SEARCH_SCREEN
117   { 0, CMD_NONE,           NULL },
118   { 0, CMD_NONE,           NULL },
119   { 1, CMD_NONE,           N_("Keys - Search screen") },
120   { 2, CMD_NONE,           NULL },
121   { 0, CMD_SCREEN_SEARCH,  N_("Search") },
122   { 0, CMD_PLAY,           N_("Select and play") },
123   { 0, CMD_SELECT,         NULL },
124   { 0, CMD_SEARCH_MODE,    NULL },
125 #endif
127   { 0, CMD_NONE, NULL },
128   {-1, CMD_NONE, NULL }
129 };
131 static int help_text_rows = -1;
132 static list_window_t *lw = NULL;
135 static char *
136 list_callback(int index, int *highlight, void *data)
138   static char buf[512];
140   if( help_text_rows<0 )
141     {
142       help_text_rows = 0;
143       while( help_text[help_text_rows].highlight != -1 )
144         help_text_rows++;
145     }
147   *highlight = 0;
148   if( index<help_text_rows )
149     {
150       *highlight = help_text[index].highlight > 0;
151       if( help_text[index].command == CMD_NONE )
152         {
153           if( help_text[index].text )
154             g_snprintf(buf, sizeof(buf), "      %s", _(help_text[index].text));
155           else 
156             if( help_text[index].highlight == 2 )
157               {
158                 int i;
160                 for(i=3; i<COLS-3 && i<sizeof(buf); i++)
161                   buf[i]='-';
162                 buf[i] = '\0';
163               }
164             else
165               g_strlcpy(buf, " ", sizeof(buf));
166           return buf;
167         }
168       if( help_text[index].text )
169         g_snprintf(buf, sizeof(buf), 
170                    "%20s : %s   ", 
171                    get_key_names(help_text[index].command, TRUE),
172                    _(help_text[index].text));
173       else
174         g_snprintf(buf, sizeof(buf), 
175                    "%20s : %s   ", 
176                    get_key_names(help_text[index].command, TRUE),
177                    get_key_description(help_text[index].command));
178       return buf;
179     }
181   return NULL;
184 static void
185 help_init(WINDOW *w, int cols, int rows)
187   lw = list_window_init(w, cols, rows);
188   lw->flags = LW_HIDE_CURSOR;
191 static void
192 help_resize(int cols, int rows)
194   lw->cols = cols;
195   lw->rows = rows;
198 static void
199 help_exit(void)
201   list_window_free(lw);
205 static char *
206 help_title(char *str, size_t size)
208   return _("Help");
211 static void 
212 help_paint(screen_t *screen, mpdclient_t *c)
214   lw->clear = 1;
215   list_window_paint(lw, list_callback, NULL);
216   wrefresh(lw->w);
219 static void 
220 help_update(screen_t *screen, mpdclient_t *c)
221 {  
222   if( lw->repaint )
223     {
224       list_window_paint(lw, list_callback, NULL);
225       wrefresh(lw->w);
226       lw->repaint = 0;
227     }
231 static int 
232 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
234   lw->repaint=1;
235   switch(cmd)
236     {
237     case CMD_LIST_NEXT:
238       if( lw->start+lw->rows < help_text_rows )
239         lw->start++;
240       return 1;
241     case CMD_LIST_PREVIOUS:
242       if( lw->start >0 )
243         lw->start--;
244       return 1;
245     case CMD_LIST_FIRST:
246       lw->start = 0;
247       return 1;
248     case CMD_LIST_LAST:
249       lw->start = help_text_rows-lw->rows;
250       if( lw->start<0 )
251         lw->start = 0;
252       return 1;
253     case CMD_LIST_NEXT_PAGE:
254       lw->start = lw->start + lw->rows;
255       if( lw->start+lw->rows >= help_text_rows )
256         lw->start = help_text_rows-lw->rows;
257       if( lw->start<0 )
258         lw->start = 0;
259        return 1;
260     case CMD_LIST_PREVIOUS_PAGE:
261       lw->start = lw->start - lw->rows;
262       if( lw->start<0 )
263         lw->start = 0;
264       return 1;
265     default:
266       break;
267     }
269   lw->selected = lw->start+lw->rows;
270   if( screen_find(screen, c, 
271                   lw,  help_text_rows,
272                   cmd, list_callback, NULL) )
273     {
274       /* center the row */
275       lw->start = lw->selected-(lw->rows/2);
276       if( lw->start+lw->rows > help_text_rows )
277         lw->start = help_text_rows-lw->rows;
278       if( lw->start<0 )
279         lw->start=0;
280       return 1;
281     }
283   return 0;
286 static list_window_t *
287 help_lw(void)
289   return lw;
292 screen_functions_t *
293 get_screen_help(void)
295   static screen_functions_t functions;
297   memset(&functions, 0, sizeof(screen_functions_t));
298   functions.init   = help_init;
299   functions.exit   = help_exit;
300   functions.open   = NULL;
301   functions.close  = NULL;
302   functions.resize = help_resize;
303   functions.paint  = help_paint;
304   functions.update = help_update;
305   functions.cmd    = help_cmd;
306   functions.get_lw = help_lw;
307   functions.get_title = help_title;
309   return &functions;