add redefine keys support in viewer
[midnight-commander.git] / src / listmode.c
blob48982c475dbb689226b83f9bf7d75cb03f09f81b
1 /* Directory panel listing format editor -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 Written by: 1994 Radek Doulik
6 1995 Janne Kukonlehto
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 /** \file listmode.c
24 * \brief Source: directory panel listing format editor
27 #include <config.h>
29 #ifdef LISTMODE_EDITOR
31 #include <stdio.h>
32 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
38 #include "global.h"
39 #include "tty.h"
40 #include "key.h"
41 #include "color.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h"
46 /* Needed for the extern declarations of integer parameters */
47 #include "dir.h"
48 #include "panel.h" /* Needed for the externs */
49 #include "file.h"
50 #include "layout.h" /* repaint_screen() */
51 #include "main.h"
52 #include "listmode.h"
54 #define UX 5
55 #define UY 2
57 #define BX 5
58 #define BY 18
60 #define B_ADD B_USER
61 #define B_REMOVE (B_USER + 1)
63 static WListbox *l_listmode;
65 static WLabel *pname;
67 static char *listmode_section = "[Listing format edit]";
69 static char *s_genwidth[2] = { "Half width", "Full width" };
70 static WRadio *radio_genwidth;
71 static char *s_columns[2] = { "One column", "Two columns" };
72 static WRadio *radio_columns;
73 static char *s_justify[3] =
74 { "Left justified", "Default justification", "Right justified" };
75 static WRadio *radio_justify;
76 static char *s_itemwidth[3] =
77 { "Free width", "Fixed width", "Growable width" };
78 static WRadio *radio_itemwidth;
80 struct listmode_button {
81 int ret_cmd, flags, y, x;
82 char *text;
83 bcback callback;
86 #define B_PLUS B_USER
87 #define B_MINUS (B_USER+1)
89 struct listmode_label {
90 int y, x;
91 char *text;
94 static char *
95 select_new_item (void)
97 /* NOTE: The following array of possible items must match the
98 formats array in screen.c. Better approach might be to make the
99 formats array global */
100 char *possible_items[] =
101 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
102 "owner", "group", "atime", "ctime", "space", "mark",
103 "inode", NULL
106 int i;
107 Listbox *mylistbox;
109 mylistbox =
110 create_listbox_window (12, 20, " Add listing format item ",
111 listmode_section);
112 for (i = 0; possible_items[i]; i++) {
113 listbox_add_item (mylistbox->list, 0, 0, possible_items[i], NULL);
116 i = run_listbox (mylistbox);
117 if (i >= 0)
118 return possible_items[i];
119 else
120 return NULL;
123 static int
124 bplus_cback (int action)
126 return 0;
129 static int
130 bminus_cback (int action)
132 return 0;
135 static int
136 badd_cback (int action)
138 char *s = select_new_item ();
139 if (s) {
140 listbox_add_item (l_listmode, 0, 0, s, NULL);
142 return 0;
145 static int
146 bremove_cback (int action)
148 listbox_remove_current (l_listmode, 0);
149 return 0;
152 static Dlg_head *
153 init_listmode (char *oldlistformat)
155 int i;
156 char *s;
157 int format_width = 0;
158 int format_columns = 0;
159 Dlg_head *listmode_dlg;
161 static struct listmode_label listmode_labels[] = {
162 {UY + 13, UX + 22, "Item width:"}
165 static struct listmode_button listmode_but[] = {
166 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
167 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
168 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
169 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
170 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
171 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
174 do_refresh ();
176 listmode_dlg =
177 create_dlg (0, 0, 22, 74, dialog_colors, NULL, listmode_section,
178 "Listing format edit", DLG_CENTER | DLG_REVERSE);
180 add_widget (listmode_dlg,
181 groupbox_new (UY, UX, 4, 63, "General options"));
182 add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items"));
183 add_widget (listmode_dlg,
184 groupbox_new (UY + 4, UX + 20, 11, 43, "Item options"));
186 for (i = 0;
187 i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
188 add_widget (listmode_dlg,
189 button_new (listmode_but[i].y, listmode_but[i].x,
190 listmode_but[i].ret_cmd,
191 listmode_but[i].flags,
192 listmode_but[i].text,
193 listmode_but[i].callback));
195 /* We add the labels. */
196 for (i = 0;
197 i < sizeof (listmode_labels) / sizeof (struct listmode_label);
198 i++) {
199 pname =
200 label_new (listmode_labels[i].y, listmode_labels[i].x,
201 listmode_labels[i].text);
202 add_widget (listmode_dlg, pname);
205 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
206 add_widget (listmode_dlg, radio_itemwidth);
207 radio_itemwidth = 0;
208 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
209 add_widget (listmode_dlg, radio_justify);
210 radio_justify->sel = 1;
212 /* get new listbox */
213 l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, NULL);
215 if (strncmp (oldlistformat, "full ", 5) == 0) {
216 format_width = 1;
217 oldlistformat += 5;
219 if (strncmp (oldlistformat, "half ", 5) == 0) {
220 oldlistformat += 5;
222 if (strncmp (oldlistformat, "2 ", 2) == 0) {
223 format_columns = 1;
224 oldlistformat += 2;
226 if (strncmp (oldlistformat, "1 ", 2) == 0) {
227 oldlistformat += 2;
229 s = strtok (oldlistformat, ",");
231 while (s) {
232 listbox_add_item (l_listmode, 0, 0, s, NULL);
233 s = strtok (NULL, ",");
236 /* add listbox to the dialogs */
237 add_widget (listmode_dlg, l_listmode);
239 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
240 add_widget (listmode_dlg, radio_columns);
241 radio_columns->sel = format_columns;
242 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
243 add_widget (listmode_dlg, radio_genwidth);
244 radio_genwidth->sel = format_width;
246 return listmode_dlg;
249 static void
250 listmode_done (Dlg_head *h)
252 destroy_dlg (h);
253 if (0)
254 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
255 repaint_screen ();
258 static char *
259 collect_new_format (void)
261 char *newformat;
262 int i;
263 char *last;
264 char *text, *extra;
266 newformat = g_malloc (1024);
267 if (radio_genwidth->sel)
268 strcpy (newformat, "full ");
269 else
270 strcpy (newformat, "half ");
271 if (radio_columns->sel)
272 strcat (newformat, "2 ");
273 last = NULL;
274 for (i = 0;; i++) {
275 listbox_select_by_number (l_listmode, i);
276 listbox_get_current (l_listmode, &text, &extra);
277 if (text == last)
278 break;
279 if (last != NULL)
280 strcat (newformat, ",");
281 strcat (newformat, text);
282 last = text;
284 return newformat;
287 /* Return new format or NULL if the user cancelled the dialog */
288 char *
289 listmode_edit (char *oldlistformat)
291 char *newformat = NULL;
292 char *s;
293 Dlg_head *listmode_dlg;
295 s = g_strdup (oldlistformat);
296 listmode_dlg = init_listmode (s);
297 g_free (s);
299 if (run_dlg (listmode_dlg) == B_ENTER) {
300 newformat = collect_new_format ();
303 listmode_done (listmode_dlg);
304 return newformat;
306 #else
307 void listmode__unused(void)
310 CFLAGS="-ansi -pedantic -Wall -Wextra -Werror"
314 #endif /* LISTMODE_EDITOR */