remove trailing whitespace at end of lines
[reactos.git] / rosapps / mc / src / listmode.c
1 /* Directory panel listing format editor -- for the Midnight Commander
2 Copyright (C) 1994, 1995 The Free Software Foundation
3
4 Written by: 1994 Radek Doulik
5 1995 Janne Kukonlehto
6
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.
11
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.
16
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.
20 */
21
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 <stdlib.h> /* For malloc() */
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #ifndef OS2_NT
33 # include <grp.h>
34 # include <pwd.h>
35 #endif
36 #include "tty.h"
37 #include "mad.h"
38 #include "util.h" /* Needed for the externs */
39 #include "win.h"
40 #include "color.h"
41 #include "dlg.h"
42 #include "widget.h"
43 #include "dialog.h" /* For do_refresh() */
44 #include "wtools.h"
45
46 /* Needed for the extern declarations of integer parameters */
47 #include "dir.h"
48 #include "panel.h" /* Needed for the externs */
49 #include "file.h"
50 #include "main.h"
51 #include "global.h"
52 #include "listmode.h"
53
54 #define UX 5
55 #define UY 2
56
57 #define BX 5
58 #define BY 18
59
60 #define BUTTONS 4
61 #define LABELS 4
62 #define B_ADD B_USER
63 #define B_REMOVE B_USER + 1
64
65 static WListbox *l_listmode;
66
67 static Dlg_head *listmode_dlg;
68
69 static WLabel *pname;
70
71 static char *listmode_section = "[Listing format edit]";
72
73 static char *s_genwidth [2] = {"Half width", "Full width"};
74 WRadio *radio_genwidth;
75 static char *s_columns [2] = {"One column", "Two columns"};
76 WRadio *radio_columns;
77 static char *s_justify [3] =
78 {"Left justified", "Default justification", "Right justified"};
79 WRadio *radio_justify;
80 static char *s_itemwidth [3] =
81 {"Free width", "Fixed width", "Growable width"};
82 WRadio *radio_itemwidth;
83
84 struct {
85 int ret_cmd, flags, y, x;
86 char *text;
87 } listmode_but[BUTTONS] = {
88 { B_CANCEL, NORMAL_BUTTON, 0, 53, "&Cancel" },
89 { B_ADD, NORMAL_BUTTON, 0, 22, "&Add item"},
90 { B_REMOVE, NORMAL_BUTTON, 0, 10, "&Remove" },
91 { B_ENTER, DEFPUSH_BUTTON, 0, 0, "&Ok" },
92 };
93
94 #define B_PLUS B_USER
95 #define B_MINUS B_USER+1
96
97 struct {
98 int y, x;
99 char *text;
100 } listmode_text [LABELS] = {
101 { UY, UX + 1, " General options " },
102 { UY+4, UX+1, " Items "},
103 { UY+4, UX+21, " Item options" },
104 { UY+13, UX+22, "Item width:" }
105 };
106
107 #ifndef HAVE_X
108 static void listmode_refresh (void)
109 {
110 attrset (COLOR_NORMAL);
111 dlg_erase (listmode_dlg);
112
113 draw_box (listmode_dlg, 1, 2, 20, 70);
114 draw_box (listmode_dlg, UY, UX, 4, 63);
115 draw_box (listmode_dlg, UY + 4, UX, 11, 18);
116 draw_box (listmode_dlg, UY + 4, UX+20, 11, 43);
117 }
118 #endif
119
120 static int bplus_cback (int action, void *data)
121 {
122 return 0;
123 }
124
125 static int bminus_cback (int action, void *data)
126 {
127 return 0;
128 }
129
130 static int listmode_callback (Dlg_head * h, int Par, int Msg)
131 {
132 switch (Msg) {
133 #ifndef HAVE_X
134 case DLG_DRAW:
135 listmode_refresh ();
136 break;
137 #endif
138
139 case DLG_POST_KEY:
140 /* fall */
141 case DLG_INIT:
142 attrset (COLOR_NORMAL);
143 dlg_move (h, UY+13, UX+35);
144 printw ("%02d", 99);
145 attrset (MENU_ENTRY_COLOR);
146 break;
147 }
148 return 0;
149 }
150
151 static int l_call (void *data)
152 {
153 return 1;
154 }
155
156 static void init_listmode (char *oldlistformat)
157 {
158 int i;
159 char *s;
160 int format_width = 0;
161 int format_columns = 0;
162
163 do_refresh ();
164
165 listmode_dlg = create_dlg (0, 0, 22, 74, dialog_colors,
166 listmode_callback, listmode_section, "listmode",
167 DLG_CENTER);
168 x_set_dialog_title (listmode_dlg, "Listing format edit");
169
170 #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
171
172 for (i = 0; i < BUTTONS; i++)
173 add_widgetl (listmode_dlg, button_new (XTRACT (i)), (i == BUTTONS - 1) ?
174 XV_WLAY_CENTERROW : XV_WLAY_RIGHTOF);
175
176 /* We add the labels. */
177 for (i = 0; i < LABELS; i++){
178 pname = label_new (listmode_text [i].y,
179 listmode_text [i].x, listmode_text [i].text, NULL);
180 add_widget (listmode_dlg, pname);
181 }
182
183 add_widget (listmode_dlg, button_new (UY+13, UX+37, B_MINUS, NORMAL_BUTTON,
184 "&-", bminus_cback, 0, NULL));
185 add_widget (listmode_dlg, button_new (UY+13, UX+34, B_PLUS, NORMAL_BUTTON,
186 "&+", bplus_cback, 0, NULL));
187 radio_itemwidth = radio_new (UY+9, UX+22, 3, s_itemwidth, 1, NULL);
188 add_widget (listmode_dlg, radio_itemwidth);
189 radio_itemwidth = 0;
190 radio_justify = radio_new (UY+5, UX+22, 3, s_justify, 1, NULL);
191 add_widget (listmode_dlg, radio_justify);
192 radio_justify->sel = 1;
193
194 /* get new listbox */
195 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, 0, l_call, NULL);
196
197 if (strncmp (oldlistformat, "full ", 5) == 0){
198 format_width = 1;
199 oldlistformat += 5;
200 }
201 if (strncmp (oldlistformat, "half ", 5) == 0){
202 oldlistformat += 5;
203 }
204 if (strncmp (oldlistformat, "2 ", 2) == 0){
205 format_columns = 1;
206 oldlistformat += 2;
207 }
208 if (strncmp (oldlistformat, "1 ", 2) == 0){
209 oldlistformat += 2;
210 }
211 s = strtok (oldlistformat, ",");
212
213 while (s){
214 listbox_add_item (l_listmode, 0, 0, s, NULL);
215 s = strtok (NULL, ",");
216 }
217
218 /* add listbox to the dialogs */
219 add_widgetl (listmode_dlg, l_listmode, XV_WLAY_EXTENDWIDTH);
220
221 radio_columns = radio_new (UY+1, UX+32, 2, s_columns, 1, NULL);
222 add_widget (listmode_dlg, radio_columns);
223 radio_columns->sel = format_columns;
224 radio_genwidth = radio_new (UY+1, UX+2, 2, s_genwidth, 1, NULL);
225 add_widget (listmode_dlg, radio_genwidth);
226 radio_genwidth->sel = format_width;
227 }
228
229 static void listmode_done (void)
230 {
231 destroy_dlg (listmode_dlg);
232 if (0)
233 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
234 repaint_screen ();
235 }
236
237 char *select_new_item (void)
238 {
239 /* NOTE: The following array of possible items must match the
240 formats array in screen.c. Better approach might be to make the
241 formats array global */
242 char *possible_items [] =
243 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
244 "owner", "group", "atime", "ctime", "space", "mark",
245 "inode", NULL };
246
247 int i;
248 Listbox *mylistbox;
249
250 mylistbox = create_listbox_window (12, 20, " Add listing format item ", listmode_section);
251 for (i = 0; possible_items [i]; i++){
252 listbox_add_item (mylistbox->list, 0, 0, possible_items [i], NULL);
253 }
254
255 i = run_listbox (mylistbox);
256 if (i >= 0)
257 return possible_items [i];
258 else
259 return NULL;
260 }
261
262 char *collect_new_format (void)
263 {
264 char *newformat;
265 int i;
266 char *last;
267 char *text, *extra;
268
269 newformat = xmalloc (1024, "collect_new_format");
270 if (radio_genwidth->sel)
271 strcpy (newformat, "full ");
272 else
273 strcpy (newformat, "half ");
274 if (radio_columns->sel)
275 strcat (newformat, "2 ");
276 last = NULL;
277 for (i = 0;;i++){
278 listbox_select_by_number (l_listmode, i);
279 listbox_get_current (l_listmode, &text, &extra);
280 if (text == last)
281 break;
282 if (last != NULL)
283 strcat (newformat, ",");
284 strcat (newformat, text);
285 last = text;
286 }
287 return newformat;
288 }
289
290 char *listmode_edit (char *oldlistformat)
291 {
292 char *newformat = NULL;
293 char *s;
294
295 s = strdup (oldlistformat);
296 init_listmode (s);
297 free (s);
298
299 while (newformat == NULL)
300 {
301 /* display file info */
302 attrset (SELECTED_COLOR);
303
304 run_dlg (listmode_dlg);
305
306 switch (listmode_dlg->ret_value) {
307 case B_CANCEL:
308 newformat = strdup (oldlistformat);
309 break;
310
311 case B_ADD:
312 s = select_new_item ();
313 if (s)
314 listbox_add_item (l_listmode, 0, 0, s, NULL);
315 break;
316
317 case B_REMOVE:
318 listbox_remove_current (l_listmode, 0);
319 break;
320
321 case B_ENTER:
322 newformat = collect_new_format ();
323 break;
324 }
325 }
326
327 listmode_done ();
328 return newformat;
329 }