Rework strutils for usage GString instread of self-made buffers.
[midnight-commander.git] / src / option.c
blobbc1bb26630f8cd5532e4fa45474a222f959a3b26
1 /* Configure box module for the Midnight Commander
2 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007 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 #include <config.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
29 #include "global.h"
30 #include "tty.h"
31 #include "win.h"
32 #include "color.h"
33 #include "dialog.h"
34 #include "widget.h"
35 #include "setup.h" /* For save_setup() */
36 #include "main.h"
37 #include "profile.h" /* For sync_profiles */
38 #include "strutil.h"
40 #include "panel.h" /* Needed for the externs */
41 #include "file.h" /* safe_delete */
42 #include "layout.h" /* For nice_rotating_dash */
43 #include "option.h"
44 static Dlg_head *conf_dlg;
46 #define TOGGLE_VARIABLE 0
48 static int first_width, second_width;
50 static struct {
51 const char *text;
52 int *variable;
53 void (*toggle_function)(void);
54 WCheck *widget;
55 } check_options [] = {
56 /* other options */
57 {N_("safe de&Lete"), &safe_delete, TOGGLE_VARIABLE, 0 },
58 {N_("cd follows lin&Ks"), &cd_symlinks, TOGGLE_VARIABLE, 0 },
59 {N_("L&ynx-like motion"), &navigate_with_arrows,TOGGLE_VARIABLE, 0 },
60 {N_("rotatin&G dash"), &nice_rotating_dash,TOGGLE_VARIABLE, 0 },
61 {N_("co&Mplete: show all"),&show_all_if_ambiguous,TOGGLE_VARIABLE, 0 },
62 {N_("&Use internal view"), &use_internal_view, TOGGLE_VARIABLE, 0 },
63 {N_("use internal ed&It"), &use_internal_edit, TOGGLE_VARIABLE, 0 },
64 {N_("auto m&Enus"), &auto_menu, TOGGLE_VARIABLE, 0 },
65 {N_("&Auto save setup"), &auto_save_setup, TOGGLE_VARIABLE, 0 },
66 {N_("shell &Patterns"), &easy_patterns, TOGGLE_VARIABLE, 0 },
67 {N_("Compute &Totals"), &file_op_compute_totals, TOGGLE_VARIABLE, 0 },
68 {N_("&Verbose operation"), &verbose, TOGGLE_VARIABLE, 0 },
69 {N_("Mkdir autoname"), &auto_fill_mkdir_name, TOGGLE_VARIABLE, 0 },
70 /* panel options */
71 {N_("&Fast dir reload"), &fast_reload, toggle_fast_reload, 0 },
72 {N_("mi&X all files"), &mix_all_files, toggle_mix_all_files, 0 },
73 {N_("&Drop down menus"), &drop_menus, TOGGLE_VARIABLE, 0 },
74 {N_("ma&Rk moves down"), &mark_moves_down, TOGGLE_VARIABLE, 0 },
75 {N_("show &Hidden files"), &show_dot_files, toggle_show_hidden, 0 },
76 {N_("show &Backup files"), &show_backups, toggle_show_backup, 0 },
77 { 0, 0, 0, 0 }
80 /* Make sure this corresponds to the check_options structure */
81 #define OTHER_OPTIONS 13
82 #define PANEL_OPTIONS 6
84 static WRadio *pause_radio;
86 static const char *pause_options [3] = {
87 N_("&Never"),
88 N_("on dumb &Terminals"),
89 N_("Alwa&ys") };
91 #define PAUSE_OPTIONS (sizeof(pause_options) / sizeof(pause_options[0]))
93 /* Heights of the panes */
94 #define PY 3
95 #define OY PY
96 /* Align bottoms of "pause after run" and "other options" */
97 #define RY (OTHER_OPTIONS - PAUSE_OPTIONS + OY)
98 #define DLG_Y (OTHER_OPTIONS + 9)
99 #define BY (DLG_Y - 3)
101 /* Horizontal dimensions */
102 #define X_MARGIN 3
103 #define X_PANE_GAP 1
104 #define PX X_MARGIN
105 #define RX X_MARGIN
106 #define OX (first_width + X_MARGIN + X_PANE_GAP)
108 /* Create the "Configure options" dialog */
109 static void
110 init_configure (void)
112 int i;
113 static int i18n_config_flag = 0;
114 static int b1, b2, b3;
115 const char *ok_button = _("&OK");
116 const char *cancel_button = _("&Cancel");
117 const char *save_button = _("&Save");
118 static const char *title1, *title2, *title3;
120 if (!i18n_config_flag) {
121 register int l1;
123 /* Similar code is in layout.c (init_layout()) */
125 title1 = _(" Panel options ");
126 title2 = _(" Pause after run... ");
127 title3 = _(" Other options ");
129 first_width = str_term_width1 (title1) + 1;
130 second_width = str_term_width1 (title3) + 1;
132 for (i = 0; check_options[i].text; i++) {
133 check_options[i].text = _(check_options[i].text);
134 l1 = str_term_width1 (check_options[i].text) + 7;
135 if (i >= OTHER_OPTIONS) {
136 if (l1 > first_width)
137 first_width = l1;
138 } else {
139 if (l1 > second_width)
140 second_width = l1;
144 i = PAUSE_OPTIONS;
145 while (i--) {
146 pause_options[i] = _(pause_options[i]);
147 l1 = str_term_width1 (pause_options[i]) + 7;
148 if (l1 > first_width)
149 first_width = l1;
152 l1 = str_term_width1 (title2) + 1;
153 if (l1 > first_width)
154 first_width = l1;
156 l1 = 11 + str_term_width1 (ok_button)
157 + str_term_width1 (save_button)
158 + str_term_width1 (cancel_button);
160 i = (first_width + second_width - l1) / 4;
161 b1 = 5 + i;
162 b2 = b1 + str_term_width1 (ok_button) + i + 6;
163 b3 = b2 + str_term_width1 (save_button) + i + 4;
165 i18n_config_flag = 1;
168 conf_dlg =
169 create_dlg (0, 0, DLG_Y,
170 first_width + second_width + 2 * X_MARGIN + X_PANE_GAP,
171 dialog_colors, NULL, "[Configuration]",
172 _("Configure options"), DLG_CENTER | DLG_REVERSE);
174 add_widget (conf_dlg,
175 groupbox_new (PX, PY, first_width, PANEL_OPTIONS + 2, title1));
177 add_widget (conf_dlg,
178 groupbox_new (RX, RY, first_width, PAUSE_OPTIONS + 2, title2));
180 add_widget (conf_dlg,
181 groupbox_new (OX, OY, second_width, OTHER_OPTIONS + 2, title3));
183 add_widget (conf_dlg,
184 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON,
185 cancel_button, 0));
187 add_widget (conf_dlg,
188 button_new (BY, b2, B_EXIT, NORMAL_BUTTON,
189 save_button, 0));
191 add_widget (conf_dlg,
192 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON,
193 ok_button, 0));
195 #define XTRACT(i) *check_options[i].variable, check_options[i].text
197 /* Add checkboxes for "other options" */
198 for (i = 0; i < OTHER_OPTIONS; i++) {
199 check_options[i].widget =
200 check_new (OY + (OTHER_OPTIONS - i), OX + 2, XTRACT (i));
201 add_widget (conf_dlg, check_options[i].widget);
204 pause_radio =
205 radio_new (RY + 1, RX + 2, 3, pause_options);
206 pause_radio->sel = pause_after_run;
207 add_widget (conf_dlg, pause_radio);
209 /* Add checkboxes for "panel options" */
210 for (i = 0; i < PANEL_OPTIONS; i++) {
211 check_options[i + OTHER_OPTIONS].widget =
212 check_new (PY + (PANEL_OPTIONS - i), PX + 2,
213 XTRACT (i + OTHER_OPTIONS));
214 add_widget (conf_dlg, check_options[i + OTHER_OPTIONS].widget);
219 void configure_box (void)
221 int result, i;
223 init_configure ();
224 run_dlg (conf_dlg);
226 result = conf_dlg->ret_value;
227 if (result == B_ENTER || result == B_EXIT){
228 for (i = 0; check_options [i].text; i++)
229 if (check_options [i].widget->state & C_CHANGE){
230 if (check_options [i].toggle_function)
231 (*check_options [i].toggle_function)();
232 else
233 *check_options [i].variable =
234 !(*check_options [i].variable);
236 pause_after_run = pause_radio->sel;
239 /* If they pressed the save button */
240 if (result == B_EXIT){
241 save_configure ();
242 sync_profiles ();
245 destroy_dlg (conf_dlg);