]>
git.ipfire.org Git - thirdparty/newt.git/blob - checkbox.c
8 enum type
{ CHECK
, RADIO
, LISTITEM
};
14 newtComponent prevButton
, lastButton
;
22 static void cbDrawIt(newtComponent c
, int active
);
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
newtListitem(int left
, int top
, const char * text
, int isDefault
,
38 newtComponent prevItem
, const void * data
, int flags
) {
42 co
= newtRadiobutton(left
, top
, text
, isDefault
, prevItem
);
45 li
->flags
= flags
& (NEWT_FLAG_RETURNEXIT
);
46 li
->inactive
= COLORSET_LISTBOX
;
47 li
->active
= COLORSET_ACTLISTBOX
;
53 void * newtListitemGetData(newtComponent co
) {
54 struct checkbox
* rb
= co
->data
;
56 return (void *)rb
->data
;
59 void newtListitemSet(newtComponent co
, const char * text
) {
60 struct checkbox
* li
= co
->data
;
63 li
->text
= strdup(text
);
65 if (strlen(text
) + 4 > (unsigned int)co
->width
)
66 co
->width
= strlen(text
) + 4;
69 newtComponent
newtRadiobutton(int left
, int top
, const char * text
, int isDefault
,
70 newtComponent prevButton
) {
81 co
= newtCheckbox(left
, top
, text
, initialValue
, " *", NULL
);
85 rb
->prevButton
= prevButton
;
87 for (curr
= co
; curr
; curr
= rb
->prevButton
) {
95 newtComponent
newtRadioGetCurrent(newtComponent setMember
) {
96 struct checkbox
* rb
= setMember
->data
;
98 setMember
= rb
->lastButton
;
101 while (rb
&& rb
->value
!= '*') {
102 setMember
= rb
->prevButton
;
103 rb
= setMember
->data
;
109 char newtCheckboxGetValue(newtComponent co
) {
110 struct checkbox
* cb
= co
->data
;
115 newtComponent
newtCheckbox(int left
, int top
, const char * text
, char defValue
,
116 const char * seq
, char * result
) {
118 struct checkbox
* cb
;
120 if (!seq
) seq
= " *";
122 co
= malloc(sizeof(*co
));
123 cb
= malloc(sizeof(struct checkbox
));
129 cb
->result
= &cb
->value
;
131 cb
->text
= strdup(text
);
132 cb
->seq
= strdup(seq
);
134 cb
->inactive
= COLORSET_CHECKBOX
;
135 cb
->active
= COLORSET_ACTCHECKBOX
;
136 defValue
? (*cb
->result
= defValue
) : (*cb
->result
= cb
->seq
[0]);
142 co
->width
= strlen(text
) + 4;
150 static void cbDraw(newtComponent c
) {
154 static void cbDrawIt(newtComponent c
, int active
) {
155 struct checkbox
* cb
= c
->data
;
157 if (c
->top
== -1) return;
159 if (cb
->type
== LISTITEM
&& *cb
->result
!= ' ')
160 SLsmg_set_color(cb
->active
);
162 SLsmg_set_color(cb
->inactive
);
164 newtGotorc(c
->top
, c
->left
);
168 SLsmg_write_string("( ) ");
172 SLsmg_write_string("[ ] ");
179 SLsmg_write_string(cb
->text
);
182 SLsmg_set_color(cb
->active
);
184 if (cb
->type
!= LISTITEM
) {
185 newtGotorc(c
->top
, c
->left
+ 1);
186 SLsmg_write_char(*cb
->result
);
190 static void cbDestroy(newtComponent co
) {
191 struct checkbox
* cb
= co
->data
;
199 struct eventResult
cbEvent(newtComponent co
, struct event ev
) {
200 struct checkbox
* cb
= co
->data
;
201 struct eventResult er
;
204 if (ev
.when
== EV_NORMAL
) {
207 if (cb
->type
== LISTITEM
)
212 er
.result
= ER_SWALLOWED
;
217 er
.result
= ER_SWALLOWED
;
221 if (ev
.u
.key
== ' ') {
222 if (cb
->type
== RADIO
) {
224 } else if (cb
->type
== CHECK
) {
225 cur
= strchr(cb
->seq
, *cb
->result
);
227 *cb
->result
= *cb
->seq
;
231 *cb
->result
= *cb
->seq
;
236 er
.result
= ER_SWALLOWED
;
238 er
.result
= ER_IGNORED
;
240 } else if(ev
.u
.key
== NEWT_KEY_ENTER
) {
241 if((cb
->type
== LISTITEM
)
242 && (cb
->flags
| NEWT_FLAG_RETURNEXIT
)) {
244 er
.result
= ER_EXITFORM
;
246 er
.result
= ER_IGNORED
;
248 er
.result
= ER_IGNORED
;
253 er
.result
= ER_IGNORED
;
255 if (er
.result
== ER_SWALLOWED
&& co
->callback
)
256 co
->callback(co
, co
->callbackData
);
261 static void makeActive(newtComponent co
) {
262 struct checkbox
* cb
= co
->data
;
263 struct checkbox
* rb
;
266 /* find the one that's turned off */
267 curr
= cb
->lastButton
;
269 while (curr
&& rb
->value
== rb
->seq
[0]) {
270 curr
= rb
->prevButton
;
271 if (curr
) rb
= curr
->data
;
274 rb
->value
= rb
->seq
[0];
277 cb
->value
= cb
->seq
[1];