]>
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]);
130 co
->width
= wstrlen(text
, -1) + 4;
138 void newtCheckboxSetFlags(newtComponent co
, int flags
, enum newtFlagsSense sense
) {
139 struct checkbox
* cb
= co
->data
;
142 cb
->flags
= newtSetFlags(cb
->flags
, flags
, sense
);
144 // If the flag just sets a property (eg. NEWT_FLAG_RETURNEXIT),
145 // don't redraw, etc. as the component might be 'hidden' and not to
146 // be drawn (eg. in a scrolled list)
147 if (flags
== NEWT_FLAG_RETURNEXIT
)
150 if (!(cb
->flags
& NEWT_FLAG_DISABLED
))
155 newtGetrc(&row
, &col
);
157 newtGotorc(row
, col
);
160 static void cbDraw(newtComponent c
) {
161 struct checkbox
* cb
= c
->data
;
163 if (c
->top
== -1 || !c
->isMapped
) return;
165 if (cb
->flags
& NEWT_FLAG_DISABLED
) {
166 cb
->inactive
= NEWT_COLORSET_DISENTRY
;
167 cb
->active
= NEWT_COLORSET_DISENTRY
;
169 cb
->inactive
= COLORSET_CHECKBOX
;
170 cb
->active
= COLORSET_ACTCHECKBOX
;
173 SLsmg_set_color(cb
->inactive
);
175 newtGotorc(c
->top
, c
->left
);
179 SLsmg_write_string("( ) ");
183 SLsmg_write_string("[ ] ");
190 SLsmg_write_string(cb
->text
);
193 SLsmg_set_color(cb
->active
);
195 newtGotorc(c
->top
, c
->left
+ 1);
196 SLsmg_write_char(*cb
->result
);
199 static void cbDestroy(newtComponent co
) {
200 struct checkbox
* cb
= co
->data
;
208 struct eventResult
cbEvent(newtComponent co
, struct event ev
) {
209 struct checkbox
* cb
= co
->data
;
210 struct eventResult er
;
213 if (ev
.when
== EV_NORMAL
) {
218 er
.result
= ER_SWALLOWED
;
224 er
.result
= ER_SWALLOWED
;
228 if (ev
.u
.key
== ' ') {
229 if (cb
->type
== RADIO
) {
231 } else if (cb
->type
== CHECK
) {
232 cur
= strchr(cb
->seq
, *cb
->result
);
234 *cb
->result
= *cb
->seq
;
238 *cb
->result
= *cb
->seq
;
243 er
.result
= ER_SWALLOWED
;
246 co
->callback(co
, co
->callbackData
);
248 er
.result
= ER_IGNORED
;
250 } else if(ev
.u
.key
== NEWT_KEY_ENTER
) {
251 if (cb
->flags
& NEWT_FLAG_RETURNEXIT
)
252 er
.result
= ER_EXITFORM
;
254 er
.result
= ER_IGNORED
;
256 er
.result
= ER_IGNORED
;
260 if (ev
.u
.mouse
.type
== MOUSE_BUTTON_DOWN
) {
261 if (cb
->type
== RADIO
) {
263 } else if (cb
->type
== CHECK
) {
264 cur
= strchr(cb
->seq
, *cb
->result
);
266 *cb
->result
= *cb
->seq
;
270 *cb
->result
= *cb
->seq
;
275 er
.result
= ER_SWALLOWED
;
278 co
->callback(co
, co
->callbackData
);
283 er
.result
= ER_IGNORED
;
288 static void makeActive(newtComponent co
) {
289 struct checkbox
* cb
= co
->data
;
290 struct checkbox
* rb
;
293 /* find the one that's turned off */
294 curr
= cb
->lastButton
;
296 while (curr
&& rb
->value
== rb
->seq
[0]) {
297 curr
= rb
->prevButton
;
298 if (curr
) rb
= curr
->data
;
301 rb
->value
= rb
->seq
[0];
304 cb
->value
= cb
->seq
[1];
308 co
->callback(co
, co
->callbackData
);