static char* tkcvscanvy(Tk *tk, char *arg, char **val) { int y, s; TkCanvas *c; Point p; char buf[Tkmaxitem]; c = TKobj(TkCanvas, tk); arg = tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil); if(buf[0] == '\0') return TkBadvl; p = tkposn(tk); y = atoi(buf) + c->view.y - (p.y + tk->borderwidth); if(*arg) { tkitem(buf, arg); s = atoi(buf); if (s) { if (y>=0) y = ((y+s/2)/s)*s; else y = ((y-s/2)/s)*s; } } return tkvalue(val, "%d", y); }
static char* tkcvscanvx(Tk *tk, char *arg, char **val) { int x, s; TkCanvas *c; Point p; char buf[Tkmaxitem]; c = TKobj(TkCanvas, tk); arg = tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil); if(buf[0] == '\0') return TkBadvl; p = tkposn(tk); x = atoi(buf) + c->view.x - (p.x + tk->borderwidth); if(*arg) { tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil); s = atoi(buf); if (s) { if (x>=0) x = ((x+s/2)/s)*s; else x = ((x-s/2)/s)*s; } } return tkvalue(val, "%d", x); }
static char * tkcvsscreeny(Tk *tk, char *arg, char **val) { int y; TkCanvas *c; Point p; char buf[Tkmaxitem]; c = TKobj(TkCanvas, tk); tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil); if(buf[0] == '\0') return TkBadvl; p = tkposn(tk); y = atoi(buf) - c->view.y + (p.y + tk->borderwidth); return tkvalue(val, "%d", y); }
Tk* tktextevent(Tk *tk, int event, void *data) { char *e; TkMouse m, vm; TkTitem *f, *tagit; TkText *tkt; TkTindex ix; Tk *dest; Point deltasv; tkt = TKobj(TkText, tk); deltasv = tkposn(tk); deltasv.x += tk->borderwidth + tk->ipad.x/2; deltasv.y += tk->borderwidth + tk->ipad.y/2; dest = nil; if(event == TkLeave && tkt->mouse != nil) { vm.x = 0; vm.y = 0; tktdeliver(tk, tkt->mouse, tkt->mouse, TkLeave, data, deltasv); tkt->mouse = nil; } else if((event & TkKey) == 0 && (event & TkEmouse)) { /* m in S space, tm in V space */ m = *(TkMouse*)data; vm = m; vm.x -= deltasv.x; vm.y -= deltasv.y; if((event & TkMotion) == 0 || m.b == 0) { tkt->current.x = vm.x; tkt->current.y = vm.y; } tktxyind(tk, vm.x, vm.y, &ix); f = ix.item; if(tkt->mouse != f) { tagit = nil; if(tkt->mouse != nil) { if(tktanytags(tkt->mouse)) { e = tktnewitem(TkTascii, tkt->mouse->tagextra, &tagit); if(e != nil) return dest; /* XXX propagate error? */ tkttagcomb(tagit, tkt->mouse, 1); tkttagcomb(tagit, f, -1); } tktdeliver(tk, tkt->mouse, tagit, TkLeave, data, deltasv); if(tagit) free(tagit); tagit = nil; } if(tktanytags(f)) { e = tktnewitem(TkTascii, f->tagextra, &tagit); if(e != nil) return dest; /* XXX propagate error? */ tkttagcomb(tagit, f, 1); if(tkt->mouse) tkttagcomb(tagit, tkt->mouse, -1); } tktdeliver(tk, f, tagit, TkEnter, data, deltasv); tkt->mouse = f; if(tagit) free(tagit); } if(tkt->mouse != nil) dest = tktdeliver(tk, tkt->mouse, tkt->mouse, event, &m, deltasv); } else if(event == TkFocusin) tktextcursor(tk, " insert", (char **) nil); /* pass all "real" events on to parent text widget - DBK */ tksubdeliver(tk, tk->binds, event, data, 0); return dest; }
void tkcmdbind(Tk *tk, int event, char *s, void *data) { Point p; TkMouse *m; TkGeom *g; int v, len; char *e, *c, *ec, *cmd; TkTop *t; if(s == nil) return; cmd = malloc(2*Tkmaxitem); if (cmd == nil) { print("tk: bind command \"%s\": %s\n", tk->name ? tk->name->name : "(noname)", TkNomem); return; } m = (TkMouse*)data; c = cmd; ec = cmd+2*Tkmaxitem-1; while(*s && c < ec) { if(*s != '%') { *c++ = *s++; continue; } s++; len = ec-c; switch(*s++) { def: default: *c++ = s[-1]; break; case '%': *c++ = '%'; break; case 'b': v = 0; if (!(event & TkKey)) { if(event & (TkButton1P|TkButton1R)) v = 1; else if(event & (TkButton2P|TkButton2R)) v = 2; else if(event & (TkButton3P|TkButton3R)) v = 3; } c += snprint(c, len, "%d", v); break; case 'h': if((event & TkConfigure) == 0) goto def; g = (TkGeom*)data; c += snprint(c, len, "%d", g->height); break; case 's': if((event & TkKey)) c += snprint(c, len, "%d", TKKEY(event)); else if((event & (TkEmouse|TkEnter))) c += snprint(c, len, "%d", m->b); else if((event & TkFocusin)) c += snprint(c, len, "%d", (int)data); else goto def; break; case 'w': if((event & TkConfigure) == 0) goto def; g = (TkGeom*)data; c += snprint(c, len, "%d", g->width); break; case 'x': /* Relative mouse coords */ case 'y': if((event & TkKey) || (event & (TkEmouse|TkEnter)) == 0) goto def; p = tkposn(tk); if(s[-1] == 'x') v = m->x - p.x; else v = m->y - p.y; c += snprint(c, len, "%d", v - tk->borderwidth); break; case 'X': /* Absolute mouse coords */ case 'Y': if((event & TkKey) || (event & TkEmouse) == 0) goto def; c += snprint(c, len, "%d", s[-1] == 'X' ? m->x : m->y); break; case 'A': if((event & TkKey) == 0) goto def; v = TKKEY(event); if(v == '{' || v == '}' || v == '\\') c += snprint(c, len, "\\%C", v); else if(v != '\0') c += snprint(c, len, "%C", v); break; case 'K': if((event & TkKey) == 0) goto def; c += snprint(c, len, "%.4X", TKKEY(event)); break; case 'W': if (tk->name != nil) c += snprint(c, len, "%s", tk->name->name); break; } } *c = '\0'; e = nil; t = tk->env->top; t->execdepth = 0; if(cmd[0] == '|') tkexec(t, cmd+1, nil); else if(cmd[0] != '\0') e = tkexec(t, cmd, nil); t->execdepth = -1; if(e == nil) { free(cmd); return; } if(tk->name != nil){ char *s; if(t->errx[0] != '\0') s = tkerrstr(t, e); else s = e; print("tk: bind command \"%s\": %s: %s\n", tk->name->name, cmd, s); if(s != e) free(s); } free(cmd); }