Not only comment it out but removing it
[midnight-commander.git] / src / listmode.c
blob69e499d70840fd5e6457d89b8d19ab4d45371804
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 #include <config.h>
25 #ifdef LISTMODE_EDITOR
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include "global.h"
35 #include "tty.h"
36 #include "win.h"
37 #include "color.h"
38 #include "dialog.h"
39 #include "widget.h"
40 #include "wtools.h"
42 /* Needed for the extern declarations of integer parameters */
43 #include "dir.h"
44 #include "panel.h" /* Needed for the externs */
45 #include "file.h"
46 #include "main.h"
47 #include "listmode.h"
49 #define UX 5
50 #define UY 2
52 #define BX 5
53 #define BY 18
55 #define B_ADD B_USER
56 #define B_REMOVE (B_USER + 1)
58 static WListbox *l_listmode;
60 static WLabel *pname;
62 static char *listmode_section = "[Listing format edit]";
64 static char *s_genwidth[2] = { "Half width", "Full width" };
65 static WRadio *radio_genwidth;
66 static char *s_columns[2] = { "One column", "Two columns" };
67 static WRadio *radio_columns;
68 static char *s_justify[3] =
69 { "Left justified", "Default justification", "Right justified" };
70 static WRadio *radio_justify;
71 static char *s_itemwidth[3] =
72 { "Free width", "Fixed width", "Growable width" };
73 static WRadio *radio_itemwidth;
75 struct listmode_button {
76 int ret_cmd, flags, y, x;
77 char *text;
78 bcback callback;
81 #define B_PLUS B_USER
82 #define B_MINUS (B_USER+1)
84 struct listmode_label {
85 int y, x;
86 char *text;
89 static char *
90 select_new_item (void)
92 /* NOTE: The following array of possible items must match the
93 formats array in screen.c. Better approach might be to make the
94 formats array global */
95 char *possible_items[] =
96 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
97 "owner", "group", "atime", "ctime", "space", "mark",
98 "inode", NULL
101 int i;
102 Listbox *mylistbox;
104 mylistbox =
105 create_listbox_window (12, 20, " Add listing format item ",
106 listmode_section);
107 for (i = 0; possible_items[i]; i++) {
108 listbox_add_item (mylistbox->list, 0, 0, possible_items[i], NULL);
111 i = run_listbox (mylistbox);
112 if (i >= 0)
113 return possible_items[i];
114 else
115 return NULL;
118 static int
119 bplus_cback (int action)
121 return 0;
124 static int
125 bminus_cback (int action)
127 return 0;
130 static int
131 badd_cback (int action)
133 char *s = select_new_item ();
134 if (s) {
135 listbox_add_item (l_listmode, 0, 0, s, NULL);
137 return 0;
140 static int
141 bremove_cback (int action)
143 listbox_remove_current (l_listmode, 0);
144 return 0;
147 static Dlg_head *
148 init_listmode (char *oldlistformat)
150 int i;
151 char *s;
152 int format_width = 0;
153 int format_columns = 0;
154 Dlg_head *listmode_dlg;
156 static struct listmode_label listmode_labels[] = {
157 {UY + 13, UX + 22, "Item width:"}
160 static struct listmode_button listmode_but[] = {
161 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
162 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
163 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
164 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
165 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
166 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
169 do_refresh ();
171 listmode_dlg =
172 create_dlg (0, 0, 22, 74, dialog_colors, NULL, listmode_section,
173 "Listing format edit", DLG_CENTER | DLG_REVERSE);
175 add_widget (listmode_dlg,
176 groupbox_new (UX, UY, 63, 4, "General options"));
177 add_widget (listmode_dlg, groupbox_new (UX, UY + 4, 18, 11, "Items"));
178 add_widget (listmode_dlg,
179 groupbox_new (UX + 20, UY + 4, 43, 11, "Item options"));
181 for (i = 0;
182 i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
183 add_widget (listmode_dlg,
184 button_new (listmode_but[i].y, listmode_but[i].x,
185 listmode_but[i].ret_cmd,
186 listmode_but[i].flags,
187 listmode_but[i].text,
188 listmode_but[i].callback));
190 /* We add the labels. */
191 for (i = 0;
192 i < sizeof (listmode_labels) / sizeof (struct listmode_label);
193 i++) {
194 pname =
195 label_new (listmode_labels[i].y, listmode_labels[i].x,
196 listmode_labels[i].text);
197 add_widget (listmode_dlg, pname);
200 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
201 add_widget (listmode_dlg, radio_itemwidth);
202 radio_itemwidth = 0;
203 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
204 add_widget (listmode_dlg, radio_justify);
205 radio_justify->sel = 1;
207 /* get new listbox */
208 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, NULL);
210 if (strncmp (oldlistformat, "full ", 5) == 0) {
211 format_width = 1;
212 oldlistformat += 5;
214 if (strncmp (oldlistformat, "half ", 5) == 0) {
215 oldlistformat += 5;
217 if (strncmp (oldlistformat, "2 ", 2) == 0) {
218 format_columns = 1;
219 oldlistformat += 2;
221 if (strncmp (oldlistformat, "1 ", 2) == 0) {
222 oldlistformat += 2;
224 s = strtok (oldlistformat, ",");
226 while (s) {
227 listbox_add_item (l_listmode, 0, 0, s, NULL);
228 s = strtok (NULL, ",");
231 /* add listbox to the dialogs */
232 add_widget (listmode_dlg, l_listmode);
234 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
235 add_widget (listmode_dlg, radio_columns);
236 radio_columns->sel = format_columns;
237 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
238 add_widget (listmode_dlg, radio_genwidth);
239 radio_genwidth->sel = format_width;
241 return listmode_dlg;
244 static void
245 listmode_done (Dlg_head *h)
247 destroy_dlg (h);
248 if (0)
249 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
250 repaint_screen ();
253 static char *
254 collect_new_format (void)
256 char *newformat;
257 int i;
258 char *last;
259 char *text, *extra;
261 newformat = g_malloc (1024);
262 if (radio_genwidth->sel)
263 strcpy (newformat, "full ");
264 else
265 strcpy (newformat, "half ");
266 if (radio_columns->sel)
267 strcat (newformat, "2 ");
268 last = NULL;
269 for (i = 0;; i++) {
270 listbox_select_by_number (l_listmode, i);
271 listbox_get_current (l_listmode, &text, &extra);
272 if (text == last)
273 break;
274 if (last != NULL)
275 strcat (newformat, ",");
276 strcat (newformat, text);
277 last = text;
279 return newformat;
282 /* Return new format or NULL if the user cancelled the dialog */
283 char *
284 listmode_edit (char *oldlistformat)
286 char *newformat = NULL;
287 char *s;
288 Dlg_head *listmode_dlg;
290 s = g_strdup (oldlistformat);
291 listmode_dlg = init_listmode (s);
292 g_free (s);
294 if (run_dlg (listmode_dlg) == B_ENTER) {
295 newformat = collect_new_format ();
298 listmode_done (listmode_dlg);
299 return newformat;
302 #endif /* LISTMODE_EDITOR */