Fix of mouse and ca capabilities check.
[midnight-commander.git] / src / viewer / dialogs.c
blob390dcee2b210071f224aba27ca064e0d67bfb484
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"
46 #include "lib/widget.h"
47 #include "lib/charsets.h"
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 (mcview_t * view)
79 int SEARCH_DLG_MIN_HEIGHT = 12;
80 int SEARCH_DLG_HEIGHT_SUPPLY = 3;
81 int SEARCH_DLG_WIDTH = 58;
83 char *exp = NULL;
84 int qd_result;
86 size_t num_of_types;
87 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
88 int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY;
90 QuickWidget quick_widgets[] = {
91 QUICK_BUTTON (6, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL,
92 NULL),
93 QUICK_BUTTON (2, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
94 #ifdef HAVE_CHARSET
95 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT,
96 N_("All charsets"), &mcview_search_options.all_codepages),
97 #endif
98 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT,
99 N_("&Whole words"), &mcview_search_options.whole_words),
100 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT,
101 N_("&Backwards"), &mcview_search_options.backwards),
102 QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
103 N_("Case &sensitive"), &mcview_search_options.case_sens),
104 QUICK_RADIO (3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT,
105 num_of_types, (const char **) list_of_types,
106 (int *) &mcview_search_options.type),
107 QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT,
108 INPUT_LAST_TEXT, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, &exp),
109 QUICK_LABEL (3, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_("Enter search string:")),
110 QUICK_END
113 QuickDialog Quick_input = {
114 SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1,
115 N_("Search"), "[Input Line Keys]",
116 quick_widgets, NULL, FALSE
119 qd_result = quick_dialog (&Quick_input);
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 = str_convert_to_input (exp);
132 if (tmp)
134 g_free (exp);
135 exp = g_string_free (tmp, FALSE);
138 #endif
140 g_free (view->last_search_string);
141 view->last_search_string = exp;
142 mcview_nroff_seq_free (&view->search_nroff_seq);
143 mc_search_free (view->search);
145 view->search = mc_search_new (view->last_search_string, -1);
146 view->search_nroff_seq = mcview_nroff_seq_new (view);
147 if (view->search != NULL)
149 view->search->search_type = mcview_search_options.type;
150 view->search->is_all_charsets = mcview_search_options.all_codepages;
151 view->search->is_case_sensitive = mcview_search_options.case_sens;
152 view->search->whole_words = mcview_search_options.whole_words;
153 view->search->search_fn = mcview_search_cmd_callback;
154 view->search->update_fn = mcview_search_update_cmd_callback;
157 return (view->search != NULL);
160 /* --------------------------------------------------------------------------------------------- */
162 gboolean
163 mcview_dialog_goto (mcview_t * view, off_t * offset)
165 typedef enum
167 MC_VIEW_GOTO_LINENUM = 0,
168 MC_VIEW_GOTO_PERCENT = 1,
169 MC_VIEW_GOTO_OFFSET_DEC = 2,
170 MC_VIEW_GOTO_OFFSET_HEX = 3
171 } mcview_goto_type_t;
173 const char *mc_view_goto_str[] = {
174 N_("&Line number (decimal)"),
175 N_("Pe&rcents"),
176 N_("&Decimal offset"),
177 N_("He&xadecimal offset")
180 const int goto_dlg_height = 12;
181 int goto_dlg_width = 40;
183 static mcview_goto_type_t current_goto_type = MC_VIEW_GOTO_LINENUM;
185 size_t i;
187 size_t num_of_types = sizeof (mc_view_goto_str) / sizeof (mc_view_goto_str[0]);
188 char *exp = NULL;
189 int qd_result;
190 gboolean res = FALSE;
192 QuickWidget quick_widgets[] = {
193 QUICK_BUTTON (6, 10, goto_dlg_height - 3, goto_dlg_height, N_("&Cancel"), B_CANCEL, NULL),
194 QUICK_BUTTON (2, 10, goto_dlg_height - 3, goto_dlg_height, N_("&OK"), B_ENTER, NULL),
195 QUICK_RADIO (3, goto_dlg_width, 4, goto_dlg_height,
196 num_of_types, (const char **) mc_view_goto_str, (int *) &current_goto_type),
197 QUICK_INPUT (3, goto_dlg_width, 2, goto_dlg_height,
198 INPUT_LAST_TEXT, goto_dlg_width - 6, 0, MC_HISTORY_VIEW_GOTO, &exp),
199 QUICK_END
202 QuickDialog Quick_input = {
203 goto_dlg_width, goto_dlg_height, -1, -1,
204 N_("Goto"), "[Input Line Keys]",
205 quick_widgets, NULL, FALSE
208 #ifdef ENABLE_NLS
209 for (i = 0; i < num_of_types; i++)
210 mc_view_goto_str[i] = _(mc_view_goto_str[i]);
212 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
213 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
214 #endif
216 /* calculate widget coordinates */
218 int b0_len, b1_len, len;
219 const int button_gap = 2;
221 /* preliminary dialog width */
222 goto_dlg_width = max (goto_dlg_width, str_term_width1 (Quick_input.title) + 4);
224 /* length of radiobuttons */
225 for (i = 0; i < num_of_types; i++)
226 goto_dlg_width = max (goto_dlg_width, str_term_width1 (mc_view_goto_str[i]) + 10);
228 /* length of buttons */
229 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
230 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
231 len = b0_len + b1_len + button_gap * 2;
233 /* dialog width */
234 Quick_input.xlen = max (goto_dlg_width, len + 6);
236 /* correct widget coordinates */
237 for (i = sizeof (quick_widgets) / sizeof (quick_widgets[0]); i > 0; i--)
238 quick_widgets[i - 1].x_divisions = Quick_input.xlen;
240 /* input length */
241 quick_widgets[3].u.input.len = Quick_input.xlen - 6;
243 /* button positions */
244 quick_widgets[1].relative_x = Quick_input.xlen / 2 - len / 2;
245 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + button_gap;
248 /* run dialog */
249 qd_result = quick_dialog (&Quick_input);
251 *offset = -1;
253 /* check input line value */
254 if ((qd_result != B_CANCEL) && (exp != NULL) && (exp[0] != '\0'))
256 int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;
257 off_t addr;
258 char *error;
260 res = TRUE;
262 addr = strtoll (exp, &error, base);
263 if ((*error == '\0') && (addr >= 0))
265 switch (current_goto_type)
267 case MC_VIEW_GOTO_LINENUM:
268 mcview_coord_to_offset (view, offset, addr, 0);
269 *offset = mcview_bol (view, *offset, 0);
270 break;
271 case MC_VIEW_GOTO_PERCENT:
272 if (addr > 100)
273 addr = 100;
274 *offset = addr * mcview_get_filesize (view) / 100;
275 if (!view->hex_mode)
276 *offset = mcview_bol (view, *offset, 0);
277 break;
278 case MC_VIEW_GOTO_OFFSET_DEC:
279 case MC_VIEW_GOTO_OFFSET_HEX:
280 *offset = addr;
281 if (!view->hex_mode)
282 *offset = mcview_bol (view, *offset, 0);
283 else
285 addr = mcview_get_filesize (view);
286 if (*offset > addr)
287 *offset = addr;
289 break;
290 default:
291 *offset = 0;
292 break;
297 g_free (exp);
298 return res;