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-2019
   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, 2012
  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 <http://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 #ifdef HAVE_CHARSET
  46 #include "lib/charsets.h"
  47 #endif
  48 
  49 #include "src/history.h"
  50 
  51 #include "internal.h"
  52 
  53 /*** global variables ****************************************************************************/
  54 
  55 mcview_search_options_t mcview_search_options = {
  56     .type = MC_SEARCH_T_NORMAL,
  57     .case_sens = FALSE,
  58     .backwards = FALSE,
  59     .whole_words = FALSE,
  60     .all_codepages = FALSE
  61 };
  62 
  63 /*** file scope macro definitions ****************************************************************/
  64 
  65 /*** file scope type declarations ****************************************************************/
  66 
  67 /*** file scope variables ************************************************************************/
  68 
  69 /*** file scope functions ************************************************************************/
  70 /* --------------------------------------------------------------------------------------------- */
  71 
  72 /* --------------------------------------------------------------------------------------------- */
  73 /*** public functions ****************************************************************************/
  74 /* --------------------------------------------------------------------------------------------- */
  75 
  76 gboolean
  77 mcview_dialog_search (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     char *exp = NULL;
  80     int qd_result;
  81     size_t num_of_types = 0;
  82     gchar **list_of_types;
  83 
  84     list_of_types = mc_search_get_types_strings_array (&num_of_types);
  85 
  86     {
  87         quick_widget_t quick_widgets[] = {
  88             /* *INDENT-OFF* */
  89             QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above,
  90                                  INPUT_LAST_TEXT, MC_HISTORY_SHARED_SEARCH, &exp,
  91                                  NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
  92             QUICK_SEPARATOR (TRUE),
  93             QUICK_START_COLUMNS,
  94                 QUICK_RADIO (num_of_types, (const char **) list_of_types,
  95                              (int *) &mcview_search_options.type, NULL),
  96             QUICK_NEXT_COLUMN,
  97                 QUICK_CHECKBOX (N_("Cas&e sensitive"), &mcview_search_options.case_sens, NULL),
  98                 QUICK_CHECKBOX (N_("&Backwards"), &mcview_search_options.backwards, NULL),
  99                 QUICK_CHECKBOX (N_("&Whole words"), &mcview_search_options.whole_words, NULL),
 100 #ifdef HAVE_CHARSET
 101                 QUICK_CHECKBOX (N_("&All charsets"), &mcview_search_options.all_codepages, NULL),
 102 #endif
 103             QUICK_STOP_COLUMNS,
 104             QUICK_BUTTONS_OK_CANCEL,
 105             QUICK_END
 106             /* *INDENT-ON* */
 107         };
 108 
 109         quick_dialog_t qdlg = {
 110             -1, -1, 58,
 111             N_("Search"), "[Input Line Keys]",
 112             quick_widgets, NULL, NULL
 113         };
 114 
 115         qd_result = quick_dialog (&qdlg);
 116     }
 117 
 118     g_strfreev (list_of_types);
 119 
 120     if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0'))
 121     {
 122         g_free (exp);
 123         return FALSE;
 124     }
 125 
 126 #ifdef HAVE_CHARSET
 127     {
 128         GString *tmp;
 129 
 130         tmp = str_convert_to_input (exp);
 131         g_free (exp);
 132         exp = g_string_free (tmp, FALSE);
 133     }
 134 #endif
 135 
 136     g_free (view->last_search_string);
 137     view->last_search_string = exp;
 138     mcview_nroff_seq_free (&view->search_nroff_seq);
 139     mc_search_free (view->search);
 140 
 141 #ifdef HAVE_CHARSET
 142     view->search = mc_search_new (view->last_search_string, cp_source);
 143 #else
 144     view->search = mc_search_new (view->last_search_string, NULL);
 145 #endif
 146     view->search_nroff_seq = mcview_nroff_seq_new (view);
 147     if (view->search != NULL)
 148     {
 149         view->search->search_type = mcview_search_options.type;
 150 #ifdef HAVE_CHARSET
 151         view->search->is_all_charsets = mcview_search_options.all_codepages;
 152 #endif
 153         view->search->is_case_sensitive = mcview_search_options.case_sens;
 154         view->search->whole_words = mcview_search_options.whole_words;
 155         view->search->search_fn = mcview_search_cmd_callback;
 156         view->search->update_fn = mcview_search_update_cmd_callback;
 157     }
 158 
 159     return (view->search != NULL);
 160 }
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 
 164 gboolean
 165 mcview_dialog_goto (WView * view, off_t * offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 166 {
 167     typedef enum
 168     {
 169         MC_VIEW_GOTO_LINENUM = 0,
 170         MC_VIEW_GOTO_PERCENT = 1,
 171         MC_VIEW_GOTO_OFFSET_DEC = 2,
 172         MC_VIEW_GOTO_OFFSET_HEX = 3
 173     } mcview_goto_type_t;
 174 
 175     const char *mc_view_goto_str[] = {
 176         N_("&Line number"),
 177         N_("Pe&rcents"),
 178         N_("&Decimal offset"),
 179         N_("He&xadecimal offset")
 180     };
 181 
 182     static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
 183 
 184     size_t num_of_types;
 185     char *exp = NULL;
 186     int qd_result;
 187     gboolean res;
 188 
 189     num_of_types = G_N_ELEMENTS (mc_view_goto_str);
 190 
 191 #ifdef ENABLE_NLS
 192     {
 193         size_t i;
 194 
 195         for (i = 0; i < num_of_types; i++)
 196             mc_view_goto_str[i] = _(mc_view_goto_str[i]);
 197     }
 198 #endif
 199 
 200     {
 201         quick_widget_t quick_widgets[] = {
 202             /* *INDENT-OFF* */
 203             QUICK_INPUT (INPUT_LAST_TEXT, MC_HISTORY_VIEW_GOTO, &exp, NULL,
 204                          FALSE, FALSE, INPUT_COMPLETE_NONE),
 205             QUICK_RADIO (num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type,
 206                          NULL),
 207             QUICK_BUTTONS_OK_CANCEL,
 208             QUICK_END
 209             /* *INDENT-ON* */
 210         };
 211 
 212         quick_dialog_t qdlg = {
 213             -1, -1, 40,
 214             N_("Goto"), "[Input Line Keys]",
 215             quick_widgets, NULL, NULL
 216         };
 217 
 218         /* run dialog */
 219         qd_result = quick_dialog (&qdlg);
 220     }
 221 
 222     *offset = -1;
 223 
 224     /* check input line value */
 225     res = (qd_result != B_CANCEL && exp != NULL && exp[0] != '\0');
 226     if (res)
 227     {
 228         int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
 229         off_t addr;
 230         char *error;
 231 
 232         addr = (off_t) g_ascii_strtoll (exp, &error, base);
 233         if ((*error == '\0') && (addr >= 0))
 234         {
 235             switch (current_goto_type)
 236             {
 237             case MC_VIEW_GOTO_LINENUM:
 238                 /* Line number entered by user is 1-based. */
 239                 if (addr > 0)
 240                     addr--;
 241                 mcview_coord_to_offset (view, offset, addr, 0);
 242                 *offset = mcview_bol (view, *offset, 0);
 243                 break;
 244             case MC_VIEW_GOTO_PERCENT:
 245                 if (addr > 100)
 246                     addr = 100;
 247                 /* read all data from pipe to get real size */
 248                 if (view->growbuf_in_use)
 249                     mcview_growbuf_read_all_data (view);
 250                 *offset = addr * mcview_get_filesize (view) / 100;
 251                 if (!view->mode_flags.hex)
 252                     *offset = mcview_bol (view, *offset, 0);
 253                 break;
 254             case MC_VIEW_GOTO_OFFSET_DEC:
 255             case MC_VIEW_GOTO_OFFSET_HEX:
 256                 if (!view->mode_flags.hex)
 257                 {
 258                     if (view->growbuf_in_use)
 259                         mcview_growbuf_read_until (view, addr);
 260 
 261                     *offset = mcview_bol (view, addr, 0);
 262                 }
 263                 else
 264                 {
 265                     /* read all data from pipe to get real size */
 266                     if (view->growbuf_in_use)
 267                         mcview_growbuf_read_all_data (view);
 268 
 269                     *offset = addr;
 270                     addr = mcview_get_filesize (view);
 271                     if (*offset > addr)
 272                         *offset = addr;
 273                 }
 274                 break;
 275             default:
 276                 *offset = 0;
 277                 break;
 278             }
 279         }
 280     }
 281 
 282     g_free (exp);
 283     return res;
 284 }
 285 
 286 /* --------------------------------------------------------------------------------------------- */

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