Editor: remove unused #define's.
[midnight-commander.git] / src / filemanager / listmode.c
blob38a385686ec85e06a819780e96b21677f8d788b0
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 "listmode.h"
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 #define UX 5
56 #define UY 2
58 #define BX 5
59 #define BY 18
61 #define B_ADD B_USER
62 #define B_REMOVE (B_USER + 1)
64 #define B_PLUS B_USER
65 #define B_MINUS (B_USER+1)
67 /*** file scope type declarations ****************************************************************/
69 struct listmode_button
71 int ret_cmd, flags, y, x;
72 char *text;
73 bcback callback;
76 struct listmode_label
78 int y, x;
79 char *text;
82 /*** file scope variables ************************************************************************/
84 static WListbox *l_listmode;
86 static WLabel *pname;
88 static char *listmode_section = "[Listing format edit]";
90 static char *s_genwidth[2] = { "Half width", "Full width" };
92 static WRadio *radio_genwidth;
93 static char *s_columns[2] = { "One column", "Two columns" };
95 static WRadio *radio_columns;
96 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
98 static WRadio *radio_justify;
99 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
101 static WRadio *radio_itemwidth;
103 /*** file scope functions ************************************************************************/
104 /* --------------------------------------------------------------------------------------------- */
106 static char *
107 select_new_item (void)
109 char **possible_items;
110 char *ret = NULL;
111 int i;
112 Listbox *mylistbox;
114 possible_items = panel_get_user_possible_fields (NULL);
116 mylistbox = create_listbox_window (20, 12, "Add listing format item", listmode_section);
117 for (i = 0; possible_items[i]; i++)
119 listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL);
122 i = run_listbox (mylistbox);
123 if (i >= 0)
124 ret = g_strdup (possible_items[i]);
126 g_strfreev (possible_items);
127 return ret;
130 /* --------------------------------------------------------------------------------------------- */
132 static int
133 bplus_cback (int action)
135 return 0;
138 /* --------------------------------------------------------------------------------------------- */
140 static int
141 bminus_cback (int action)
143 return 0;
146 /* --------------------------------------------------------------------------------------------- */
148 static int
149 badd_cback (int action)
151 char *s = select_new_item ();
152 if (s)
154 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL);
155 g_free (s);
157 return 0;
160 /* --------------------------------------------------------------------------------------------- */
162 static int
163 bremove_cback (int action)
165 listbox_remove_current (l_listmode);
166 return 0;
169 /* --------------------------------------------------------------------------------------------- */
171 static Dlg_head *
172 init_listmode (char *oldlistformat)
174 int i;
175 char *s;
176 int format_width = 0;
177 int format_columns = 0;
178 Dlg_head *listmode_dlg;
180 static struct listmode_label listmode_labels[] = {
181 {UY + 13, UX + 22, "Item width:"}
184 static struct listmode_button listmode_but[] = {
185 {B_CANCEL, NORMAL_BUTTON, BY, BX + 53, "&Cancel", NULL},
186 {B_ADD, NORMAL_BUTTON, BY, BX + 22, "&Add item", badd_cback},
187 {B_REMOVE, NORMAL_BUTTON, BY, BX + 10, "&Remove", bremove_cback},
188 {B_ENTER, DEFPUSH_BUTTON, BY, BX, "&OK", NULL},
189 {B_PLUS, NARROW_BUTTON, UY + 13, UX + 37, "&+", bplus_cback},
190 {B_MINUS, NARROW_BUTTON, UY + 13, UX + 34, "&-", bminus_cback},
193 do_refresh ();
195 listmode_dlg =
196 create_dlg (TRUE, 0, 0, 22, 74, dialog_colors, NULL, listmode_section,
197 "Listing format edit", DLG_CENTER | DLG_REVERSE);
199 add_widget (listmode_dlg, groupbox_new (UY, UX, 4, 63, "General options"));
200 add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items"));
201 add_widget (listmode_dlg, groupbox_new (UY + 4, UX + 20, 11, 43, "Item options"));
203 for (i = 0; i < sizeof (listmode_but) / sizeof (struct listmode_button); i++)
204 add_widget (listmode_dlg,
205 button_new (listmode_but[i].y, listmode_but[i].x,
206 listmode_but[i].ret_cmd,
207 listmode_but[i].flags,
208 listmode_but[i].text, listmode_but[i].callback));
210 /* We add the labels. */
211 for (i = 0; i < sizeof (listmode_labels) / sizeof (struct listmode_label); i++)
213 pname = label_new (listmode_labels[i].y, listmode_labels[i].x, listmode_labels[i].text);
214 add_widget (listmode_dlg, pname);
217 radio_itemwidth = radio_new (UY + 9, UX + 22, 3, s_itemwidth);
218 add_widget (listmode_dlg, radio_itemwidth);
219 radio_itemwidth = 0;
220 radio_justify = radio_new (UY + 5, UX + 22, 3, s_justify);
221 add_widget (listmode_dlg, radio_justify);
222 radio_justify->sel = 1;
224 /* get new listbox */
225 l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, FALSE, NULL);
227 if (strncmp (oldlistformat, "full ", 5) == 0)
229 format_width = 1;
230 oldlistformat += 5;
232 if (strncmp (oldlistformat, "half ", 5) == 0)
234 oldlistformat += 5;
236 if (strncmp (oldlistformat, "2 ", 2) == 0)
238 format_columns = 1;
239 oldlistformat += 2;
241 if (strncmp (oldlistformat, "1 ", 2) == 0)
243 oldlistformat += 2;
245 s = strtok (oldlistformat, ",");
247 while (s)
249 listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL);
250 s = strtok (NULL, ",");
253 /* add listbox to the dialogs */
254 add_widget (listmode_dlg, l_listmode);
256 radio_columns = radio_new (UY + 1, UX + 32, 2, s_columns);
257 add_widget (listmode_dlg, radio_columns);
258 radio_columns->sel = format_columns;
259 radio_genwidth = radio_new (UY + 1, UX + 2, 2, s_genwidth);
260 add_widget (listmode_dlg, radio_genwidth);
261 radio_genwidth->sel = format_width;
263 return listmode_dlg;
266 /* --------------------------------------------------------------------------------------------- */
268 static void
269 listmode_done (Dlg_head * h)
271 destroy_dlg (h);
272 if (0)
273 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
274 repaint_screen ();
277 /* --------------------------------------------------------------------------------------------- */
279 static char *
280 collect_new_format (void)
282 char *newformat;
283 int i;
284 char *last;
285 char *text, *extra;
287 newformat = g_malloc (1024);
288 if (radio_genwidth->sel)
289 strcpy (newformat, "full ");
290 else
291 strcpy (newformat, "half ");
292 if (radio_columns->sel)
293 strcat (newformat, "2 ");
294 last = NULL;
295 for (i = 0;; i++)
297 listbox_select_entry (l_listmode, i);
298 listbox_get_current (l_listmode, &text, &extra);
299 if (text == last)
300 break;
301 if (last != NULL)
302 strcat (newformat, ",");
303 strcat (newformat, text);
304 last = text;
306 return newformat;
309 /* --------------------------------------------------------------------------------------------- */
310 /*** public functions ****************************************************************************/
311 /* --------------------------------------------------------------------------------------------- */
313 /** Return new format or NULL if the user cancelled the dialog */
314 char *
315 listmode_edit (char *oldlistformat)
317 char *newformat = NULL;
318 char *s;
319 Dlg_head *listmode_dlg;
321 s = g_strdup (oldlistformat);
322 listmode_dlg = init_listmode (s);
323 g_free (s);
325 if (run_dlg (listmode_dlg) == B_ENTER)
327 newformat = collect_new_format ();
330 listmode_done (listmode_dlg);
331 return newformat;
334 /* --------------------------------------------------------------------------------------------- */
336 #endif /* LISTMODE_EDITOR */