1 /* Checkbox widget handlers. */
9 #include "bfu/button.h"
10 #include "bfu/checkbox.h"
11 #include "bfu/dialog.h"
13 #include "intl/gettext/libintl.h"
14 #include "terminal/draw.h"
15 #include "terminal/mouse.h"
16 #include "terminal/terminal.h"
18 #define CHECKBOX_HEIGHT 1
19 #define CHECKBOX_LEN 3 /* "[X]" or "(X)" */
20 #define CHECKBOX_SPACING 1 /* "[X]" + " " + "Label" */
21 #define CHECKBOX_LS (CHECKBOX_LEN + CHECKBOX_SPACING) /* "[X] " */
24 add_dlg_radio_do(struct dialog
*dlg
, unsigned char *text
,
25 int groupid
, int groupnum
, int *data
)
27 struct widget
*widget
= &dlg
->widgets
[dlg
->number_of_widgets
++];
29 widget
->type
= WIDGET_CHECKBOX
;
31 widget
->datalen
= sizeof(*data
);
32 widget
->data
= (void *) data
;
34 widget
->info
.checkbox
.gid
= groupid
;
35 widget
->info
.checkbox
.gnum
= groupnum
;
39 dlg_format_checkbox(struct terminal
*term
,
40 struct widget_data
*widget_data
,
41 int x
, int *y
, int w
, int *rw
,
42 enum format_align align
, int format_only
)
44 unsigned char *text
= widget_data
->widget
->text
;
46 set_box(&widget_data
->box
, x
, *y
, CHECKBOX_LEN
, CHECKBOX_HEIGHT
);
48 if (w
<= CHECKBOX_LS
) return;
51 if (rw
) *rw
-= CHECKBOX_LS
;
52 dlg_format_text_do(term
, text
, x
+ CHECKBOX_LS
, y
,
54 get_bfu_color(term
, "dialog.checkbox-label"),
56 if (rw
) *rw
+= CHECKBOX_LS
;
60 static widget_handler_status_T
61 display_checkbox(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
63 struct terminal
*term
= dlg_data
->win
->term
;
64 struct color_pair
*color
;
66 struct box
*pos
= &widget_data
->box
;
67 int selected
= is_selected_widget(dlg_data
, widget_data
);
70 color
= get_bfu_color(term
, "dialog.checkbox-selected");
72 color
= get_bfu_color(term
, "dialog.checkbox");
74 if (!color
) return EVENT_PROCESSED
;
76 if (widget_data
->info
.checkbox
.checked
)
77 text
= widget_data
->widget
->info
.checkbox
.gid
? "(X)" : "[X]";
79 text
= widget_data
->widget
->info
.checkbox
.gid
? "( )" : "[ ]";
81 draw_text(term
, pos
->x
, pos
->y
, text
, CHECKBOX_LEN
, 0, color
);
84 set_cursor(term
, pos
->x
+ 1, pos
->y
, 1);
85 set_window_ptr(dlg_data
->win
, pos
->x
, pos
->y
);
88 return EVENT_PROCESSED
;
91 static widget_handler_status_T
92 init_checkbox(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
94 int *cdata
= (int *) widget_data
->cdata
;
98 if (widget_data
->widget
->info
.checkbox
.gid
) {
99 if (*cdata
== widget_data
->widget
->info
.checkbox
.gnum
)
100 widget_data
->info
.checkbox
.checked
= 1;
103 widget_data
->info
.checkbox
.checked
= 1;
105 return EVENT_PROCESSED
;
108 static widget_handler_status_T
109 mouse_checkbox(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
111 struct terminal
*term
= dlg_data
->win
->term
;
112 struct term_event
*ev
= dlg_data
->term_event
;
114 if (check_mouse_wheel(ev
)
115 || !check_mouse_position(ev
, &widget_data
->box
))
116 return EVENT_NOT_PROCESSED
;
118 select_widget(dlg_data
, widget_data
);
120 do_not_ignore_next_mouse_event(term
);
122 if (check_mouse_action(ev
, B_UP
) && widget_data
->widget
->ops
->select
)
123 widget_data
->widget
->ops
->select(dlg_data
, widget_data
);
125 return EVENT_PROCESSED
;
128 static widget_handler_status_T
129 select_checkbox(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
132 if (!widget_data
->widget
->info
.checkbox
.gid
) {
134 int *cdata
= (int *) widget_data
->cdata
;
138 widget_data
->info
.checkbox
.checked
= *cdata
= !*cdata
;
141 /* Group of radio buttons. */
142 struct widget_data
*wdata
;
144 foreach_widget(dlg_data
, wdata
) {
145 int *cdata
= (int *) wdata
->cdata
;
147 if (wdata
->widget
->type
!= WIDGET_CHECKBOX
)
150 if (wdata
->widget
->info
.checkbox
.gid
151 != widget_data
->widget
->info
.checkbox
.gid
)
156 *cdata
= widget_data
->widget
->info
.checkbox
.gnum
;
157 wdata
->info
.checkbox
.checked
= 0;
158 display_widget(dlg_data
, wdata
);
160 widget_data
->info
.checkbox
.checked
= 1;
163 display_widget(dlg_data
, widget_data
);
164 return EVENT_PROCESSED
;
167 const struct widget_ops checkbox_ops
= {