1999-09-09 Federico Mena Quintero <federico@redhat.com>
[midnight-commander.git] / src / listmode.c
bloba9f9195d62dd1303c7d6d0dc7b11ca8e2581987c
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #ifndef HAS_NO_GRP_PWD_H
32 # include <grp.h>
33 # include <pwd.h>
34 #endif
35 #include "tty.h"
36 #include "global.h"
37 #include "win.h"
38 #include "color.h"
39 #include "dlg.h"
40 #include "widget.h"
41 #include "dialog.h" /* For do_refresh() */
42 #include "wtools.h"
44 /* Needed for the extern declarations of integer parameters */
45 #include "dir.h"
46 #include "panel.h" /* Needed for the externs */
47 #include "file.h"
48 #include "main.h"
49 #include "listmode.h"
51 #define UX 5
52 #define UY 2
54 #define BX 5
55 #define BY 18
57 #define BUTTONS 4
58 #define LABELS 4
59 #define B_ADD B_USER
60 #define B_REMOVE B_USER + 1
62 static WListbox *l_listmode;
64 static Dlg_head *listmode_dlg;
66 static WLabel *pname;
68 static char *listmode_section = "[Listing format edit]";
70 static char *s_genwidth [2] = {"Half width", "Full width"};
71 WRadio *radio_genwidth;
72 static char *s_columns [2] = {"One column", "Two columns"};
73 WRadio *radio_columns;
74 static char *s_justify [3] =
75 {"Left justified", "Default justification", "Right justified"};
76 WRadio *radio_justify;
77 static char *s_itemwidth [3] =
78 {"Free width", "Fixed width", "Growable width"};
79 WRadio *radio_itemwidth;
81 struct {
82 int ret_cmd, flags, y, x;
83 char *text;
84 } listmode_but[BUTTONS] = {
85 { B_CANCEL, NORMAL_BUTTON, 0, 53, "&Cancel" },
86 { B_ADD, NORMAL_BUTTON, 0, 22, "&Add item"},
87 { B_REMOVE, NORMAL_BUTTON, 0, 10, "&Remove" },
88 { B_ENTER, DEFPUSH_BUTTON, 0, 0, "&Ok" },
91 #define B_PLUS B_USER
92 #define B_MINUS B_USER+1
94 struct {
95 int y, x;
96 char *text;
97 } listmode_text [LABELS] = {
98 { UY, UX + 1, " General options " },
99 { UY+4, UX+1, " Items "},
100 { UY+4, UX+21, " Item options" },
101 { UY+13, UX+22, "Item width:" }
104 #ifndef HAVE_X
105 static void listmode_refresh (void)
107 attrset (COLOR_NORMAL);
108 dlg_erase (listmode_dlg);
110 draw_box (listmode_dlg, 1, 2, 20, 70);
111 draw_box (listmode_dlg, UY, UX, 4, 63);
112 draw_box (listmode_dlg, UY + 4, UX, 11, 18);
113 draw_box (listmode_dlg, UY + 4, UX+20, 11, 43);
115 #endif
117 static int bplus_cback (int action, void *data)
119 return 0;
122 static int bminus_cback (int action, void *data)
124 return 0;
127 static int listmode_callback (Dlg_head * h, int Par, int Msg)
129 switch (Msg) {
130 #ifndef HAVE_X
131 case DLG_DRAW:
132 listmode_refresh ();
133 break;
134 #endif
136 case DLG_POST_KEY:
137 /* fall */
138 case DLG_INIT:
139 attrset (COLOR_NORMAL);
140 dlg_move (h, UY+13, UX+35);
141 printw ("%02d", 99);
142 attrset (MENU_ENTRY_COLOR);
143 break;
145 return 0;
148 static int l_call (void *data)
150 return 1;
153 static void init_listmode (char *oldlistformat)
155 int i;
156 char *s;
157 int format_width = 0;
158 int format_columns = 0;
160 do_refresh ();
162 listmode_dlg = create_dlg (0, 0, 22, 74, dialog_colors,
163 listmode_callback, listmode_section, "listmode",
164 DLG_CENTER);
165 x_set_dialog_title (listmode_dlg, "Listing format edit");
167 #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
169 for (i = 0; i < BUTTONS; i++)
170 add_widgetl (listmode_dlg, button_new (XTRACT (i)), (i == BUTTONS - 1) ?
171 XV_WLAY_CENTERROW : XV_WLAY_RIGHTOF);
173 /* We add the labels. */
174 for (i = 0; i < LABELS; i++){
175 pname = label_new (listmode_text [i].y,
176 listmode_text [i].x, listmode_text [i].text, NULL);
177 add_widget (listmode_dlg, pname);
180 add_widget (listmode_dlg, button_new (UY+13, UX+37, B_MINUS, NORMAL_BUTTON,
181 "&-", bminus_cback, 0, NULL));
182 add_widget (listmode_dlg, button_new (UY+13, UX+34, B_PLUS, NORMAL_BUTTON,
183 "&+", bplus_cback, 0, NULL));
184 radio_itemwidth = radio_new (UY+9, UX+22, 3, s_itemwidth, 1, NULL);
185 add_widget (listmode_dlg, radio_itemwidth);
186 radio_itemwidth = 0;
187 radio_justify = radio_new (UY+5, UX+22, 3, s_justify, 1, NULL);
188 add_widget (listmode_dlg, radio_justify);
189 radio_justify->sel = 1;
191 /* get new listbox */
192 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, 0, l_call, NULL);
194 if (strncmp (oldlistformat, "full ", 5) == 0){
195 format_width = 1;
196 oldlistformat += 5;
198 if (strncmp (oldlistformat, "half ", 5) == 0){
199 oldlistformat += 5;
201 if (strncmp (oldlistformat, "2 ", 2) == 0){
202 format_columns = 1;
203 oldlistformat += 2;
205 if (strncmp (oldlistformat, "1 ", 2) == 0){
206 oldlistformat += 2;
208 s = strtok (oldlistformat, ",");
210 while (s){
211 listbox_add_item (l_listmode, 0, 0, s, NULL);
212 s = strtok (NULL, ",");
215 /* add listbox to the dialogs */
216 add_widgetl (listmode_dlg, l_listmode, XV_WLAY_EXTENDWIDTH);
218 radio_columns = radio_new (UY+1, UX+32, 2, s_columns, 1, NULL);
219 add_widget (listmode_dlg, radio_columns);
220 radio_columns->sel = format_columns;
221 radio_genwidth = radio_new (UY+1, UX+2, 2, s_genwidth, 1, NULL);
222 add_widget (listmode_dlg, radio_genwidth);
223 radio_genwidth->sel = format_width;
226 static void listmode_done (void)
228 destroy_dlg (listmode_dlg);
229 if (0)
230 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
231 repaint_screen ();
234 static char *select_new_item (void)
236 /* NOTE: The following array of possible items must match the
237 formats array in screen.c. Better approach might be to make the
238 formats array global */
239 char *possible_items [] =
240 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
241 "owner", "group", "atime", "ctime", "space", "mark",
242 "inode", NULL };
244 int i;
245 Listbox *mylistbox;
247 mylistbox = create_listbox_window (12, 20, " Add listing format item ", listmode_section);
248 for (i = 0; possible_items [i]; i++){
249 listbox_add_item (mylistbox->list, 0, 0, possible_items [i], NULL);
252 i = run_listbox (mylistbox);
253 if (i >= 0)
254 return possible_items [i];
255 else
256 return NULL;
259 static char *
260 collect_new_format (void)
262 char *newformat;
263 int i;
264 char *last;
265 char *text, *extra;
267 newformat = g_malloc (1024);
268 if (radio_genwidth->sel)
269 strcpy (newformat, "full ");
270 else
271 strcpy (newformat, "half ");
272 if (radio_columns->sel)
273 strcat (newformat, "2 ");
274 last = NULL;
275 for (i = 0;;i++){
276 listbox_select_by_number (l_listmode, i);
277 listbox_get_current (l_listmode, &text, &extra);
278 if (text == last)
279 break;
280 if (last != NULL)
281 strcat (newformat, ",");
282 strcat (newformat, text);
283 last = text;
285 return newformat;
288 char *listmode_edit (char *oldlistformat)
290 char *newformat = NULL;
291 char *s;
293 s = g_strdup (oldlistformat);
294 init_listmode (s);
295 g_free (s);
297 while (newformat == NULL)
299 /* display file info */
300 attrset (SELECTED_COLOR);
302 run_dlg (listmode_dlg);
304 switch (listmode_dlg->ret_value) {
305 case B_CANCEL:
306 newformat = g_strdup (oldlistformat);
307 break;
309 case B_ADD:
310 s = select_new_item ();
311 if (s)
312 listbox_add_item (l_listmode, 0, 0, s, NULL);
313 break;
315 case B_REMOVE:
316 listbox_remove_current (l_listmode, 0);
317 break;
319 case B_ENTER:
320 newformat = collect_new_format ();
321 break;
325 listmode_done ();
326 return newformat;