Just a little correction at the it.po file.
[midnight-commander.git] / src / listmode.c
blobf66904c8f3b8db6bb594a12e0fa66512e2d8249f
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <config.h>
24 #ifdef LISTMODE_EDITOR
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
34 #include "global.h"
35 #include "tty.h"
36 #include "win.h"
37 #include "color.h"
38 #include "dlg.h"
39 #include "widget.h"
40 #include "dialog.h" /* For do_refresh() */
41 #include "wtools.h"
43 /* Needed for the extern declarations of integer parameters */
44 #include "dir.h"
45 #include "panel.h" /* Needed for the externs */
46 #include "file.h"
47 #include "main.h"
48 #include "listmode.h"
50 #define UX 5
51 #define UY 2
53 #define BX 5
54 #define BY 18
56 #define BUTTONS 4
57 #define LABELS 4
58 #define B_ADD B_USER
59 #define B_REMOVE B_USER + 1
61 static WListbox *l_listmode;
63 static WLabel *pname;
65 static char *listmode_section = "[Listing format edit]";
67 static char *s_genwidth[2] = { "Half width", "Full width" };
68 static WRadio *radio_genwidth;
69 static char *s_columns[2] = { "One column", "Two columns" };
70 static WRadio *radio_columns;
71 static char *s_justify[3] =
72 { "Left justified", "Default justification", "Right justified" };
73 static WRadio *radio_justify;
74 static char *s_itemwidth[3] =
75 { "Free width", "Fixed width", "Growable width" };
76 static WRadio *radio_itemwidth;
78 struct listmode_button {
79 int ret_cmd, flags, y, x;
80 char *text;
83 static struct listmode_button listmode_but[BUTTONS] = {
84 {B_CANCEL, NORMAL_BUTTON, 0, 53, "&Cancel"},
85 {B_ADD, NORMAL_BUTTON, 0, 22, "&Add item"},
86 {B_REMOVE, NORMAL_BUTTON, 0, 10, "&Remove"},
87 {B_ENTER, DEFPUSH_BUTTON, 0, 0, "&OK"},
90 #define B_PLUS B_USER
91 #define B_MINUS B_USER+1
93 struct listmode_label {
94 int y, x;
95 char *text;
98 static struct listmode_label listmode_labels[LABELS] = {
99 {UY, UX + 1, " General options "},
100 {UY + 4, UX + 1, " Items "},
101 {UY + 4, UX + 21, " Item options"},
102 {UY + 13, UX + 22, "Item width:"}
105 static void
106 listmode_refresh (Dlg_head * h)
108 common_dialog_repaint (h);
110 attrset (COLOR_NORMAL);
111 draw_box (h, UY, UX, 4, 63);
112 draw_box (h, UY + 4, UX, 11, 18);
113 draw_box (h, UY + 4, UX + 20, 11, 43);
116 static int
117 bplus_cback (int action, void *data)
119 return 0;
122 static int
123 bminus_cback (int action, void *data)
125 return 0;
128 static int
129 listmode_callback (Dlg_head * h, int Par, int Msg)
131 switch (Msg) {
133 case DLG_DRAW:
134 listmode_refresh (h);
135 break;
137 case DLG_POST_KEY:
138 /* fall */
140 case DLG_INIT:
141 attrset (COLOR_NORMAL);
142 dlg_move (h, UY + 13, UX + 35);
143 printw ("%02d", 99);
144 attrset (MENU_ENTRY_COLOR);
145 break;
147 return 0;
150 static int
151 l_call (void *data)
153 return 1;
156 static Dlg_head *
157 init_listmode (char *oldlistformat)
159 int i;
160 char *s;
161 int format_width = 0;
162 int format_columns = 0;
163 Dlg_head *listmode_dlg;
165 do_refresh ();
167 listmode_dlg =
168 create_dlg (0, 0, 22, 74, dialog_colors, listmode_callback,
169 listmode_section, "Listing format edit", DLG_CENTER);
171 #define XTRACT(i) BY+listmode_but[i].y, BX+listmode_but[i].x, listmode_but[i].ret_cmd, listmode_but[i].flags, listmode_but[i].text, 0, 0, NULL
173 for (i = 0; i < BUTTONS; i++)
174 add_widget (listmode_dlg, button_new (XTRACT (i)));
176 /* We add the labels. */
177 for (i = 0; i < LABELS; i++) {
178 pname =
179 label_new (listmode_labels[i].y, listmode_labels[i].x,
180 listmode_labels[i].text, NULL);
181 add_widget (listmode_dlg, pname);
184 add_widget (listmode_dlg,
185 button_new (UY + 13, UX + 37, B_MINUS, NORMAL_BUTTON, "&-",
186 bminus_cback, 0, NULL));
187 add_widget (listmode_dlg,
188 button_new (UY + 13, UX + 34, B_PLUS, NORMAL_BUTTON, "&+",
189 bplus_cback, 0, NULL));
190 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth, 1, NULL);
191 add_widget (listmode_dlg, radio_itemwidth);
192 radio_itemwidth = 0;
193 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify, 1, NULL);
194 add_widget (listmode_dlg, radio_justify);
195 radio_justify->sel = 1;
197 /* get new listbox */
198 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, 0, l_call, NULL);
200 if (strncmp (oldlistformat, "full ", 5) == 0) {
201 format_width = 1;
202 oldlistformat += 5;
204 if (strncmp (oldlistformat, "half ", 5) == 0) {
205 oldlistformat += 5;
207 if (strncmp (oldlistformat, "2 ", 2) == 0) {
208 format_columns = 1;
209 oldlistformat += 2;
211 if (strncmp (oldlistformat, "1 ", 2) == 0) {
212 oldlistformat += 2;
214 s = strtok (oldlistformat, ",");
216 while (s) {
217 listbox_add_item (l_listmode, 0, 0, s, NULL);
218 s = strtok (NULL, ",");
221 /* add listbox to the dialogs */
222 add_widget (listmode_dlg, l_listmode);
224 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns, 1, NULL);
225 add_widget (listmode_dlg, radio_columns);
226 radio_columns->sel = format_columns;
227 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth, 1, NULL);
228 add_widget (listmode_dlg, radio_genwidth);
229 radio_genwidth->sel = format_width;
231 return listmode_dlg;
234 static void
235 listmode_done (Dlg_head * h)
237 destroy_dlg (h);
238 if (0)
239 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
240 repaint_screen ();
243 static char *
244 select_new_item (void)
246 /* NOTE: The following array of possible items must match the
247 formats array in screen.c. Better approach might be to make the
248 formats array global */
249 char *possible_items[] =
250 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
251 "owner", "group", "atime", "ctime", "space", "mark",
252 "inode", NULL
255 int i;
256 Listbox *mylistbox;
258 mylistbox =
259 create_listbox_window (12, 20, " Add listing format item ",
260 listmode_section);
261 for (i = 0; possible_items[i]; i++) {
262 listbox_add_item (mylistbox->list, 0, 0, possible_items[i], NULL);
265 i = run_listbox (mylistbox);
266 if (i >= 0)
267 return possible_items[i];
268 else
269 return NULL;
272 static char *
273 collect_new_format (void)
275 char *newformat;
276 int i;
277 char *last;
278 char *text, *extra;
280 newformat = g_malloc (1024);
281 if (radio_genwidth->sel)
282 strcpy (newformat, "full ");
283 else
284 strcpy (newformat, "half ");
285 if (radio_columns->sel)
286 strcat (newformat, "2 ");
287 last = NULL;
288 for (i = 0;; i++) {
289 listbox_select_by_number (l_listmode, i);
290 listbox_get_current (l_listmode, &text, &extra);
291 if (text == last)
292 break;
293 if (last != NULL)
294 strcat (newformat, ",");
295 strcat (newformat, text);
296 last = text;
298 return newformat;
301 char *
302 listmode_edit (char *oldlistformat)
304 char *newformat = NULL;
305 char *s;
306 Dlg_head *listmode_dlg;
308 s = g_strdup (oldlistformat);
309 listmode_dlg = init_listmode (s);
310 g_free (s);
312 while (newformat == NULL) {
313 /* display file info */
314 attrset (SELECTED_COLOR);
317 * FIXME: Calling run_dlg() more than once doesn't work.
318 * This code is broken.
320 run_dlg (listmode_dlg);
322 switch (listmode_dlg->ret_value) {
323 case B_CANCEL:
324 newformat = g_strdup (oldlistformat);
325 break;
327 case B_ADD:
328 s = select_new_item ();
329 if (s)
330 listbox_add_item (l_listmode, 0, 0, s, NULL);
331 break;
333 case B_REMOVE:
334 listbox_remove_current (l_listmode, 0);
335 break;
337 case B_ENTER:
338 newformat = collect_new_format ();
339 break;
343 listmode_done (listmode_dlg);
344 return newformat;
347 #endif /* LISTMODE_EDITOR */