Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / listmode.c
blobe83e0c1fb942d47a9f4a687625bfae0336a59c65
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 "lib/global.h"
39 #include "lib/widget.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/key.h"
43 #include "lib/skin/skin.h"
45 /* Needed for the extern declarations of integer parameters */
46 #include "dir.h"
47 #include "panel.h" /* Needed for the externs */
48 #include "file.h"
49 #include "layout.h" /* repaint_screen() */
50 #include "main.h"
51 #include "listmode.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define UX 5
58 #define UY 2
60 #define BX 5
61 #define BY 18
63 #define B_ADD B_USER
64 #define B_REMOVE (B_USER + 1)
66 #define B_PLUS B_USER
67 #define B_MINUS (B_USER+1)
69 /*** file scope type declarations ****************************************************************/
71 struct listmode_button
73 int ret_cmd, flags, y, x;
74 char *text;
75 bcback callback;
78 struct listmode_label
80 int y, x;
81 char *text;
84 /*** file scope variables ************************************************************************/
86 static WListbox *l_listmode;
88 static WLabel *pname;
90 static char *listmode_section = "[Listing format edit]";
92 static char *s_genwidth[2] = { "Half width", "Full width" };
94 static WRadio *radio_genwidth;
95 static char *s_columns[2] = { "One column", "Two columns" };
97 static WRadio *radio_columns;
98 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
100 static WRadio *radio_justify;
101 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
103 static WRadio *radio_itemwidth;
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 static char *
109 select_new_item (void)
111 char **possible_items;
112 char *ret = NULL;
113 int i;
114 Listbox *mylistbox;
116 possible_items = panel_get_user_possible_fields (NULL);
118 mylistbox = create_listbox_window (20, 12, "Add listing format item", listmode_section);
119 for (i = 0; possible_items[i]; i++)
121 listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL);
124 i = run_listbox (mylistbox);
125 if (i >= 0)
126 ret = g_strdup (possible_items[i]);
128 g_strfreev (possible_items);
129 return ret;
132 /* --------------------------------------------------------------------------------------------- */
134 static int
135 bplus_cback (int action)
137 return 0;
140 /* --------------------------------------------------------------------------------------------- */
142 static int
143 bminus_cback (int action)
145 return 0;
148 /* --------------------------------------------------------------------------------------------- */
150 static int
151 badd_cback (int action)
153 char *s = select_new_item ();
154 if (s)
156 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL);
157 g_free (s);
159 return 0;
162 /* --------------------------------------------------------------------------------------------- */
164 static int
165 bremove_cback (int action)
167 listbox_remove_current (l_listmode);
168 return 0;
171 /* --------------------------------------------------------------------------------------------- */
173 static Dlg_head *
174 init_listmode (char *oldlistformat)
176 int i;
177 char *s;
178 int format_width = 0;
179 int format_columns = 0;
180 Dlg_head *listmode_dlg;
182 static struct listmode_label listmode_labels[] = {
183 {UY + 13, UX + 22, "Item width:"}
186 static struct listmode_button listmode_but[] = {
187 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
188 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
189 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
190 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
191 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
192 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
195 do_refresh ();
197 listmode_dlg =
198 create_dlg (TRUE, 0, 0, 22, 74, dialog_colors, NULL, listmode_section,
199 "Listing format edit", DLG_CENTER | DLG_REVERSE);
201 add_widget (listmode_dlg, groupbox_new (UY, UX, 4, 63, "General options"));
202 add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items"));
203 add_widget (listmode_dlg, groupbox_new (UY + 4, UX + 20, 11, 43, "Item options"));
205 for (i = 0; i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
206 add_widget (listmode_dlg,
207 button_new (listmode_but[i].y, listmode_but[i].x,
208 listmode_but[i].ret_cmd,
209 listmode_but[i].flags,
210 listmode_but[i].text, listmode_but[i].callback));
212 /* We add the labels. */
213 for (i = 0; i < sizeof (listmode_labels) / sizeof (struct listmode_label); i++)
215 pname = label_new (listmode_labels[i].y, listmode_labels[i].x, listmode_labels[i].text);
216 add_widget (listmode_dlg, pname);
219 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
220 add_widget (listmode_dlg, radio_itemwidth);
221 radio_itemwidth = 0;
222 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
223 add_widget (listmode_dlg, radio_justify);
224 radio_justify->sel = 1;
226 /* get new listbox */
227 l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, FALSE, NULL);
229 if (strncmp (oldlistformat, "full ", 5) == 0)
231 format_width = 1;
232 oldlistformat += 5;
234 if (strncmp (oldlistformat, "half ", 5) == 0)
236 oldlistformat += 5;
238 if (strncmp (oldlistformat, "2 ", 2) == 0)
240 format_columns = 1;
241 oldlistformat += 2;
243 if (strncmp (oldlistformat, "1 ", 2) == 0)
245 oldlistformat += 2;
247 s = strtok (oldlistformat, ",");
249 while (s)
251 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL);
252 s = strtok (NULL, ",");
255 /* add listbox to the dialogs */
256 add_widget (listmode_dlg, l_listmode);
258 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
259 add_widget (listmode_dlg, radio_columns);
260 radio_columns->sel = format_columns;
261 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
262 add_widget (listmode_dlg, radio_genwidth);
263 radio_genwidth->sel = format_width;
265 return listmode_dlg;
268 /* --------------------------------------------------------------------------------------------- */
270 static void
271 listmode_done (Dlg_head * h)
273 destroy_dlg (h);
274 if (0)
275 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
276 repaint_screen ();
279 /* --------------------------------------------------------------------------------------------- */
281 static char *
282 collect_new_format (void)
284 char *newformat;
285 int i;
286 char *last;
287 char *text, *extra;
289 newformat = g_malloc (1024);
290 if (radio_genwidth->sel)
291 strcpy (newformat, "full ");
292 else
293 strcpy (newformat, "half ");
294 if (radio_columns->sel)
295 strcat (newformat, "2 ");
296 last = NULL;
297 for (i = 0;; i++)
299 listbox_select_entry (l_listmode, i);
300 listbox_get_current (l_listmode, &text, &extra);
301 if (text == last)
302 break;
303 if (last != NULL)
304 strcat (newformat, ",");
305 strcat (newformat, text);
306 last = text;
308 return newformat;
311 /* --------------------------------------------------------------------------------------------- */
312 /*** public functions ****************************************************************************/
313 /* --------------------------------------------------------------------------------------------- */
315 /** Return new format or NULL if the user cancelled the dialog */
316 char *
317 listmode_edit (char *oldlistformat)
319 char *newformat = NULL;
320 char *s;
321 Dlg_head *listmode_dlg;
323 s = g_strdup (oldlistformat);
324 listmode_dlg = init_listmode (s);
325 g_free (s);
327 if (run_dlg (listmode_dlg) == B_ENTER)
329 newformat = collect_new_format ();
332 listmode_done (listmode_dlg);
333 return newformat;
336 /* --------------------------------------------------------------------------------------------- */
338 #endif /* LISTMODE_EDITOR */