summaryrefslogtreecommitdiff
path: root/libtk/panel.c
blob: f482a79647cb91594cb30d855c7e4a3a9609be51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "lib9.h"
#include "draw.h"
#include "tk.h"

#define	O(t, e)		((long)(&((t*)0)->e))

typedef struct TkPanel TkPanel;
struct TkPanel
{
	Image*	image;
	Image*	matte;
	Point		view;	/* vector from image origin to widget origin */
	Rectangle		r;		/* drawn rectangle (in image coords) */
	int		anchor;
	int		hasalpha;	/* does the image include an alpha channel? */
};

static TkOption tkpanelopts[] =
{
	"anchor",	OPTflag,	O(TkPanel, anchor),	tkanchor,
	nil
};

static int
tkdrawnrect(Image *image, Image *matte, Rectangle *r)
{
	*r = image->clipr;
	if (matte != nil) {
		if (!rectclip(r, matte->clipr))
			return 0;
		if (!matte->repl && !rectclip(r, matte->r))
			return 0;
	}
	if (!image->repl && !rectclip(r, image->r))
		return 0;
	return 1;
}

char*
tkpanel(TkTop *t, char *arg, char **ret)
{
	TkOptab tko[3];
	Tk *tk;
	TkPanel *tkp;
	TkName *names;
	char *e;

	tk = tknewobj(t, TKpanel, sizeof(Tk)+sizeof(TkPanel));
	if(tk == nil)
		return TkNomem;

	tkp = TKobj(TkPanel, tk);
	tkp->anchor = Tkcenter;

	tko[0].ptr = tk;
	tko[0].optab = tkgeneric;
	tko[1].ptr = tkp;
	tko[1].optab = tkpanelopts;
	tko[2].ptr = nil;
	names = nil;

	e = tkparse(t, arg, tko, &names);
	if(e != nil) {
		tkfreeobj(tk);
		return e;
	}

	tksettransparent(tk, tkhasalpha(tk->env, TkCbackgnd));

	e = tkaddchild(t, tk, &names);

	tkfreename(names);
	if (e != nil) {
		tkfreeobj(tk);
		return e;
	}

	tk->name->link = nil;
	return tkvalue(ret, "%s", tk->name->name);
}

void
tkgetpanelimage(Tk *tk, Image **i, Image **m)
{
	TkPanel *tkp = TKobj(TkPanel, tk);
	*i = tkp->image;
	*m = tkp->matte;
}

void
tksetpanelimage(Tk *tk, Image *image, Image *matte)
{
	TkPanel *tkp = TKobj(TkPanel, tk);
	int ishuge;
	TkGeom g;

	g = tk->req;

	tkp->image = image;
	tkp->matte = matte;

	if (!tkdrawnrect(image, matte, &tkp->r)) {
		tkp->r.min = image->r.min;
		tkp->r.max = image->r.min;
	}

	tkp->view = tkp->r.min;		/* XXX do we actually want to keep the old one? */
	/*
	 * if both image and matte are replicated, then we've got no idea what
	 * the rectangle should be, so request zero size, and set origin to (0, 0).
	 */
	ishuge = (Dx(tkp->r) >= 10000000);
	if((tk->flag & Tksetwidth) == 0){
		if(ishuge)
			tk->req.width = 0;
		else
			tk->req.width = Dx(tkp->r);
	}
	if(ishuge)
		tkp->view.x = 0;

	ishuge = (Dy(tkp->r) >= 10000000);
	if((tk->flag & Tksetheight) == 0){
		if(ishuge)
			tk->req.height = 0;
		else
			tk->req.height = Dy(tkp->r);
	}
	if(ishuge)
		tkp->view.y = 0;

	tkp->hasalpha = tkchanhastype(image->chan, CAlpha);
	tkgeomchg(tk, &g, tk->borderwidth);
	tksettransparent(tk, tkp->hasalpha || tkhasalpha(tk->env, TkCbackgnd));
	tk->dirty = tkrect(tk, 0);
}

static void
tkfreepanel(Tk *tk)
{
	TkPanel *tkp = TKobj(TkPanel, tk);
	tkdelpanelimage(tk->env->top, tkp->image);
	tkdelpanelimage(tk->env->top, tkp->matte);
}

static Point
tkpanelview(Tk *tk)
{
	int dx, dy;
	Point view;
	TkPanel *tkp = TKobj(TkPanel, tk);
	
	dx = tk->act.width - Dx(tkp->r);
	dy = tk->act.height - Dy(tkp->r);

	view = tkp->view;

	if (dx > 0) {
		if((tkp->anchor & (Tkeast|Tkwest)) == 0)
			view.x -= dx/2;
		else
		if(tkp->anchor & Tkeast)
			view.x -= dx;
	}
	if (dy > 0) {
		if((tkp->anchor & (Tknorth|Tksouth)) == 0)
			view.y -= dy/2;
		else
		if(tkp->anchor & Tksouth)
			view.y -= dy;
	}
	return view;
}

static char*
tkdrawpanel(Tk *tk, Point orig)
{
	Rectangle r, pr;
	TkPanel *tkp = TKobj(TkPanel, tk);
	Image *i;
	int any;
	Point view, p;

	i = tkimageof(tk);
	if (i == nil)
		return nil;

	p.x = orig.x + tk->act.x + tk->borderwidth;
	p.y = orig.y + tk->act.y + tk->borderwidth;

	view = tkpanelview(tk);

	/*
	 * if the image doesn't fully cover the dirty rectangle, then
	 * paint some background in there
	 */
	r = rectsubpt(tkp->r, view);		/* convert to widget coords */
	pr = tkrect(tk, 0);
	any = rectclip(&r, pr);				/* clip to inside widget borders */

	if (!any || tkp->hasalpha || !rectinrect(tk->dirty, r))
		draw(i, rectaddpt(tk->dirty, p), tkgc(tk->env, TkCbackgnd), nil, ZP);

	if (any && rectclip(&r, tk->dirty))
		draw(i, rectaddpt(r, p), tkp->image, tkp->matte, addpt(r.min, view));

	if (!rectinrect(tk->dirty, pr)) {
		p.x -= tk->borderwidth;
		p.y -= tk->borderwidth;
		tkdrawrelief(i, tk, p, TkCbackgnd, tk->relief);
	}
	return nil;
}

static char*
tkpanelcget(Tk *tk, char *arg, char **val)
{
	TkOptab tko[3];
	TkPanel *tkp = TKobj(TkPanel, tk);

	tko[0].ptr = tk;
	tko[0].optab = tkgeneric;
	tko[1].ptr = tkp;
	tko[1].optab = tkpanelopts;
	tko[2].ptr = nil;

	return tkgencget(tko, arg, val, tk->env->top);
}

static char*
tkpanelcvt(Tk *tk, char *arg, int rel, int *p)
{
	char buf[Tkmaxitem];

	tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil);
	if(buf[0] == '\0')
		return TkBadvl;
	*p = atoi(buf) + rel;
	return nil;
}

/*
 * screen to image
 */
static char*
tkpanelpanelx(Tk *tk, char *arg, char **val)
{
	Point p;
	char *e;

	USED(val);
	p = subpt(tkposn(tk), tkpanelview(tk));
	e = tkpanelcvt(tk, arg, -p.x, &p.x);
	if (e != nil)
		return e;
	return tkvalue(val, "%d", p.x);
}

static char*
tkpanelpanely(Tk *tk, char *arg, char **val)
{
	Point p;
	char *e;

	USED(val);
	p = subpt(tkposn(tk), tkpanelview(tk));
	e = tkpanelcvt(tk, arg, -p.y, &p.y);
	if (e != nil)
		return e;
	return tkvalue(val, "%d", p.y);
}

/*
 * image to screen
 */
static char*
tkpanelscreenx(Tk *tk, char *arg, char **val)
{
	Point p;
	char *e;

	USED(val);
	p = subpt(tkposn(tk), tkpanelview(tk));
	e = tkpanelcvt(tk, arg, p.x, &p.x);
	if (e != nil)
		return e;
	return tkvalue(val, "%d", p.x);
}

static char*
tkpanelscreeny(Tk *tk, char *arg, char **val)
{
	Point p;
	char *e;

	USED(val);
	p = subpt(tkposn(tk), tkpanelview(tk));
	e = tkpanelcvt(tk, arg, p.y, &p.y);
	if (e != nil)
		return e;
	return tkvalue(val, "%d", p.y);
}

static char*
tkpanelconf(Tk *tk, char *arg, char **val)
{
	char *e;
	TkGeom g;
	int bd;
	TkOptab tko[3];
	TkPanel *tkp = TKobj(TkPanel, tk);

	tko[0].ptr = tk;
	tko[0].optab = tkgeneric;
	tko[1].ptr = tkp;
	tko[1].optab = tkpanelopts;
	tko[2].ptr = nil;

	if(*arg == '\0')
		return tkconflist(tko, val);

	g = tk->req;
	bd = tk->borderwidth;
	e = tkparse(tk->env->top, arg, tko, nil);
	tkgeomchg(tk, &g, bd);
	tksettransparent(tk, tkp->hasalpha || tkhasalpha(tk->env, TkCbackgnd));

	tk->dirty = tkrect(tk, 1);

	return e;
}

static char*
tkpaneldirty(Tk *tk, char *arg, char **val)
{
	char buf[Tkmaxitem];
	int n, coords[4];
	Rectangle r;
	char *e, *p;
	TkPanel *tkp = TKobj(TkPanel, tk);

	USED(val);
	n = 0;
	while (n < 4) {
		arg = tkword(tk->env->top, arg, buf, buf+sizeof(buf), nil);
		if (buf[0] == 0)
			break;
		p = buf;
		e = tkfrac(&p, &coords[n++], nil);
		if (e != nil)
			return TkBadvl;
	}
	if (n == 0)
		r = tkp->r;
	else {
		 if (n != 4)
			return TkBadvl;
		r.min.x = TKF2I(coords[0]);
		r.min.y = TKF2I(coords[1]);
		r.max.x = TKF2I(coords[2]);
		r.max.y = TKF2I(coords[3]);
	}
	if (rectclip(&r, tkp->r)) {
		r = rectsubpt(r, tkpanelview(tk));		/* convert to widget coords */
		if (rectclip(&r, tkrect(tk, 0)))			/* clip to visible area */
			combinerect(&tk->dirty, r);
	}
	return nil;
}

static char*
tkpanelorigin(Tk *tk, char *arg, char **val)
{
	char *e;
	Point view;
	TkPanel *tkp = TKobj(TkPanel, tk);

	e = tkxyparse(tk, &arg, &view);
	if (e != nil) {
		if (e == TkOparg)
			return tkvalue(val, "%d %d", tkp->view.x, tkp->view.y);
		return e;
	}
	tkp->view = view;
	tk->dirty = tkrect(tk, 0);
	return nil;
}

static
TkCmdtab tkpanelcmd[] =
{
	"cget",			tkpanelcget,
	"configure",		tkpanelconf,
	"dirty",			tkpaneldirty,
	"origin",			tkpanelorigin,
	"panelx",			tkpanelpanelx,
	"panely",			tkpanelpanely,
	"screenx",			tkpanelscreenx,
	"screeny",			tkpanelscreeny,
	nil
};

TkMethod panelmethod = {
	"panel",
	tkpanelcmd,
	tkfreepanel,
	tkdrawpanel
};