Some fixups with wrongly highlighted words
[midnight-commander.git] / src / listmode.c
blobdff24845bbd9db23bcc34af72d295c32a6ab15a2
1 /* Directory panel listing format editor -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 Written by: 1994 Radek Doulik
6 1995 Janne Kukonlehto
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 /** \file listmode.c
24 * \brief Source: directory panel listing format editor
27 #include <config.h>
29 #ifdef LISTMODE_EDITOR
31 #include <stdio.h>
32 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
38 #include "global.h"
39 #include "tty.h"
40 #include "win.h"
41 #include "color.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h"
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 "listmode.h"
53 #define UX 5
54 #define UY 2
56 #define BX 5
57 #define BY 18
59 #define B_ADD B_USER
60 #define B_REMOVE (B_USER + 1)
62 static WListbox *l_listmode;
64 static WLabel *pname;
66 static char *listmode_section = "[Listing format edit]";
68 static char *s_genwidth[2] = { "Half width", "Full width" };
69 static WRadio *radio_genwidth;
70 static char *s_columns[2] = { "One column", "Two columns" };
71 static WRadio *radio_columns;
72 static char *s_justify[3] =
73 { "Left justified", "Default justification", "Right justified" };
74 static WRadio *radio_justify;
75 static char *s_itemwidth[3] =
76 { "Free width", "Fixed width", "Growable width" };
77 static WRadio *radio_itemwidth;
79 struct listmode_button {
80 int ret_cmd, flags, y, x;
81 char *text;
82 bcback callback;
85 #define B_PLUS B_USER
86 #define B_MINUS (B_USER+1)
88 struct listmode_label {
89 int y, x;
90 char *text;
93 static char *
94 select_new_item (void)
96 /* NOTE: The following array of possible items must match the
97 formats array in screen.c. Better approach might be to make the
98 formats array global */
99 char *possible_items[] =
100 { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
101 "owner", "group", "atime", "ctime", "space", "mark",
102 "inode", NULL
105 int i;
106 Listbox *mylistbox;
108 mylistbox =
109 create_listbox_window (12, 20, " Add listing format item ",
110 listmode_section);
111 for (i = 0; possible_items[i]; i++) {
112 listbox_add_item (mylistbox->list, 0, 0, possible_items[i], NULL);
115 i = run_listbox (mylistbox);
116 if (i >= 0)
117 return possible_items[i];
118 else
119 return NULL;
122 static int
123 bplus_cback (int action)
125 return 0;
128 static int
129 bminus_cback (int action)
131 return 0;
134 static int
135 badd_cback (int action)
137 char *s = select_new_item ();
138 if (s) {
139 listbox_add_item (l_listmode, 0, 0, s, NULL);
141 return 0;
144 static int
145 bremove_cback (int action)
147 listbox_remove_current (l_listmode, 0);
148 return 0;
151 static Dlg_head *
152 init_listmode (char *oldlistformat)
154 int i;
155 char *s;
156 int format_width = 0;
157 int format_columns = 0;
158 Dlg_head *listmode_dlg;
160 static struct listmode_label listmode_labels[] = {
161 {UY + 13, UX + 22, "Item width:"}
164 static struct listmode_button listmode_but[] = {
165 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
166 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
167 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
168 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
169 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
170 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
173 do_refresh ();
175 listmode_dlg =
176 create_dlg (0, 0, 22, 74, dialog_colors, NULL, listmode_section,
177 "Listing format edit", DLG_CENTER | DLG_REVERSE);
179 add_widget (listmode_dlg,
180 groupbox_new (UX, UY, 63, 4, "General options"));
181 add_widget (listmode_dlg, groupbox_new (UX, UY + 4, 18, 11, "Items"));
182 add_widget (listmode_dlg,
183 groupbox_new (UX + 20, UY + 4, 43, 11, "Item options"));
185 for (i = 0;
186 i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
187 add_widget (listmode_dlg,
188 button_new (listmode_but[i].y, listmode_but[i].x,
189 listmode_but[i].ret_cmd,
190 listmode_but[i].flags,
191 listmode_but[i].text,
192 listmode_but[i].callback));
194 /* We add the labels. */
195 for (i = 0;
196 i < sizeof (listmode_labels) / sizeof (struct listmode_label);
197 i++) {
198 pname =
199 label_new (listmode_labels[i].y, listmode_labels[i].x,
200 listmode_labels[i].text);
201 add_widget (listmode_dlg, pname);
204 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
205 add_widget (listmode_dlg, radio_itemwidth);
206 radio_itemwidth = 0;
207 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
208 add_widget (listmode_dlg, radio_justify);
209 radio_justify->sel = 1;
211 /* get new listbox */
212 l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, NULL);
214 if (strncmp (oldlistformat, "full ", 5) == 0) {
215 format_width = 1;
216 oldlistformat += 5;
218 if (strncmp (oldlistformat, "half ", 5) == 0) {
219 oldlistformat += 5;
221 if (strncmp (oldlistformat, "2 ", 2) == 0) {
222 format_columns = 1;
223 oldlistformat += 2;
225 if (strncmp (oldlistformat, "1 ", 2) == 0) {
226 oldlistformat += 2;
228 s = strtok (oldlistformat, ",");
230 while (s) {
231 listbox_add_item (l_listmode, 0, 0, s, NULL);
232 s = strtok (NULL, ",");
235 /* add listbox to the dialogs */
236 add_widget (listmode_dlg, l_listmode);
238 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
239 add_widget (listmode_dlg, radio_columns);
240 radio_columns->sel = format_columns;
241 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
242 add_widget (listmode_dlg, radio_genwidth);
243 radio_genwidth->sel = format_width;
245 return listmode_dlg;
248 static void
249 listmode_done (Dlg_head *h)
251 destroy_dlg (h);
252 if (0)
253 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
254 repaint_screen ();
257 static char *
258 collect_new_format (void)
260 char *newformat;
261 int i;
262 char *last;
263 char *text, *extra;
265 newformat = g_malloc (1024);
266 if (radio_genwidth->sel)
267 strcpy (newformat, "full ");
268 else
269 strcpy (newformat, "half ");
270 if (radio_columns->sel)
271 strcat (newformat, "2 ");
272 last = NULL;
273 for (i = 0;; i++) {
274 listbox_select_by_number (l_listmode, i);
275 listbox_get_current (l_listmode, &text, &extra);
276 if (text == last)
277 break;
278 if (last != NULL)
279 strcat (newformat, ",");
280 strcat (newformat, text);
281 last = text;
283 return newformat;
286 /* Return new format or NULL if the user cancelled the dialog */
287 char *
288 listmode_edit (char *oldlistformat)
290 char *newformat = NULL;
291 char *s;
292 Dlg_head *listmode_dlg;
294 s = g_strdup (oldlistformat);
295 listmode_dlg = init_listmode (s);
296 g_free (s);
298 if (run_dlg (listmode_dlg) == B_ENTER) {
299 newformat = collect_new_format ();
302 listmode_done (listmode_dlg);
303 return newformat;
306 #endif /* LISTMODE_EDITOR */