Update base version to 4.6.1.
[midnight-commander.git] / src / listmode.c
blob9d6ad68293f3949d721f863d909392b24f0a11a2
1 /* Directory panel listing format editor -- for the Midnight Commander
2 Copyright (C) 1994, 1995 The Free Software Foundation
4 Written by: 1994 Radek Doulik
5 1995 Janne Kukonlehto
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <config.h>
24 #ifdef LISTMODE_EDITOR
26 #include <stdio.h>
27 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
33 #include "global.h"
34 #include "tty.h"
35 #include "win.h"
36 #include "color.h"
37 #include "dialog.h"
38 #include "widget.h"
39 #include "wtools.h"
41 /* Needed for the extern declarations of integer parameters */
42 #include "dir.h"
43 #include "panel.h" /* Needed for the externs */
44 #include "file.h"
45 #include "main.h"
46 #include "listmode.h"
48 #define UX 5
49 #define UY 2
51 #define BX 5
52 #define BY 18
54 #define B_ADD B_USER
55 #define B_REMOVE (B_USER + 1)
57 static WListbox *l_listmode;
59 static WLabel *pname;
61 static char *listmode_section = "[Listing format edit]";
63 static char *s_genwidth[2] = { "Half width", "Full width" };
64 static WRadio *radio_genwidth;
65 static char *s_columns[2] = { "One column", "Two columns" };
66 static WRadio *radio_columns;
67 static char *s_justify[3] =
68 { "Left justified", "Default justification", "Right justified" };
69 static WRadio *radio_justify;
70 static char *s_itemwidth[3] =
71 { "Free width", "Fixed width", "Growable width" };
72 static WRadio *radio_itemwidth;
74 struct listmode_button {
75 int ret_cmd, flags, y, x;
76 char *text;
77 bcback callback;
80 #define B_PLUS B_USER
81 #define B_MINUS (B_USER+1)
83 struct listmode_label {
84 int y, x;
85 char *text;
88 static char *
89 select_new_item (void)
91 /* NOTE: The following array of possible items must match the
92 formats array in screen.c. Better approach might be to make the
93 formats array global */
94 char *possible_items[] =
95 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
96 "owner", "group", "atime", "ctime", "space", "mark",
97 "inode", NULL
100 int i;
101 Listbox *mylistbox;
103 mylistbox =
104 create_listbox_window (12, 20, " Add listing format item ",
105 listmode_section);
106 for (i = 0; possible_items[i]; i++) {
107 listbox_add_item (mylistbox->list, 0, 0, possible_items[i], NULL);
110 i = run_listbox (mylistbox);
111 if (i >= 0)
112 return possible_items[i];
113 else
114 return NULL;
117 static int
118 bplus_cback (int action)
120 return 0;
123 static int
124 bminus_cback (int action)
126 return 0;
129 static int
130 badd_cback (int action)
132 char *s = select_new_item ();
133 if (s) {
134 listbox_add_item (l_listmode, 0, 0, s, NULL);
136 return 0;
139 static int
140 bremove_cback (int action)
142 listbox_remove_current (l_listmode, 0);
143 return 0;
146 static Dlg_head *
147 init_listmode (char *oldlistformat)
149 int i;
150 char *s;
151 int format_width = 0;
152 int format_columns = 0;
153 Dlg_head *listmode_dlg;
155 static struct listmode_label listmode_labels[] = {
156 {UY + 13, UX + 22, "Item width:"}
159 static struct listmode_button listmode_but[] = {
160 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
161 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
162 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
163 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
164 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
165 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
168 do_refresh ();
170 listmode_dlg =
171 create_dlg (0, 0, 22, 74, dialog_colors, NULL, listmode_section,
172 "Listing format edit", DLG_CENTER | DLG_REVERSE);
174 add_widget (listmode_dlg,
175 groupbox_new (UX, UY, 63, 4, "General options"));
176 add_widget (listmode_dlg, groupbox_new (UX, UY + 4, 18, 11, "Items"));
177 add_widget (listmode_dlg,
178 groupbox_new (UX + 20, UY + 4, 43, 11, "Item options"));
180 for (i = 0;
181 i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
182 add_widget (listmode_dlg,
183 button_new (listmode_but[i].y, listmode_but[i].x,
184 listmode_but[i].ret_cmd,
185 listmode_but[i].flags,
186 listmode_but[i].text,
187 listmode_but[i].callback));
189 /* We add the labels. */
190 for (i = 0;
191 i < sizeof (listmode_labels) / sizeof (struct listmode_label);
192 i++) {
193 pname =
194 label_new (listmode_labels[i].y, listmode_labels[i].x,
195 listmode_labels[i].text);
196 add_widget (listmode_dlg, pname);
199 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth, 1);
200 add_widget (listmode_dlg, radio_itemwidth);
201 radio_itemwidth = 0;
202 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify, 1);
203 add_widget (listmode_dlg, radio_justify);
204 radio_justify->sel = 1;
206 /* get new listbox */
207 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, NULL);
209 if (strncmp (oldlistformat, "full ", 5) == 0) {
210 format_width = 1;
211 oldlistformat += 5;
213 if (strncmp (oldlistformat, "half ", 5) == 0) {
214 oldlistformat += 5;
216 if (strncmp (oldlistformat, "2 ", 2) == 0) {
217 format_columns = 1;
218 oldlistformat += 2;
220 if (strncmp (oldlistformat, "1 ", 2) == 0) {
221 oldlistformat += 2;
223 s = strtok (oldlistformat, ",");
225 while (s) {
226 listbox_add_item (l_listmode, 0, 0, s, NULL);
227 s = strtok (NULL, ",");
230 /* add listbox to the dialogs */
231 add_widget (listmode_dlg, l_listmode);
233 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns, 1);
234 add_widget (listmode_dlg, radio_columns);
235 radio_columns->sel = format_columns;
236 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth, 1);
237 add_widget (listmode_dlg, radio_genwidth);
238 radio_genwidth->sel = format_width;
240 return listmode_dlg;
243 static void
244 listmode_done (Dlg_head *h)
246 destroy_dlg (h);
247 if (0)
248 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
249 repaint_screen ();
252 static char *
253 collect_new_format (void)
255 char *newformat;
256 int i;
257 char *last;
258 char *text, *extra;
260 newformat = g_malloc (1024);
261 if (radio_genwidth->sel)
262 strcpy (newformat, "full ");
263 else
264 strcpy (newformat, "half ");
265 if (radio_columns->sel)
266 strcat (newformat, "2 ");
267 last = NULL;
268 for (i = 0;; i++) {
269 listbox_select_by_number (l_listmode, i);
270 listbox_get_current (l_listmode, &text, &extra);
271 if (text == last)
272 break;
273 if (last != NULL)
274 strcat (newformat, ",");
275 strcat (newformat, text);
276 last = text;
278 return newformat;
281 /* Return new format or NULL if the user cancelled the dialog */
282 char *
283 listmode_edit (char *oldlistformat)
285 char *newformat = NULL;
286 char *s;
287 Dlg_head *listmode_dlg;
289 s = g_strdup (oldlistformat);
290 listmode_dlg = init_listmode (s);
291 g_free (s);
293 if (run_dlg (listmode_dlg) == B_ENTER) {
294 newformat = collect_new_format ();
297 listmode_done (listmode_dlg);
298 return newformat;
301 #endif /* LISTMODE_EDITOR */