2 Directory panel listing format editor -- for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
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/>.
28 * \brief Source: directory panel listing format editor
33 #ifdef LISTMODE_EDITOR
38 #include <sys/types.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 */
51 #include "panel.h" /* Needed for the externs */
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
66 #define B_REMOVE (B_USER + 1)
69 #define B_MINUS (B_USER+1)
71 /*** file scope type declarations ****************************************************************/
73 struct listmode_button
75 int ret_cmd
, flags
, y
, x
;
86 /*** forward declarations (file scope functions) *************************************************/
88 /*** file scope variables ************************************************************************/
90 static WListbox
*l_listmode
;
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 /* --------------------------------------------------------------------------------------------- */
114 select_new_item (void)
116 char **possible_items
;
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
,
130 i
= listbox_run (mylistbox
);
132 ret
= g_strdup (possible_items
[i
]);
134 g_strfreev (possible_items
);
138 /* --------------------------------------------------------------------------------------------- */
141 bplus_cback (int action
)
146 /* --------------------------------------------------------------------------------------------- */
149 bminus_cback (int action
)
154 /* --------------------------------------------------------------------------------------------- */
157 badd_cback (int action
)
159 char *s
= select_new_item ();
162 listbox_add_item (l_listmode
, LISTBOX_APPEND_AT_END
, 0, s
, NULL
, FALSE
);
168 /* --------------------------------------------------------------------------------------------- */
171 bremove_cback (int action
)
173 listbox_remove_current (l_listmode
);
177 /* --------------------------------------------------------------------------------------------- */
180 init_listmode (char *oldlistformat
)
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
},
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
);
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)
240 if (strncmp (oldlistformat
, "half ", 5) == 0)
244 if (strncmp (oldlistformat
, "2 ", 2) == 0)
249 if (strncmp (oldlistformat
, "1 ", 2) == 0)
253 s
= strtok (oldlistformat
, ",");
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
;
274 /* --------------------------------------------------------------------------------------------- */
277 listmode_done (WDialog
*h
)
279 widget_destroy (WIDGET (h
));
281 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
285 /* --------------------------------------------------------------------------------------------- */
288 collect_new_format (void)
295 newformat
= g_malloc (1024);
296 if (radio_genwidth
->sel
)
297 strcpy (newformat
, "full ");
299 strcpy (newformat
, "half ");
300 if (radio_columns
->sel
)
301 strcat (newformat
, "2 ");
305 listbox_set_current (l_listmode
, i
);
306 listbox_get_current (l_listmode
, &text
, &extra
);
310 strcat (newformat
, ",");
311 strcat (newformat
, text
);
317 /* --------------------------------------------------------------------------------------------- */
318 /*** public functions ****************************************************************************/
319 /* --------------------------------------------------------------------------------------------- */
321 /** Return new format or NULL if the user cancelled the dialog */
323 listmode_edit (char *oldlistformat
)
325 char *newformat
= NULL
;
327 WDialog
*listmode_dlg
;
329 s
= g_strdup (oldlistformat
);
330 listmode_dlg
= init_listmode (s
);
333 if (dlg_run (listmode_dlg
) == B_ENTER
)
335 newformat
= collect_new_format ();
338 listmode_done (listmode_dlg
);
342 /* --------------------------------------------------------------------------------------------- */
344 #endif /* LISTMODE_EDITOR */