*** empty log message ***
[midnight-commander.git] / src / listmode.c
blob539e7a072b0b618529d8e38e860b433c77e88dfc
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 Dlg_head *listmode_dlg;
65 static WLabel *pname;
67 static char *listmode_section = "[Listing format edit]";
69 static char *s_genwidth [2] = {"Half width", "Full width"};
70 WRadio *radio_genwidth;
71 static char *s_columns [2] = {"One column", "Two columns"};
72 WRadio *radio_columns;
73 static char *s_justify [3] =
74 {"Left justified", "Default justification", "Right justified"};
75 WRadio *radio_justify;
76 static char *s_itemwidth [3] =
77 {"Free width", "Fixed width", "Growable width"};
78 WRadio *radio_itemwidth;
80 struct {
81 int ret_cmd, flags, y, x;
82 char *text;
83 } 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 {
94 int y, x;
95 char *text;
96 } listmode_text [LABELS] = {
97 { UY, UX + 1, " General options " },
98 { UY+4, UX+1, " Items "},
99 { UY+4, UX+21, " Item options" },
100 { UY+13, UX+22, "Item width:" }
103 static void listmode_refresh (void)
105 attrset (COLOR_NORMAL);
106 dlg_erase (listmode_dlg);
108 draw_box (listmode_dlg, 1, 2, 20, 70);
109 draw_box (listmode_dlg, UY, UX, 4, 63);
110 draw_box (listmode_dlg, UY + 4, UX, 11, 18);
111 draw_box (listmode_dlg, UY + 4, UX+20, 11, 43);
114 static int bplus_cback (int action, void *data)
116 return 0;
119 static int bminus_cback (int action, void *data)
121 return 0;
124 static int listmode_callback (Dlg_head * h, int Par, int Msg)
126 switch (Msg) {
128 case DLG_DRAW:
129 listmode_refresh ();
130 break;
132 case DLG_POST_KEY:
133 /* fall */
135 case DLG_INIT:
136 attrset (COLOR_NORMAL);
137 dlg_move (h, UY+13, UX+35);
138 printw ("%02d", 99);
139 attrset (MENU_ENTRY_COLOR);
140 break;
142 return 0;
145 static int l_call (void *data)
147 return 1;
150 static void init_listmode (char *oldlistformat)
152 int i;
153 char *s;
154 int format_width = 0;
155 int format_columns = 0;
157 do_refresh ();
159 listmode_dlg = create_dlg (0, 0, 22, 74, dialog_colors,
160 listmode_callback, listmode_section, "listmode",
161 DLG_CENTER);
162 x_set_dialog_title (listmode_dlg, "Listing format edit");
164 #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
166 for (i = 0; i < BUTTONS; i++)
167 add_widget (listmode_dlg, button_new (XTRACT (i)));
169 /* We add the labels. */
170 for (i = 0; i < LABELS; i++){
171 pname = label_new (listmode_text [i].y,
172 listmode_text [i].x, listmode_text [i].text, NULL);
173 add_widget (listmode_dlg, pname);
176 add_widget (listmode_dlg, button_new (UY+13, UX+37, B_MINUS, NORMAL_BUTTON,
177 "&-", bminus_cback, 0, NULL));
178 add_widget (listmode_dlg, button_new (UY+13, UX+34, B_PLUS, NORMAL_BUTTON,
179 "&+", bplus_cback, 0, NULL));
180 radio_itemwidth = radio_new (UY+9, UX+22, 3, s_itemwidth, 1, NULL);
181 add_widget (listmode_dlg, radio_itemwidth);
182 radio_itemwidth = 0;
183 radio_justify = radio_new (UY+5, UX+22, 3, s_justify, 1, NULL);
184 add_widget (listmode_dlg, radio_justify);
185 radio_justify->sel = 1;
187 /* get new listbox */
188 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, 0, l_call, NULL);
190 if (strncmp (oldlistformat, "full ", 5) == 0){
191 format_width = 1;
192 oldlistformat += 5;
194 if (strncmp (oldlistformat, "half ", 5) == 0){
195 oldlistformat += 5;
197 if (strncmp (oldlistformat, "2 ", 2) == 0){
198 format_columns = 1;
199 oldlistformat += 2;
201 if (strncmp (oldlistformat, "1 ", 2) == 0){
202 oldlistformat += 2;
204 s = strtok (oldlistformat, ",");
206 while (s){
207 listbox_add_item (l_listmode, 0, 0, s, NULL);
208 s = strtok (NULL, ",");
211 /* add listbox to the dialogs */
212 add_widget (listmode_dlg, l_listmode);
214 radio_columns = radio_new (UY+1, UX+32, 2, s_columns, 1, NULL);
215 add_widget (listmode_dlg, radio_columns);
216 radio_columns->sel = format_columns;
217 radio_genwidth = radio_new (UY+1, UX+2, 2, s_genwidth, 1, NULL);
218 add_widget (listmode_dlg, radio_genwidth);
219 radio_genwidth->sel = format_width;
222 static void listmode_done (void)
224 destroy_dlg (listmode_dlg);
225 if (0)
226 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
227 repaint_screen ();
230 static char *select_new_item (void)
232 /* NOTE: The following array of possible items must match the
233 formats array in screen.c. Better approach might be to make the
234 formats array global */
235 char *possible_items [] =
236 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
237 "owner", "group", "atime", "ctime", "space", "mark",
238 "inode", NULL };
240 int i;
241 Listbox *mylistbox;
243 mylistbox = create_listbox_window (12, 20, " Add listing format item ", listmode_section);
244 for (i = 0; possible_items [i]; i++){
245 listbox_add_item (mylistbox->list, 0, 0, possible_items [i], NULL);
248 i = run_listbox (mylistbox);
249 if (i >= 0)
250 return possible_items [i];
251 else
252 return NULL;
255 static char *
256 collect_new_format (void)
258 char *newformat;
259 int i;
260 char *last;
261 char *text, *extra;
263 newformat = g_malloc (1024);
264 if (radio_genwidth->sel)
265 strcpy (newformat, "full ");
266 else
267 strcpy (newformat, "half ");
268 if (radio_columns->sel)
269 strcat (newformat, "2 ");
270 last = NULL;
271 for (i = 0;;i++){
272 listbox_select_by_number (l_listmode, i);
273 listbox_get_current (l_listmode, &text, &extra);
274 if (text == last)
275 break;
276 if (last != NULL)
277 strcat (newformat, ",");
278 strcat (newformat, text);
279 last = text;
281 return newformat;
284 char *listmode_edit (char *oldlistformat)
286 char *newformat = NULL;
287 char *s;
289 s = g_strdup (oldlistformat);
290 init_listmode (s);
291 g_free (s);
293 while (newformat == NULL)
295 /* display file info */
296 attrset (SELECTED_COLOR);
298 run_dlg (listmode_dlg);
300 switch (listmode_dlg->ret_value) {
301 case B_CANCEL:
302 newformat = g_strdup (oldlistformat);
303 break;
305 case B_ADD:
306 s = select_new_item ();
307 if (s)
308 listbox_add_item (l_listmode, 0, 0, s, NULL);
309 break;
311 case B_REMOVE:
312 listbox_remove_current (l_listmode, 0);
313 break;
315 case B_ENTER:
316 newformat = collect_new_format ();
317 break;
321 listmode_done ();
322 return newformat;
325 #endif /* LISTMODE_EDITOR */