Code refactoring in tests.
[midnight-commander.git] / src / filemanager / listmode.c
blob0af5845888040e084686108669a8780aa11a1e67
1 /*
2 Directory panel listing format editor -- for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2005,
5 2006, 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Radek Doulik, 1994
10 Janne Kukonlehto, 1995
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file listmode.c
29 * \brief Source: directory panel listing format editor
32 #include <config.h>
34 #ifdef LISTMODE_EDITOR
36 #include <stdio.h>
37 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
43 #include "lib/global.h"
44 #include "lib/widget.h"
46 #include "lib/tty/tty.h"
47 #include "lib/tty/key.h"
48 #include "lib/skin/skin.h"
50 /* Needed for the extern declarations of integer parameters */
51 #include "dir.h"
52 #include "panel.h" /* Needed for the externs */
53 #include "file.h"
54 #include "listmode.h"
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
60 #define UX 5
61 #define UY 2
63 #define BX 5
64 #define BY 18
66 #define B_ADD B_USER
67 #define B_REMOVE (B_USER + 1)
69 #define B_PLUS B_USER
70 #define B_MINUS (B_USER+1)
72 /*** file scope type declarations ****************************************************************/
74 struct listmode_button
76 int ret_cmd, flags, y, x;
77 char *text;
78 bcback callback;
81 struct listmode_label
83 int y, x;
84 char *text;
87 /*** file scope variables ************************************************************************/
89 static WListbox *l_listmode;
91 static WLabel *pname;
93 static char *listmode_section = "[Listing format edit]";
95 static char *s_genwidth[2] = { "Half width", "Full width" };
97 static WRadio *radio_genwidth;
98 static char *s_columns[2] = { "One column", "Two columns" };
100 static WRadio *radio_columns;
101 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
103 static WRadio *radio_justify;
104 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
106 static WRadio *radio_itemwidth;
108 /*** file scope functions ************************************************************************/
109 /* --------------------------------------------------------------------------------------------- */
111 static char *
112 select_new_item (void)
114 char **possible_items;
115 char *ret = NULL;
116 int i;
117 Listbox *mylistbox;
119 possible_items = panel_get_user_possible_fields (NULL);
121 mylistbox = create_listbox_window (20, 12, "Add listing format item", listmode_section);
122 for (i = 0; possible_items[i]; i++)
124 listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL);
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);
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 create_dlg (TRUE, 0, 0, 22, 74, dialog_colors, NULL, NULL, listmode_section,
202 "Listing format edit", DLG_CENTER | DLG_REVERSE);
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);
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 destroy_dlg (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 (run_dlg (listmode_dlg) == B_ENTER)
332 newformat = collect_new_format ();
335 listmode_done (listmode_dlg);
336 return newformat;
339 /* --------------------------------------------------------------------------------------------- */
341 #endif /* LISTMODE_EDITOR */