(mc_ctl): join conditions.
[midnight-commander.git] / src / filemanager / listmode.c
blob2f54aa7681f0da5bd92a7a1cddebca6392574174
1 /*
2 Directory panel listing format editor -- for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Radek Doulik, 1994
9 Janne Kukonlehto, 1995
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file listmode.c
28 * \brief Source: directory panel listing format editor
31 #include <config.h>
33 #ifdef LISTMODE_EDITOR
35 #include <stdio.h>
36 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
42 #include "lib/global.h"
43 #include "lib/widget.h"
45 #include "lib/tty/tty.h"
46 #include "lib/tty/key.h"
47 #include "lib/skin/skin.h"
49 /* Needed for the extern declarations of integer parameters */
50 #include "dir.h"
51 #include "panel.h" /* Needed for the externs */
52 #include "file.h"
53 #include "listmode.h"
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
59 #define UX 5
60 #define UY 2
62 #define BX 5
63 #define BY 18
65 #define B_ADD B_USER
66 #define B_REMOVE (B_USER + 1)
68 #define B_PLUS B_USER
69 #define B_MINUS (B_USER+1)
71 /*** file scope type declarations ****************************************************************/
73 struct listmode_button
75 int ret_cmd, flags, y, x;
76 char *text;
77 bcback callback;
80 struct listmode_label
82 int y, x;
83 char *text;
86 /*** file scope variables ************************************************************************/
88 static WListbox *l_listmode;
90 static WLabel *pname;
92 static char *listmode_section = "[Listing format edit]";
94 static char *s_genwidth[2] = { "Half width", "Full width" };
96 static WRadio *radio_genwidth;
97 static char *s_columns[2] = { "One column", "Two columns" };
99 static WRadio *radio_columns;
100 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
102 static WRadio *radio_justify;
103 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
105 static WRadio *radio_itemwidth;
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 static char *
111 select_new_item (void)
113 char **possible_items;
114 char *ret = NULL;
115 int i;
116 Listbox *mylistbox;
118 possible_items = panel_get_user_possible_fields (NULL);
120 mylistbox = create_listbox_window (20, 12, "Add listing format item", listmode_section);
121 for (i = 0; possible_items[i]; i++)
123 listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL,
124 FALSE);
127 i = run_listbox (mylistbox);
128 if (i >= 0)
129 ret = g_strdup (possible_items[i]);
131 g_strfreev (possible_items);
132 return ret;
135 /* --------------------------------------------------------------------------------------------- */
137 static int
138 bplus_cback (int action)
140 return 0;
143 /* --------------------------------------------------------------------------------------------- */
145 static int
146 bminus_cback (int action)
148 return 0;
151 /* --------------------------------------------------------------------------------------------- */
153 static int
154 badd_cback (int action)
156 char *s = select_new_item ();
157 if (s)
159 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
160 g_free (s);
162 return 0;
165 /* --------------------------------------------------------------------------------------------- */
167 static int
168 bremove_cback (int action)
170 listbox_remove_current (l_listmode);
171 return 0;
174 /* --------------------------------------------------------------------------------------------- */
176 static WDialog *
177 init_listmode (char *oldlistformat)
179 int i;
180 char *s;
181 int format_width = 0;
182 int format_columns = 0;
183 WDialog *listmode_dlg;
185 static struct listmode_label listmode_labels[] = {
186 {UY + 13, UX + 22, "Item width:"}
189 static struct listmode_button listmode_but[] = {
190 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
191 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
192 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
193 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
194 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
195 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
198 do_refresh ();
200 listmode_dlg =
201 dlg_create (TRUE, 0, 0, 22, 74, WPOS_CENTER, FALSE, dialog_colors, NULL, NULL,
202 listmode_section, "Listing format edit");
204 add_widget (listmode_dlg, groupbox_new (UY, UX, 4, 63, "General options"));
205 add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items"));
206 add_widget (listmode_dlg, groupbox_new (UY + 4, UX + 20, 11, 43, "Item options"));
208 for (i = 0; i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
209 add_widget (listmode_dlg,
210 button_new (listmode_but[i].y, listmode_but[i].x,
211 listmode_but[i].ret_cmd,
212 listmode_but[i].flags,
213 listmode_but[i].text, listmode_but[i].callback));
215 /* We add the labels. */
216 for (i = 0; i < sizeof (listmode_labels) / sizeof (struct listmode_label); i++)
218 pname = label_new (listmode_labels[i].y, listmode_labels[i].x, listmode_labels[i].text);
219 add_widget (listmode_dlg, pname);
222 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
223 add_widget (listmode_dlg, radio_itemwidth);
224 radio_itemwidth = 0;
225 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
226 add_widget (listmode_dlg, radio_justify);
227 radio_justify->sel = 1;
229 /* get new listbox */
230 l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, FALSE, NULL);
232 if (strncmp (oldlistformat, "full ", 5) == 0)
234 format_width = 1;
235 oldlistformat += 5;
237 if (strncmp (oldlistformat, "half ", 5) == 0)
239 oldlistformat += 5;
241 if (strncmp (oldlistformat, "2 ", 2) == 0)
243 format_columns = 1;
244 oldlistformat += 2;
246 if (strncmp (oldlistformat, "1 ", 2) == 0)
248 oldlistformat += 2;
250 s = strtok (oldlistformat, ",");
252 while (s)
254 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
255 s = strtok (NULL, ",");
258 /* add listbox to the dialogs */
259 add_widget (listmode_dlg, l_listmode);
261 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
262 add_widget (listmode_dlg, radio_columns);
263 radio_columns->sel = format_columns;
264 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
265 add_widget (listmode_dlg, radio_genwidth);
266 radio_genwidth->sel = format_width;
268 return listmode_dlg;
271 /* --------------------------------------------------------------------------------------------- */
273 static void
274 listmode_done (WDialog * h)
276 dlg_destroy (h);
277 if (0)
278 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
279 repaint_screen ();
282 /* --------------------------------------------------------------------------------------------- */
284 static char *
285 collect_new_format (void)
287 char *newformat;
288 int i;
289 char *last;
290 char *text, *extra;
292 newformat = g_malloc (1024);
293 if (radio_genwidth->sel)
294 strcpy (newformat, "full ");
295 else
296 strcpy (newformat, "half ");
297 if (radio_columns->sel)
298 strcat (newformat, "2 ");
299 last = NULL;
300 for (i = 0;; i++)
302 listbox_select_entry (l_listmode, i);
303 listbox_get_current (l_listmode, &text, &extra);
304 if (text == last)
305 break;
306 if (last != NULL)
307 strcat (newformat, ",");
308 strcat (newformat, text);
309 last = text;
311 return newformat;
314 /* --------------------------------------------------------------------------------------------- */
315 /*** public functions ****************************************************************************/
316 /* --------------------------------------------------------------------------------------------- */
318 /** Return new format or NULL if the user cancelled the dialog */
319 char *
320 listmode_edit (char *oldlistformat)
322 char *newformat = NULL;
323 char *s;
324 WDialog *listmode_dlg;
326 s = g_strdup (oldlistformat);
327 listmode_dlg = init_listmode (s);
328 g_free (s);
330 if (dlg_run (listmode_dlg) == B_ENTER)
332 newformat = collect_new_format ();
335 listmode_done (listmode_dlg);
336 return newformat;
339 /* --------------------------------------------------------------------------------------------- */
341 #endif /* LISTMODE_EDITOR */