Code

Enable playlist saving on the browse 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_SAVE_PLAYLIST,  NULL },
114   { 0, CMD_DELETE,         N_("Delete playlist") },
115   { 0, CMD_SCREEN_UPDATE,  NULL },
117 #ifdef ENABLE_SEARCH_SCREEN
118   { 0, CMD_NONE,           NULL },
119   { 0, CMD_NONE,           NULL },
120   { 1, CMD_NONE,           N_("Keys - Search screen") },
121   { 2, CMD_NONE,           NULL },
122   { 0, CMD_SCREEN_SEARCH,  N_("Search") },
123   { 0, CMD_PLAY,           N_("Select and play") },
124   { 0, CMD_SELECT,         NULL },
125   { 0, CMD_SEARCH_MODE,    NULL },
126 #endif
128   { 0, CMD_NONE, NULL },
129   {-1, CMD_NONE, NULL }
130 };
132 static int help_text_rows = -1;
133 static list_window_t *lw = NULL;
136 static char *
137 list_callback(int index, int *highlight, void *data)
139   static char buf[512];
141   if( help_text_rows<0 )
142     {
143       help_text_rows = 0;
144       while( help_text[help_text_rows].highlight != -1 )
145         help_text_rows++;
146     }
148   *highlight = 0;
149   if( index<help_text_rows )
150     {
151       *highlight = help_text[index].highlight > 0;
152       if( help_text[index].command == CMD_NONE )
153         {
154           if( help_text[index].text )
155             g_snprintf(buf, sizeof(buf), "      %s", _(help_text[index].text));
156           else 
157             if( help_text[index].highlight == 2 )
158               {
159                 int i;
161                 for(i=3; i<COLS-3 && i<sizeof(buf); i++)
162                   buf[i]='-';
163                 buf[i] = '\0';
164               }
165             else
166               g_strlcpy(buf, " ", sizeof(buf));
167           return buf;
168         }
169       if( help_text[index].text )
170         g_snprintf(buf, sizeof(buf), 
171                    "%20s : %s   ", 
172                    get_key_names(help_text[index].command, TRUE),
173                    _(help_text[index].text));
174       else
175         g_snprintf(buf, sizeof(buf), 
176                    "%20s : %s   ", 
177                    get_key_names(help_text[index].command, TRUE),
178                    get_key_description(help_text[index].command));
179       return buf;
180     }
182   return NULL;
185 static void
186 help_init(WINDOW *w, int cols, int rows)
188   lw = list_window_init(w, cols, rows);
189   lw->flags = LW_HIDE_CURSOR;
192 static void
193 help_resize(int cols, int rows)
195   lw->cols = cols;
196   lw->rows = rows;
199 static void
200 help_exit(void)
202   list_window_free(lw);
206 static char *
207 help_title(char *str, size_t size)
209   return _("Help");
212 static void 
213 help_paint(screen_t *screen, mpdclient_t *c)
215   lw->clear = 1;
216   list_window_paint(lw, list_callback, NULL);
217   wrefresh(lw->w);
220 static void 
221 help_update(screen_t *screen, mpdclient_t *c)
222 {  
223   if( lw->repaint )
224     {
225       list_window_paint(lw, list_callback, NULL);
226       wrefresh(lw->w);
227       lw->repaint = 0;
228     }
232 static int 
233 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
235   lw->repaint=1;
236   switch(cmd)
237     {
238     case CMD_LIST_NEXT:
239       if( lw->start+lw->rows < help_text_rows )
240         lw->start++;
241       return 1;
242     case CMD_LIST_PREVIOUS:
243       if( lw->start >0 )
244         lw->start--;
245       return 1;
246     case CMD_LIST_FIRST:
247       lw->start = 0;
248       return 1;
249     case CMD_LIST_LAST:
250       lw->start = help_text_rows-lw->rows;
251       if( lw->start<0 )
252         lw->start = 0;
253       return 1;
254     case CMD_LIST_NEXT_PAGE:
255       lw->start = lw->start + lw->rows;
256       if( lw->start+lw->rows >= help_text_rows )
257         lw->start = help_text_rows-lw->rows;
258       if( lw->start<0 )
259         lw->start = 0;
260        return 1;
261     case CMD_LIST_PREVIOUS_PAGE:
262       lw->start = lw->start - lw->rows;
263       if( lw->start<0 )
264         lw->start = 0;
265       return 1;
266     default:
267       break;
268     }
270   lw->selected = lw->start+lw->rows;
271   if( screen_find(screen, c, 
272                   lw,  help_text_rows,
273                   cmd, list_callback, NULL) )
274     {
275       /* center the row */
276       lw->start = lw->selected-(lw->rows/2);
277       if( lw->start+lw->rows > help_text_rows )
278         lw->start = help_text_rows-lw->rows;
279       if( lw->start<0 )
280         lw->start=0;
281       return 1;
282     }
284   return 0;
287 static list_window_t *
288 help_lw(void)
290   return lw;
293 screen_functions_t *
294 get_screen_help(void)
296   static screen_functions_t functions;
298   memset(&functions, 0, sizeof(screen_functions_t));
299   functions.init   = help_init;
300   functions.exit   = help_exit;
301   functions.open   = NULL;
302   functions.close  = NULL;
303   functions.resize = help_resize;
304   functions.paint  = help_paint;
305   functions.update = help_update;
306   functions.cmd    = help_cmd;
307   functions.get_lw = help_lw;
308   functions.get_title = help_title;
310   return &functions;