Code

4d1db15940f4a978c3c6e3649b480ba22661618f
[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_CLOCK_SCREEN
57   { 0, CMD_SCREEN_CLOCK,   NULL },
58 #endif
59 #ifdef ENABLE_KEYDEF_SCREEN
60   { 0, CMD_SCREEN_KEYDEF,  NULL },
61 #endif
63   { 0, CMD_NONE,           NULL },
64   { 0, CMD_NONE,           NULL },
65   { 1, CMD_NONE,           N_("Keys - Global") },
66   { 2, CMD_NONE,           NULL },
67   { 0, CMD_STOP,           NULL },
68   { 0, CMD_PAUSE,          NULL },
69   { 0, CMD_TRACK_NEXT,     NULL },
70   { 0, CMD_TRACK_PREVIOUS, NULL },
71   { 0, CMD_SEEK_FORWARD,   NULL },
72   { 0, CMD_SEEK_BACKWARD,  NULL },
73   { 0, CMD_VOLUME_DOWN,    NULL },
74   { 0, CMD_VOLUME_UP,      NULL },
75   { 0, CMD_NONE,           NULL },
76   { 0, CMD_REPEAT,         NULL },
77   { 0, CMD_RANDOM,         NULL },
78   { 0, CMD_CROSSFADE,      NULL },
79   { 0, CMD_SHUFFLE,        NULL },
80   { 0, CMD_DB_UPDATE,      NULL },
81   { 0, CMD_NONE,           NULL },
82   { 0, CMD_LIST_FIND,      NULL },
83   { 0, CMD_LIST_RFIND,     NULL },
84   { 0, CMD_LIST_FIND_NEXT, NULL },
85   { 0, CMD_LIST_RFIND_NEXT,  NULL },
86   { 0, CMD_TOGGLE_FIND_WRAP, NULL },
87   { 0, CMD_NONE,           NULL },
88   { 0, CMD_QUIT,           NULL },
90   { 0, CMD_NONE,           NULL },
91   { 0, CMD_NONE,           NULL },
92   { 1, CMD_NONE,           N_("Keys - Playlist screen") },
93   { 2, CMD_NONE,           NULL },
94   { 0, CMD_PLAY,           N_("Play") },
95   { 0, CMD_DELETE,         NULL },
96   { 0, CMD_CLEAR,          NULL },
97   { 0, CMD_LIST_MOVE_UP,   N_("Move song up") },
98   { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
99   { 0, CMD_ADD,            NULL },
100   { 0, CMD_SAVE_PLAYLIST,  NULL },
101   { 0, CMD_SCREEN_UPDATE,  N_("Center") },
102   { 0, CMD_TOGGLE_AUTOCENTER, NULL },
104   { 0, CMD_NONE,           NULL },
105   { 0, CMD_NONE,           NULL },
106   { 1, CMD_NONE,           N_("Keys - Browse screen") },
107   { 2, CMD_NONE,           NULL },
108   { 0, CMD_PLAY,           N_("Enter directory") },
109   { 0, CMD_SELECT,         NULL },
110   { 0, CMD_DELETE,         NULL },
111   { 0, CMD_SCREEN_UPDATE,  NULL },
113   { 0, CMD_NONE, NULL },
114   { 0, CMD_NONE, NULL },
115   { 1, CMD_NONE, "ncmpc build information" },
116   { 2, CMD_NONE, NULL },
117   { 0, CMD_NONE, "             Version : " VERSION },
118   { 0, CMD_NONE, "  Configuration dirs : ~/.ncmpc, " SYSCONFDIR "/" PACKAGE },
119 #ifdef ENABLE_NLS
120   { 0, CMD_NONE, "NLS support : yes" },
121 #else
122   { 0, CMD_NONE, "NLS support : no" },
123 #endif
124 #ifdef ENABLE_KEYDEF_SCREEN
125   { 0, CMD_NONE, "Key Editor : yes" },
126 #else
127   { 0, CMD_NONE, "Key Editor : no" },
128 #endif
130   { 0, CMD_NONE, NULL },
131   {-1, CMD_NONE, NULL }
132 };
134 static int help_text_rows = -1;
135 static list_window_t *lw = NULL;
138 static char *
139 list_callback(int index, int *highlight, void *data)
141   static char buf[256];
143   if( help_text_rows<0 )
144     {
145       help_text_rows = 0;
146       while( help_text[help_text_rows].highlight != -1 )
147         help_text_rows++;
148     }
150   *highlight = 0;
151   if( index<help_text_rows )
152     {
153       *highlight = help_text[index].highlight > 0;
154       if( help_text[index].command == CMD_NONE )
155         {
156           if( help_text[index].text )
157             snprintf(buf, 256, "%28s", _(help_text[index].text));
158           else 
159             if( help_text[index].highlight == 2 )
160               {
161                 int i;
163                 for(i=3; i<COLS-3 && i<256; i++)
164                   buf[i]='-';
165                 buf[i] = '\0';
166               }
167             else
168               strcpy(buf, " ");
169           return buf;
170         }
171       if( help_text[index].text )
172         snprintf(buf, 256, 
173                  "%20s : %s   ", 
174                  get_key_names(help_text[index].command, TRUE),
175                  _(help_text[index].text));
176       else
177         snprintf(buf, 256, 
178                  "%20s : %s   ", 
179                  get_key_names(help_text[index].command, TRUE),
180                  get_key_description(help_text[index].command));
181       return buf;
182     }
184   return NULL;
187 static void
188 help_init(WINDOW *w, int cols, int rows)
190   lw = list_window_init(w, cols, rows);
191   lw->flags = LW_HIDE_CURSOR;
194 static void
195 help_resize(int cols, int rows)
197   lw->cols = cols;
198   lw->rows = rows;
201 static void
202 help_exit(void)
204   list_window_free(lw);
208 static char *
209 help_title(char *str, size_t size)
211   return _("Help");
214 static void 
215 help_paint(screen_t *screen, mpdclient_t *c)
217   lw->clear = 1;
218   list_window_paint(lw, list_callback, NULL);
219   wrefresh(lw->w);
222 static void 
223 help_update(screen_t *screen, mpdclient_t *c)
224 {  
225   if( lw->repaint )
226     {
227       list_window_paint(lw, list_callback, NULL);
228       wrefresh(lw->w);
229       lw->repaint = 0;
230     }
234 static int 
235 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
237   lw->repaint=1;
238   switch(cmd)
239     {
240     case CMD_LIST_NEXT:
241       if( lw->start+lw->rows < help_text_rows )
242         lw->start++;
243       return 1;
244     case CMD_LIST_PREVIOUS:
245       if( lw->start >0 )
246         lw->start--;
247       return 1;
248     case CMD_LIST_FIRST:
249       lw->start = 0;
250       return 1;
251     case CMD_LIST_LAST:
252       lw->start = help_text_rows-lw->rows;
253       if( lw->start<0 )
254         lw->start = 0;
255       return 1;
256     case CMD_LIST_NEXT_PAGE:
257       lw->start = lw->start + lw->rows;
258       if( lw->start+lw->rows >= help_text_rows )
259         lw->start = help_text_rows-lw->rows;
260       if( lw->start<0 )
261         lw->start = 0;
262        return 1;
263     case CMD_LIST_PREVIOUS_PAGE:
264       lw->start = lw->start - lw->rows;
265       if( lw->start<0 )
266         lw->start = 0;
267       return 1;
268     default:
269       break;
270     }
272   lw->selected = lw->start+lw->rows;
273   if( screen_find(screen, c, 
274                   lw,  help_text_rows,
275                   cmd, list_callback) )
276     {
277       /* center the row */
278       lw->start = lw->selected-(lw->rows/2);
279       if( lw->start+lw->rows > help_text_rows )
280         lw->start = help_text_rows-lw->rows;
281       if( lw->start<0 )
282         lw->start=0;
283       return 1;
284     }
286   return 0;
289 static list_window_t *
290 help_lw(void)
292   return lw;
295 screen_functions_t *
296 get_screen_help(void)
298   static screen_functions_t functions;
300   memset(&functions, 0, sizeof(screen_functions_t));
301   functions.init   = help_init;
302   functions.exit   = help_exit;
303   functions.open   = NULL;
304   functions.close  = NULL;
305   functions.resize = help_resize;
306   functions.paint  = help_paint;
307   functions.update = help_update;
308   functions.cmd    = help_cmd;
309   functions.get_lw = help_lw;
310   functions.get_title = help_title;
312   return &functions;