]>
git.ipfire.org Git - thirdparty/newt.git/blob - checkbox.c
8 enum type
{ CHECK
, RADIO
};
14 newtComponent prevButton
, lastButton
;
23 static void makeActive(newtComponent co
);
25 static void cbDraw(newtComponent c
);
26 static void cbDestroy(newtComponent co
);
27 struct eventResult
cbEvent(newtComponent co
, struct event ev
);
29 static struct componentOps cbOps
= {
33 newtDefaultPlaceHandler
,
34 newtDefaultMappedHandler
,
37 newtComponent
newtRadiobutton(int left
, int top
, const char * text
, int isDefault
,
38 newtComponent prevButton
) {
49 co
= newtCheckbox(left
, top
, text
, initialValue
, " *", NULL
);
53 rb
->prevButton
= prevButton
;
55 for (curr
= co
; curr
; curr
= rb
->prevButton
) {
63 newtComponent
newtRadioGetCurrent(newtComponent setMember
) {
64 struct checkbox
* rb
= setMember
->data
;
66 setMember
= rb
->lastButton
;
69 while (rb
&& rb
->value
!= '*') {
70 setMember
= rb
->prevButton
;
79 char newtCheckboxGetValue(newtComponent co
) {
80 struct checkbox
* cb
= co
->data
;
85 void newtCheckboxSetValue(newtComponent co
, char value
) {
86 struct checkbox
* cb
= co
->data
;
93 * returns NULL on error.
94 * FIXME: Check all calls.
96 newtComponent
newtCheckbox(int left
, int top
, const char * text
, char defValue
,
97 const char * seq
, char * result
) {
101 if (!seq
) seq
= " *";
103 co
= malloc(sizeof(*co
));
106 cb
= malloc(sizeof(struct checkbox
));
116 cb
->result
= &cb
->value
;
118 cb
->text
= strdup(text
);
119 cb
->seq
= strdup(seq
);
122 cb
->inactive
= COLORSET_CHECKBOX
;
123 cb
->active
= COLORSET_ACTCHECKBOX
;
124 defValue
? (*cb
->result
= defValue
) : (*cb
->result
= cb
->seq
[0]);
129 co
->destroyCallback
= NULL
;
131 co
->width
= wstrlen(text
, -1) + 4;
140 void newtCheckboxSetFlags(newtComponent co
, int flags
, enum newtFlagsSense sense
) {
141 struct checkbox
* cb
= co
->data
;
144 cb
->flags
= newtSetFlags(cb
->flags
, flags
, sense
);
146 // If the flag just sets a property (eg. NEWT_FLAG_RETURNEXIT),
147 // don't redraw, etc. as the component might be 'hidden' and not to
148 // be drawn (eg. in a scrolled list)
149 if (flags
== NEWT_FLAG_RETURNEXIT
)
152 if (!(cb
->flags
& NEWT_FLAG_DISABLED
))
157 newtGetrc(&row
, &col
);
159 newtGotorc(row
, col
);
162 static void cbDraw(newtComponent c
) {
163 struct checkbox
* cb
= c
->data
;
165 if (c
->top
== -1 || !c
->isMapped
) return;
167 if (cb
->flags
& NEWT_FLAG_DISABLED
) {
168 cb
->inactive
= NEWT_COLORSET_DISENTRY
;
169 cb
->active
= NEWT_COLORSET_DISENTRY
;
171 cb
->inactive
= COLORSET_CHECKBOX
;
172 cb
->active
= COLORSET_ACTCHECKBOX
;
175 SLsmg_set_color(cb
->inactive
);
177 newtGotorc(c
->top
, c
->left
);
181 SLsmg_write_string("( ) ");
185 SLsmg_write_string("[ ] ");
192 SLsmg_write_string(cb
->text
);
195 SLsmg_set_color(cb
->active
);
197 newtGotorc(c
->top
, c
->left
+ 1);
198 SLsmg_write_char(*cb
->result
);
199 newtGotorc(c
->top
, c
->left
+ 4);
202 static void cbDestroy(newtComponent co
) {
203 struct checkbox
* cb
= co
->data
;
211 struct eventResult
cbEvent(newtComponent co
, struct event ev
) {
212 struct checkbox
* cb
= co
->data
;
213 struct eventResult er
;
216 er
.result
= ER_IGNORED
;
218 if (ev
.when
== EV_NORMAL
) {
223 er
.result
= ER_SWALLOWED
;
229 er
.result
= ER_SWALLOWED
;
233 if (ev
.u
.key
== ' ') {
234 if (cb
->type
== RADIO
) {
236 } else if (cb
->type
== CHECK
) {
237 cur
= strchr(cb
->seq
, *cb
->result
);
239 *cb
->result
= *cb
->seq
;
243 *cb
->result
= *cb
->seq
;
248 er
.result
= ER_SWALLOWED
;
251 co
->callback(co
, co
->callbackData
);
253 er
.result
= ER_IGNORED
;
255 } else if(ev
.u
.key
== NEWT_KEY_ENTER
) {
256 if (cb
->flags
& NEWT_FLAG_RETURNEXIT
)
257 er
.result
= ER_EXITFORM
;
259 er
.result
= ER_IGNORED
;
261 er
.result
= ER_IGNORED
;
265 if (ev
.u
.mouse
.type
== MOUSE_BUTTON_DOWN
) {
266 if (cb
->type
== RADIO
) {
268 } else if (cb
->type
== CHECK
) {
269 cur
= strchr(cb
->seq
, *cb
->result
);
271 *cb
->result
= *cb
->seq
;
275 *cb
->result
= *cb
->seq
;
280 er
.result
= ER_SWALLOWED
;
283 co
->callback(co
, co
->callbackData
);
292 static void makeActive(newtComponent co
) {
293 struct checkbox
* cb
= co
->data
;
294 struct checkbox
* rb
;
297 /* find the one that's turned off */
298 curr
= cb
->lastButton
;
300 while (curr
&& rb
->value
== rb
->seq
[0]) {
301 curr
= rb
->prevButton
;
302 if (curr
) rb
= curr
->data
;
305 rb
->value
= rb
->seq
[0];
308 cb
->value
= cb
->seq
[1];
312 co
->callback(co
, co
->callbackData
);