Exemple #1
0
static char*
tkscrollset(Tk *tk, char *arg, char **val)
{
	TkTop *t;
	char *e;
	TkScroll *tks = TKobj(TkScroll, tk);

	USED(val);
	t = tk->env->top;
	e = tkfracword(t, &arg, &tks->top, nil);
	if (e != nil)
		return e;
	e = tkfracword(t, &arg, &tks->bot, nil);
	if (e != nil)
		return e;
	if(tks->top < 0)
		tks->top = 0;
	if(tks->top > TKI2F(1))
		tks->top = TKI2F(1);
	if(tks->bot < 0)
		tks->bot = 0;
	if(tks->bot > TKI2F(1))
		tks->bot = TKI2F(1);

	tk->dirty = tkrect(tk, 1);
	return nil;
}
Exemple #2
0
static char*
tkscaledrag(Tk *tk, char *arg, char **val)
{
	int x, y, v;
	char *e, buf[Tkmaxitem], f[32];
	TkScale *tks = TKobj(TkScale, tk);

	USED(val);
	if((tks->flag & Dragging) == 0)
		return nil;
	if(tks->flag & Autorepeat)
		return nil;

	e = tkfracword(tk->env->top, &arg, &x, tk->env);
	if(e != nil)
		return e;
	e = tkfracword(tk->env->top, &arg, &y, tk->env);
	if(e != nil)
		return e;

	if(tks->orient == Tkvertical)
		v = TKF2I(y) + tk->borderwidth;
	else
		v = TKF2I(x) + tk->borderwidth;

	v -= tks->pix;
	x = tks->pixmax-tks->pixmin;
	if (x!=tks->sl)
		v = tks->base + (vlong)v * (tks->to-tks->from)/(x-tks->sl);
	else
		v = tks->base;
	if(tks->res > 0) {
		int a = tks->res / 2;
		if (v < 0)
			a = -a;
		v = ((v+a)/tks->res)*tks->res;
	}

	tks->value = v;
	tkscalecheckvalue(tk);

	if(tks->command != nil && tks->jump != BoolT) {
		tkfprint(f, tks->value);
		snprint(buf, sizeof(buf), "%s %s", tks->command, f);
		e = tkexec(tk->env->top, buf, nil);
	}
	tk->dirty = tkrect(tk, 1);
	return e;
}
Exemple #3
0
static char*
tkscalecoords(Tk *tk, char *arg, char **val)
{
	int p, x, y, l, value;
	TkScale *tks = TKobj(TkScale, tk);
	char *e;

	value = tks->value;
	if(arg != nil && arg[0] != '\0') {
		e = tkfracword(tk->env->top, &arg, &value, tk->env);
		if (e != nil)
			return e;
	}

	value -= tks->from;
	p = tks->pixmax - tks->pixmin;
	l = TKF2I(tks->to-tks->from);
	if (l==0)
		p /= 2;
	else
		p = TKF2I(value*p/l);
	p += tks->pixmin;
	if(tks->orient == Tkvertical) {
		x = tks->center;
		y = p;
	}
	else {
		x = p;
		y = tks->center;
	}
	return tkvalue(val, "%d %d", x, y);
}
Exemple #4
0
char*
tkscaleposn(TkEnv *env, Tk *tk, char *arg, int *z)
{
	int x, y;
	TkScale *tks = TKobj(TkScale, tk);
	char *e;

	e = tkfracword(env->top, &arg, &x, env);
	if(e != nil)
		return e;
	e = tkfracword(env->top, &arg, &y, env);
	if(e != nil)
		return e;

	x = TKF2I(x) + tk->borderwidth;
	y = TKF2I(y) + tk->borderwidth;

	if(tks->orient == Tkvertical) {
		if(z != nil) {
			z[0] = x;
			z[1] = y;
		}
		x = y;
	}
	else {
		if(z != nil) {
			z[0] = y;
			z[1] = x;
		}
	}
	if(x > tks->pixmin && x < tks->pixpos)
		return trough1;
	else if(x >= tks->pixpos && x < tks->pixpos+tks->sl+2*ScaleBW)
		return slider;
	else if(x >= tks->pixpos+tks->sl+2*ScaleBW && x < tks->pixmax)
		return trough2;

	return "";
}
Exemple #5
0
static char*
tkcvsmove(Tk *tk, char *arg, char **val)
{
	TkCtag *t;
	int fx, fy;
	TkTop *top;
	TkCpoints *p;
	TkName *tag;
	Rectangle *u;
	TkCitem *item;
	char *e;
	char buf[Tkmaxitem];

	USED(val);
	top = tk->env->top;
	arg = tkword(top, arg, buf, buf+sizeof(buf), nil);
	tag = tkctaglook(tk, nil, buf);
	if(tag == nil)
		return nil;

	e = tkfracword(top, &arg, &fx, nil);
	if (e != nil)
		return e;
	e = tkfracword(top, &arg, &fy, nil);
	if(e != nil)
		return e;

	u = &TKobj(TkCanvas, tk)->update;
	for(t = tag->obj; t; t = t->taglist) {
		item = t->item;
		p = &item->p;
		tkbbmax(u, &p->bb);
		tkcimethod[item->type].coord(item, nil, fx, fy);
		tkbbmax(u, &p->bb);
	}
	tkcvssetdirty(tk);
	return nil;
}
Exemple #6
0
static char*
tkscaleget(Tk *tk, char *arg, char **val)
{
	int x, y, value, v, l;
	char buf[Tkminitem], *e;
	TkScale *tks = TKobj(TkScale, tk);

	value = tks->value;
	if(arg[0] != '\0') {
		e = tkfracword(tk->env->top, &arg, &x, tk->env);
		if (e != nil)
			return e;
		e = tkfracword(tk->env->top, &arg, &y, tk->env);
		if (e != nil)
			return e;
		if(tks->orient == Tkvertical)
			v = TKF2I(y) + tk->borderwidth;
		else
			v = TKF2I(x) + tk->borderwidth;

		if(v < tks->pixmin)
			value = tks->from;
		else
		if(v > tks->pixmax)
			value = tks->to;
		else {
			l = tks->pixmax-tks->pixmin;
			value = 0;
			if (l!=0)
				value = v * ((tks->to-tks->from)/l);
			value += tks->from;
		}
		if(tks->res > 0)
			value = (value/tks->res)*tks->res;
	}
	tkfprint(buf, value);
	return tkvalue(val, "%s", buf);
}
Exemple #7
0
static char*
tkscaleset(Tk *tk, char *arg, char **val)
{
	TkScale *tks = TKobj(TkScale, tk);
	char *e;

	USED(val);

	e = tkfracword(tk->env->top, &arg, &tks->value, tk->env);
	if (e != nil)
		return e;
	tkscalecheckvalue(tk);
	tk->dirty = tkrect(tk, 1);
	return nil;		
}
Exemple #8
0
static char*
tkcvssee(Tk *tk, char *arg, char **val)
{
	Rectangle r;
	int n, coords[4];
	char *e;

	USED(val);
	n = 0;
	while (n < 4) {
		if (*arg == '\0')
			break;
		e = tkfracword(tk->env->top, &arg, &coords[n++], nil);
		if (e != nil)
			return e;
	}

	if (n != 2 && n != 4)
		return TkFewpt;

	r.min.x = TKF2I(coords[0]);
	r.min.y = TKF2I(coords[1]);
	if (n == 4) {
		r.max.x = TKF2I(coords[2]);
		r.max.y = TKF2I(coords[3]);
	} else
		r.max = r.min;
	r = canonrect(r);
	/*
	 * XXX should intersect r with scrollregion here, as you shouldn't
	 * be able to display things outside the scroll region. (??)
	 */

	tkcvsseerect(tk, r, r.min);
	return nil;
}
Exemple #9
0
char*
tkcvstags(Tk *tk, char *arg, char **val, int af)
{
	TkTop *o;
	int x, y;
	TkName *f;
	TkCtag *t, *tt;
	char *fmt;
	TkCpoints p;
	TkCanvas *c;
	TkCitem *i, *b;
	int d, dist, dx, dy;
	char tag[Tkmaxitem], buf[Tkmaxitem];
	char *e;

	USED(val);

	c = TKobj(TkCanvas, tk);

	o = tk->env->top;
	if(af == TkCadd) {
		arg = tkword(o, arg, tag, tag+sizeof(tag), nil);
		if(tag[0] == '\0' || (tag[0] >= '0' && tag[0] <= '9'))
			return TkBadtg;
	}

	fmt = "%d";
	arg = tkword(o, arg, buf, buf+sizeof(buf), nil);
	if(strcmp(buf, "above") == 0) {
		tkword(o, arg, buf, buf+sizeof(buf), nil);
		f = tkctaglook(tk, nil, buf);
		if(f == nil)
			return TkBadtg;

		t = tkclasttag(c->head, f->obj);
		if(t == nil)
			return TkBadtg;

		for(i = t->item->next; i; i = i->next) {
			if(af == TkCadd) {
				i->tags = tkmkname(tag);
				if(i->tags == nil)
					return TkNomem;
				tkcaddtag(tk, i, 0);
			}
			else {
				e = tkvalue(val, fmt, i->id);
				if(e != nil)
					return e;
				fmt = " %d";
			}
		}
		return nil;
	}

	if(strcmp(buf, "all") == 0) {
		for(i = c->head; i; i = i->next) {
			if(af == TkCadd) {
				i->tags = tkmkname(tag);
				if(i->tags == nil)
					return TkNomem;
				tkcaddtag(tk, i, 0);
			}
			else {
				e = tkvalue(val, fmt, i->id);
				if(e != nil)
					return e;
				fmt = " %d";
			}
		}
		return nil;
	}

	if(strcmp(buf, "below") == 0) {
		tkword(o, arg, buf, buf+sizeof(buf), nil);
		f = tkctaglook(tk, nil, buf);
		if(f == nil)
			return TkBadtg;
		tt = f->obj;
		for(b = c->head; b; b = b->next) {
			for(t = tt; t; t = t->itemlist)
				if(t->item == b)
					goto found;
		}
	found:
		for(i = c->head; i != b; i = i->next) {
			if(af == TkCadd) {
				i->tags = tkmkname(tag);
				if(i->tags == nil)
					return TkNomem;
				tkcaddtag(tk, i, 0);
			}
			else {
				e = tkvalue(val, fmt, i->id);
				if(e != nil)
					return e;
				fmt = " %d";
			}
		}
		return nil;
	}

	if(strcmp(buf, "closest") == 0) {
		e = tkfracword(o, &arg, &x, nil);
		if (e == nil)
			e = tkfracword(o, &arg, &y, nil);
		if (e != nil)
			return e;
		if(*arg != '\0')
			return "!not implemented";

		x = TKF2I(x);
		y = TKF2I(y);
		i = nil;
		dist = 0;
		for(b = c->head; b != nil; b = b->next) {
			dx = x - (b->p.bb.min.x + Dx(b->p.bb)/2);
			dy = y - (b->p.bb.min.y + Dy(b->p.bb)/2);
			d = dx*dx + dy*dy;
			if(d < dist || dist == 0) {
				i = b;
				dist = d;
			}
		}
		if(i == nil)
			return nil;

		if(af == TkCadd) {
			i->tags = tkmkname(tag);
			if(i->tags == nil)
				e = TkNomem;
			else
				tkcaddtag(tk, i, 0);
		}
		else
			e = tkvalue(val, fmt, i->id);
		return e;
	}

	if(strcmp(buf, "withtag") == 0) {
		tkword(o, arg, buf, buf+sizeof(buf), nil);
		f = tkctaglook(tk, nil, buf);
		if(f == nil)
			return TkBadtg;
		for(t = f->obj; t; t = t->taglist) {
			i = t->item;
			if(af == TkCadd) {
				i->tags = tkmkname(tag);
				if(i->tags == nil)
					return TkNomem;
				tkcaddtag(tk, i, 0);
			}
			else {
				e = tkvalue(val, fmt, i->id);
				if(e != nil)
					return e;
				fmt = " %d";
			}
		}
		return nil;
	}

	if(strcmp(buf, "enclosed") == 0) {
		e = tkparsepts(o, &p, &arg, 0);
		if(e != nil)
			goto done;
		if(p.npoint != 2) {
			e = TkFewpt;
			goto done;
		}
		for(i = c->head; i; i = i->next) {
			if(rectinrect(i->p.bb, p.bb)) {
				if(af == TkCadd) {
					i->tags = tkmkname(tag);
					if(i->tags == nil) {
						e = TkNomem;
						goto done;
					}
					tkcaddtag(tk, i, 0);
				}
				else {
					e = tkvalue(val, fmt, i->id);
					if(e != nil)
						goto done;
					fmt = " %d";
				}
			}
		}
		goto done;
	}

	if(strcmp(buf, "overlapping") == 0) {
		e = tkparsepts(o, &p, &arg, 0);
		if(e != nil)
			goto done;
		if(p.npoint != 2) {
			e = TkFewpt;
			goto done;
		}
		for(i = c->head; i; i = i->next) {
			if(rectXrect(i->p.bb, p.bb)) {
				if(af == TkCadd) {
					i->tags = tkmkname(tag);
					if(i->tags == nil) {
						e = TkNomem;
						goto done;
					}
					tkcaddtag(tk, i, 0);
				}
				else {
					e = tkvalue(val, "%d ", i->id);
					if(e != nil)
						goto done;
				}
			}
		}
		goto done;
	}

	return TkBadcm;

done: 		 /* both no error and error do the same thing */
	tkfreepoint(&p);
	return e;
}
Exemple #10
0
static char*
tkentryxview(Tk *tk, char *arg, char **val)
{
	int locked;
	TkEnv *env;
	TkEntry *tke;
	char *buf, *v;
	int dx, top, bot, amount, ix, x;
	char *e;

	tke = TKobj(TkEntry, tk);
	env = tk->env;
	dx = tk->act.width - 2*xinset(tk);

	buf = mallocz(Tkmaxitem, 0);
	if(buf == nil)
		return TkNomem;

	if(*arg == '\0') {
		if (tke->textlen == 0 || tke->xlen < dx) {
			bot = TKI2F(0);
			top = TKI2F(1);
		} else {
			bot = TKI2F(tke->x0) / tke->xlen;
			top = TKI2F(tke->x0 + dx) / tke->xlen;
		}
		v = tkfprint(buf, bot);
		*v++ = ' ';
		tkfprint(v, top);
		e = tkvalue(val, "%s", buf);
		free(buf);
		return e;
	}

	arg = tkitem(buf, arg);
	if(strcmp(buf, "moveto") == 0) {
		e = tkfracword(env->top, &arg, &top, nil);
		if (e != nil) {
			free(buf);
			return e;
		}
		tke->x0 = TKF2I(top*tke->xlen);
	}
	else
	if(strcmp(buf, "scroll") == 0) {
		arg = tkitem(buf, arg);
		amount = atoi(buf);
		if(*arg == 'p')		/* Pages */
			amount *= (9*tke->xlen)/10;
		else
		if(*arg == 's') {		/* Inferno-ism, "scr", must be used in the context of button2p */
			x = amount;
			amount = x < tke->oldx ? env->wzero : (x > tke->oldx ? -env->wzero : 0);
			tke->oldx = x;
		}
		tke->x0 += amount;
	}
	else {
		e = tkentryparseindex(tk, buf, &ix);
		if(e != nil) {
			free(buf);
			return e;
		}
		locked = lockdisplay(env->top->display);
		tke->x0 = entrytextwidth(tk, ix);
		if (locked)
			unlockdisplay(env->top->display);
	}
	free(buf);

	if (tke->x0 > tke->xlen - dx)
		tke->x0 = tke->xlen - dx;
	if (tke->x0 < 0)
		tke->x0 = 0;
	recalcentry(tk);
	e = tkentrysh(tk);
	blinkreset(tk);
	tk->dirty = tkrect(tk, 1);
	return e;
}