root/src/filemanager/listmode.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. select_new_item
  2. bplus_cback
  3. bminus_cback
  4. badd_cback
  5. bremove_cback
  6. init_listmode
  7. listmode_done
  8. collect_new_format
  9. listmode_edit

   1 /*
   2    Directory panel listing format editor -- for the Midnight Commander
   3 
   4    Copyright (C) 1994-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Radek Doulik, 1994
   9    Janne Kukonlehto, 1995
  10 
  11    This file is part of the Midnight Commander.
  12 
  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.
  17 
  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.
  22 
  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/>.
  25  */
  26 
  27 /** \file listmode.c
  28  *  \brief Source: directory panel listing format editor
  29  */
  30 
  31 #include <config.h>
  32 
  33 #ifdef LISTMODE_EDITOR
  34 
  35 #include <stdio.h>
  36 #include <string.h>
  37 
  38 #include <sys/types.h>
  39 #include <sys/stat.h>
  40 #include <unistd.h>
  41 
  42 #include "lib/global.h"
  43 #include "lib/widget.h"
  44 
  45 #include "lib/tty/tty.h"
  46 #include "lib/tty/key.h"
  47 #include "lib/skin/skin.h"
  48 
  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"
  54 
  55 /*** global variables ****************************************************************************/
  56 
  57 /*** file scope macro definitions ****************************************************************/
  58 
  59 #define UX 5
  60 #define UY 2
  61 
  62 #define BX 5
  63 #define BY 18
  64 
  65 #define B_ADD    B_USER
  66 #define B_REMOVE (B_USER + 1)
  67 
  68 #define B_PLUS B_USER
  69 #define B_MINUS (B_USER+1)
  70 
  71 /*** file scope type declarations ****************************************************************/
  72 
  73 struct listmode_button
  74 {
  75     int ret_cmd, flags, y, x;
  76     char *text;
  77     bcback callback;
  78 };
  79 
  80 struct listmode_label
  81 {
  82     int y, x;
  83     char *text;
  84 };
  85 
  86 /*** forward declarations (file scope functions) *************************************************/
  87 
  88 /*** file scope variables ************************************************************************/
  89 
  90 static WListbox *l_listmode;
  91 
  92 static WLabel *pname;
  93 
  94 static char *listmode_section = "[Listing format edit]";
  95 
  96 static char *s_genwidth[2] = { "Half width", "Full width" };
  97 
  98 static WRadio *radio_genwidth;
  99 static char *s_columns[2] = { "One column", "Two columns" };
 100 
 101 static WRadio *radio_columns;
 102 static char *s_justify[3] = { "Left justified", "Default justification", "Right justified" };
 103 
 104 static WRadio *radio_justify;
 105 static char *s_itemwidth[3] = { "Free width", "Fixed width", "Growable width" };
 106 
 107 static WRadio *radio_itemwidth;
 108 
 109 /* --------------------------------------------------------------------------------------------- */
 110 /*** file scope functions ************************************************************************/
 111 /* --------------------------------------------------------------------------------------------- */
 112 
 113 static char *
 114 select_new_item (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 115 {
 116     char **possible_items;
 117     char *ret = NULL;
 118     int i;
 119     Listbox *mylistbox;
 120 
 121     possible_items = panel_get_user_possible_fields (NULL);
 122 
 123     mylistbox = listbox_window_new (20, 12, "Add listing format item", listmode_section);
 124     for (i = 0; possible_items[i]; i++)
 125     {
 126         listbox_add_item (mylistbox->list, LISTBOX_APPEND_AT_END, 0, possible_items[i], NULL,
 127                           FALSE);
 128     }
 129 
 130     i = listbox_run (mylistbox);
 131     if (i >= 0)
 132         ret = g_strdup (possible_items[i]);
 133 
 134     g_strfreev (possible_items);
 135     return ret;
 136 }
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 
 140 static int
 141 bplus_cback (int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 142 {
 143     return 0;
 144 }
 145 
 146 /* --------------------------------------------------------------------------------------------- */
 147 
 148 static int
 149 bminus_cback (int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 150 {
 151     return 0;
 152 }
 153 
 154 /* --------------------------------------------------------------------------------------------- */
 155 
 156 static int
 157 badd_cback (int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 158 {
 159     char *s = select_new_item ();
 160     if (s)
 161     {
 162         listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
 163         g_free (s);
 164     }
 165     return 0;
 166 }
 167 
 168 /* --------------------------------------------------------------------------------------------- */
 169 
 170 static int
 171 bremove_cback (int action)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     listbox_remove_current (l_listmode);
 174     return 0;
 175 }
 176 
 177 /* --------------------------------------------------------------------------------------------- */
 178 
 179 static WDialog *
 180 init_listmode (char *oldlistformat)
     /* [previous][next][first][last][top][bottom][index][help]  */
 181 {
 182     int i;
 183     char *s;
 184     int format_width = 0;
 185     int format_columns = 0;
 186     WDialog *listmode_dlg;
 187 
 188     static struct listmode_label listmode_labels[] = {
 189         {UY + 13, UX + 22, "Item width:"}
 190     };
 191 
 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},
 199     };
 200 
 201     do_refresh ();
 202 
 203     listmode_dlg =
 204         dlg_create (TRUE, 0, 0, 22, 74, WPOS_CENTER, FALSE, dialog_colors, NULL, NULL,
 205                     listmode_section, "Listing format edit");
 206 
 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"));
 210 
 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));
 217 
 218     /* We add the labels. */
 219     for (i = 0; i < sizeof (listmode_labels) / sizeof (struct listmode_label); i++)
 220     {
 221         pname = label_new (listmode_labels[i].y, listmode_labels[i].x, listmode_labels[i].text);
 222         add_widget (listmode_dlg, pname);
 223     }
 224 
 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;
 231 
 232     /* get new listbox */
 233     l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, FALSE, NULL);
 234 
 235     if (strncmp (oldlistformat, "full ", 5) == 0)
 236     {
 237         format_width = 1;
 238         oldlistformat += 5;
 239     }
 240     if (strncmp (oldlistformat, "half ", 5) == 0)
 241     {
 242         oldlistformat += 5;
 243     }
 244     if (strncmp (oldlistformat, "2 ", 2) == 0)
 245     {
 246         format_columns = 1;
 247         oldlistformat += 2;
 248     }
 249     if (strncmp (oldlistformat, "1 ", 2) == 0)
 250     {
 251         oldlistformat += 2;
 252     }
 253     s = strtok (oldlistformat, ",");
 254 
 255     while (s)
 256     {
 257         listbox_add_item (l_listmode, LISTBOX_APPEND_AT_END, 0, s, NULL, FALSE);
 258         s = strtok (NULL, ",");
 259     }
 260 
 261     /* add listbox to the dialogs */
 262     add_widget (listmode_dlg, l_listmode);
 263 
 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;
 270 
 271     return listmode_dlg;
 272 }
 273 
 274 /* --------------------------------------------------------------------------------------------- */
 275 
 276 static void
 277 listmode_done (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 278 {
 279     widget_destroy (WIDGET (h));
 280     if (0)
 281         update_panels (UP_OPTIMIZE, UP_KEEPSEL);
 282     repaint_screen ();
 283 }
 284 
 285 /* --------------------------------------------------------------------------------------------- */
 286 
 287 static char *
 288 collect_new_format (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     char *newformat;
 291     int i;
 292     char *last;
 293     char *text, *extra;
 294 
 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++)
 304     {
 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;
 313     }
 314     return newformat;
 315 }
 316 
 317 /* --------------------------------------------------------------------------------------------- */
 318 /*** public functions ****************************************************************************/
 319 /* --------------------------------------------------------------------------------------------- */
 320 
 321 /** Return new format or NULL if the user cancelled the dialog */
 322 char *
 323 listmode_edit (char *oldlistformat)
     /* [previous][next][first][last][top][bottom][index][help]  */
 324 {
 325     char *newformat = NULL;
 326     char *s;
 327     WDialog *listmode_dlg;
 328 
 329     s = g_strdup (oldlistformat);
 330     listmode_dlg = init_listmode (s);
 331     g_free (s);
 332 
 333     if (dlg_run (listmode_dlg) == B_ENTER)
 334     {
 335         newformat = collect_new_format ();
 336     }
 337 
 338     listmode_done (listmode_dlg);
 339     return newformat;
 340 }
 341 
 342 /* --------------------------------------------------------------------------------------------- */
 343 
 344 #endif /* LISTMODE_EDITOR */

/* [previous][next][first][last][top][bottom][index][help]  */