Aggressive use WIDGET macro.
[midnight-commander.git] / src / filemanager / option.c
blob1aaf8ba251a9e77cca8d1eb4ea8bc49d2bfd9c52
1 /*
2 Configure box module for the Midnight Commander
4 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file option.c
25 * \brief Source: configure boxes module
28 #include <config.h>
30 #include <stdlib.h> /* atoi() */
32 #include <sys/types.h>
33 #include <unistd.h>
35 #include "lib/global.h"
36 #include "lib/mcconfig.h"
37 #include "lib/strutil.h" /* str_term_width1() */
38 #include "lib/tty/key.h" /* old_esc_mode_timeout */
39 #include "lib/widget.h"
41 #include "src/setup.h" /* variables */
42 #include "src/history.h" /* MC_HISTORY_ESC_TIMEOUT */
43 #include "src/execute.h" /* pause_after_run */
45 #include "layout.h" /* nice_rotating_dash */
46 #include "panel.h" /* update_panels() */
48 #include "option.h"
50 /*** global variables ****************************************************************************/
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
62 #undef max
64 static int
65 max (int a, int b)
67 return a > b ? a : b;
70 /* --------------------------------------------------------------------------------------------- */
72 static cb_ret_t
73 configure_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
75 switch (msg)
77 case DLG_ACTION:
78 /* message from "Single press" checkbutton */
79 if (sender != NULL && sender->id == 17)
81 const gboolean not_single = !(((WCheck *) sender)->state & C_BOOL);
82 Widget *w;
84 /* label */
85 w = dlg_find_by_id (h, sender->id - 1);
86 widget_disable (w, not_single);
87 send_message (w, WIDGET_DRAW, 0);
88 /* input */
89 w = dlg_find_by_id (h, sender->id - 2);
90 widget_disable (w, not_single);
91 send_message (w, WIDGET_DRAW, 0);
93 return MSG_HANDLED;
95 return MSG_NOT_HANDLED;
97 default:
98 return default_dlg_callback (h, sender, msg, parm, data);
102 /* --------------------------------------------------------------------------------------------- */
103 /*** public functions ****************************************************************************/
104 /* --------------------------------------------------------------------------------------------- */
106 void
107 configure_box (void)
109 int dlg_width = 60;
110 int dlg_height = 21;
112 char time_out[BUF_TINY] = "";
113 char *time_out_new;
115 const char *pause_options[] = {
116 N_("&Never"),
117 N_("On dum&b terminals"),
118 N_("Alwa&ys")
121 int pause_options_num = G_N_ELEMENTS (pause_options);
123 QuickWidget quick_widgets[] = {
124 /* buttons */
125 QUICK_BUTTON (38, dlg_width, dlg_height - 3, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
126 QUICK_BUTTON (14, dlg_width, dlg_height - 3, dlg_height, N_("&OK"), B_ENTER, NULL),
127 /* other options */
128 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 12, dlg_height, N_("A&uto save setup"),
129 &auto_save_setup),
130 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 11, dlg_height, N_("Sa&fe delete"),
131 &safe_delete),
132 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 10, dlg_height, N_("Cd follows lin&ks"),
133 &mc_global.vfs.cd_symlinks),
134 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 9, dlg_height, N_("Rotating d&ash"),
135 &nice_rotating_dash),
136 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 8, dlg_height, N_("Co&mplete: show all"),
137 &mc_global.widget.show_all_if_ambiguous),
138 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 7, dlg_height, N_("Shell &patterns"),
139 &easy_patterns),
140 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 6, dlg_height, N_("&Drop down menus"),
141 &drop_menus),
142 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 5, dlg_height, N_("Auto m&enus"), &auto_menu),
143 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 4, dlg_height, N_("Use internal vie&w"),
144 &use_internal_view),
145 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 3, dlg_height, N_("Use internal edi&t"),
146 &use_internal_edit),
147 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 16,
148 N_("Other options")),
149 /* pause options */
150 QUICK_RADIO (5, dlg_width, 14, dlg_height, pause_options_num, pause_options,
151 &pause_after_run),
152 QUICK_GROUPBOX (3, dlg_width, 13, dlg_height, dlg_width / 2 - 4, 5, N_("Pause after run")),
154 /* Esc key mode */
155 QUICK_INPUT (10, dlg_width, 11, dlg_height, (const char *) time_out, 8, 0,
156 MC_HISTORY_ESC_TIMEOUT, &time_out_new),
157 QUICK_LABEL (5, dlg_width, 11, dlg_height, N_("Timeout:")),
158 QUICK_CHECKBOX (5, dlg_width, 10, dlg_height, N_("S&ingle press"), &old_esc_mode),
159 QUICK_GROUPBOX (3, dlg_width, 9, dlg_height, dlg_width / 2 - 4, 4, N_("Esc key mode")),
161 /* file operation options */
162 QUICK_CHECKBOX (5, dlg_width, 7, dlg_height, N_("Preallocate &space"),
163 &mc_global.vfs.preallocate_space),
164 QUICK_CHECKBOX (5, dlg_width, 6, dlg_height, N_("Mkdi&r autoname"), &auto_fill_mkdir_name),
165 QUICK_CHECKBOX (5, dlg_width, 5, dlg_height, N_("Classic pro&gressbar"),
166 &classic_progressbar),
167 QUICK_CHECKBOX (5, dlg_width, 4, dlg_height, N_("Compute tota&ls"),
168 &file_op_compute_totals),
169 QUICK_CHECKBOX (5, dlg_width, 3, dlg_height, N_("&Verbose operation"), &verbose),
170 QUICK_GROUPBOX (3, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 7,
171 N_("File operation options")),
172 QUICK_END
175 const size_t qw_num = G_N_ELEMENTS (quick_widgets) - 1;
177 QuickDialog Quick_input = {
178 dlg_width, dlg_height, -1, -1,
179 N_("Configure options"), "[Configuration]",
180 quick_widgets, configure_callback, NULL, TRUE
183 int b0_len, b1_len;
184 int b_len, c_len, g_len, l_len;
185 size_t i;
187 #ifdef ENABLE_NLS
188 for (i = 0; i < qw_num; i++)
189 switch (i)
191 case 0:
192 case 1:
193 /* buttons */
194 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
195 break;
196 case 12:
197 case 14:
198 case 18:
199 case 24:
200 /* groupboxes */
201 quick_widgets[i].u.groupbox.title = _(quick_widgets[i].u.groupbox.title);
202 break;
203 case 13:
205 /* radio button */
206 size_t j;
207 for (j = 0; j < (size_t) pause_options_num; j++)
208 pause_options[j] = _(pause_options[j]);
210 break;
211 case 15:
212 /* input line */
213 break;
214 case 16:
215 /* label */
216 quick_widgets[i].u.label.text = _(quick_widgets[i].u.label.text);
217 break;
218 default:
219 /* checkboxes */
220 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
221 break;
224 Quick_input.title = _(Quick_input.title);
225 #endif /* ENABLE_NLS */
227 /* calculate widget and dialog widths */
228 /* dialog title */
229 dlg_width = max (dlg_width, str_term_width1 (Quick_input.title) + 4);
230 /* buttons */
231 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
232 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5;
233 b_len = b0_len + b1_len + 1;
235 /* checkboxes within groupboxes */
236 c_len = 0;
237 for (i = 2; i < 24; i++)
238 if ((i < 12) || (i == 17) || (i > 18))
239 c_len = max (c_len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 3);
240 /* radiobuttons */
241 for (i = 0; i < (size_t) pause_options_num; i++)
242 c_len = max (c_len, str_term_width1 (pause_options[i]) + 3);
243 /* label + input */
244 l_len = str_term_width1 (quick_widgets[16].u.label.text);
245 c_len = max (c_len, l_len + 1 + 8);
246 /* groupboxes */
247 g_len = max (c_len + 2, str_term_width1 (quick_widgets[24].u.groupbox.title) + 4);
248 g_len = max (g_len, str_term_width1 (quick_widgets[18].u.groupbox.title) + 4);
249 g_len = max (g_len, str_term_width1 (quick_widgets[14].u.groupbox.title) + 4);
250 g_len = max (g_len, str_term_width1 (quick_widgets[12].u.groupbox.title) + 4);
251 /* dialog width */
252 Quick_input.xlen = max (dlg_width, g_len * 2 + 9);
253 Quick_input.xlen = max (Quick_input.xlen, b_len + 2);
254 if ((Quick_input.xlen & 1) != 0)
255 Quick_input.xlen++;
257 /* fix widget parameters */
258 for (i = 0; i < qw_num; i++)
259 quick_widgets[i].x_divisions = Quick_input.xlen;
261 /* groupboxes */
262 quick_widgets[14].u.groupbox.width =
263 quick_widgets[18].u.groupbox.width =
264 quick_widgets[24].u.groupbox.width = Quick_input.xlen / 2 - 4;
265 quick_widgets[12].u.groupbox.width = Quick_input.xlen / 2 - 3;
267 /* input */
268 quick_widgets[15].relative_x = quick_widgets[16].relative_x + l_len + 1;
269 quick_widgets[15].u.input.len = quick_widgets[18].u.groupbox.width - l_len - 4;
271 /* right column */
272 quick_widgets[12].relative_x = Quick_input.xlen / 2;
273 for (i = 2; i < 12; i++)
274 quick_widgets[i].relative_x = quick_widgets[12].relative_x + 2;
276 /* buttons */
277 quick_widgets[1].relative_x = (Quick_input.xlen - b_len) / 3;
278 quick_widgets[0].relative_x = 2 * quick_widgets[1].relative_x + b1_len + 1;
280 g_snprintf (time_out, sizeof (time_out), "%d", old_esc_mode_timeout);
282 if (!old_esc_mode)
283 quick_widgets[15].options = quick_widgets[16].options = W_DISABLED;
285 #ifndef HAVE_POSIX_FALLOCATE
286 mc_global.vfs.preallocate_space = FALSE;
287 quick_widgets[19].options = W_DISABLED;
288 #endif
290 if (quick_dialog (&Quick_input) == B_ENTER)
291 old_esc_mode_timeout = atoi (time_out_new);
293 g_free (time_out_new);
296 /* --------------------------------------------------------------------------------------------- */
298 void
299 panel_options_box (void)
301 int dlg_width = 60;
302 int dlg_height = 19;
304 int simple_swap = mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION,
305 "simple_swap", FALSE) ? 1 : 0;
307 const char *qsearch_options[] = {
308 N_("Case &insensitive"),
309 N_("Case s&ensitive"),
310 N_("Use panel sort mo&de")
313 QuickWidget quick_widgets[] = {
314 /* buttons */
315 QUICK_BUTTON (38, dlg_width, dlg_height - 3, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
316 QUICK_BUTTON (14, dlg_width, dlg_height - 3, dlg_height, N_("&OK"), B_ENTER, NULL),
317 /* quick search */
318 QUICK_RADIO (dlg_width / 2 + 2, dlg_width, 12, dlg_height, QSEARCH_NUM, qsearch_options,
319 (int *) &panels_options.qsearch_mode),
320 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 11, dlg_height, dlg_width / 2 - 4,
321 QSEARCH_NUM + 2,
322 N_("Quick search")),
323 /* file highlighting */
324 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 9, dlg_height, N_("&Permissions"),
325 &panels_options.permission_mode),
326 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 8, dlg_height, N_("File &types"),
327 &panels_options.filetype_mode),
328 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 7, dlg_height, dlg_width / 2 - 4, 4,
329 N_("File highlight")),
330 /* navigation */
331 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 5, dlg_height, N_("&Mouse page scrolling"),
332 &panels_options.mouse_move_pages),
333 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 4, dlg_height, N_("Pa&ge scrolling"),
334 &panels_options.scroll_pages),
335 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 3, dlg_height, N_("L&ynx-like motion"),
336 &panels_options.navigate_with_arrows),
337 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 5,
338 N_("Navigation")),
339 /* main panel options */
340 QUICK_CHECKBOX (5, dlg_width, 12, dlg_height, N_("A&uto save panels setup"),
341 &panels_options.auto_save_setup),
342 QUICK_CHECKBOX (5, dlg_width, 11, dlg_height, N_("Simple s&wap"),
343 &simple_swap),
344 QUICK_CHECKBOX (5, dlg_width, 10, dlg_height, N_("Re&verse files only"),
345 &panels_options.reverse_files_only),
346 QUICK_CHECKBOX (5, dlg_width, 9, dlg_height, N_("Ma&rk moves down"),
347 &panels_options.mark_moves_down),
348 QUICK_CHECKBOX (5, dlg_width, 8, dlg_height, N_("&Fast dir reload"),
349 &panels_options.fast_reload),
350 QUICK_CHECKBOX (5, dlg_width, 7, dlg_height, N_("Show &hidden files"),
351 &panels_options.show_dot_files),
352 QUICK_CHECKBOX (5, dlg_width, 6, dlg_height, N_("Show &backup files"),
353 &panels_options.show_backups),
354 QUICK_CHECKBOX (5, dlg_width, 5, dlg_height, N_("Mi&x all files"),
355 &panels_options.mix_all_files),
356 QUICK_CHECKBOX (5, dlg_width, 4, dlg_height, N_("Use SI si&ze units"),
357 &panels_options.kilobyte_si),
358 QUICK_CHECKBOX (5, dlg_width, 3, dlg_height, N_("Show mi&ni-status"),
359 &panels_options.show_mini_info),
360 QUICK_GROUPBOX (3, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 14,
361 N_("Main options")),
362 QUICK_END
365 const size_t qw_num = G_N_ELEMENTS (quick_widgets) - 1;
367 QuickDialog Quick_input = {
368 dlg_width, dlg_height, -1, -1,
369 N_("Panel options"), "[Panel options]",
370 quick_widgets, NULL, NULL, TRUE
373 int b0_len, b1_len;
374 int b_len, c_len, g_len;
375 size_t i;
377 #ifdef ENABLE_NLS
378 for (i = 0; i < qw_num; i++)
379 switch (i)
381 case 0:
382 case 1:
383 /* buttons */
384 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
385 break;
386 case 2:
388 /* radio button */
389 size_t j;
390 for (j = 0; j < QSEARCH_NUM; j++)
391 qsearch_options[j] = _(qsearch_options[j]);
393 break;
394 case 3:
395 case 6:
396 case 10:
397 case 21:
398 /* groupboxes */
399 quick_widgets[i].u.groupbox.title = _(quick_widgets[i].u.groupbox.title);
400 break;
401 default:
402 /* checkboxes */
403 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
404 break;
407 Quick_input.title = _(Quick_input.title);
408 #endif /* ENABLE_NLS */
410 /* calculate widget and dialog widths */
411 /* dialog title */
412 dlg_width = max (dlg_width, str_term_width1 (Quick_input.title) + 4);
413 /* buttons */
414 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
415 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5;
416 b_len = b0_len + b1_len + 1;
418 /* checkboxes within groupboxes */
419 c_len = 0;
420 for (i = 4; i < 21; i++)
421 if ((i != 6) && (i != 10))
422 c_len = max (c_len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 4);
424 /* radiobuttons */
425 for (i = 0; i < QSEARCH_NUM; i++)
426 c_len = max (c_len, str_term_width1 (qsearch_options[i]) + 3);
427 /* groupboxes */
428 g_len = max (c_len + 2, str_term_width1 (quick_widgets[3].u.groupbox.title) + 4);
429 g_len = max (g_len, str_term_width1 (quick_widgets[6].u.groupbox.title) + 4);
430 g_len = max (g_len, str_term_width1 (quick_widgets[10].u.groupbox.title) + 4);
431 g_len = max (g_len, str_term_width1 (quick_widgets[21].u.groupbox.title) + 4);
432 /* dialog width */
433 Quick_input.xlen = max (dlg_width, g_len * 2 + 9);
434 Quick_input.xlen = max (Quick_input.xlen, b_len + 2);
435 if ((Quick_input.xlen & 1) != 0)
436 Quick_input.xlen++;
438 /* fix widget parameters */
439 for (i = 0; i < qw_num; i++)
440 quick_widgets[i].x_divisions = Quick_input.xlen;
442 /* groupboxes */
443 quick_widgets[3].u.groupbox.width =
444 quick_widgets[6].u.groupbox.width =
445 quick_widgets[10].u.groupbox.width = Quick_input.xlen / 2 - 3;
446 quick_widgets[21].u.groupbox.width = Quick_input.xlen / 2 - 4;
448 /* right column */
449 quick_widgets[3].relative_x =
450 quick_widgets[6].relative_x = quick_widgets[10].relative_x = Quick_input.xlen / 2;
451 for (i = 2; i < 10; i++)
452 if ((i != 3) && (i != 6))
453 quick_widgets[i].relative_x = quick_widgets[3].relative_x + 2;
455 /* buttons */
456 quick_widgets[1].relative_x = (Quick_input.xlen - b_len) / 3;
457 quick_widgets[0].relative_x = 2 * quick_widgets[1].relative_x + b1_len + 1;
459 if (quick_dialog (&Quick_input) != B_ENTER)
460 return;
462 mc_config_set_bool (mc_main_config, CONFIG_PANELS_SECTION,
463 "simple_swap", (gboolean) (simple_swap & C_BOOL));
465 if (!panels_options.fast_reload_msg_shown && panels_options.fast_reload)
467 message (D_NORMAL, _("Information"),
468 _("Using the fast reload option may not reflect the exact\n"
469 "directory contents. In this case you'll need to do a\n"
470 "manual reload of the directory. See the man page for\n" "the details."));
471 panels_options.fast_reload_msg_shown = TRUE;
474 update_panels (UP_RELOAD, UP_KEEPSEL);
477 /* --------------------------------------------------------------------------------------------- */