Set WOP_TOP_SELECT options for panel widgets: WPanel, WView and WTree.
[midnight-commander.git] / src / viewer / dialogs.c
blob844b9ae94e2ec65bdfeb64985b22901f7ccb5367
1 /*
2 Internal file viewer for the Midnight Commander
3 Function for paint dialogs
5 Copyright (C) 1994-2016
6 Free Software Foundation, Inc.
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
20 This file is part of the Midnight Commander.
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.
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.
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/>.
36 #include <config.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
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
49 #include "src/history.h"
51 #include "internal.h"
53 /*** global variables ****************************************************************************/
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
63 /*** file scope macro definitions ****************************************************************/
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 /*** file scope functions ************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
72 /* --------------------------------------------------------------------------------------------- */
73 /*** public functions ****************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 gboolean
77 mcview_dialog_search (WView * view)
79 char *exp = NULL;
80 int qd_result;
81 size_t num_of_types;
82 gchar **list_of_types;
84 list_of_types = mc_search_get_types_strings_array (&num_of_types);
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* */
109 quick_dialog_t qdlg = {
110 -1, -1, 58,
111 N_("Search"), "[Input Line Keys]",
112 quick_widgets, NULL, NULL
115 qd_result = quick_dialog (&qdlg);
118 g_strfreev (list_of_types);
120 if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0'))
122 g_free (exp);
123 return FALSE;
126 #ifdef HAVE_CHARSET
128 GString *tmp;
130 tmp = str_convert_to_input (exp);
131 if (tmp != NULL)
133 g_free (exp);
134 exp = g_string_free (tmp, FALSE);
137 #endif
139 g_free (view->last_search_string);
140 view->last_search_string = exp;
141 mcview_nroff_seq_free (&view->search_nroff_seq);
142 mc_search_free (view->search);
144 #ifdef HAVE_CHARSET
145 view->search = mc_search_new (view->last_search_string, cp_source);
146 #else
147 view->search = mc_search_new (view->last_search_string, NULL);
148 #endif
149 view->search_nroff_seq = mcview_nroff_seq_new (view);
150 if (view->search != NULL)
152 view->search->search_type = mcview_search_options.type;
153 #ifdef HAVE_CHARSET
154 view->search->is_all_charsets = mcview_search_options.all_codepages;
155 #endif
156 view->search->is_case_sensitive = mcview_search_options.case_sens;
157 view->search->whole_words = mcview_search_options.whole_words;
158 view->search->search_fn = mcview_search_cmd_callback;
159 view->search->update_fn = mcview_search_update_cmd_callback;
162 return (view->search != NULL);
165 /* --------------------------------------------------------------------------------------------- */
167 gboolean
168 mcview_dialog_goto (WView * view, off_t * offset)
170 typedef enum
172 MC_VIEW_GOTO_LINENUM = 0,
173 MC_VIEW_GOTO_PERCENT = 1,
174 MC_VIEW_GOTO_OFFSET_DEC = 2,
175 MC_VIEW_GOTO_OFFSET_HEX = 3
176 } mcview_goto_type_t;
178 const char *mc_view_goto_str[] = {
179 N_("&Line number"),
180 N_("Pe&rcents"),
181 N_("&Decimal offset"),
182 N_("He&xadecimal offset")
185 static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
187 size_t num_of_types;
188 char *exp = NULL;
189 int qd_result;
190 gboolean res;
192 num_of_types = G_N_ELEMENTS (mc_view_goto_str);
194 #ifdef ENABLE_NLS
196 size_t i;
198 for (i = 0; i < num_of_types; i++)
199 mc_view_goto_str[i] = _(mc_view_goto_str[i]);
201 #endif
204 quick_widget_t quick_widgets[] = {
205 /* *INDENT-OFF* */
206 QUICK_INPUT (INPUT_LAST_TEXT, MC_HISTORY_VIEW_GOTO, &exp, NULL,
207 FALSE, FALSE, INPUT_COMPLETE_NONE),
208 QUICK_RADIO (num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type,
209 NULL),
210 QUICK_BUTTONS_OK_CANCEL,
211 QUICK_END
212 /* *INDENT-ON* */
215 quick_dialog_t qdlg = {
216 -1, -1, 40,
217 N_("Goto"), "[Input Line Keys]",
218 quick_widgets, NULL, NULL
221 /* run dialog */
222 qd_result = quick_dialog (&qdlg);
225 *offset = -1;
227 /* check input line value */
228 res = (qd_result != B_CANCEL && exp != NULL && exp[0] != '\0');
229 if (res)
231 int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
232 off_t addr;
233 char *error;
235 addr = (off_t) g_ascii_strtoll (exp, &error, base);
236 if ((*error == '\0') && (addr >= 0))
238 switch (current_goto_type)
240 case MC_VIEW_GOTO_LINENUM:
241 /* Line number entered by user is 1-based. */
242 if (addr > 0)
243 addr--;
244 mcview_coord_to_offset (view, offset, addr, 0);
245 *offset = mcview_bol (view, *offset, 0);
246 break;
247 case MC_VIEW_GOTO_PERCENT:
248 if (addr > 100)
249 addr = 100;
250 *offset = addr * mcview_get_filesize (view) / 100;
251 if (!view->hex_mode)
252 *offset = mcview_bol (view, *offset, 0);
253 break;
254 case MC_VIEW_GOTO_OFFSET_DEC:
255 case MC_VIEW_GOTO_OFFSET_HEX:
256 *offset = addr;
257 if (!view->hex_mode)
258 *offset = mcview_bol (view, *offset, 0);
259 else
261 addr = mcview_get_filesize (view);
262 if (*offset > addr)
263 *offset = addr;
265 break;
266 default:
267 *offset = 0;
268 break;
273 g_free (exp);
274 return res;