1 /* use button 3 for a proper function to the application, that is not for plumber
2 * as default control(2) supposes.
3 * codes are mostly from /sys/src/libcontrol/textbutton.c
13 typedef struct Textbutton3 Textbutton3;
24 CImage *pressedtextcolor;
64 static char *cmds[] = {
67 [EBordercolor] = "bordercolor",
75 [EPressedtextcolor] ="pressedtextcolor",
81 [ETextcolor] = "textcolor",
83 [EDisable] = "disable",
85 [EGettext] = "gettext",
90 static void textbutton3show(Textbutton3 *);
93 textbutton3mouse(Control *c, Mouse *m)
99 if((m->buttons&1) == 1 && (t->lastbut&1) == 0){
102 t->lastbut = m->buttons & 1;
103 }else if((m->buttons&1) == 0 && (t->lastbut&1) == 1){
104 if(t->gettextflg == 0)
105 chanprint(t->event, t->format, t->name, t->pressed, m->xy.x, m->xy.y);
107 chanprint(t->event, "%q: value %q", t->name, t->line[0]);
110 t->lastbut = m->buttons & 1;
114 if((m->buttons&2) == 2 && (t->lastbut&2) == 0){
117 t->lastbut = m->buttons & 2;
118 }else if((m->buttons&2) == 0 && (t->lastbut&2) == 2){
119 if(t->gettextflg == 0)
120 chanprint(t->event, t->format, t->name, t->pressed, m->xy.x, m->xy.y);
122 chanprint(t->event, "%q: value %q", t->name, t->line[0]);
125 t->lastbut = m->buttons & 2;
129 if((m->buttons&4) == 4 && (t->lastbut&4) == 0){
132 t->lastbut = m->buttons & 4;
133 }else if((m->buttons&4) == 0 && (t->lastbut&4) == 4){
134 if(t->gettextflg == 0)
135 chanprint(t->event, t->format, t->name, t->pressed, m->xy.x, m->xy.y);
137 chanprint(t->event, "%q: value %q", t->name, t->line[0]);
140 t->lastbut = m->buttons & 4;
146 textbutton3free(Control *c)
152 _putctlfont(t->font);
153 _putctlimage(t->image);
154 _putctlimage(t->light);
155 _putctlimage(t->mask);
156 _putctlimage(t->textcolor);
157 _putctlimage(t->bordercolor);
158 _putctlimage(t->pressedtextcolor);
159 for(i=0; i<t->nline; i++)
165 textbutton3show(Textbutton3 *t)
173 if(t->hidden || t->lastshow == t->pressed)
176 draw(t->screen, t->rect, t->image->image, nil, t->image->image->r.min);
177 if(t->pressed || t->toggle)
178 draw(t->screen, t->rect, t->light->image, t->mask->image, t->mask->image->r.min);
180 border(t->screen, t->rect, t->border, t->bordercolor->image, ZP);
183 for(i=0; i<t->nline; i++){
184 w = stringwidth(f, t->line[i]); /*****/
188 dy = t->nline*f->height;
189 clipr = insetrect(t->rect, t->border);
190 p = _ctlalignpoint(clipr, dx, dy, t->align);
191 im = t->textcolor->image;
193 im = t->pressedtextcolor->image;
194 for(i=0; i<t->nline; i++){
197 r.max.y = p.y+f->height;
198 q = _ctlalignpoint(r, stringwidth(f, t->line[i]), f->height, t->align%3);
199 _string(t->screen, q, im,
200 ZP, f, t->line[i], nil, strlen(t->line[i]),
201 clipr, nil, ZP, SoverD);
204 t->lastshow = t->pressed;
205 flushimage(display, 1);
209 textbutton3ctl(Control *c, CParse *cp)
216 cmd = _ctllookup(cp->args[0], cmds, nelem(cmds));
219 ctlerror("%q: unrecognized message '%s'", t->name, cp->str);
222 _ctlargcount(t, cp, 2);
223 t->align = _ctlalignment(cp->args[1]);
224 t->lastshow = -1; /* force redraw */
227 _ctlargcount(t, cp, 2);
228 t->border = cp->iargs[1];
229 t->lastshow = -1; /* force redraw */
232 _ctlargcount(t, cp, 2);
233 _setctlimage(t, &t->bordercolor, cp->args[1]);
234 t->lastshow = -1; /* force redraw */
239 _ctlargcount(t, cp, 2);
240 _setctlfont(t, &t->font, cp->args[1]);
241 t->lastshow = -1; /* force redraw */
244 _ctlargcount(t, cp, 2);
245 t->format = ctlstrdup(cp->args[1]);
248 _ctlargcount(t, cp, 1);
252 _ctlargcount(t, cp, 2);
253 _setctlimage(t, &t->image, cp->args[1]);
254 t->lastshow = -1; /* force redraw */
257 _ctlargcount(t, cp, 2);
258 _setctlimage(t, &t->light, cp->args[1]);
259 t->lastshow = -1; /* force redraw */
262 _ctlargcount(t, cp, 2);
263 _setctlimage(t, &t->mask, cp->args[1]);
264 t->lastshow = -1; /* force redraw */
266 case EPressedtextcolor:
267 _ctlargcount(t, cp, 2);
268 _setctlimage(t, &t->pressedtextcolor, cp->args[1]);
269 t->lastshow = -1; /* force redraw */
272 _ctlargcount(t, cp, 5);
273 r.min.x = cp->iargs[1];
274 r.min.y = cp->iargs[2];
275 r.max.x = cp->iargs[3];
276 r.max.y = cp->iargs[4];
277 if(Dx(r)<=0 || Dy(r)<=0)
278 ctlerror("%q: bad rectangle: %s", t->name, cp->str);
280 t->lastshow = -1; /* force redraw */
283 _ctlargcount(t, cp, 1);
285 t->lastshow = -1; /* force redraw */
289 _ctlargcount(t, cp, 1);
290 t->lastshow = -1; /* force redraw */
295 r.max = Pt(0x7fffffff, 0x7fffffff);
297 _ctlargcount(t, cp, 5);
298 r.max.x = cp->iargs[3];
299 r.max.y = cp->iargs[4];
301 r.min.x = cp->iargs[1];
302 r.min.y = cp->iargs[2];
303 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)
304 ctlerror("%q: bad sizes: %s", t->name, cp->str);
309 /* free existing text */
310 for(i=0; i<t->nline; i++)
312 t->nline = cp->nargs-1;
313 t->line = ctlrealloc(t->line, t->nline*sizeof(char*));
314 for(i=0; i<t->nline; i++)
315 t->line[i] = ctlstrdup(cp->args[i+1]);
316 t->lastshow = -1; /* force redraw */
320 _ctlargcount(t, cp, 2);
321 _setctlimage(t, &t->textcolor, cp->args[1]);
322 t->lastshow = -1; /* force redraw */
325 _ctlargcount(t, cp, 2);
326 if(strcmp(cp->args[1], "left") == 0)
328 else if(strcmp(cp->args[1], "middle") == 0)
330 else if(strcmp(cp->args[1], "right") == 0)
334 _ctlargcount(t, cp, 2);
335 if(strcmp(cp->args[1], "left") == 0)
337 else if(strcmp(cp->args[1], "middle") == 0)
339 else if(strcmp(cp->args[1], "right") == 0)
343 _ctlargcount(t, cp, 2);
344 if(strcmp(cp->args[1], "on") == 0)
346 else if(strcmp(cp->args[1], "off") == 0)
348 t->lastshow = -1; /* force redraw */
351 _ctlargcount(t, cp, 2);
352 if(strcmp(cp->args[1], "on") == 0)
354 else if(strcmp(cp->args[1], "off") == 0)
358 _ctlargcount(t, cp, 2);
359 if((cp->iargs[1]!=0) != t->pressed){
368 createtextbutton3(Controlset *cs, char *name)
372 t = (Textbutton3 *)_createctl(cs, "textbutton3", sizeof(Textbutton3), name);
373 t->line = ctlmalloc(sizeof(char*));
375 t->image = _getctlimage("white");
376 t->light = _getctlimage("yellow");
377 t->mask = _getctlimage("opaque");
378 t->bordercolor = _getctlimage("black");
379 t->textcolor = _getctlimage("black");
380 t->pressedtextcolor = _getctlimage("black");
381 t->font = _getctlfont("font");
382 t->format = ctlstrdup("%q: value %d %d %d");
384 t->mouse = textbutton3mouse;
385 t->ctl = textbutton3ctl;
386 t->exit = textbutton3free;