Manual pages: mcmcdiffmceditmcview

root/src/viewer/dialogs.c

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

DEFINITIONS

This source file includes following definitions.
  1. mcview_dialog_search
  2. mcview_dialog_goto

   1 /*
   2    Internal file viewer for the Midnight Commander
   3    Function for paint dialogs
   4 
   5    Copyright (C) 1994-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Miguel de Icaza, 1994, 1995, 1998
  10    Janne Kukonlehto, 1994, 1995
  11    Jakub Jelinek, 1995
  12    Joseph M. Hinkle, 1996
  13    Norbert Warmuth, 1997
  14    Pavel Machek, 1998
  15    Roland Illig <roland.illig@gmx.de>, 2004, 2005
  16    Slava Zanko <slavazanko@google.com>, 2009
  17    Andrew Borodin <aborodin@vmail.ru>, 2009-2022
  18    Ilia Maslakov <il.smind@gmail.com>, 2009
  19 
  20    This file is part of the Midnight Commander.
  21 
  22    The Midnight Commander is free software: you can redistribute it
  23    and/or modify it under the terms of the GNU General Public License as
  24    published by the Free Software Foundation, either version 3 of the License,
  25    or (at your option) any later version.
  26 
  27    The Midnight Commander is distributed in the hope that it will be useful,
  28    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30    GNU General Public License for more details.
  31 
  32    You should have received a copy of the GNU General Public License
  33    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  34  */
  35 
  36 #include <config.h>
  37 
  38 #include <stdlib.h>
  39 #include <sys/types.h>
  40 
  41 #include "lib/global.h"
  42 #include "lib/search.h"
  43 #include "lib/strutil.h"
  44 #include "lib/widget.h"
  45 #include "lib/charsets.h"
  46 
  47 #include "src/history.h"
  48 
  49 #include "internal.h"
  50 
  51 /*** global variables ****************************************************************************/
  52 
  53 /*** file scope macro definitions ****************************************************************/
  54 
  55 /*** file scope type declarations ****************************************************************/
  56 
  57 /*** file scope variables ************************************************************************/
  58 
  59 /*** file scope functions ************************************************************************/
  60 /* --------------------------------------------------------------------------------------------- */
  61 
  62 /* --------------------------------------------------------------------------------------------- */
  63 /*** public functions ****************************************************************************/
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 gboolean
  67 mcview_dialog_search (WView *view)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69     char *exp = NULL;
  70     int qd_result;
  71     size_t num_of_types = 0;
  72     gchar **list_of_types;
  73 
  74     list_of_types = mc_search_get_types_strings_array (&num_of_types);
  75 
  76     {
  77         quick_widget_t quick_widgets[] = {
  78             // clang-format off
  79             QUICK_LABELED_INPUT (_ ("Enter search string:"), input_label_above, INPUT_LAST_TEXT,
  80                                  MC_HISTORY_SHARED_SEARCH, &exp, NULL, FALSE, FALSE,
  81                                  INPUT_COMPLETE_NONE),
  82             QUICK_SEPARATOR (TRUE),
  83             QUICK_START_COLUMNS,
  84                 QUICK_RADIO (num_of_types, (const char **) list_of_types,
  85                              (int *) &mcview_search_options.type, NULL),
  86             QUICK_NEXT_COLUMN,
  87                 QUICK_CHECKBOX (_ ("Cas&e sensitive"), &mcview_search_options.case_sens, NULL),
  88                 QUICK_CHECKBOX (_ ("&Backwards"), &mcview_search_options.backwards, NULL),
  89                 QUICK_CHECKBOX (_ ("&Whole words"), &mcview_search_options.whole_words, NULL),
  90                 QUICK_CHECKBOX (_ ("&All charsets"), &mcview_search_options.all_codepages, NULL),
  91             QUICK_STOP_COLUMNS,
  92             QUICK_BUTTONS_OK_CANCEL,
  93             QUICK_END,
  94             // clang-format on
  95         };
  96 
  97         WRect r = { -1, -1, 0, 58 };
  98 
  99         quick_dialog_t qdlg = {
 100             .rect = r,
 101             .title = _ ("Search"),
 102             .help = "[Input Line Keys]",
 103             .widgets = quick_widgets,
 104             .callback = NULL,
 105             .mouse_callback = NULL,
 106         };
 107 
 108         qd_result = quick_dialog (&qdlg);
 109     }
 110 
 111     g_strfreev (list_of_types);
 112 
 113     if (qd_result == B_CANCEL || exp[0] == '\0')
 114     {
 115         g_free (exp);
 116         return FALSE;
 117     }
 118 
 119     {
 120         GString *tmp;
 121 
 122         tmp = str_convert_to_input (exp);
 123         g_free (exp);
 124         if (tmp != NULL)
 125             exp = g_string_free (tmp, FALSE);
 126         else
 127             exp = g_strdup ("");
 128     }
 129 
 130     mcview_search_deinit (view);
 131     view->last_search_string = exp;
 132 
 133     return mcview_search_init (view);
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 gboolean
 139 mcview_dialog_goto (WView *view, off_t *offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     typedef enum
 142     {
 143         MC_VIEW_GOTO_LINENUM = 0,
 144         MC_VIEW_GOTO_PERCENT = 1,
 145         MC_VIEW_GOTO_OFFSET_DEC = 2,
 146         MC_VIEW_GOTO_OFFSET_HEX = 3
 147     } mcview_goto_type_t;
 148 
 149     const char *mc_view_goto_str[] = {
 150         _ ("&Line number"),
 151         _ ("Pe&rcents"),
 152         _ ("&Decimal offset"),
 153         _ ("He&xadecimal offset"),
 154     };
 155 
 156     static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
 157 
 158     size_t num_of_types;
 159     char *exp = NULL;
 160     int qd_result;
 161     gboolean res;
 162 
 163     num_of_types = G_N_ELEMENTS (mc_view_goto_str);
 164 
 165     {
 166         quick_widget_t quick_widgets[] = {
 167             QUICK_INPUT (INPUT_LAST_TEXT, MC_HISTORY_VIEW_GOTO, &exp, NULL, FALSE, FALSE,
 168                          INPUT_COMPLETE_NONE),
 169             QUICK_RADIO (num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type,
 170                          NULL),
 171             QUICK_BUTTONS_OK_CANCEL,
 172             QUICK_END,
 173         };
 174 
 175         WRect r = { -1, -1, 0, 40 };
 176 
 177         quick_dialog_t qdlg = {
 178             .rect = r,
 179             .title = _ ("Goto"),
 180             .help = "[Input Line Keys]",
 181             .widgets = quick_widgets,
 182             .callback = NULL,
 183             .mouse_callback = NULL,
 184         };
 185 
 186         // run dialog
 187         qd_result = quick_dialog (&qdlg);
 188     }
 189 
 190     *offset = -1;
 191 
 192     // check input line value
 193     res = (qd_result != B_CANCEL && exp[0] != '\0');
 194     if (res)
 195     {
 196         int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
 197         off_t addr;
 198         char *error;
 199 
 200         addr = (off_t) g_ascii_strtoll (exp, &error, base);
 201         if ((*error == '\0') && (addr >= 0))
 202         {
 203             switch (current_goto_type)
 204             {
 205             case MC_VIEW_GOTO_LINENUM:
 206                 // Line number entered by user is 1-based.
 207                 if (addr > 0)
 208                     addr--;
 209                 mcview_coord_to_offset (view, offset, addr, 0);
 210                 *offset = mcview_bol (view, *offset, 0);
 211                 break;
 212             case MC_VIEW_GOTO_PERCENT:
 213                 if (addr > 100)
 214                     addr = 100;
 215                 // read all data from pipe to get real size
 216                 if (view->growbuf_in_use)
 217                     mcview_growbuf_read_all_data (view);
 218                 *offset = addr * mcview_get_filesize (view) / 100;
 219                 if (!view->mode_flags.hex)
 220                     *offset = mcview_bol (view, *offset, 0);
 221                 break;
 222             case MC_VIEW_GOTO_OFFSET_DEC:
 223             case MC_VIEW_GOTO_OFFSET_HEX:
 224                 if (!view->mode_flags.hex)
 225                 {
 226                     if (view->growbuf_in_use)
 227                         mcview_growbuf_read_until (view, addr);
 228 
 229                     *offset = mcview_bol (view, addr, 0);
 230                 }
 231                 else
 232                 {
 233                     // read all data from pipe to get real size
 234                     if (view->growbuf_in_use)
 235                         mcview_growbuf_read_all_data (view);
 236 
 237                     *offset = addr;
 238                     addr = mcview_get_filesize (view);
 239                     if (*offset > addr)
 240                         *offset = addr;
 241                 }
 242                 break;
 243             default:
 244                 *offset = 0;
 245                 break;
 246             }
 247         }
 248     }
 249 
 250     g_free (exp);
 251     return res;
 252 }
 253 
 254 /* --------------------------------------------------------------------------------------------- */

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