File: tile.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (476 lines) | stat: -rw-r--r-- 9,554 bytes parent folder | download | duplicates (2)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "tile.h"

/* Return true if 'tile' represents a window that
 * should have its resize box filled in
 */
Bool
tile_isdirty(Tile*tile) {
	return data_isdirty(view_data(tile->body));
}

/* Is the tile hidden? */
Bool
tile_hidden(Tile *t)
{
	return t->ishidden || (t->up && tile_hidden(t->up));
}

Bool
tile_invariant(Tile *tile)
{
	int	size ,body,tag, diff;
	Tile	*last;

	assert(tile);

	/* Can't have _both_ a body and and a child */
	assert(!(tile->body && tile->down));	

	if (! tile->ishidden) {
		size = TILESIZE(tile);
		assert( size >= tile->base);
		if ( tile->body ) {

			/* The size of the tile == the sum of the sizes of its body and tag,
			 * except if we're the "bottom" tile, in which case the size of the
			* tile might be a _little_ bit bigger than that sum.
			 */
			body = view_height(tile->body);
			tag = view_height(tile->tag);
			last = last_visible_body(tile, 0);

			if (tile != last) {
				assert(size == body + tag);
			} else {
				/* bottom tile might have a little 'slop' */
				diff = size - (body + tag);
				assert(diff >= 0);
				assert (diff <= tag);
			}
		}
	}

	return true;
}

Bool
list_invariant(Tile *list)
{
	int	prev;
	Tile	*t;
	int	diff;
	
	assert(tile_invariant(list));	/* A list is also a tile */

	assert(!list->body);			/* Lists don't have bodies */

	for (prev = list->cmin, t= list->down; t; t=t->right) {
		assert(tile_invariant(t));	/* Every child is a valid tile */
		if(t->ishidden)
			continue;

		/* Every visible tile abutts the previous one */
		assert(t->min == prev);

		prev = t->max;
	}
	if(list->down) {
		diff = list->cmax - prev;	/* slop at the bottom */
		assert(diff >= 0);
		assert(diff <= list->down->base);
	}
	return true;
}

/* "Snap" the size of 't' to some "neat" value,
 * without increasing 't's size.
 */
static void
quantize(Tile*t)
{
	int	tag, body,size;

	if (!t->body)
		return;	/* only really makes sense for windows */

	size = TILESIZE(t);
	tag = snapheight(t->tag, size);
	assert(size >= tag);	/* or we should have been hidden */

	body = snapheight( t->body, size -tag);
	t->max = t->min + tag  + body;
}

/* Ensure 't' wil fit inside 'list'. */
static void
crop(Tile*t, Tile *list)
{
	int	size = TILESIZE(t);
	int	listsize = LISTSIZE(list);

	assert(t->up == list);
	assert (size >= 0);

	if(size > listsize) {
		t->max = t->min + listsize;
		size = listsize;
	}

	if (t->min < list->cmin)
		moveto(t, list->cmin);
	if (t->max > list->cmax)
		t->max = list->cmax;

	assert (t->max >= t->min);
	assert (t->max <= list->cmax);
	assert (t->min >= list->cmin);
}

/* 't's shape has changed.  Redisplay it, and any of its children */
void
tile_reshaped(Tile *t)
{
	Rectangle r = rectangle(t);

	assert(!t->ishidden);	/* we don't reshape hidden tiles */
	cls(r);
	view_reshaped(t->tag, r);
	if(t->body) {
		r.min.y += view_height(t->tag);
		view_reshaped(t->body, r);
	} else {
		list_reshaped(t,0);
	}
	assert(tile_invariant(t));
}

/* Add 'tile' to 'list' and redisplay the modified 'list'
 * Try to maintain 'tile's min and max, but also try to avoid hiding
 * other tiles.  Make sure that 'tile' ends up with a reasonable amount
 * of space.
 */
void
list_add(Tile *list, Tile *tile)
{
	Tile	*left;
	Tile	*next;
	int	max;

	assert(!list->ishidden);
	assert(!tile->ishidden);
	assert(list_invariant(list));

	if(tile->body) {
		Path	buf;
		Data	*d;

		d = view_data(tile->body);
		data_getlabel(d, buf);
		placedcol( buf, list);
	}

	tile->up = list;
	crop(tile, list);

	if( (left = list_find(list, tile->min)) ) {
		/* Add 'tile' just to the right of 'left' */
		left->max = tile->min;
		if(TILESIZE(left) < tile->base)
			left->ishidden = true;
		else
			quantize(left);
		tile->left = left;
		tile->right = left->right;
		left->right = tile;
		if (tile->right)
			tile->right->left = tile;
	} else {
		assert(!list->down);	/* ...or we would have found a tile */
		tile->right = tile->left = 0;
		list->down = tile;
		tile->min = list->cmin;
		tile->max = list->cmax;
	}

	/* Maybe expand tile->max */
	next = next_visible(tile);
	max = next? next->min : list->cmax;
	tile->max = MAX(tile->max, max);
		
	list_reshaped(list, tile);
}

/* Adjust the position of 't', to try to make sure we don't
 * have to hide all of t's siblings.  On the other hand, don't
 * move 't' _too_ much.
 */
static void
adjust_position(Tile *t)
{
	Tile	*l = t->up;
	int	size, available, before, after, minsize, diff, move;

	before = 	list_basesize(l->down, t);
	after = 	list_basesize(t->right, 0);
	size =	TILESIZE(t);

	/* We're prepared to shrink to half our asked-for size,
	 * or the minimum size for a tile of our type, whichever
	 * is bigger.
	 */
	minsize = MAX(size/2, tile_minsize(t));
	available = size - minsize;

	/* move t->min down a bit? */
	diff = l->cmin + before - t->min;
	if (diff>0) {
		move= MIN(available, diff);
		if(move > 0) {
			t->min += move;
			available -= move;
		}
	}
	
	/* move t->min up a bit? */
	diff = t->max - (l->cmax - after);
	if (diff>0) {
		move= MIN(available, diff);
		if(move > 0) {
			t->max -= move;
		}
	}
}

/* 
* Adjust the sizes of the children, redisplay them if necessary.
* Avoid moving or resizing 't'.  Ensure that 't' remains visible.
* We can assume that all of the
* tiles have the right approximate size, but that's it.
*/
void
list_reshaped(Tile *l, Tile *tile)
{
	Tile	*t;
	int	cmin, cmax;
	Tile	*slop;
	int	diff;

	assert(list_oksizes(l));
	if (tile) {
		crop(tile,l);
		adjust_position(tile);
		adjust_sizes_in_range(l->down, tile, tile->min - l->cmin);
		adjust_sizes_in_range(tile->right, 0, l->cmax - tile->max);
	} else
		adjust_sizes_in_range(l->down, 0, LISTSIZE(l));

	assert(list_oksizes(l));
	assert(list_size(l->down, 0) <= LISTSIZE(l));

	FOR_EACH_VISIBLE(l->down,0) {
		quantize(t);
	}

	diff = LISTSIZE(l) - list_size(l->down, 0);
	assert(diff>=0);

	/* Even things up a little bit */
	if ((slop = last_visible_body(l->down,0)) ||
				(slop = last_visible(l->down, 0)) ) {
		slop->max += diff;
	}

	list_slide(l);

	setcminmax(l, &cmin, &cmax);
	FOR_EACH_TILE(l->down,0) {
		t->cmin = cmin;
		t->cmax = cmax;
		if(!t->ishidden)
			tile_reshaped(t);
	}
	assert(list_invariant(l));
}

/*
 * Find a good place to create a window to display 'path'.
 * Fill *col with the column to contain the window,
 * *min, *max with a suggested position within the column
 */
/* Change the font of 't' and t's children */
void
tile_setfont(Tile *t,char* arg)
{
	if(t->body) {
		view_setfont( t->body, arg);
	} else {
		FOR_EACH_TILE(t->down, 0) {
			tile_setfont(t, arg);
		}
	}
}

int
list_del(Tile*t)
{
	int	retval=0;

	if(ISWIN(t)) {
		retval =  win_del(t);
	} else {
		for(t=t->down; t; t=t->right)
			if (list_del(t))
				retval = -1;
	}
	if(!retval)
		tile_del(t);
	return retval;
}

void
tile_del(Tile *tile)
{
	if(tile->right)
		tile->right->left = tile->left;
	if(tile->left) {
		tile->left->right = tile->right;
		/* Give the preceding window all the space. */
		tile->left->max = tile->max;
	} else {
		tile->up->down = tile->right;
	}
	list_unhide(tile->up);
	cls(rectangle(tile));
	list_reshaped(tile->up, 0);
}

/* Find a new parent for 'tile' somewhere at point 'p' */
Tile*
newparent(Tile*tile, Point p)
{
	Tile	*t = point2tile(wily, p);
	assert(t);
	while((t->ori == tile->ori) || t->body)
		t = t->up;
	return t;
}

void
tile_move(Tile *tile, Point p)
{
	Tile	*parent;
	int	dest;

	/* Make sure we'll have someplace worth moving to */
	parent = point2tile(wily,p);
	if (parent == 0 || parent == wily)
		return;

	tile_del(tile);
	dest = (tile->ori == H) ? p.x : p.y;
	if (tile->min < dest && dest < tile->max)
		tile->min = dest;
	else
		moveto(tile, dest);

	parent = newparent(tile, p);
	list_add(parent, tile);
	cursorset(buttonpos(tile));
}

void
tile_show(Tile *tile) {
	if(tile->up)
		tile_show(tile->up);
	if (tile->ishidden || (TILESIZE(tile) < tile_minsize(tile)))
		tile_grow(tile, 1);
}

View*
tile_body(Tile*t) {
	return t? t->body: 0;
}

View*
tile_tag(Tile*t)
{
	return t? t->tag: 0;
}

/* Fill in 'min' and 'max' with a subregion within 'tile' for creating a new tile */
static void
tile_split(Tile *tile, int *min, int *max)
{
	int	lastline, average;

	*max = tile->max;
	average = (tile->max + tile->min)/ 2;

	if(tile->body) {
		lastline = view_lastlinepos(tile->body);
		assert(lastline <= tile->max);
		*min = MIN(average, lastline);
	} else {
		*min = average;
	}
}

/* Fill 'min' and 'max' with a good place to add a tile within 'list'
 */
void
findplace(Tile*list, int *min, int *max)
{
	/* Split the largest visible tile, or use all the available space. */

	Tile*biggest;

	if ( (biggest=biggest_visible(list->down, 0)) ) {
		tile_split(biggest, min, max);
	} else {
		*max = list->cmax;
		*min = list->cmin;
	}
}

/* Change t's position, keeping its size. */
void
moveto(Tile*t, int pos)
{
	int	size = TILESIZE(t);
	t->min = pos;
	t->max = t->min + size;
}

/* Create (but don't display) a new tile */
Tile*
tile_new(Ori ori, int min, int max, int base, 
	Tile*parent, Text *tagt, Text*bodyt)
{
	Tile*tile = NEW(Tile);
	int	minsize;

	tile->ishidden = false;
	tile->ori = ori;
	tile->min = min;
	tile->max = max;
	tile->base = base;
	tile->up = parent;
	setcminmax(parent, &tile->cmin, &tile->cmax);
	tile->down = tile->left = tile->right = 0;
	tile->tag = view_new(font, true, tagt, tile);
	tile->body = bodyt ? view_new(font, false, bodyt, tile): 0;

	minsize = tile_minsize(tile);
	if(TILESIZE(tile) < minsize)
		tile->max = tile->min + minsize;
	
	return tile;
}

/* The minimum comfortable viewing size for 't' */
int
tile_minsize(Tile*t)
{
	return 3 * t->base;
}