Merge branch '4535_tmpdir_name'
[midnight-commander.git] / src / filemanager / listmode.c
bloba8881d2634fa241faf7b1107a5ff47311c884836
1 /*
2 Directory panel listing format editor -- for the Midnight Commander
4 Copyright (C) 1994-2024
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 /*** forward declarations (file scope functions) *************************************************/
88 /*** file scope variables ************************************************************************/
90 static WListbox *l_listmode;
92 static WLabel *pname;
94 static char *listmode_section = "[Listing format edit]";
96 static char *s_genwidth[2] = { "Half width", "Full width" };
98 static WRadio *radio_genwidth;
99 static char *s_columns[2] = { "One column", "Two columns" };
101 static WRadio *radio_columns;
102 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
104 static WRadio *radio_justify;
105 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
107 static WRadio *radio_itemwidth;
109 /* --------------------------------------------------------------------------------------------- */
110 /*** file scope functions ************************************************************************/
111 /* --------------------------------------------------------------------------------------------- */
113 static char *
114 select_new_item (void)
116 char **possible_items;
117 char *ret = NULL;
118 int i;
119 Listbox *mylistbox;
121 possible_items = panel_get_user_possible_fields (NULL);
123 mylistbox = listbox_window_new (20, 12, "Add listing format item", listmode_section);
124 for (i = 0; possible_items[i]; i++)
126 listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL,
127 FALSE);
130 i = listbox_run (mylistbox);
131 if (i >= 0)
132 ret = g_strdup (possible_items[i]);
134 g_strfreev (possible_items);
135 return ret;
138 /* --------------------------------------------------------------------------------------------- */
140 static int
141 bplus_cback (int action)
143 return 0;
146 /* --------------------------------------------------------------------------------------------- */
148 static int
149 bminus_cback (int action)
151 return 0;
154 /* --------------------------------------------------------------------------------------------- */
156 static int
157 badd_cback (int action)
159 char *s = select_new_item ();
160 if (s)
162 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
163 g_free (s);
165 return 0;
168 /* --------------------------------------------------------------------------------------------- */
170 static int
171 bremove_cback (int action)
173 listbox_remove_current (l_listmode);
174 return 0;
177 /* --------------------------------------------------------------------------------------------- */
179 static WDialog *
180 init_listmode (char *oldlistformat)
182 int i;
183 char *s;
184 int format_width = 0;
185 int format_columns = 0;
186 WDialog *listmode_dlg;
188 static struct listmode_label listmode_labels[] = {
189 {UY + 13, UX + 22, "Item width:"}
192 static struct listmode_button listmode_but[] = {
193 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
194 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
195 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
196 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
197 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
198 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
201 do_refresh ();
203 listmode_dlg =
204 dlg_create (TRUE, 0, 0, 22, 74, WPOS_CENTER, FALSE, dialog_colors, NULL, NULL,
205 listmode_section, "Listing format edit");
207 add_widget (listmode_dlg, groupbox_new (UY, UX, 4, 63, "General options"));
208 add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items"));
209 add_widget (listmode_dlg, groupbox_new (UY + 4, UX + 20, 11, 43, "Item options"));
211 for (i = 0; i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
212 add_widget (listmode_dlg,
213 button_new (listmode_but[i].y, listmode_but[i].x,
214 listmode_but[i].ret_cmd,
215 listmode_but[i].flags,
216 listmode_but[i].text, listmode_but[i].callback));
218 /* We add the labels. */
219 for (i = 0; i < sizeof (listmode_labels) / sizeof (struct listmode_label); i++)
221 pname = label_new (listmode_labels[i].y, listmode_labels[i].x, listmode_labels[i].text);
222 add_widget (listmode_dlg, pname);
225 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
226 add_widget (listmode_dlg, radio_itemwidth);
227 radio_itemwidth = 0;
228 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
229 add_widget (listmode_dlg, radio_justify);
230 radio_justify->sel = 1;
232 /* get new listbox */
233 l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, FALSE, NULL);
235 if (strncmp (oldlistformat, "full ", 5) == 0)
237 format_width = 1;
238 oldlistformat += 5;
240 if (strncmp (oldlistformat, "half ", 5) == 0)
242 oldlistformat += 5;
244 if (strncmp (oldlistformat, "2 ", 2) == 0)
246 format_columns = 1;
247 oldlistformat += 2;
249 if (strncmp (oldlistformat, "1 ", 2) == 0)
251 oldlistformat += 2;
253 s = strtok (oldlistformat, ",");
255 while (s)
257 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
258 s = strtok (NULL, ",");
261 /* add listbox to the dialogs */
262 add_widget (listmode_dlg, l_listmode);
264 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
265 add_widget (listmode_dlg, radio_columns);
266 radio_columns->sel = format_columns;
267 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
268 add_widget (listmode_dlg, radio_genwidth);
269 radio_genwidth->sel = format_width;
271 return listmode_dlg;
274 /* --------------------------------------------------------------------------------------------- */
276 static void
277 listmode_done (WDialog * h)
279 widget_destroy (WIDGET (h));
280 if (0)
281 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
282 repaint_screen ();
285 /* --------------------------------------------------------------------------------------------- */
287 static char *
288 collect_new_format (void)
290 char *newformat;
291 int i;
292 char *last;
293 char *text, *extra;
295 newformat = g_malloc (1024);
296 if (radio_genwidth->sel)
297 strcpy (newformat, "full ");
298 else
299 strcpy (newformat, "half ");
300 if (radio_columns->sel)
301 strcat (newformat, "2 ");
302 last = NULL;
303 for (i = 0;; i++)
305 listbox_set_current (l_listmode, i);
306 listbox_get_current (l_listmode, &text, &extra);
307 if (text == last)
308 break;
309 if (last != NULL)
310 strcat (newformat, ",");
311 strcat (newformat, text);
312 last = text;
314 return newformat;
317 /* --------------------------------------------------------------------------------------------- */
318 /*** public functions ****************************************************************************/
319 /* --------------------------------------------------------------------------------------------- */
321 /** Return new format or NULL if the user cancelled the dialog */
322 char *
323 listmode_edit (char *oldlistformat)
325 char *newformat = NULL;
326 char *s;
327 WDialog *listmode_dlg;
329 s = g_strdup (oldlistformat);
330 listmode_dlg = init_listmode (s);
331 g_free (s);
333 if (dlg_run (listmode_dlg) == B_ENTER)
335 newformat = collect_new_format ();
338 listmode_done (listmode_dlg);
339 return newformat;
342 /* --------------------------------------------------------------------------------------------- */
344 #endif /* LISTMODE_EDITOR */