big dialogs: set_curosr2 -> set_dlg_cursor.
[elinks.git] / src / bfu / group.c
blob655df96b7805e5de4472cbab5906abd88240c540
1 /* Widget group implementation. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/dialog.h"
12 #include "bfu/button.h"
13 #include "bfu/group.h"
14 #include "intl/gettext/libintl.h"
15 #include "terminal/draw.h"
16 #include "terminal/terminal.h"
17 #include "util/color.h"
19 /* Same as in src/bfu/checkbox.c */
20 #define CHECKBOX_LEN 3 /* "[X]" or "(X)" */
22 void
23 dlg_format_group(struct terminal *term, struct dialog_data *dlg_data,
24 struct widget_data *widget_data,
25 int n, int x, int *y, int w, int *rw, int format_only)
27 int space_between_widgets = 1;
28 int line_width = 0;
29 int xpos;
30 struct color_pair *color = get_bfu_color(term, "dialog.text");
32 assert(n > 0);
33 if_assert_failed return;
35 while (n--) {
36 int widget_width;
37 int width;
38 unsigned char *text = widget_data->widget->text;
39 int label_length;
40 int label_padding;
42 #ifdef CONFIG_UTF8
43 if (term->utf8_cp) {
44 if (text && *text)
45 label_length = utf8_ptr2cells(text, NULL);
46 else
47 label_length = 0;
48 } else
49 #endif /* CONFIG_UTF8 */
50 label_length = (text && *text) ? strlen(text) : 0;
52 label_padding = (label_length > 0);
54 if (widget_data->widget->type == WIDGET_CHECKBOX) {
55 width = CHECKBOX_LEN;
56 } else if (widget_is_textfield(widget_data)) {
57 #ifdef CONFIG_UTF8
58 if (term->utf8_cp) {
59 width = utf8_ptr2cells(widget_data->widget->data,
60 NULL);
61 } else
62 #endif /* CONFIG_UTF8 */
63 width = widget_data->widget->datalen;
64 } else {
65 /* TODO: handle all widget types. */
66 widget_data++;
67 continue;
70 int_bounds(&label_length, 0, w - width - label_padding);
72 widget_width = width + label_padding + label_length;
73 if (line_width + widget_width > w) {
74 line_width = 0;
75 (*y) += 2; /* Next line */
78 xpos = x + line_width;
80 if (!format_only) {
81 if (widget_data->widget->type == WIDGET_CHECKBOX) {
82 /* Draw text at right of checkbox. */
83 if (label_length) {
84 #ifdef CONFIG_UTF8
85 if (term->utf8_cp) {
86 int lb = utf8_cells2bytes(
87 text,
88 label_length,
89 NULL);
90 draw_dlg_text(term, dlg_data, xpos + width
91 + label_padding,
92 *y, text, lb, 0,
93 color);
94 } else
95 #endif /* CONFIG_UTF8 */
97 draw_dlg_text(term, dlg_data, xpos + width
98 + label_padding,
99 *y, text,
100 label_length, 0,
101 color);
105 set_box(&widget_data->box, xpos, *y, width, 1);
107 } else if (widget_is_textfield(widget_data)) {
108 /* Draw label at left of widget. */
109 if (label_length) {
110 #ifdef CONFIG_UTF8
111 if (term->utf8_cp) {
112 int lb = utf8_cells2bytes(
113 text,
114 label_length,
115 NULL);
116 draw_dlg_text(term, dlg_data, xpos, *y,
117 text, lb, 0, color);
118 } else
119 #endif /* CONFIG_UTF8 */
121 draw_dlg_text(term, dlg_data, xpos, *y,
122 text, label_length,
123 0, color);
127 set_box(&widget_data->box,
128 xpos + label_padding + label_length, *y,
129 width, 1);
133 line_width += widget_width;
134 if (rw) int_bounds(rw, line_width, w);
135 line_width += space_between_widgets;
137 widget_data++;
139 (*y)++;
142 void
143 group_layouter(struct dialog_data *dlg_data)
145 struct terminal *term = dlg_data->win->term;
146 int w = dialog_max_width(term);
147 int rw;
148 int y = 0;
149 int n = dlg_data->number_of_widgets - 2;
151 #ifdef CONFIG_UTF8
152 if (term->utf8_cp)
153 rw = int_min(w, utf8_ptr2cells(dlg_data->dlg->title, NULL));
154 else
155 #endif /* CONFIG_UTF8 */
156 rw = int_min(w, strlen(dlg_data->dlg->title));
158 dlg_format_group(term, dlg_data, dlg_data->widgets_data, n,
159 0, &y, w, &rw, 1);
161 y++;
162 dlg_format_buttons(term, dlg_data, dlg_data->widgets_data + n, 2, 0, &y, w,
163 &rw, ALIGN_CENTER, 1);
165 w = rw;
167 draw_dialog(dlg_data, w, y);
169 y = dlg_data->box.y + DIALOG_TB + 1;
170 dlg_format_group(term, dlg_data, dlg_data->widgets_data, n,
171 dlg_data->box.x + DIALOG_LB, &y, w, NULL, 0);
173 y++;
174 dlg_format_buttons(term, dlg_data, dlg_data->widgets_data + n, 2,
175 dlg_data->box.x + DIALOG_LB, &y, w, &rw, ALIGN_CENTER, 0);