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