Drop old QuickWidget engine.
[midnight-commander.git] / src / viewer / dialogs.c
blobe7602854018babf58f435ce40b0a652c750a5168
1 /*
2 Internal file viewer for the Midnight Commander
3 Function for paint dialogs
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009, 2011
7 The Free Software Foundation, Inc.
9 Written by:
10 Miguel de Icaza, 1994, 1995, 1998
11 Janne Kukonlehto, 1994, 1995
12 Jakub Jelinek, 1995
13 Joseph M. Hinkle, 1996
14 Norbert Warmuth, 1997
15 Pavel Machek, 1998
16 Roland Illig <roland.illig@gmx.de>, 2004, 2005
17 Slava Zanko <slavazanko@google.com>, 2009
18 Andrew Borodin <aborodin@vmail.ru>, 2009, 2012
19 Ilia Maslakov <il.smind@gmail.com>, 2009
21 This file is part of the Midnight Commander.
23 The Midnight Commander is free software: you can redistribute it
24 and/or modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation, either version 3 of the License,
26 or (at your option) any later version.
28 The Midnight Commander is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #include <config.h>
39 #include <stdlib.h>
40 #include <sys/types.h>
42 #include "lib/global.h"
43 #include "lib/search.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #ifdef HAVE_CHARSET
47 #include "lib/charsets.h"
48 #endif
50 #include "src/history.h"
52 #include "internal.h"
54 /*** global variables ****************************************************************************/
56 mcview_search_options_t mcview_search_options = {
57 .type = MC_SEARCH_T_NORMAL,
58 .case_sens = FALSE,
59 .backwards = FALSE,
60 .whole_words = FALSE,
61 .all_codepages = FALSE
64 /*** file scope macro definitions ****************************************************************/
66 /*** file scope type declarations ****************************************************************/
68 /*** file scope variables ************************************************************************/
70 /*** file scope functions ************************************************************************/
71 /* --------------------------------------------------------------------------------------------- */
73 /* --------------------------------------------------------------------------------------------- */
74 /*** public functions ****************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 gboolean
78 mcview_dialog_search (mcview_t * view)
80 char *exp = NULL;
81 int qd_result;
82 size_t num_of_types;
83 gchar **list_of_types;
85 list_of_types = mc_search_get_types_strings_array (&num_of_types);
88 quick_widget_t quick_widgets[] = {
89 /* *INDENT-OFF* */
90 QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above,
91 INPUT_LAST_TEXT, 0, MC_HISTORY_SHARED_SEARCH, &exp, NULL),
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_START_BUTTONS (TRUE, TRUE),
105 QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL),
106 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
107 /* *INDENT-ON* */
108 QUICK_END
111 quick_dialog_t qdlg = {
112 -1, -1, 58,
113 N_("Search"), "[Input Line Keys]",
114 quick_widgets, NULL, NULL
117 qd_result = quick_dialog (&qdlg);
120 g_strfreev (list_of_types);
122 if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0'))
124 g_free (exp);
125 return FALSE;
128 #ifdef HAVE_CHARSET
130 GString *tmp;
132 tmp = str_convert_to_input (exp);
133 if (tmp != NULL)
135 g_free (exp);
136 exp = g_string_free (tmp, FALSE);
139 #endif
141 g_free (view->last_search_string);
142 view->last_search_string = exp;
143 mcview_nroff_seq_free (&view->search_nroff_seq);
144 mc_search_free (view->search);
146 view->search = mc_search_new (view->last_search_string, -1);
147 view->search_nroff_seq = mcview_nroff_seq_new (view);
148 if (view->search != NULL)
150 view->search->search_type = mcview_search_options.type;
151 view->search->is_all_charsets = mcview_search_options.all_codepages;
152 view->search->is_case_sensitive = mcview_search_options.case_sens;
153 view->search->whole_words = mcview_search_options.whole_words;
154 view->search->search_fn = mcview_search_cmd_callback;
155 view->search->update_fn = mcview_search_update_cmd_callback;
158 return (view->search != NULL);
161 /* --------------------------------------------------------------------------------------------- */
163 gboolean
164 mcview_dialog_goto (mcview_t * view, off_t * offset)
166 typedef enum
168 MC_VIEW_GOTO_LINENUM = 0,
169 MC_VIEW_GOTO_PERCENT = 1,
170 MC_VIEW_GOTO_OFFSET_DEC = 2,
171 MC_VIEW_GOTO_OFFSET_HEX = 3
172 } mcview_goto_type_t;
174 const char *mc_view_goto_str[] = {
175 N_("&Line number (decimal)"),
176 N_("Pe&rcents"),
177 N_("&Decimal offset"),
178 N_("He&xadecimal offset")
181 static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
183 size_t num_of_types;
184 char *exp = NULL;
185 int qd_result;
186 gboolean res;
188 num_of_types = G_N_ELEMENTS (mc_view_goto_str);
190 #ifdef ENABLE_NLS
192 size_t i;
194 for (i = 0; i < num_of_types; i++)
195 mc_view_goto_str[i] = _(mc_view_goto_str[i]);
197 #endif
200 quick_widget_t quick_widgets[] = {
201 /* *INDENT-OFF* */
202 QUICK_INPUT (INPUT_LAST_TEXT, 0, MC_HISTORY_VIEW_GOTO, &exp, NULL),
203 QUICK_RADIO (num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type,
204 NULL),
205 QUICK_START_BUTTONS (TRUE, TRUE),
206 QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL),
207 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
208 QUICK_END
209 /* *INDENT-ON* */
212 quick_dialog_t qdlg = {
213 -1, -1, 40,
214 N_("Goto"), "[Input Line Keys]",
215 quick_widgets, NULL, NULL
218 /* run dialog */
219 qd_result = quick_dialog (&qdlg);
222 *offset = -1;
224 /* check input line value */
225 res = (qd_result != B_CANCEL && exp != NULL && exp[0] != '\0');
226 if (res)
228 int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
229 off_t addr;
230 char *error;
232 addr = strtoll (exp, &error, base);
233 if ((*error == '\0') && (addr >= 0))
235 switch (current_goto_type)
237 case MC_VIEW_GOTO_LINENUM:
238 mcview_coord_to_offset (view, offset, addr, 0);
239 *offset = mcview_bol (view, *offset, 0);
240 break;
241 case MC_VIEW_GOTO_PERCENT:
242 if (addr > 100)
243 addr = 100;
244 *offset = addr * mcview_get_filesize (view) / 100;
245 if (!view->hex_mode)
246 *offset = mcview_bol (view, *offset, 0);
247 break;
248 case MC_VIEW_GOTO_OFFSET_DEC:
249 case MC_VIEW_GOTO_OFFSET_HEX:
250 *offset = addr;
251 if (!view->hex_mode)
252 *offset = mcview_bol (view, *offset, 0);
253 else
255 addr = mcview_get_filesize (view);
256 if (*offset > addr)
257 *offset = addr;
259 break;
260 default:
261 *offset = 0;
262 break;
267 g_free (exp);
268 return res;