Added two macro examples:
[midnight-commander/osp/borarpet2.git] / src / filemanager / option.c
blob1f5860f9669eb9b07ad9f72199712ee534c97f5c
1 /* Configure box module for the Midnight Commander
2 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2009, 2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /** \file option.c
21 * \brief Source: configure boxes module
24 #include <config.h>
26 #include <stdlib.h> /* atoi() */
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include "lib/global.h"
35 #include "lib/mcconfig.h"
36 #include "lib/strutil.h" /* str_term_width1() */
37 #include "lib/tty/key.h" /* old_esc_mode_timeout */
38 #include "lib/widget.h"
40 #include "src/setup.h" /* variables */
41 #include "src/history.h" /* MC_HISTORY_ESC_TIMEOUT */
42 #include "src/execute.h" /* pause_after_run */
44 #include "layout.h" /* nice_rotating_dash */
45 #include "panel.h" /* update_panels() */
47 #include "option.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 static cb_ret_t
61 configure_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
63 switch (msg)
65 case DLG_ACTION:
66 if (sender->id == 18)
68 /* message from "Single press" checkbutton */
69 const gboolean not_single = !(((WCheck *) sender)->state & C_BOOL);
70 Widget *w;
72 /* label */
73 w = dlg_find_by_id (h, sender->id - 1);
74 widget_disable (*w, not_single);
75 send_message (w, WIDGET_DRAW, 0);
76 /* input */
77 w = dlg_find_by_id (h, sender->id - 2);
78 widget_disable (*w, not_single);
79 send_message (w, WIDGET_DRAW, 0);
81 return MSG_HANDLED;
83 return MSG_NOT_HANDLED;
85 default:
86 return default_dlg_callback (h, sender, msg, parm, data);
90 /* --------------------------------------------------------------------------------------------- */
91 /*** public functions ****************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
94 void
95 configure_box (void)
97 int dlg_width = 60;
98 int dlg_height = 20;
100 char time_out[BUF_TINY] = "";
101 char *time_out_new;
103 const char *pause_options[] = {
104 N_("&Never"),
105 N_("On dum&b terminals"),
106 N_("Alwa&ys")
109 int pause_options_num = sizeof (pause_options) / sizeof (pause_options[0]);
111 QuickWidget quick_widgets[] = {
112 /* buttons */
113 QUICK_BUTTON (38, dlg_width, dlg_height - 3, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
114 QUICK_BUTTON (26, dlg_width, dlg_height - 3, dlg_height, N_("&Save"), B_EXIT, NULL),
115 QUICK_BUTTON (14, dlg_width, dlg_height - 3, dlg_height, N_("&OK"), B_ENTER, NULL),
116 /* other options */
117 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 12, dlg_height, N_("A&uto save setup"),
118 &auto_save_setup),
119 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 11, dlg_height, N_("Sa&fe delete"),
120 &safe_delete),
121 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 10, dlg_height, N_("Cd follows lin&ks"),
122 &cd_symlinks),
123 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 9, dlg_height, N_("Rotating d&ash"),
124 &nice_rotating_dash),
125 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 8, dlg_height, N_("Co&mplete: show all"),
126 &show_all_if_ambiguous),
127 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 7, dlg_height, N_("Shell &patterns"),
128 &easy_patterns),
129 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 6, dlg_height, N_("&Drop down menus"),
130 &drop_menus),
131 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 5, dlg_height, N_("Auto m&enus"), &auto_menu),
132 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 4, dlg_height, N_("Use internal vie&w"),
133 &use_internal_view),
134 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 3, dlg_height, N_("Use internal edi&t"),
135 &use_internal_edit),
136 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 15,
137 N_("Other options")),
138 /* pause options */
139 QUICK_RADIO (5, dlg_width, 13, dlg_height, pause_options_num, pause_options,
140 &pause_after_run),
141 QUICK_GROUPBOX (3, dlg_width, 12, dlg_height, dlg_width / 2 - 4, 5, N_("Pause after run")),
143 /* Esc key mode */
144 QUICK_INPUT (10, dlg_width, 10, dlg_height, (const char *) time_out, 8, 0,
145 MC_HISTORY_ESC_TIMEOUT, &time_out_new),
146 QUICK_LABEL (5, dlg_width, 10, dlg_height, N_("Timeout:")),
147 QUICK_CHECKBOX (5, dlg_width, 9, dlg_height, N_("S&ingle press"), &old_esc_mode),
148 QUICK_GROUPBOX (3, dlg_width, 8, dlg_height, dlg_width / 2 - 4, 4, N_("Esc key mode")),
150 /* file operation options */
151 QUICK_CHECKBOX (5, dlg_width, 6, dlg_height, N_("Mkdi&r autoname"), &auto_fill_mkdir_name),
152 QUICK_CHECKBOX (5, dlg_width, 5, dlg_height, N_("Classic pro&gressbar"),
153 &classic_progressbar),
154 QUICK_CHECKBOX (5, dlg_width, 4, dlg_height, N_("Compute tota&ls"),
155 &file_op_compute_totals),
156 QUICK_CHECKBOX (5, dlg_width, 3, dlg_height, N_("&Verbose operation"), &verbose),
157 QUICK_GROUPBOX (3, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 6,
158 N_("File operation options")),
159 QUICK_END
162 const size_t qw_num = sizeof (quick_widgets) / sizeof (quick_widgets[0]) - 1;
164 QuickDialog Quick_input = {
165 dlg_width, dlg_height, -1, -1,
166 N_("Configure options"), "[Configuration]",
167 quick_widgets, configure_callback, TRUE
170 int qd_result;
172 int b0_len, b1_len, b2_len;
173 int b_len, c_len, g_len, l_len;
174 size_t i;
176 #ifdef ENABLE_NLS
178 for (i = 0; i < qw_num; i++)
179 if (i < 3)
180 /* buttons */
181 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
182 else if ((i == 13) || (i == 15) || (i == 19) || (i == 24))
183 /* groupboxes */
184 quick_widgets[i].u.groupbox.title = _(quick_widgets[i].u.groupbox.title);
185 else if (i == 14)
187 /* radio button */
188 size_t j;
189 for (j = 0; j < (size_t) pause_options_num; j++)
190 pause_options[j] = _(pause_options[j]);
192 else if (i == 17)
193 /* label */
194 quick_widgets[i].u.label.text = _(quick_widgets[i].u.label.text);
195 else if (i != 16)
196 /* checkboxes */
197 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
199 Quick_input.title = _(Quick_input.title);
201 #endif /* ENABLE_NLS */
203 /* calculate widget and dialog widths */
204 /* dialog title */
205 dlg_width = max (dlg_width, str_term_width1 (Quick_input.title) + 4);
206 /* buttons */
207 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
208 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 3;
209 b2_len = str_term_width1 (quick_widgets[2].u.button.text) + 5;
210 b_len = b0_len + b1_len + b2_len + 2;
212 /* checkboxes within groupboxes */
213 c_len = 0;
214 for (i = 3; i < 24; i++)
215 if ((i < 13) || (i == 18) || (i > 19))
216 c_len = max (c_len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 3);
217 /* radiobuttons */
218 for (i = 0; i < (size_t) pause_options_num; i++)
219 c_len = max (c_len, str_term_width1 (pause_options[i]) + 3);
220 /* label + input */
221 l_len = str_term_width1 (quick_widgets[17].u.label.text);
222 c_len = max (c_len, l_len + 1 + 8);
223 /* groupboxes */
224 g_len = max (c_len + 2, str_term_width1 (quick_widgets[24].u.groupbox.title) + 4);
225 g_len = max (g_len, str_term_width1 (quick_widgets[19].u.groupbox.title) + 4);
226 g_len = max (g_len, str_term_width1 (quick_widgets[15].u.groupbox.title) + 4);
227 g_len = max (g_len, str_term_width1 (quick_widgets[13].u.groupbox.title) + 4);
228 /* dialog width */
229 Quick_input.xlen = max (dlg_width, g_len * 2 + 9);
230 Quick_input.xlen = max (Quick_input.xlen, b_len + 2);
231 if ((Quick_input.xlen & 1) != 0)
232 Quick_input.xlen++;
234 /* fix widget parameters */
235 for (i = 0; i < qw_num; i++)
236 quick_widgets[i].x_divisions = Quick_input.xlen;
238 /* groupboxes */
239 quick_widgets[15].u.groupbox.width =
240 quick_widgets[19].u.groupbox.width =
241 quick_widgets[24].u.groupbox.width = Quick_input.xlen / 2 - 4;
242 quick_widgets[13].u.groupbox.width = Quick_input.xlen / 2 - 3;
244 /* input */
245 quick_widgets[16].relative_x = quick_widgets[17].relative_x + l_len + 1;
246 quick_widgets[16].u.input.len = quick_widgets[19].u.groupbox.width - l_len - 4;
248 /* right column */
249 quick_widgets[13].relative_x = Quick_input.xlen / 2;
250 for (i = 3; i < 13; i++)
251 quick_widgets[i].relative_x = quick_widgets[13].relative_x + 2;
253 /* buttons */
254 quick_widgets[2].relative_x = (Quick_input.xlen - b_len) / 2;
255 quick_widgets[1].relative_x = quick_widgets[2].relative_x + b2_len + 1;
256 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + 1;
258 g_snprintf (time_out, sizeof (time_out), "%d", old_esc_mode_timeout);
260 if (!old_esc_mode)
261 quick_widgets[16].options = quick_widgets[17].options = W_DISABLED;
263 qd_result = quick_dialog (&Quick_input);
265 if ((qd_result == B_ENTER) || (qd_result == B_EXIT))
266 old_esc_mode_timeout = atoi (time_out_new);
268 g_free (time_out_new);
270 /* Save button */
271 if (qd_result == B_EXIT)
273 save_config ();
274 mc_config_save_file (mc_main_config, NULL);
278 /* --------------------------------------------------------------------------------------------- */
280 void
281 panel_options_box (void)
283 int dlg_width = 60;
284 int dlg_height = 19;
286 int simple_swap = mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION,
287 "simple_swap", FALSE) ? 1 : 0;
289 const char *qsearch_options[] = {
290 N_("Case &insensitive"),
291 N_("Case s&ensitive"),
292 N_("Use panel sort mo&de")
295 QuickWidget quick_widgets[] = {
296 /* buttons */
297 QUICK_BUTTON (38, dlg_width, dlg_height - 3, dlg_height, N_("&Cancel"), B_CANCEL, NULL),
298 QUICK_BUTTON (26, dlg_width, dlg_height - 3, dlg_height, N_("&Save"), B_EXIT, NULL),
299 QUICK_BUTTON (14, dlg_width, dlg_height - 3, dlg_height, N_("&OK"), B_ENTER, NULL),
300 /* quick search */
301 QUICK_RADIO (dlg_width / 2 + 2, dlg_width, 12, dlg_height, QSEARCH_NUM, qsearch_options,
302 (int *) &panels_options.qsearch_mode),
303 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 11, dlg_height, dlg_width / 2 - 4,
304 QSEARCH_NUM + 2,
305 N_("Quick search")),
306 /* file highlighting */
307 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 9, dlg_height, N_("&Permissions"),
308 &panels_options.permission_mode),
309 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 8, dlg_height, N_("File &types"),
310 &panels_options.filetype_mode),
311 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 7, dlg_height, dlg_width / 2 - 4, 4,
312 N_("File highlight")),
313 /* navigation */
314 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 5, dlg_height, N_("&Mouse page scrolling"),
315 &panels_options.mouse_move_pages),
316 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 4, dlg_height, N_("Pa&ge scrolling"),
317 &panels_options.scroll_pages),
318 QUICK_CHECKBOX (dlg_width / 2 + 2, dlg_width, 3, dlg_height, N_("L&ynx-like motion"),
319 &panels_options.navigate_with_arrows),
320 QUICK_GROUPBOX (dlg_width / 2, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 5,
321 N_("Navigation")),
322 /* main panel options */
323 QUICK_CHECKBOX (5, dlg_width, 12, dlg_height, N_("A&uto save panels setup"),
324 &panels_options.auto_save_setup),
325 QUICK_CHECKBOX (5, dlg_width, 11, dlg_height, N_("Simple s&wap"),
326 &simple_swap),
327 QUICK_CHECKBOX (5, dlg_width, 10, dlg_height, N_("Re&verse files only"),
328 &panels_options.reverse_files_only),
329 QUICK_CHECKBOX (5, dlg_width, 9, dlg_height, N_("Ma&rk moves down"),
330 &panels_options.mark_moves_down),
331 QUICK_CHECKBOX (5, dlg_width, 8, dlg_height, N_("&Fast dir reload"),
332 &panels_options.fast_reload),
333 QUICK_CHECKBOX (5, dlg_width, 7, dlg_height, N_("Show &hidden files"),
334 &panels_options.show_dot_files),
335 QUICK_CHECKBOX (5, dlg_width, 6, dlg_height, N_("Show &backup files"),
336 &panels_options.show_backups),
337 QUICK_CHECKBOX (5, dlg_width, 5, dlg_height, N_("Mi&x all files"),
338 &panels_options.mix_all_files),
339 QUICK_CHECKBOX (5, dlg_width, 4, dlg_height, N_("Use SI si&ze units"),
340 &panels_options.kilobyte_si),
341 QUICK_CHECKBOX (5, dlg_width, 3, dlg_height, N_("Show mi&ni-status"),
342 &panels_options.show_mini_info),
343 QUICK_GROUPBOX (3, dlg_width, 2, dlg_height, dlg_width / 2 - 4, 14,
344 N_("Main options")),
345 QUICK_END
348 const size_t qw_num = G_N_ELEMENTS (quick_widgets) - 1;
350 QuickDialog Quick_input = {
351 dlg_width, dlg_height, -1, -1,
352 N_("Panel options"), "[Panel options]",
353 quick_widgets, NULL, TRUE
356 int qd_result;
358 int b0_len, b1_len, b2_len;
359 int b_len, c_len, g_len;
360 size_t i;
362 #ifdef ENABLE_NLS
364 for (i = 0; i < qw_num; i++)
365 if (i < 3)
366 /* buttons */
367 quick_widgets[i].u.button.text = _(quick_widgets[i].u.button.text);
368 else if (i == 3)
370 /* radio button */
371 size_t j;
372 for (j = 0; j < QSEARCH_NUM; j++)
373 qsearch_options[j] = _(qsearch_options[j]);
375 else if ((i == 4) || (i == 7) || (i == 11) || (i == 22))
376 /* groupboxes */
377 quick_widgets[i].u.groupbox.title = _(quick_widgets[i].u.groupbox.title);
378 else
379 /* checkboxes */
380 quick_widgets[i].u.checkbox.text = _(quick_widgets[i].u.checkbox.text);
382 Quick_input.title = _(Quick_input.title);
384 #endif /* ENABLE_NLS */
386 /* calculate widget and dialog widths */
387 /* dialog title */
388 dlg_width = max (dlg_width, str_term_width1 (Quick_input.title) + 4);
389 /* buttons */
390 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
391 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 3;
392 b2_len = str_term_width1 (quick_widgets[2].u.button.text) + 5;
393 b_len = b0_len + b1_len + b2_len + 2;
395 /* checkboxes within groupboxes */
396 c_len = 0;
397 for (i = 5; i < 22; i++)
398 if ((i != 7) && (i != 11))
399 c_len = max (c_len, str_term_width1 (quick_widgets[i].u.checkbox.text) + 4);
401 /* radiobuttons */
402 for (i = 0; i < QSEARCH_NUM; i++)
403 c_len = max (c_len, str_term_width1 (qsearch_options[i]) + 3);
404 /* groupboxes */
405 g_len = max (c_len + 2, str_term_width1 (quick_widgets[4].u.groupbox.title) + 4);
406 g_len = max (g_len, str_term_width1 (quick_widgets[7].u.groupbox.title) + 4);
407 g_len = max (g_len, str_term_width1 (quick_widgets[11].u.groupbox.title) + 4);
408 g_len = max (g_len, str_term_width1 (quick_widgets[22].u.groupbox.title) + 4);
409 /* dialog width */
410 Quick_input.xlen = max (dlg_width, g_len * 2 + 9);
411 Quick_input.xlen = max (Quick_input.xlen, b_len + 2);
412 if ((Quick_input.xlen & 1) != 0)
413 Quick_input.xlen++;
415 /* fix widget parameters */
416 for (i = 0; i < qw_num; i++)
417 quick_widgets[i].x_divisions = Quick_input.xlen;
419 /* groupboxes */
420 quick_widgets[4].u.groupbox.width =
421 quick_widgets[7].u.groupbox.width =
422 quick_widgets[11].u.groupbox.width = Quick_input.xlen / 2 - 3;
423 quick_widgets[22].u.groupbox.width = Quick_input.xlen / 2 - 4;
425 /* right column */
426 quick_widgets[4].relative_x =
427 quick_widgets[7].relative_x = quick_widgets[11].relative_x = Quick_input.xlen / 2;
428 for (i = 3; i < 11; i++)
429 if ((i != 4) && (i != 7))
430 quick_widgets[i].relative_x = quick_widgets[4].relative_x + 2;
432 /* buttons */
433 quick_widgets[2].relative_x = (Quick_input.xlen - b_len) / 2;
434 quick_widgets[1].relative_x = quick_widgets[2].relative_x + b2_len + 1;
435 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + 1;
437 qd_result = quick_dialog (&Quick_input);
439 if ((qd_result == B_ENTER) || (qd_result == B_EXIT))
441 mc_config_set_bool (mc_main_config, CONFIG_PANELS_SECTION,
442 "simple_swap", (gboolean) (simple_swap & C_BOOL));
444 if (!panels_options.fast_reload_msg_shown && panels_options.fast_reload)
446 message (D_NORMAL, _("Information"),
447 _("Using the fast reload option may not reflect the exact\n"
448 "directory contents. In this case you'll need to do a\n"
449 "manual reload of the directory. See the man page for\n" "the details."));
450 panels_options.fast_reload_msg_shown = TRUE;
452 update_panels (UP_RELOAD, UP_KEEPSEL);
455 if (qd_result == B_EXIT)
457 /* save panel options */
458 panels_save_options ();
459 mc_config_save_file (mc_main_config, NULL);
463 /* --------------------------------------------------------------------------------------------- */