Merge branch '1921_lslR_fix'
[midnight-commander.git] / src / viewer / dialogs.c
blobf0c25c62bc60721f0f0cb7ebdf680a52cddfee3e
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 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
10 1995 Jakub Jelinek
11 1996 Joseph M. Hinkle
12 1997 Norbert Warmuth
13 1998 Pavel Machek
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009 Ilia Maslakov <il.smind@gmail.com>
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 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 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, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 MA 02110-1301, USA.
38 #include <config.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
43 #include "lib/global.h"
44 #include "lib/search.h"
45 #include "lib/strutil.h"
47 #include "src/wtools.h"
48 #include "src/history.h"
49 #include "src/charsets.h"
51 #include "internal.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 /*** file scope type declarations ****************************************************************/
59 /*** file scope variables ************************************************************************/
61 /*** file scope functions ************************************************************************/
63 /*** public functions ****************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
67 gboolean
68 mcview_dialog_search (mcview_t * view)
70 int SEARCH_DLG_MIN_HEIGHT = 12;
71 int SEARCH_DLG_HEIGHT_SUPPLY = 3;
72 int SEARCH_DLG_WIDTH = 58;
74 char *exp = NULL;
75 int qd_result;
77 size_t num_of_types;
78 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
79 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
81 QuickWidget quick_widgets[] =
83 QUICK_BUTTON (6, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
84 QUICK_BUTTON (2, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
85 #ifdef HAVE_CHARSET
86 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT,
87 N_("All charsets"), &view->search_all_codepages),
88 #endif
89 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT,
90 N_("&Whole words"), &view->whole_words),
91 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT,
92 N_("&Backwards"), &view->search_backwards),
93 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
94 N_("case &Sensitive"), &view->search_case),
95 QUICK_RADIO (3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
96 num_of_types, (const char **) list_of_types, (int *) &view->search_type),
97 QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
98 INPUT_LAST_TEXT, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, &exp),
99 QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")),
100 QUICK_END
103 QuickDialog Quick_input =
105 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1,
106 N_("Search"), "[Input Line Keys]",
107 quick_widgets, FALSE
110 qd_result = quick_dialog (&Quick_input);
111 g_strfreev (list_of_types);
113 if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0')) {
114 g_free (exp);
115 return FALSE;
118 #ifdef HAVE_CHARSET
120 GString *tmp = str_convert_to_input (exp);
122 if (tmp) {
123 g_free (exp);
124 exp = g_string_free (tmp, FALSE);
127 #endif
129 g_free (view->last_search_string);
130 view->last_search_string = exp;
132 if (view->search_nroff_seq != NULL)
133 mcview_nroff_seq_free (&(view->search_nroff_seq));
135 if (view->search != NULL)
136 mc_search_free (view->search);
138 view->search = mc_search_new (view->last_search_string, -1);
139 view->search_nroff_seq = mcview_nroff_seq_new (view);
140 if (view->search != NULL) {
141 view->search->search_type = view->search_type;
142 view->search->is_all_charsets = view->search_all_codepages;
143 view->search->is_case_sentitive = view->search_case;
144 view->search->search_fn = mcview_search_cmd_callback;
145 view->search->update_fn = mcview_search_update_cmd_callback;
146 view->search->whole_words = view->whole_words;
149 return (view->search != NULL);
152 /* --------------------------------------------------------------------------------------------- */
154 gboolean
155 mcview_dialog_goto (mcview_t *view, off_t *offset)
157 typedef enum {
158 MC_VIEW_GOTO_LINENUM = 0,
159 MC_VIEW_GOTO_PERCENT = 1,
160 MC_VIEW_GOTO_OFFSET_DEC = 2,
161 MC_VIEW_GOTO_OFFSET_HEX = 3
162 } mcview_goto_type_t;
164 const char *mc_view_goto_str[] =
166 N_("&Line number (decimal)"),
167 N_("Pe&rcents"),
168 N_("&Decimal offset"),
169 N_("He&xadecimal offset")
172 const int goto_dlg_height = 12;
173 int goto_dlg_width = 40;
175 static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
177 size_t i;
179 size_t num_of_types = sizeof (mc_view_goto_str) /sizeof (mc_view_goto_str[0]);
180 char *exp = NULL;
181 int qd_result;
182 gboolean res = FALSE;
184 QuickWidget quick_widgets[] =
186 QUICK_BUTTON (6, 10, goto_dlg_height - 3, goto_dlg_height, N_("&Cancel"), B_CANCEL, NULL),
187 QUICK_BUTTON (2, 10, goto_dlg_height - 3, goto_dlg_height, N_("&OK"), B_ENTER, NULL),
188 QUICK_RADIO (3, goto_dlg_width, 4, goto_dlg_height,
189 num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type),
190 QUICK_INPUT (3, goto_dlg_width, 2, goto_dlg_height,
191 INPUT_LAST_TEXT, goto_dlg_width - 6, 0, MC_HISTORY_VIEW_GOTO, &exp),
192 QUICK_END
195 QuickDialog Quick_input =
197 goto_dlg_width, goto_dlg_height, -1, -1,
198 N_("Goto"), "[Input Line Keys]",
199 quick_widgets, FALSE
202 #ifdef ENABLE_NLS
203 for (i = 0; i < num_of_types; i++)
204 mc_view_goto_str [i] = _(mc_view_goto_str [i]);
206 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
207 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
208 #endif
210 /* calculate widget coordinates */
212 int b0_len, b1_len, len;
213 const int button_gap = 2;
215 /* preliminary dialog width */
216 goto_dlg_width = max (goto_dlg_width, str_term_width1 (Quick_input.title) + 4);
218 /* length of radiobuttons */
219 for (i = 0; i < num_of_types; i++)
220 goto_dlg_width = max (goto_dlg_width, str_term_width1 (mc_view_goto_str [i]) + 10);
222 /* length of buttons */
223 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
224 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
225 len = b0_len + b1_len + button_gap * 2;
227 /* dialog width */
228 Quick_input.xlen = max (goto_dlg_width, len + 6);
230 /* correct widget coordinates */
231 for (i = sizeof (quick_widgets)/sizeof (quick_widgets[0]); i > 0; i--)
232 quick_widgets[i - 1].x_divisions = Quick_input.xlen;
234 /* input length */
235 quick_widgets[3].u.input.len = Quick_input.xlen - 6;
237 /* button positions */
238 quick_widgets[1].relative_x = Quick_input.xlen/2 - len/2;
239 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + button_gap;
242 /* run dialog*/
243 qd_result = quick_dialog (&Quick_input);
245 *offset = -1;
247 /* check input line value */
248 if ((qd_result != B_CANCEL) && (exp != NULL) && (exp[0] != '\0')) {
249 int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
250 off_t addr;
251 char *error;
253 res = TRUE;
255 addr = strtoll (exp, &error, base);
256 if ((*error == '\0') && (addr >= 0)) {
257 switch (current_goto_type) {
258 case MC_VIEW_GOTO_LINENUM:
259 mcview_coord_to_offset (view, offset, addr, 0);
260 break;
261 case MC_VIEW_GOTO_PERCENT:
262 if (addr > 100)
263 addr = 100;
264 *offset = addr * mcview_get_filesize (view) / 100;
265 break;
266 case MC_VIEW_GOTO_OFFSET_DEC:
267 *offset = addr;
268 break;
269 case MC_VIEW_GOTO_OFFSET_HEX:
270 *offset = addr;
271 break;
272 default:
273 break;
275 *offset = mcview_bol (view, *offset);
279 g_free (exp);
280 return res;