]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libcontrol/radiobutton.c
gre: don't drop pptp packets when smaller than v4 header
[plan9front.git] / sys / src / libcontrol / radiobutton.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <mouse.h>
6 #include <keyboard.h>
7 #include <control.h>
8
9 typedef struct Radio Radio;
10
11 struct Radio
12 {
13         Control;
14         int             value;
15         int             lastbut;
16         Control **buttons;
17         int             nbuttons;
18         char            *eventstr;
19 };
20
21 enum{
22         EAdd,
23         EButton,
24         EFocus,
25         EFormat,
26         EHide,
27         ERect,
28         EReveal,
29         EShow,
30         ESize,
31         EValue,
32 };
33
34 static char *cmds[] = {
35         [EAdd] =                "add",
36         [EButton] =     "button",
37         [EFocus] =      "focus",
38         [EFormat] =     "format",
39         [EHide] =               "hide",
40         [ERect] =               "rect",
41         [EReveal] =     "reveal",
42         [EShow] =               "show",
43         [ESize] =               "size",
44         [EValue] =              "value",
45         nil
46 };
47
48 static void     radioshow(Radio*);
49
50 static void
51 radiomouse(Control *c, Mouse *m)
52 {
53         Radio *r;
54         int i;
55
56         r = (Radio*)c;
57         for(i=0; i<r->nbuttons; i++)
58                 if(ptinrect(m->xy, r->buttons[i]->rect) && r->buttons[i]->mouse){
59                         (r->buttons[i]->mouse)(r->buttons[i], m);
60                         break;
61                 }
62 }
63
64 static void
65 radioshow(Radio *r)
66 {
67         int i;
68
69         if (r->hidden)
70                 return;
71         for(i=0; i<r->nbuttons; i++){
72                 _ctlprint(r->buttons[i], "value %d", (r->value==i));
73                 _ctlprint(r->buttons[i], "show");
74         }
75 }
76
77 static void
78 radioctl(Control *c, CParse *cp)
79 {
80         int cmd, i;
81         Rectangle rect;
82         Radio *r;
83         char fmt[256];
84
85         r = (Radio*)c;
86
87         cmd = _ctllookup(cp->args[0], cmds, nelem(cmds));
88         switch(cmd){
89         default:
90                 ctlerror("%q: unrecognized message '%s'", r->name, cp->str);
91                 break;
92         case EAdd:
93                 _ctlargcount(r, cp, 2);
94                 c = controlcalled(cp->args[1]);
95                 if(c == nil)
96                         ctlerror("%q: can't add %s: %r", r->name, cp->args[1]);
97                 snprint(fmt, sizeof fmt, "%%q: %q button %%d", r->name);
98                 _ctlprint(c, "format %q", fmt);
99                 controlwire(c, "event", c->controlset->ctl);
100                 r->buttons = ctlrealloc(r->buttons, (r->nbuttons+1)*sizeof(Control*));
101                 r->buttons[r->nbuttons] = c;
102                 r->nbuttons++;
103                 r->value = -1;
104                 radioshow(r);
105                 break;
106         case EButton:
107                 if (cp->sender == nil)
108                         ctlerror("%q: senderless buttonevent: %q", r->name, cp->str);
109                 c = controlcalled(cp->sender);
110                 for(i=0; i<r->nbuttons; i++)
111                         if (c == r->buttons[i])
112                                 break;
113                 if (i == r->nbuttons)
114                         ctlerror("%q: not my event: %q", r->name, cp->str);
115                 if(cp->iargs[1] == 0){
116                         /* button was turned off; turn it back on */
117                         _ctlprint(c, "value 1");
118                 }else{
119                         r->value = i;
120                         chanprint(r->event, r->format, r->name, i);
121                         radioshow(r);
122                 }
123                 break;
124         case EFormat:
125                 _ctlargcount(r, cp, 2);
126                 r->format = ctlstrdup(cp->args[1]);
127                 break;
128         case EHide:
129                 _ctlargcount(r, cp, 1);
130                 r->hidden = 1;
131                 break;
132         case EFocus:
133                 /* ignore focus change */
134                 break;
135         case ERect:
136                 _ctlargcount(r, cp, 5);
137                 rect.min.x = cp->iargs[1];
138                 rect.min.y = cp->iargs[2];
139                 rect.max.x = cp->iargs[3];
140                 rect.max.y = cp->iargs[4];
141                 r->rect = rect;
142                 break;
143         case EReveal:
144                 _ctlargcount(r, cp, 1);
145                 r->hidden = 0;
146                 radioshow(r);
147                 break;
148         case EShow:
149                 _ctlargcount(r, cp, 1);
150                 radioshow(r);
151                 break;
152         case ESize:
153                 if (cp->nargs == 3)
154                         rect.max = Pt(0x7fffffff, 0x7fffffff);
155                 else{
156                         _ctlargcount(r, cp, 5);
157                         rect.max.x = cp->iargs[3];
158                         rect.max.y = cp->iargs[4];
159                 }
160                 rect.min.x = cp->iargs[1];
161                 rect.min.y = cp->iargs[2];
162                 if(rect.min.x<=0 || rect.min.y<=0 || rect.max.x<=0 || rect.max.y<=0 || rect.max.x < rect.min.x || rect.max.y < rect.min.y)
163                         ctlerror("%q: bad sizes: %s", r->name, cp->str);
164                 r->size.min = rect.min;
165                 r->size.max = rect.max;
166                 break;
167         case EValue:
168                 _ctlargcount(r, cp, 2);
169                 r->value = cp->iargs[1];
170                 radioshow(r);
171                 break;
172         }
173 }
174
175 Control*
176 createradiobutton(Controlset *cs, char *name)
177 {
178         Radio *r;
179
180         r = (Radio*)_createctl(cs, "label", sizeof(Radio), name);
181         r->format = ctlstrdup("%q: value %d");
182         r->value = -1;  /* nobody set at first */
183         r->mouse = radiomouse;
184         r->ctl = radioctl;
185         return (Control*)r;
186 }