9 typedef struct Textbutton Textbutton;
20 CImage *pressedtextcolor;
21 CImage *paletextcolor;
56 static char *cmds[] = {
59 [EBordercolor] = "bordercolor",
67 [EPaletextcolor] ="paletextcolor",
68 [EPressedtextcolor] ="pressedtextcolor",
74 [ETextcolor] = "textcolor",
79 static void textbuttonshow(Textbutton*);
82 textbuttonmouse(Control *c, Mouse *m)
88 if (ptinrect(m->xy,t->rect)) {
100 if((m->buttons&7) != t->lastbut){
102 t->prepress = t->pressed;
108 }else{ /* generate event on button up */
109 if (ptinrect(m->xy,t->rect))
110 chanprint(t->event, t->format, t->name, t->pressed);
113 t->pressed = t->prepress;
118 t->lastbut = m->buttons & 7;
122 textbuttonfree(Control *c)
128 _putctlfont(t->font);
129 _putctlimage(t->image);
130 _putctlimage(t->light);
131 _putctlimage(t->mask);
132 _putctlimage(t->textcolor);
133 _putctlimage(t->bordercolor);
134 _putctlimage(t->paletextcolor);
135 _putctlimage(t->pressedtextcolor);
136 for(i=0; i<t->nline; i++)
142 textbuttonshow(Textbutton *t)
150 if(t->hidden || (t->lastshow == t->pressed && t->showoff == t->off))
153 draw(t->screen, t->rect, t->image->image, nil, t->image->image->r.min);
155 border(t->screen, t->rect, t->border, t->bordercolor->image, ZP);
158 for(i=0; i<t->nline; i++){
159 w = stringwidth(f, t->line[i]);
163 dy = t->nline*f->height;
164 clipr = insetrect(t->rect, t->border);
165 p = _ctlalignpoint(clipr, dx, dy, t->align);
166 im = t->textcolor->image;
168 im = t->paletextcolor->image;
170 im = t->pressedtextcolor->image;
171 for(i=0; i<t->nline; i++){
174 r.max.y = p.y+f->height;
175 q = _ctlalignpoint(r, stringwidth(f, t->line[i]), f->height, t->align%3);
176 _string(t->screen, q, im,
177 ZP, f, t->line[i], nil, strlen(t->line[i]),
178 clipr, nil, ZP, SoverD);
181 if(t->off || t->pressed)
182 draw(t->screen, t->rect, t->light->image, t->mask->image, t->mask->image->r.min);
183 t->lastshow = t->pressed;
185 flushimage(display, 1);
189 textbuttonctl(Control *c, CParse *cp)
196 cmd = _ctllookup(cp->args[0], cmds, nelem(cmds));
199 ctlerror("%q: unrecognized message '%s'", t->name, cp->str);
202 _ctlargcount(t, cp, 2);
203 t->align = _ctlalignment(cp->args[1]);
204 t->lastshow = -1; /* force redraw */
207 _ctlargcount(t, cp, 2);
208 t->border = cp->iargs[1];
209 t->lastshow = -1; /* force redraw */
212 _ctlargcount(t, cp, 2);
213 _setctlimage(t, &t->bordercolor, cp->args[1]);
214 t->lastshow = -1; /* force redraw */
219 _ctlargcount(t, cp, 2);
220 _setctlfont(t, &t->font, cp->args[1]);
221 t->lastshow = -1; /* force redraw */
224 _ctlargcount(t, cp, 2);
225 t->format = ctlstrdup(cp->args[1]);
228 _ctlargcount(t, cp, 1);
232 _ctlargcount(t, cp, 2);
233 _setctlimage(t, &t->image, cp->args[1]);
234 t->lastshow = -1; /* force redraw */
237 _ctlargcount(t, cp, 2);
238 _setctlimage(t, &t->light, cp->args[1]);
239 t->lastshow = -1; /* force redraw */
242 _ctlargcount(t, cp, 2);
243 _setctlimage(t, &t->mask, cp->args[1]);
244 t->lastshow = -1; /* force redraw */
247 _ctlargcount(t, cp, 2);
248 _setctlimage(t, &t->paletextcolor, cp->args[1]);
249 t->lastshow = -1; /* force redraw */
251 case EPressedtextcolor:
252 _ctlargcount(t, cp, 2);
253 _setctlimage(t, &t->pressedtextcolor, cp->args[1]);
254 t->lastshow = -1; /* force redraw */
257 _ctlargcount(t, cp, 5);
258 r.min.x = cp->iargs[1];
259 r.min.y = cp->iargs[2];
260 r.max.x = cp->iargs[3];
261 r.max.y = cp->iargs[4];
262 if(Dx(r)<=0 || Dy(r)<=0)
263 ctlerror("%q: bad rectangle: %s", t->name, cp->str);
265 t->lastshow = -1; /* force redraw */
268 _ctlargcount(t, cp, 1);
270 t->lastshow = -1; /* force redraw */
274 _ctlargcount(t, cp, 1);
275 t->lastshow = -1; /* force redraw */
280 r.max = Pt(0x7fffffff, 0x7fffffff);
282 _ctlargcount(t, cp, 5);
283 r.max.x = cp->iargs[3];
284 r.max.y = cp->iargs[4];
286 r.min.x = cp->iargs[1];
287 r.min.y = cp->iargs[2];
288 if(r.min.x<=0 || r.min.y<=0 || r.max.x<=0 || r.max.y<=0 || r.max.x < r.min.x || r.max.y < r.min.y)
289 ctlerror("%q: bad sizes: %s", t->name, cp->str);
294 /* free existing text */
295 for(i=0; i<t->nline; i++)
297 t->nline = cp->nargs-1;
298 t->line = ctlrealloc(t->line, t->nline*sizeof(char*));
299 for(i=0; i<t->nline; i++)
300 t->line[i] = ctlstrdup(cp->args[i+1]);
301 t->lastshow = -1; /* force redraw */
305 _ctlargcount(t, cp, 2);
306 _setctlimage(t, &t->textcolor, cp->args[1]);
307 t->lastshow = -1; /* force redraw */
310 _ctlargcount(t, cp, 2);
311 if((cp->iargs[1]!=0) != t->pressed){
320 createtextbutton(Controlset *cs, char *name)
324 t = (Textbutton *)_createctl(cs, "textbutton", sizeof(Textbutton), name);
325 t->line = ctlmalloc(sizeof(char*));
327 t->image = _getctlimage("white");
328 t->light = _getctlimage("yellow");
329 t->mask = _getctlimage("opaque");
330 t->bordercolor = _getctlimage("black");
331 t->textcolor = _getctlimage("black");
332 t->pressedtextcolor = _getctlimage("black");
333 t->paletextcolor = _getctlimage("paleyellow");
334 t->font = _getctlfont("font");
335 t->format = ctlstrdup("%q: value %d");
337 t->mouse = textbuttonmouse;
338 t->ctl = textbuttonctl;
339 t->exit = textbuttonfree;