test: The CGI script showing POST DATA sent to it.
[elinks.git] / src / dialogs / options.c
blob06ddeb2a6dba851c9030a546bf296fda277a44c4
1 /* Options dialogs */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "elinks.h"
13 #include "bfu/dialog.h"
14 #include "bfu/menu.h"
15 #include "config/conf.h"
16 #include "config/options.h"
17 #include "dialogs/options.h"
18 #include "intl/charsets.h"
19 #include "intl/gettext/libintl.h"
20 #include "osdep/osdep.h"
21 #include "session/session.h"
22 #include "terminal/color.h"
23 #include "terminal/terminal.h"
24 #include "util/conv.h"
25 #include "util/memory.h"
26 #include "util/memlist.h"
29 static void
30 display_codepage(struct terminal *term, void *name_, void *xxx)
32 unsigned char *name = name_;
33 struct option *opt = get_opt_rec(term->spec, "charset");
34 int index = get_cp_index(name);
36 assertm(index != -1, "%s", name);
38 if (opt->value.number != index) {
39 opt->value.number = index;
40 option_changed(NULL, opt);
43 cls_redraw_all_terminals();
46 void
47 charset_list(struct terminal *term, void *xxx, void *ses_)
49 struct session *ses = ses_;
50 int i, items;
51 int sel = int_max(0, get_opt_codepage_tree(term->spec, "charset"));
52 struct menu_item *mi = new_menu(FREE_LIST);
54 if (!mi) return;
56 for (i = 0, items = 0; ; i++) {
57 unsigned char *name = get_cp_name(i);
59 if (!name) break;
61 #ifndef CONFIG_UTF8
62 if (is_cp_utf8(i)) continue;
63 #endif /* CONFIG_UTF8 */
65 items++;
66 add_to_menu(&mi, name, NULL, ACT_MAIN_NONE,
67 display_codepage, get_cp_mime_name(i), 0);
70 /* Special codepages are not in the menu and it may cause assertion
71 * failures later if the selected item is out of bound. */
72 if (sel >= items)
73 sel = 0;
75 do_menu_selected(term, mi, ses, sel, 0);
79 /* TODO: Build this automagically. But that will need gettextted options
80 * captions not to lose translations and so on. 0.5 stuff or even later.
81 * --pasky */
83 enum termopt {
84 TERM_OPT_TYPE = 0,
85 TERM_OPT_M11_HACK,
86 TERM_OPT_RESTRICT_852,
87 TERM_OPT_BLOCK_CURSOR,
88 TERM_OPT_COLORS,
89 TERM_OPT_UTF_8_IO,
90 TERM_OPT_TRANSPARENCY,
91 TERM_OPT_UNDERLINE,
93 TERM_OPTIONS,
96 static struct option_resolver resolvers[] = {
97 { TERM_OPT_TYPE, "type" },
98 { TERM_OPT_M11_HACK, "m11_hack" },
99 { TERM_OPT_RESTRICT_852, "restrict_852" },
100 { TERM_OPT_BLOCK_CURSOR, "block_cursor" },
101 { TERM_OPT_COLORS, "colors" },
102 { TERM_OPT_TRANSPARENCY, "transparency" },
103 { TERM_OPT_UTF_8_IO, "utf_8_io" },
104 { TERM_OPT_UNDERLINE, "underline" },
107 static widget_handler_status_T
108 push_ok_button(struct dialog_data *dlg_data, struct widget_data *button)
110 struct terminal *term = dlg_data->win->term;
111 union option_value *values = dlg_data->dlg->udata;
113 update_dialog_data(dlg_data);
115 commit_option_values(resolvers, term->spec, values, TERM_OPTIONS);
117 if (button->widget->handler == push_ok_button)
118 return cancel_dialog(dlg_data, button);
120 return EVENT_PROCESSED;
123 static widget_handler_status_T
124 push_save_button(struct dialog_data *dlg_data, struct widget_data *button)
126 push_ok_button(dlg_data, button);
127 write_config(dlg_data->win->term);
129 return EVENT_PROCESSED;
132 #if defined(CONFIG_88_COLORS)
133 #define RADIO_88 1
134 #else
135 #define RADIO_88 0
136 #endif
138 #if defined(CONFIG_256_COLORS)
139 #define RADIO_256 1
140 #else
141 #define RADIO_256 0
142 #endif
144 #if defined(CONFIG_TRUE_COLOR)
145 #define RADIO_TRUE 1
146 #else
147 #define RADIO_TRUE 0
148 #endif
150 #define TERMOPT_WIDGETS_COUNT (19 + RADIO_88 + RADIO_256 + RADIO_TRUE)
152 #define TERM_OPTION_VALUE_SIZE (sizeof(union option_value) * TERM_OPTIONS)
154 void
155 terminal_options(struct terminal *term, void *xxx, struct session *ses)
157 /* [gettext_accelerator_context(terminal_options)] */
158 struct dialog *dlg;
159 union option_value *values;
160 int anonymous = get_cmd_opt_bool("anonymous");
161 unsigned char help_text[MAX_STR_LEN], *text;
162 size_t help_textlen = 0;
163 size_t add_size = TERM_OPTION_VALUE_SIZE;
165 /* XXX: we don't display help text when terminal height is too low,
166 * because then user can't change values.
167 * This should be dropped when we'll have scrollable dialog boxes.
168 * --Zas */
169 if (term->height > 30) {
170 snprintf(help_text, sizeof(help_text) - 3 /* 2 '\n' + 1 '\0' */,
171 _("The environmental variable TERM is set to '%s'.\n"
172 "\n"
173 "ELinks maintains separate sets of values for these options\n"
174 "and chooses the appropriate set based on the value of TERM.\n"
175 "This allows you to configure the settings appropriately for\n"
176 "each terminal in which you run ELinks.", term),
177 term->spec->name);
179 help_textlen = strlen(help_text);
181 /* Two newlines are needed to get a blank line between the help text and
182 * the first group of widgets. */
183 help_text[help_textlen++] = '\n';
184 help_text[help_textlen++] = '\n';
187 help_text[help_textlen++] = '\0';
189 add_size += help_textlen;
191 dlg = calloc_dialog(TERMOPT_WIDGETS_COUNT, add_size);
192 if (!dlg) return;
194 values = (union option_value *) get_dialog_offset(dlg, TERMOPT_WIDGETS_COUNT);
195 checkout_option_values(resolvers, term->spec, values, TERM_OPTIONS);
197 dlg->title = _("Terminal options", term);
198 dlg->layouter = generic_dialog_layouter;
199 dlg->layout.padding_top = 1;
200 dlg->udata = values;
202 text = ((unsigned char *) values) + TERM_OPTION_VALUE_SIZE;
203 memcpy(text, help_text, help_textlen);
204 add_dlg_text(dlg, text, ALIGN_LEFT, 1);
206 add_dlg_text(dlg, _("Frame handling:", term), ALIGN_LEFT, 1);
207 add_dlg_radio(dlg, _("No frames", term), 1, TERM_DUMB, &values[TERM_OPT_TYPE].number);
208 add_dlg_radio(dlg, _("VT 100 frames", term), 1, TERM_VT100, &values[TERM_OPT_TYPE].number);
209 add_dlg_radio(dlg, _("Linux or OS/2 frames", term), 1, TERM_LINUX, &values[TERM_OPT_TYPE].number);
210 add_dlg_radio(dlg, _("FreeBSD frames", term), 1, TERM_FREEBSD, &values[TERM_OPT_TYPE].number);
211 add_dlg_radio(dlg, _("KOI8-R frames", term), 1, TERM_KOI8, &values[TERM_OPT_TYPE].number);
213 add_dlg_text(dlg, _("Color mode:", term), ALIGN_LEFT, 1);
214 add_dlg_radio(dlg, _("No colors (mono)", term), 2, COLOR_MODE_MONO, &values[TERM_OPT_COLORS].number);
215 add_dlg_radio(dlg, _("16 colors", term), 2, COLOR_MODE_16, &values[TERM_OPT_COLORS].number);
216 #ifdef CONFIG_88_COLORS
217 add_dlg_radio(dlg, _("88 colors", term), 2, COLOR_MODE_88, &values[TERM_OPT_COLORS].number);
218 #endif
219 #ifdef CONFIG_256_COLORS
220 add_dlg_radio(dlg, _("256 colors", term), 2, COLOR_MODE_256, &values[TERM_OPT_COLORS].number);
221 #endif
222 #ifdef CONFIG_TRUE_COLOR
223 add_dlg_radio(dlg, _("true color", term), 2, COLOR_MODE_TRUE_COLOR, &values[TERM_OPT_COLORS].number);
224 #endif
225 add_dlg_checkbox(dlg, _("Switch fonts for line drawing", term), &values[TERM_OPT_M11_HACK].number);
226 add_dlg_checkbox(dlg, _("Restrict frames in cp850/852", term), &values[TERM_OPT_RESTRICT_852].number);
227 add_dlg_checkbox(dlg, _("Block cursor", term), &values[TERM_OPT_BLOCK_CURSOR].number);
228 add_dlg_checkbox(dlg, _("Transparency", term), &values[TERM_OPT_TRANSPARENCY].number);
229 add_dlg_checkbox(dlg, _("Underline", term), &values[TERM_OPT_UNDERLINE].number);
230 add_dlg_checkbox(dlg, _("UTF-8 I/O", term), &values[TERM_OPT_UTF_8_IO].number);
232 add_dlg_button(dlg, _("~OK", term), B_ENTER, push_ok_button, NULL);
233 if (!anonymous)
234 add_dlg_button(dlg, _("Sa~ve", term), B_ENTER, push_save_button, NULL);
235 add_dlg_button(dlg, _("~Cancel", term), B_ESC, cancel_dialog, NULL);
237 add_dlg_end(dlg, TERMOPT_WIDGETS_COUNT - anonymous);
239 do_dialog(term, dlg, getml(dlg, NULL));
242 #ifdef CONFIG_NLS
243 static void
244 menu_set_language(struct terminal *term, void *pcp_, void *xxx)
246 int pcp = (long) pcp_;
248 set_language(pcp);
249 cls_redraw_all_terminals();
251 #endif
253 void
254 menu_language_list(struct terminal *term, void *xxx, void *ses)
256 #ifdef CONFIG_NLS
257 int i;
258 struct menu_item *mi = new_menu(FREE_LIST);
260 if (!mi) return;
261 for (i = 0; languages[i].name; i++) {
262 add_to_menu(&mi, languages[i].name, language_to_iso639(i), ACT_MAIN_NONE,
263 menu_set_language, (void *) (long) i, 0);
266 do_menu_selected(term, mi, ses, current_language, 0);
267 #endif
271 /* FIXME: This doesn't in fact belong here at all. --pasky */
273 static unsigned char width_str[4];
274 static unsigned char height_str[4];
276 static void
277 push_resize_button(void *data)
279 struct terminal *term = data;
280 unsigned char str[MAX_STR_LEN];
282 snprintf(str, sizeof(str), "%s,%s,%d,%d",
283 width_str, height_str, term->width, term->height);
285 do_terminal_function(term, TERM_FN_RESIZE, str);
288 /* menu_func_T */
289 void
290 resize_terminal_dialog(struct terminal *term)
292 /* [gettext_accelerator_context(resize_terminal_dialog)] */
293 struct dialog *dlg;
294 int width = int_min(term->width, 999);
295 int height = int_min(term->height, 999);
297 if (!can_resize_window(term->environment))
298 return;
300 ulongcat(width_str, NULL, width, 3, ' ');
301 ulongcat(height_str, NULL, height, 3, ' ');
303 #define RESIZE_WIDGETS_COUNT 4
304 dlg = calloc_dialog(RESIZE_WIDGETS_COUNT, 0);
305 if (!dlg) return;
307 dlg->title = _("Resize terminal", term);
308 dlg->layouter = group_layouter;
310 add_dlg_field(dlg, _("Width=",term), 1, 999, check_number, 4, width_str, NULL);
311 add_dlg_field(dlg, _("Height=",term), 1, 999, check_number, 4, height_str, NULL);
313 add_dlg_ok_button(dlg, _("~OK", term), B_ENTER, push_resize_button, term);
314 add_dlg_button(dlg, _("~Cancel", term), B_ESC, cancel_dialog, NULL);
316 add_dlg_end(dlg, RESIZE_WIDGETS_COUNT);
318 do_dialog(term, dlg, getml(dlg, NULL));