]>
git.ipfire.org Git - thirdparty/newt.git/blob - checkbox.c
8 enum type
{ CHECK
, RADIO
};
14 newtComponent prevButton
, lastButton
;
23 static void cbDraw(newtComponent c
);
24 static void cbDestroy(newtComponent co
);
25 struct eventResult
cbEvent(newtComponent co
, struct event ev
);
27 static struct componentOps cbOps
= {
31 newtDefaultPlaceHandler
,
32 newtDefaultMappedHandler
,
35 newtComponent
newtRadiobutton(int left
, int top
, const char * text
, int isDefault
,
36 newtComponent prevButton
) {
47 co
= newtCheckbox(left
, top
, text
, initialValue
, " *", NULL
);
51 rb
->prevButton
= prevButton
;
53 for (curr
= co
; curr
; curr
= rb
->prevButton
) {
61 newtComponent
newtRadioGetCurrent(newtComponent setMember
) {
62 struct checkbox
* rb
= setMember
->data
;
64 setMember
= rb
->lastButton
;
67 while (rb
&& rb
->value
!= '*') {
68 setMember
= rb
->prevButton
;
77 void newtRadioSetCurrent(newtComponent setMember
) {
78 struct checkbox
* cb
= setMember
->data
;
82 /* find the one that's turned on */
83 curr
= cb
->lastButton
;
85 while (curr
&& rb
->value
== rb
->seq
[0]) {
86 curr
= rb
->prevButton
;
87 if (curr
) rb
= curr
->data
;
90 rb
->value
= rb
->seq
[0];
93 cb
->value
= cb
->seq
[1];
96 if (setMember
->callback
)
97 setMember
->callback(setMember
, setMember
->callbackData
);
100 char newtCheckboxGetValue(newtComponent co
) {
101 struct checkbox
* cb
= co
->data
;
106 void newtCheckboxSetValue(newtComponent co
, char value
) {
107 struct checkbox
* cb
= co
->data
;
114 * returns NULL on error.
115 * FIXME: Check all calls.
117 newtComponent
newtCheckbox(int left
, int top
, const char * text
, char defValue
,
118 const char * seq
, char * result
) {
120 struct checkbox
* cb
;
122 if (!seq
) seq
= " *";
124 co
= malloc(sizeof(*co
));
127 cb
= malloc(sizeof(struct checkbox
));
137 cb
->result
= &cb
->value
;
139 cb
->text
= strdup(text
);
140 cb
->seq
= strdup(seq
);
143 cb
->inactive
= COLORSET_CHECKBOX
;
144 cb
->active
= COLORSET_ACTCHECKBOX
;
145 defValue
? (*cb
->result
= defValue
) : (*cb
->result
= cb
->seq
[0]);
150 co
->destroyCallback
= NULL
;
152 co
->width
= wstrlen(text
, -1) + 4;
161 void newtCheckboxSetFlags(newtComponent co
, int flags
, enum newtFlagsSense sense
) {
162 struct checkbox
* cb
= co
->data
;
165 cb
->flags
= newtSetFlags(cb
->flags
, flags
, sense
);
167 // If the flag just sets a property (eg. NEWT_FLAG_RETURNEXIT),
168 // don't redraw, etc. as the component might be 'hidden' and not to
169 // be drawn (eg. in a scrolled list)
170 if (flags
== NEWT_FLAG_RETURNEXIT
)
173 if (!(cb
->flags
& NEWT_FLAG_DISABLED
))
178 newtGetrc(&row
, &col
);
180 newtGotorc(row
, col
);
183 static void cbDraw(newtComponent c
) {
184 struct checkbox
* cb
= c
->data
;
186 if (!c
->isMapped
) return;
188 if (cb
->flags
& NEWT_FLAG_DISABLED
) {
189 cb
->inactive
= NEWT_COLORSET_DISENTRY
;
190 cb
->active
= NEWT_COLORSET_DISENTRY
;
192 cb
->inactive
= COLORSET_CHECKBOX
;
193 cb
->active
= COLORSET_ACTCHECKBOX
;
196 SLsmg_set_color(cb
->inactive
);
198 newtGotorc(c
->top
, c
->left
);
202 SLsmg_write_string("( ) ");
206 SLsmg_write_string("[ ] ");
213 SLsmg_write_string(cb
->text
);
216 SLsmg_set_color(cb
->active
);
218 newtGotorc(c
->top
, c
->left
+ 1);
219 SLsmg_write_char(*cb
->result
);
220 newtGotorc(c
->top
, c
->left
+ 4);
223 static void cbDestroy(newtComponent co
) {
224 struct checkbox
* cb
= co
->data
;
232 struct eventResult
cbEvent(newtComponent co
, struct event ev
) {
233 struct checkbox
* cb
= co
->data
;
234 struct eventResult er
;
237 er
.result
= ER_IGNORED
;
239 if (ev
.when
== EV_NORMAL
) {
244 er
.result
= ER_SWALLOWED
;
250 er
.result
= ER_SWALLOWED
;
254 if (ev
.u
.key
== ' ') {
255 if (cb
->type
== RADIO
) {
256 newtRadioSetCurrent(co
);
257 } else if (cb
->type
== CHECK
) {
258 cur
= strchr(cb
->seq
, *cb
->result
);
260 *cb
->result
= *cb
->seq
;
264 *cb
->result
= *cb
->seq
;
269 er
.result
= ER_SWALLOWED
;
272 co
->callback(co
, co
->callbackData
);
274 er
.result
= ER_IGNORED
;
276 } else if(ev
.u
.key
== NEWT_KEY_ENTER
) {
277 if (cb
->flags
& NEWT_FLAG_RETURNEXIT
)
278 er
.result
= ER_EXITFORM
;
280 er
.result
= ER_IGNORED
;
282 er
.result
= ER_IGNORED
;
286 if (ev
.u
.mouse
.type
== MOUSE_BUTTON_DOWN
) {
287 if (cb
->type
== RADIO
) {
288 newtRadioSetCurrent(co
);
289 } else if (cb
->type
== CHECK
) {
290 cur
= strchr(cb
->seq
, *cb
->result
);
292 *cb
->result
= *cb
->seq
;
296 *cb
->result
= *cb
->seq
;
301 er
.result
= ER_SWALLOWED
;
304 co
->callback(co
, co
->callbackData
);