1 /*
2  * Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
3  * Copyright 2020 Vincent Sanders <vince@netsurf-browser.org>
4  *
5  * This file is part of NetSurf, http://www.netsurf-browser.org/
6  *
7  * NetSurf is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * NetSurf is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  * implementation of box tree inspection.
23  */
24 
25 #include <stdio.h>
26 #include <dom/dom.h>
27 
28 #include "utils/nsurl.h"
29 #include "utils/errors.h"
30 #include "netsurf/types.h"
31 #include "netsurf/content.h"
32 #include "netsurf/mouse.h"
33 #include "css/utils.h"
34 #include "css/dump.h"
35 #include "desktop/scrollbar.h"
36 
37 #include "html/private.h"
38 #include "html/box.h"
39 #include "html/box_inspect.h"
40 
41 /**
42  * Direction to move in a box-tree walk
43  */
44 enum box_walk_dir {
45 		   BOX_WALK_CHILDREN,
46 		   BOX_WALK_PARENT,
47 		   BOX_WALK_NEXT_SIBLING,
48 		   BOX_WALK_FLOAT_CHILDREN,
49 		   BOX_WALK_NEXT_FLOAT_SIBLING,
50 		   BOX_WALK_FLOAT_CONTAINER
51 };
52 
53 #define box_is_float(box) (box->type == BOX_FLOAT_LEFT ||	\
54 			   box->type == BOX_FLOAT_RIGHT)
55 
56 /**
57  * Determine if a point lies within a box.
58  *
59  * \param[in]  len_ctx     CSS length conversion context to use.
60  * \param[in]  box         Box to consider
61  * \param[in]  x           Coordinate relative to box
62  * \param[in]  y           Coordinate relative to box
63  * \param[out] physically  If function returning true, physically is set true
64  *                         iff point is within the box's physical dimensions and
65  *                         false if the point is not within the box's physical
66  *                         dimensions but is in the area defined by the box's
67  *                         descendants.  If function returns false, physically
68  *                         is undefined.
69  * \return  true if the point is within the box or a descendant box
70  *
71  * This is a helper function for box_at_point().
72  */
73 static bool
box_contains_point(const nscss_len_ctx * len_ctx,const struct box * box,int x,int y,bool * physically)74 box_contains_point(const nscss_len_ctx *len_ctx,
75 		   const struct box *box,
76 		   int x,
77 		   int y,
78 		   bool *physically)
79 {
80 	css_computed_clip_rect css_rect;
81 
82 	if (box->style != NULL &&
83 	    css_computed_position(box->style) == CSS_POSITION_ABSOLUTE &&
84 	    css_computed_clip(box->style, &css_rect) == CSS_CLIP_RECT) {
85 		/* We have an absolutly positioned box with a clip rect */
86 		struct rect r = {
87 				 .x0 = box->border[LEFT].width,
88 				 .y0 = box->border[TOP].width,
89 				 .x1 = box->padding[LEFT] + box->width +
90 				 box->border[RIGHT].width +
91 				 box->padding[RIGHT],
92 				 .y1 = box->padding[TOP] + box->height +
93 				 box->border[BOTTOM].width +
94 				 box->padding[BOTTOM]
95 		};
96 		if (x >= r.x0 && x < r.x1 && y >= r.y0 && y < r.y1) {
97 			*physically = true;
98 		} else {
99 			*physically = false;
100 		}
101 
102 		/* Adjust rect to css clip region */
103 		if (css_rect.left_auto == false) {
104 			r.x0 += FIXTOINT(nscss_len2px(len_ctx,
105 						      css_rect.left,
106 						      css_rect.lunit,
107 						      box->style));
108 		}
109 		if (css_rect.top_auto == false) {
110 			r.y0 += FIXTOINT(nscss_len2px(len_ctx,
111 						      css_rect.top,
112 						      css_rect.tunit,
113 						      box->style));
114 		}
115 		if (css_rect.right_auto == false) {
116 			r.x1 = box->border[LEFT].width +
117 				FIXTOINT(nscss_len2px(len_ctx,
118 						      css_rect.right,
119 						      css_rect.runit,
120 						      box->style));
121 		}
122 		if (css_rect.bottom_auto == false) {
123 			r.y1 = box->border[TOP].width +
124 				FIXTOINT(nscss_len2px(len_ctx,
125 						      css_rect.bottom,
126 						      css_rect.bunit,
127 						      box->style));
128 		}
129 
130 		/* Test if point is in clipped box */
131 		if (x >= r.x0 && x < r.x1 && y >= r.y0 && y < r.y1) {
132 			/* inside clip area */
133 			return true;
134 		}
135 
136 		/* Not inside clip area */
137 		return false;
138 	}
139 	if (x >= -box->border[LEFT].width &&
140 	    x < box->padding[LEFT] + box->width +
141 	    box->padding[RIGHT] + box->border[RIGHT].width &&
142 	    y >= -box->border[TOP].width &&
143 	    y < box->padding[TOP] + box->height +
144 	    box->padding[BOTTOM] + box->border[BOTTOM].width) {
145 		*physically = true;
146 		return true;
147 	}
148 	if (box->list_marker && box->list_marker->x - box->x <= x +
149 	    box->list_marker->border[LEFT].width &&
150 	    x < box->list_marker->x - box->x +
151 	    box->list_marker->padding[LEFT] +
152 	    box->list_marker->width +
153 	    box->list_marker->border[RIGHT].width +
154 	    box->list_marker->padding[RIGHT] &&
155 	    box->list_marker->y - box->y <= y +
156 	    box->list_marker->border[TOP].width &&
157 	    y < box->list_marker->y - box->y +
158 	    box->list_marker->padding[TOP] +
159 	    box->list_marker->height +
160 	    box->list_marker->border[BOTTOM].width +
161 	    box->list_marker->padding[BOTTOM]) {
162 		*physically = true;
163 		return true;
164 	}
165 	if ((box->style && css_computed_overflow_x(box->style) ==
166 	     CSS_OVERFLOW_VISIBLE) || !box->style) {
167 		if (box->descendant_x0 <= x &&
168 		    x < box->descendant_x1) {
169 			*physically = false;
170 			return true;
171 		}
172 	}
173 	if ((box->style && css_computed_overflow_y(box->style) ==
174 	     CSS_OVERFLOW_VISIBLE) || !box->style) {
175 		if (box->descendant_y0 <= y &&
176 		    y < box->descendant_y1) {
177 			*physically = false;
178 			return true;
179 		}
180 	}
181 	return false;
182 }
183 
184 
185 /**
186  * Move from box to next box in given direction, adjusting for box coord change
187  *
188  * \param b box to move from from
189  * \param dir direction to move in
190  * \param x box's global x-coord, updated to position of next box
191  * \param y box's global y-coord, updated to position of next box
192  *
193  * If no box can be found in given direction, NULL is returned.
194  */
195 static inline struct box *
box_move_xy(struct box * b,enum box_walk_dir dir,int * x,int * y)196 box_move_xy(struct box *b, enum box_walk_dir dir, int *x, int *y)
197 {
198 	struct box *rb = NULL;
199 
200 	switch (dir) {
201 	case BOX_WALK_CHILDREN:
202 		b = b->children;
203 		if (b == NULL)
204 			break;
205 		*x += b->x;
206 		*y += b->y;
207 		if (!box_is_float(b)) {
208 			rb = b;
209 			break;
210 		}
211 		/* fall through */
212 
213 	case BOX_WALK_NEXT_SIBLING:
214 		do {
215 			*x -= b->x;
216 			*y -= b->y;
217 			b = b->next;
218 			if (b == NULL)
219 				break;
220 			*x += b->x;
221 			*y += b->y;
222 		} while (box_is_float(b));
223 		rb = b;
224 		break;
225 
226 	case BOX_WALK_PARENT:
227 		*x -= b->x;
228 		*y -= b->y;
229 		rb = b->parent;
230 		break;
231 
232 	case BOX_WALK_FLOAT_CHILDREN:
233 		b = b->float_children;
234 		if (b == NULL)
235 			break;
236 		*x += b->x;
237 		*y += b->y;
238 		rb = b;
239 		break;
240 
241 	case BOX_WALK_NEXT_FLOAT_SIBLING:
242 		*x -= b->x;
243 		*y -= b->y;
244 		b = b->next_float;
245 		if (b == NULL)
246 			break;
247 		*x += b->x;
248 		*y += b->y;
249 		rb = b;
250 		break;
251 
252 	case BOX_WALK_FLOAT_CONTAINER:
253 		*x -= b->x;
254 		*y -= b->y;
255 		rb = b->float_container;
256 		break;
257 
258 	default:
259 		assert(0 && "Bad box walk type.");
260 	}
261 
262 	return rb;
263 }
264 
265 
266 /**
267  * Itterator for walking to next box in interaction order
268  *
269  * \param b	box to find next box from
270  * \param x	box's global x-coord, updated to position of next box
271  * \param y	box's global y-coord, updated to position of next box
272  * \param skip_children	whether to skip box's children
273  *
274  * This walks to a boxes float children before its children.  When walking
275  * children, floating boxes are skipped.
276  */
277 static inline struct box *
box_next_xy(struct box * b,int * x,int * y,bool skip_children)278 box_next_xy(struct box *b, int *x, int *y, bool skip_children)
279 {
280 	struct box *n;
281 	int tx, ty;
282 
283 	assert(b != NULL);
284 
285 	if (skip_children) {
286 		/* Caller is not interested in any kind of children */
287 		goto skip_children;
288 	}
289 
290 	tx = *x; ty = *y;
291 	n = box_move_xy(b, BOX_WALK_FLOAT_CHILDREN, &tx, &ty);
292 	if (n) {
293 		/* Next node is float child */
294 		*x = tx;
295 		*y = ty;
296 		return n;
297 	}
298  done_float_children:
299 
300 	tx = *x; ty = *y;
301 	n = box_move_xy(b, BOX_WALK_CHILDREN, &tx, &ty);
302 	if (n) {
303 		/* Next node is child */
304 		*x = tx;
305 		*y = ty;
306 		return n;
307 	}
308 
309  skip_children:
310 	tx = *x; ty = *y;
311 	n = box_move_xy(b, BOX_WALK_NEXT_FLOAT_SIBLING, &tx, &ty);
312 	if (n) {
313 		/* Go to next float sibling */
314 		*x = tx;
315 		*y = ty;
316 		return n;
317 	}
318 
319 	if (box_is_float(b)) {
320 		/* Done floats, but the float container may have children,
321 		 * or siblings, or ansestors with siblings.  Change to
322 		 * float container and move past handling its float children.
323 		 */
324 		b = box_move_xy(b, BOX_WALK_FLOAT_CONTAINER, x, y);
325 		goto done_float_children;
326 	}
327 
328 	/* Go to next sibling, or nearest ancestor with next sibling. */
329 	while (b) {
330 		while (!b->next && b->parent) {
331 			b = box_move_xy(b, BOX_WALK_PARENT, x, y);
332 			if (box_is_float(b)) {
333 				/* Go on to next float, if there is one */
334 				goto skip_children;
335 			}
336 		}
337 		if (!b->next) {
338 			/* No more boxes */
339 			return NULL;
340 		}
341 
342 		tx = *x; ty = *y;
343 		n = box_move_xy(b, BOX_WALK_NEXT_SIBLING, &tx, &ty);
344 		if (n) {
345 			/* Go to non-float (ancestor) sibling */
346 			*x = tx;
347 			*y = ty;
348 			return n;
349 
350 		} else if (b->parent) {
351 			b = box_move_xy(b, BOX_WALK_PARENT, x, y);
352 			if (box_is_float(b)) {
353 				/* Go on to next float, if there is one */
354 				goto skip_children;
355 			}
356 
357 		} else {
358 			/* No more boxes */
359 			return NULL;
360 		}
361 	}
362 
363 	assert(b != NULL);
364 	return NULL;
365 }
366 
367 
368 /**
369  * Check whether box is nearer mouse coordinates than current nearest box
370  *
371  * \param  box      box to test
372  * \param  bx	    position of box, in global document coordinates
373  * \param  by	    position of box, in global document coordinates
374  * \param  x	    mouse point, in global document coordinates
375  * \param  y	    mouse point, in global document coordinates
376  * \param  dir      direction in which to search (-1 = above-left,
377  *						  +1 = below-right)
378  * \param  nearest  nearest text box found, or NULL if none
379  *		    updated if box is nearer than existing nearest
380  * \param  tx	    position of text_box, in global document coordinates
381  *		    updated if box is nearer than existing nearest
382  * \param  ty	    position of text_box, in global document coordinates
383  *		    updated if box is nearer than existing nearest
384  * \param  nr_xd    distance to nearest text box found
385  *		    updated if box is nearer than existing nearest
386  * \param  nr_yd    distance to nearest text box found
387  *		    updated if box is nearer than existing nearest
388  * \return true if mouse point is inside box
389  */
390 static bool
box_nearer_text_box(struct box * box,int bx,int by,int x,int y,int dir,struct box ** nearest,int * tx,int * ty,int * nr_xd,int * nr_yd)391 box_nearer_text_box(struct box *box,
392 		    int bx, int by,
393 		    int x, int y,
394 		    int dir,
395 		    struct box **nearest,
396 		    int *tx, int *ty,
397 		    int *nr_xd, int *nr_yd)
398 {
399 	int w = box->padding[LEFT] + box->width + box->padding[RIGHT];
400 	int h = box->padding[TOP] + box->height + box->padding[BOTTOM];
401 	int y1 = by + h;
402 	int x1 = bx + w;
403 	int yd = INT_MAX;
404 	int xd = INT_MAX;
405 
406 	if (x >= bx && x1 > x && y >= by && y1 > y) {
407 		*nearest = box;
408 		*tx = bx;
409 		*ty = by;
410 		return true;
411 	}
412 
413 	if (box->parent->list_marker != box) {
414 		if (dir < 0) {
415 			/* consider only those children (partly) above-left */
416 			if (by <= y && bx < x) {
417 				yd = y <= y1 ? 0 : y - y1;
418 				xd = x <= x1 ? 0 : x - x1;
419 			}
420 		} else {
421 			/* consider only those children (partly) below-right */
422 			if (y1 > y && x1 > x) {
423 				yd = y > by ? 0 : by - y;
424 				xd = x > bx ? 0 : bx - x;
425 			}
426 		}
427 
428 		/* give y displacement precedence over x */
429 		if (yd < *nr_yd || (yd == *nr_yd && xd <= *nr_xd)) {
430 			*nr_yd = yd;
431 			*nr_xd = xd;
432 			*nearest = box;
433 			*tx = bx;
434 			*ty = by;
435 		}
436 	}
437 	return false;
438 }
439 
440 
441 /**
442  * Pick the text box child of 'box' that is closest to and above-left
443  * (dir -ve) or below-right (dir +ve) of the point 'x,y'
444  *
445  * \param  box      parent box
446  * \param  bx	    position of box, in global document coordinates
447  * \param  by	    position of box, in global document coordinates
448  * \param  fx	    position of float parent, in global document coordinates
449  * \param  fy	    position of float parent, in global document coordinates
450  * \param  x	    mouse point, in global document coordinates
451  * \param  y	    mouse point, in global document coordinates
452  * \param  dir      direction in which to search (-1 = above-left,
453  *						  +1 = below-right)
454  * \param  nearest  nearest text box found, or NULL if none
455  *		    updated if a descendant of box is nearer than old nearest
456  * \param  tx	    position of nearest, in global document coordinates
457  *		    updated if a descendant of box is nearer than old nearest
458  * \param  ty	    position of nearest, in global document coordinates
459  *		    updated if a descendant of box is nearer than old nearest
460  * \param  nr_xd    distance to nearest text box found
461  *		    updated if a descendant of box is nearer than old nearest
462  * \param  nr_yd    distance to nearest text box found
463  *		    updated if a descendant of box is nearer than old nearest
464  * \return true if mouse point is inside text_box
465  */
466 static bool
box_nearest_text_box(struct box * box,int bx,int by,int fx,int fy,int x,int y,int dir,struct box ** nearest,int * tx,int * ty,int * nr_xd,int * nr_yd)467 box_nearest_text_box(struct box *box,
468 		     int bx, int by,
469 		     int fx, int fy,
470 		     int x, int y,
471 		     int dir,
472 		     struct box **nearest,
473 		     int *tx, int *ty,
474 		     int *nr_xd, int *nr_yd)
475 {
476 	struct box *child = box->children;
477 	int c_bx, c_by;
478 	int c_fx, c_fy;
479 	bool in_box = false;
480 
481 	if (*nearest == NULL) {
482 		*nr_xd = INT_MAX / 2; /* displacement of 'nearest so far' */
483 		*nr_yd = INT_MAX / 2;
484 	}
485 	if (box->type == BOX_INLINE_CONTAINER) {
486 		int bw = box->padding[LEFT] + box->width + box->padding[RIGHT];
487 		int bh = box->padding[TOP] + box->height + box->padding[BOTTOM];
488 		int b_y1 = by + bh;
489 		int b_x1 = bx + bw;
490 		if (x >= bx && b_x1 > x && y >= by && b_y1 > y) {
491 			in_box = true;
492 		}
493 	}
494 
495 	while (child) {
496 		if (child->type == BOX_FLOAT_LEFT ||
497 		    child->type == BOX_FLOAT_RIGHT) {
498 			c_bx = fx + child->x -
499 				scrollbar_get_offset(child->scroll_x);
500 			c_by = fy + child->y -
501 				scrollbar_get_offset(child->scroll_y);
502 		} else {
503 			c_bx = bx + child->x -
504 				scrollbar_get_offset(child->scroll_x);
505 			c_by = by + child->y -
506 				scrollbar_get_offset(child->scroll_y);
507 		}
508 		if (child->float_children) {
509 			c_fx = c_bx;
510 			c_fy = c_by;
511 		} else {
512 			c_fx = fx;
513 			c_fy = fy;
514 		}
515 		if (in_box && child->text && !child->object) {
516 			if (box_nearer_text_box(child,
517 						c_bx, c_by, x, y, dir, nearest,
518 						tx, ty, nr_xd, nr_yd))
519 				return true;
520 		} else {
521 			if (child->list_marker) {
522 				if (box_nearer_text_box(
523 						child->list_marker,
524 						c_bx + child->list_marker->x,
525 						c_by + child->list_marker->y,
526 						x, y, dir, nearest,
527 						tx, ty, nr_xd, nr_yd))
528 					return true;
529 			}
530 			if (box_nearest_text_box(child, c_bx, c_by,
531 						 c_fx, c_fy,
532 						 x, y, dir, nearest, tx, ty,
533 						 nr_xd, nr_yd))
534 				return true;
535 		}
536 		child = child->next;
537 	}
538 
539 	return false;
540 }
541 
542 
543 /* Exported function documented in html/box.h */
box_coords(struct box * box,int * x,int * y)544 void box_coords(struct box *box, int *x, int *y)
545 {
546 	*x = box->x;
547 	*y = box->y;
548 	while (box->parent) {
549 		if (box_is_float(box)) {
550 			assert(box->float_container);
551 			box = box->float_container;
552 		} else {
553 			box = box->parent;
554 		}
555 		*x += box->x - scrollbar_get_offset(box->scroll_x);
556 		*y += box->y - scrollbar_get_offset(box->scroll_y);
557 	}
558 }
559 
560 
561 /* Exported function documented in html/box.h */
box_bounds(struct box * box,struct rect * r)562 void box_bounds(struct box *box, struct rect *r)
563 {
564 	int width, height;
565 
566 	box_coords(box, &r->x0, &r->y0);
567 
568 	width = box->padding[LEFT] + box->width + box->padding[RIGHT];
569 	height = box->padding[TOP] + box->height + box->padding[BOTTOM];
570 
571 	r->x1 = r->x0 + width;
572 	r->y1 = r->y0 + height;
573 }
574 
575 
576 /* Exported function documented in html/box.h */
577 struct box *
box_at_point(const nscss_len_ctx * len_ctx,struct box * box,const int x,const int y,int * box_x,int * box_y)578 box_at_point(const nscss_len_ctx *len_ctx,
579 	     struct box *box,
580 	     const int x, const int y,
581 	     int *box_x, int *box_y)
582 {
583 	bool skip_children;
584 	bool physically;
585 
586 	assert(box);
587 
588 	skip_children = false;
589 	while ((box = box_next_xy(box, box_x, box_y, skip_children))) {
590 		if (box_contains_point(len_ctx, box, x - *box_x, y - *box_y,
591 				       &physically)) {
592 			*box_x -= scrollbar_get_offset(box->scroll_x);
593 			*box_y -= scrollbar_get_offset(box->scroll_y);
594 
595 			if (physically)
596 				return box;
597 
598 			skip_children = false;
599 		} else {
600 			skip_children = true;
601 		}
602 	}
603 
604 	return NULL;
605 }
606 
607 
608 /* Exported function documented in html/box.h */
box_find_by_id(struct box * box,lwc_string * id)609 struct box *box_find_by_id(struct box *box, lwc_string *id)
610 {
611 	struct box *a, *b;
612 	bool m;
613 
614 	if (box->id != NULL &&
615 	    lwc_string_isequal(id, box->id, &m) == lwc_error_ok &&
616 	    m == true) {
617 		return box;
618 	}
619 
620 	for (a = box->children; a; a = a->next) {
621 		if ((b = box_find_by_id(a, id)) != NULL) {
622 			return b;
623 		}
624 	}
625 
626 	return NULL;
627 }
628 
629 
630 /* Exported function documented in html/box.h */
box_visible(struct box * box)631 bool box_visible(struct box *box)
632 {
633 	/* visibility: hidden */
634 	if (box->style &&
635 	    css_computed_visibility(box->style) == CSS_VISIBILITY_HIDDEN) {
636 		return false;
637 	}
638 
639 	return true;
640 }
641 
642 
643 /* Exported function documented in html/box.h */
box_dump(FILE * stream,struct box * box,unsigned int depth,bool style)644 void box_dump(FILE *stream, struct box *box, unsigned int depth, bool style)
645 {
646 	unsigned int i;
647 	struct box *c, *prev;
648 
649 	for (i = 0; i != depth; i++) {
650 		fprintf(stream, "  ");
651 	}
652 
653 	fprintf(stream, "%p ", box);
654 	fprintf(stream, "x%i y%i w%i h%i ",
655 		box->x, box->y, box->width, box->height);
656 	if (box->max_width != UNKNOWN_MAX_WIDTH) {
657 		fprintf(stream, "min%i max%i ", box->min_width, box->max_width);
658 	}
659 	fprintf(stream, "(%i %i %i %i) ",
660 		box->descendant_x0, box->descendant_y0,
661 		box->descendant_x1, box->descendant_y1);
662 
663 	fprintf(stream, "m(%i %i %i %i) ",
664 		box->margin[TOP], box->margin[LEFT],
665 		box->margin[BOTTOM], box->margin[RIGHT]);
666 
667 	switch (box->type) {
668 	case BOX_BLOCK:
669 		fprintf(stream, "BLOCK ");
670 		break;
671 
672 	case BOX_INLINE_CONTAINER:
673 		fprintf(stream, "INLINE_CONTAINER ");
674 		break;
675 
676 	case BOX_INLINE:
677 		fprintf(stream, "INLINE ");
678 		break;
679 
680 	case BOX_INLINE_END:
681 		fprintf(stream, "INLINE_END ");
682 		break;
683 
684 	case BOX_INLINE_BLOCK:
685 		fprintf(stream, "INLINE_BLOCK ");
686 		break;
687 
688 	case BOX_TABLE:
689 		fprintf(stream, "TABLE [columns %i] ", box->columns);
690 		break;
691 
692 	case BOX_TABLE_ROW:
693 		fprintf(stream, "TABLE_ROW ");
694 		break;
695 
696 	case BOX_TABLE_CELL:
697 		fprintf(stream, "TABLE_CELL [columns %i, start %i, rows %i] ",
698 			box->columns,
699 			box->start_column,
700 			box->rows);
701 		break;
702 
703 	case BOX_TABLE_ROW_GROUP:
704 		fprintf(stream, "TABLE_ROW_GROUP ");
705 		break;
706 
707 	case BOX_FLOAT_LEFT:
708 		fprintf(stream, "FLOAT_LEFT ");
709 		break;
710 
711 	case BOX_FLOAT_RIGHT:
712 		fprintf(stream, "FLOAT_RIGHT ");
713 		break;
714 
715 	case BOX_BR:
716 		fprintf(stream, "BR ");
717 		break;
718 
719 	case BOX_TEXT:
720 		fprintf(stream, "TEXT ");
721 		break;
722 
723 	default:
724 		fprintf(stream, "Unknown box type ");
725 	}
726 
727 	if (box->text)
728 		fprintf(stream, "%li '%.*s' ", (unsigned long) box->byte_offset,
729 			(int) box->length, box->text);
730 	if (box->space)
731 		fprintf(stream, "space ");
732 	if (box->object) {
733 		fprintf(stream, "(object '%s') ",
734 			nsurl_access(hlcache_handle_get_url(box->object)));
735 	}
736 	if (box->iframe) {
737 		fprintf(stream, "(iframe) ");
738 	}
739 	if (box->gadget)
740 		fprintf(stream, "(gadget) ");
741 	if (style && box->style)
742 		nscss_dump_computed_style(stream, box->style);
743 	if (box->href)
744 		fprintf(stream, " -> '%s'", nsurl_access(box->href));
745 	if (box->target)
746 		fprintf(stream, " |%s|", box->target);
747 	if (box->title)
748 		fprintf(stream, " [%s]", box->title);
749 	if (box->id)
750 		fprintf(stream, " ID:%s", lwc_string_data(box->id));
751 	if (box->type == BOX_INLINE || box->type == BOX_INLINE_END)
752 		fprintf(stream, " inline_end %p", box->inline_end);
753 	if (box->float_children)
754 		fprintf(stream, " float_children %p", box->float_children);
755 	if (box->next_float)
756 		fprintf(stream, " next_float %p", box->next_float);
757 	if (box->float_container)
758 		fprintf(stream, " float_container %p", box->float_container);
759 	if (box->col) {
760 		fprintf(stream, " (columns");
761 		for (i = 0; i != box->columns; i++) {
762 			fprintf(stream, " (%s %s %i %i %i)",
763 				((const char *[]) {
764 					"UNKNOWN",
765 					"FIXED",
766 					"AUTO",
767 					"PERCENT",
768 					"RELATIVE"
769 						})
770 				[box->col[i].type],
771 				((const char *[]) {
772 					"normal",
773 					"positioned"})
774 				[box->col[i].positioned],
775 				box->col[i].width,
776 				box->col[i].min, box->col[i].max);
777 		}
778 		fprintf(stream, ")");
779 	}
780 	if (box->node != NULL) {
781 		dom_string *name;
782 		if (dom_node_get_node_name(box->node, &name) == DOM_NO_ERR) {
783 			fprintf(stream, " <%s>", dom_string_data(name));
784 			dom_string_unref(name);
785 		}
786 	}
787 	fprintf(stream, "\n");
788 
789 	if (box->list_marker) {
790 		for (i = 0; i != depth; i++)
791 			fprintf(stream, "  ");
792 		fprintf(stream, "list_marker:\n");
793 		box_dump(stream, box->list_marker, depth + 1, style);
794 	}
795 
796 	for (c = box->children; c && c->next; c = c->next)
797 		;
798 	if (box->last != c)
799 		fprintf(stream, "warning: box->last %p (should be %p) "
800 			"(box %p)\n", box->last, c, box);
801 	for (prev = 0, c = box->children; c; prev = c, c = c->next) {
802 		if (c->parent != box)
803 			fprintf(stream, "warning: box->parent %p (should be "
804 				"%p) (box on next line)\n",
805 				c->parent, box);
806 		if (c->prev != prev)
807 			fprintf(stream, "warning: box->prev %p (should be "
808 				"%p) (box on next line)\n",
809 				c->prev, prev);
810 		box_dump(stream, c, depth + 1, style);
811 	}
812 }
813 
814 
815 /* exported interface documented in html/box.h */
box_vscrollbar_present(const struct box * const box)816 bool box_vscrollbar_present(const struct box * const box)
817 {
818 	return box->padding[TOP] +
819 		box->height +
820 		box->padding[BOTTOM] +
821 		box->border[BOTTOM].width < box->descendant_y1;
822 }
823 
824 
825 /* exported interface documented in html/box.h */
box_hscrollbar_present(const struct box * const box)826 bool box_hscrollbar_present(const struct box * const box)
827 {
828 	return box->padding[LEFT] +
829 		box->width +
830 		box->padding[RIGHT] +
831 		box->border[RIGHT].width < box->descendant_x1;
832 }
833 
834 
835 /* Exported function documented in html/box.h */
836 struct box *
box_pick_text_box(struct html_content * html,int x,int y,int dir,int * dx,int * dy)837 box_pick_text_box(struct html_content *html,
838 		  int x, int y,
839 		  int dir,
840 		  int *dx, int *dy)
841 {
842 	struct box *text_box = NULL;
843 	struct box *box;
844 	int nr_xd, nr_yd;
845 	int bx, by;
846 	int fx, fy;
847 	int tx, ty;
848 
849 	if (html == NULL)
850 		return NULL;
851 
852 	box = html->layout;
853 	bx = box->margin[LEFT];
854 	by = box->margin[TOP];
855 	fx = bx;
856 	fy = by;
857 
858 	if (!box_nearest_text_box(box, bx, by, fx, fy, x, y,
859 				  dir, &text_box, &tx, &ty, &nr_xd, &nr_yd)) {
860 		if (text_box && text_box->text && !text_box->object) {
861 			int w = (text_box->padding[LEFT] +
862 				 text_box->width +
863 				 text_box->padding[RIGHT]);
864 			int h = (text_box->padding[TOP] +
865 				 text_box->height +
866 				 text_box->padding[BOTTOM]);
867 			int x1, y1;
868 
869 			y1 = ty + h;
870 			x1 = tx + w;
871 
872 			/* ensure point lies within the text box */
873 			if (x < tx) x = tx;
874 			if (y < ty) y = ty;
875 			if (y > y1) y = y1;
876 			if (x > x1) x = x1;
877 		}
878 	}
879 
880 	/* return coordinates relative to box */
881 	*dx = x - tx;
882 	*dy = y - ty;
883 
884 	return text_box;
885 }
886