1 /*
2  * Copyright 2006 Richard Wilson <info@tinct.net>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /** \file
20  * Frame and frameset creation and manipulation (implementation).
21  */
22 
23 #include <assert.h>
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <math.h>
31 
32 #include "utils/log.h"
33 #include "utils/utils.h"
34 #include "netsurf/content.h"
35 #include "content/hlcache.h"
36 #include "html/html.h"
37 #include "html/box.h"
38 #include "html/box_inspect.h"
39 
40 #include "desktop/browser_private.h"
41 #include "desktop/frames.h"
42 #include "desktop/scrollbar.h"
43 
44 /** maximum frame resize margin */
45 #define FRAME_RESIZE 6
46 
47 static bool browser_window_resolve_frame_dimension(struct browser_window *bw,
48 		struct browser_window *sibling, int x, int y, bool width,
49 		bool height);
50 
51 
52 /**
53  * Callback for (i)frame scrollbars.
54  */
browser_window_scroll_callback(void * client_data,struct scrollbar_msg_data * scrollbar_data)55 void browser_window_scroll_callback(void *client_data,
56 		struct scrollbar_msg_data *scrollbar_data)
57 {
58 	struct browser_window *bw = client_data;
59 
60 	switch(scrollbar_data->msg) {
61 	case SCROLLBAR_MSG_MOVED:
62 		if (bw->browser_window_type == BROWSER_WINDOW_IFRAME) {
63 			html_redraw_a_box(bw->parent->current_content, bw->box);
64 		} else {
65 			struct rect rect;
66 
67 			rect.x0 = scrollbar_get_offset(bw->scroll_x);
68 			rect.y0 = scrollbar_get_offset(bw->scroll_y);
69 			rect.x1 = rect.x0 + bw->width;
70 			rect.y1 = rect.y0 + bw->height;
71 
72 			browser_window_update_box(bw, &rect);
73 		}
74 		break;
75 	case SCROLLBAR_MSG_SCROLL_START:
76 	{
77 		struct rect rect = {
78 			.x0 = scrollbar_data->x0,
79 			.y0 = scrollbar_data->y0,
80 			.x1 = scrollbar_data->x1,
81 			.y1 = scrollbar_data->y1
82 		};
83 
84 		if (scrollbar_is_horizontal(scrollbar_data->scrollbar))
85 			browser_window_set_drag_type(bw, DRAGGING_SCR_X, &rect);
86 		else
87 			browser_window_set_drag_type(bw, DRAGGING_SCR_Y, &rect);
88 	}
89 		break;
90 	case SCROLLBAR_MSG_SCROLL_FINISHED:
91 		browser_window_set_drag_type(bw, DRAGGING_NONE, NULL);
92 
93 		browser_window_set_pointer(bw, BROWSER_POINTER_DEFAULT);
94 		break;
95 	}
96 }
97 
98 /* exported interface, documented in browser.h */
browser_window_handle_scrollbars(struct browser_window * bw)99 void browser_window_handle_scrollbars(struct browser_window *bw)
100 {
101 	struct hlcache_handle *h = bw->current_content;
102 	bool scroll_x;
103 	bool scroll_y;
104 	int c_width = 0;
105 	int c_height = 0;
106 
107 	assert(!bw->window); /* Core-handled windows only */
108 
109 	if (h != NULL) {
110 		c_width  = content_get_width(h);
111 		c_height = content_get_height(h);
112 	}
113 
114 	if (bw->scrolling == BW_SCROLLING_YES) {
115 		scroll_x = true;
116 		scroll_y = true;
117 	} else if (bw->scrolling == BW_SCROLLING_AUTO &&
118 			bw->current_content) {
119 		int bw_width = bw->width;
120 		int bw_height = bw->height;
121 
122 		/* subtract existing scrollbar width */
123 		bw_width -= bw->scroll_y ? SCROLLBAR_WIDTH : 0;
124 		bw_height -= bw->scroll_x ? SCROLLBAR_WIDTH : 0;
125 
126 		scroll_y = (c_height > bw_height) ? true : false;
127 		scroll_x = (c_width > bw_width) ? true : false;
128 	} else {
129 		/* No scrollbars */
130 		scroll_x = false;
131 		scroll_y = false;
132 	}
133 
134 	if (!scroll_x && bw->scroll_x != NULL) {
135 		scrollbar_destroy(bw->scroll_x);
136 		bw->scroll_x = NULL;
137 	}
138 
139 	if (!scroll_y && bw->scroll_y != NULL) {
140 		scrollbar_destroy(bw->scroll_y);
141 		bw->scroll_y = NULL;
142 	}
143 
144 	if (scroll_y) {
145 		int length = bw->height;
146 		int visible = bw->height - (scroll_x ? SCROLLBAR_WIDTH : 0);
147 
148 		if (bw->scroll_y == NULL) {
149 			/* create vertical scrollbar */
150 			if (scrollbar_create(false, length, c_height, visible,
151 					     bw, browser_window_scroll_callback,
152 					     &(bw->scroll_y)) != NSERROR_OK) {
153 				return;
154 			}
155 		} else {
156 			/* update vertical scrollbar */
157 			scrollbar_set_extents(bw->scroll_y, length,
158 					visible, c_height);
159 		}
160 	}
161 
162 	if (scroll_x) {
163 		int length = bw->width - (scroll_y ? SCROLLBAR_WIDTH : 0);
164 		int visible = length;
165 
166 		if (bw->scroll_x == NULL) {
167 			/* create horizontal scrollbar */
168 			if (scrollbar_create(true, length, c_width, visible,
169 					     bw, browser_window_scroll_callback,
170 					     &(bw->scroll_x)) != NSERROR_OK) {
171 				return;
172 			}
173 		} else {
174 			/* update horizontal scrollbar */
175 			scrollbar_set_extents(bw->scroll_x, length,
176 					visible, c_width);
177 		}
178 	}
179 
180 	if (scroll_x && scroll_y)
181 		scrollbar_make_pair(bw->scroll_x, bw->scroll_y);
182 }
183 
184 
185 /* exported function documented in desktop/frames.h */
browser_window_create_iframes(struct browser_window * bw,struct content_html_iframe * iframe)186 nserror browser_window_create_iframes(struct browser_window *bw,
187 		struct content_html_iframe *iframe)
188 {
189 	struct browser_window *window;
190 	struct content_html_iframe *cur;
191 	struct rect rect;
192 	int iframes = 0;
193 	int index;
194 	nserror ret = NSERROR_OK;
195 
196 	if (iframe == NULL) {
197 		return NSERROR_BAD_PARAMETER;
198 	}
199 
200 	/* Count iframe list and allocate enough space within the
201 	 * browser window.
202 	 */
203 	for (cur = iframe; cur; cur = cur->next) {
204 		iframes++;
205 	}
206 	bw->iframes = calloc(iframes, sizeof(*bw));
207 	if (!bw->iframes) {
208 		return NSERROR_NOMEM;
209 	}
210 	bw->iframe_count = iframes;
211 
212 	index = 0;
213 	for (cur = iframe; cur; cur = cur->next) {
214 		window = &(bw->iframes[index++]);
215 
216 		/* Initialise common parts */
217 		browser_window_initialise_common(BW_CREATE_NONE,
218 				window, NULL);
219 
220 		/* window characteristics */
221 		window->browser_window_type = BROWSER_WINDOW_IFRAME;
222 		window->scrolling = cur->scrolling;
223 		window->border = cur->border;
224 		window->border_colour = cur->border_colour;
225 		window->no_resize = true;
226 		window->margin_width = cur->margin_width;
227 		window->margin_height = cur->margin_height;
228 		window->scale = bw->scale;
229 		if (cur->name != NULL) {
230 			window->name = strdup(cur->name);
231 			if (window->name == NULL) {
232 				free(bw->iframes) ;
233 				bw->iframes = 0;
234 				bw->iframe_count = 0;
235 				return NSERROR_NOMEM;
236 			}
237 		}
238 
239 		/* linking */
240 		window->box = cur->box;
241 		window->parent = bw;
242 		window->box->iframe = window;
243 
244 		/* iframe dimensions */
245 		box_bounds(window->box, &rect);
246 
247 		browser_window_set_position(window, rect.x0, rect.y0);
248 		browser_window_set_dimensions(window, rect.x1 - rect.x0,
249 				rect.y1 - rect.y0);
250 	}
251 
252 	/* calculate dimensions */
253 	browser_window_update_extent(bw);
254 	browser_window_recalculate_iframes(bw);
255 
256 	index = 0;
257 	for (cur = iframe; cur; cur = cur->next) {
258 		window = &(bw->iframes[index++]);
259 		if (cur->url) {
260 			/* fetch iframe's content */
261 			ret = browser_window_navigate(window,
262 				cur->url,
263 				hlcache_handle_get_url(bw->current_content),
264 				BW_NAVIGATE_UNVERIFIABLE,
265 				NULL,
266 				NULL,
267 				bw->current_content);
268 		}
269 	}
270 
271 	return ret;
272 }
273 
274 
275 /**
276  * Recalculate iframe positions following a resize.
277  *
278  * \param  bw	    The browser window to reposition iframes for
279  */
280 
browser_window_recalculate_iframes(struct browser_window * bw)281 void browser_window_recalculate_iframes(struct browser_window *bw)
282 {
283 	struct browser_window *window;
284 	int index;
285 
286 	for (index = 0; index < bw->iframe_count; index++) {
287 		window = &(bw->iframes[index]);
288 
289 		if (window != NULL) {
290 			browser_window_handle_scrollbars(window);
291 		}
292 	}
293 }
294 
295 
296 /* exported interface documented in desktop/frames.h */
browser_window_create_frameset(struct browser_window * bw,struct content_html_frames * frameset)297 nserror browser_window_create_frameset(struct browser_window *bw,
298 		struct content_html_frames *frameset)
299 {
300 	int row, col, index;
301 	struct content_html_frames *frame;
302 	struct browser_window *window;
303 	hlcache_handle *parent;
304 
305 	assert(bw && frameset);
306 
307 	/* 1. Create children */
308 	assert(bw->children == NULL);
309 	assert(frameset->cols + frameset->rows != 0);
310 
311 	bw->children = calloc((frameset->cols * frameset->rows), sizeof(*bw));
312 	if (!bw->children) {
313 		return NSERROR_NOMEM;
314 	}
315 
316 	bw->cols = frameset->cols;
317 	bw->rows = frameset->rows;
318 	for (row = 0; row < bw->rows; row++) {
319 		for (col = 0; col < bw->cols; col++) {
320 			index = (row * bw->cols) + col;
321 			frame = &frameset->children[index];
322 			window = &bw->children[index];
323 
324 			/* Initialise common parts */
325 			browser_window_initialise_common(BW_CREATE_NONE,
326 					window, NULL);
327 
328 			/* window characteristics */
329 			if (frame->children)
330 				window->browser_window_type =
331 						BROWSER_WINDOW_FRAMESET;
332 			else
333 				window->browser_window_type =
334 						BROWSER_WINDOW_FRAME;
335 			window->scrolling = frame->scrolling;
336 			window->border = frame->border;
337 			window->border_colour = frame->border_colour;
338 			window->no_resize = frame->no_resize;
339 			window->frame_width = frame->width;
340 			window->frame_height = frame->height;
341 			window->margin_width = frame->margin_width;
342 			window->margin_height = frame->margin_height;
343 			if (frame->name) {
344 				window->name = strdup(frame->name);
345 				if (!window->name) {
346 					free(bw->children);
347 					bw->children = NULL;
348 					return NSERROR_NOMEM;
349 				}
350 			}
351 
352 			window->scale = bw->scale;
353 
354 			/* linking */
355 			window->parent = bw;
356 
357 			if (window->name)
358 				NSLOG(netsurf, INFO, "Created frame '%s'",
359 				      window->name);
360 			else
361 				NSLOG(netsurf, INFO,
362 				      "Created frame (unnamed)");
363 		}
364 	}
365 
366 	/* 2. Calculate dimensions */
367 	browser_window_update_extent(bw);
368 	browser_window_recalculate_frameset(bw);
369 
370 	/* 3. Recurse for grandchildren */
371 	for (row = 0; row < bw->rows; row++) {
372 		for (col = 0; col < bw->cols; col++) {
373 			index = (row * bw->cols) + col;
374 			frame = &frameset->children[index];
375 			window = &bw->children[index];
376 
377 			if (frame->children)
378 				browser_window_create_frameset(window, frame);
379 		}
380 	}
381 
382 	/* Use the URL of the first ancestor window containing html content
383 	 * as the referer */
384 	for (window = bw; window->parent; window = window->parent) {
385 		if (window->current_content &&
386 				content_get_type(window->current_content) ==
387 				CONTENT_HTML)
388 			break;
389 	}
390 
391 	parent = window->current_content;
392 
393 	/* 4. Launch content */
394 	for (row = 0; row < bw->rows; row++) {
395 		for (col = 0; col < bw->cols; col++) {
396 			index = (row * bw->cols) + col;
397 			frame = &frameset->children[index];
398 			window = &bw->children[index];
399 
400 			if (frame->url) {
401 				browser_window_navigate(window,
402 					frame->url,
403 					hlcache_handle_get_url(parent),
404 					BW_NAVIGATE_HISTORY |
405 					BW_NAVIGATE_UNVERIFIABLE,
406 					NULL,
407 					NULL,
408 					parent);
409 			}
410 		}
411 	}
412 
413 	return NSERROR_OK;
414 }
415 
416 
417 /**
418  * Recalculate frameset positions following a resize.
419  *
420  * \param  bw	    The browser window to reposition framesets for
421  */
422 
browser_window_recalculate_frameset(struct browser_window * bw)423 void browser_window_recalculate_frameset(struct browser_window *bw)
424 {
425 	int widths[bw->cols][bw->rows];
426 	int heights[bw->cols][bw->rows];
427 	int bw_width, bw_height;
428 	int avail_width, avail_height;
429 	int row, row2, col, index;
430 	struct browser_window *window;
431 	float relative;
432 	int size, extent, applied;
433 	int x, y;
434 	int new_width, new_height;
435 
436 	assert(bw);
437 
438 	/* window dimensions */
439 	if (!bw->parent) {
440 		browser_window_get_dimensions(bw, &bw_width, &bw_height);
441 		bw_width /= bw->scale;
442 		bw_height /= bw->scale;
443 		bw->x = 0;
444 		bw->y = 0;
445 		bw->width = bw_width;
446 		bw->height = bw_height;
447 	} else {
448 		bw_width = bw->width;
449 		bw_height = bw->height;
450 	}
451 	bw_width++;
452 	bw_height++;
453 
454 	/* widths */
455 	for (row = 0; row < bw->rows; row++) {
456 		avail_width = bw_width;
457 		relative = 0;
458 		for (col = 0; col < bw->cols; col++) {
459 			index = (row * bw->cols) + col;
460 			window = &bw->children[index];
461 
462 			switch (window->frame_width.unit) {
463 			case FRAME_DIMENSION_PIXELS:
464 				widths[col][row] = window->frame_width.value *
465 						window->scale;
466 				if (window->border) {
467 					if (col != 0)
468 						widths[col][row] += 1;
469 					if (col != bw->cols - 1)
470 						widths[col][row] += 1;
471 				}
472 				break;
473 			case FRAME_DIMENSION_PERCENT:
474 				widths[col][row] = bw_width *
475 						window->frame_width.value / 100;
476 				break;
477 			case FRAME_DIMENSION_RELATIVE:
478 				widths[col][row] = 0;
479 				relative += window->frame_width.value;
480 				break;
481 			default:
482 				/* unknown frame dimension unit */
483 				assert(window->frame_width.unit ==
484 						FRAME_DIMENSION_PIXELS ||
485 						window->frame_width.unit ==
486 						FRAME_DIMENSION_PERCENT ||
487 						window->frame_width.unit ==
488 						FRAME_DIMENSION_RELATIVE);
489 				break;
490 			}
491 			avail_width -= widths[col][row];
492 		}
493 
494 		/* Redistribute to fit window */
495 		if ((relative > 0) && (avail_width > 0)) {
496 			/* Expand the relative sections to fill remainder */
497 			for (col = 0; col < bw->cols; col++) {
498 				index = (row * bw->cols) + col;
499 				window = &bw->children[index];
500 
501 				if (window->frame_width.unit ==
502 						FRAME_DIMENSION_RELATIVE) {
503 					size = avail_width * window->
504 							frame_width.value /
505 							relative;
506 					avail_width -= size;
507 					relative -= window->frame_width.value;
508 					widths[col][row] += size;
509 				}
510 			}
511 		} else if (bw_width != avail_width) {
512 			/* proportionally distribute error */
513 			extent = avail_width;
514 			applied = 0;
515 			for (col = 0; col < bw->cols; col++) {
516 				if (col == bw->cols - 1) {
517 					/* Last cell, use up remainder */
518 					widths[col][row] += extent - applied;
519 					widths[col][row] =
520 							widths[col][row] < 0 ?
521 							0 : widths[col][row];
522 				} else {
523 					/* Find size of cell adjustment */
524 					size = (widths[col][row] * extent) /
525 							(bw_width - extent);
526 					/* Modify cell */
527 					widths[col][row] += size;
528 					applied += size;
529 				}
530 			}
531 		}
532 	}
533 
534 	/* heights */
535 	for (col = 0; col < bw->cols; col++) {
536 		avail_height = bw_height;
537 		relative = 0;
538 		for (row = 0; row < bw->rows; row++) {
539 			index = (row * bw->cols) + col;
540 			window = &bw->children[index];
541 
542 			switch (window->frame_height.unit) {
543 			case FRAME_DIMENSION_PIXELS:
544 				heights[col][row] = window->frame_height.value *
545 						window->scale;
546 				if (window->border) {
547 					if (row != 0)
548 						heights[col][row] += 1;
549 					if (row != bw->rows - 1)
550 						heights[col][row] += 1;
551 				}
552 				break;
553 			case FRAME_DIMENSION_PERCENT:
554 				heights[col][row] = bw_height *
555 						window->frame_height.value / 100;
556 				break;
557 			case FRAME_DIMENSION_RELATIVE:
558 				heights[col][row] = 0;
559 				relative += window->frame_height.value;
560 				break;
561 			default:
562 				/* unknown frame dimension unit */
563 				assert(window->frame_height.unit ==
564 						FRAME_DIMENSION_PIXELS ||
565 						window->frame_height.unit ==
566 						FRAME_DIMENSION_PERCENT ||
567 						window->frame_height.unit ==
568 						FRAME_DIMENSION_RELATIVE);
569 				break;
570 			}
571 			avail_height -= heights[col][row];
572 		}
573 
574 		if (avail_height == 0)
575 			continue;
576 
577 		/* Redistribute to fit window */
578 		if ((relative > 0) && (avail_height > 0)) {
579 			/* Expand the relative sections to fill remainder */
580 			for (row = 0; row < bw->rows; row++) {
581 				index = (row * bw->cols) + col;
582 				window = &bw->children[index];
583 
584 				if (window->frame_height.unit ==
585 						FRAME_DIMENSION_RELATIVE) {
586 					size = avail_height * window->
587 							frame_height.value /
588 							relative;
589 					avail_height -= size;
590 					relative -= window->frame_height.value;
591 					heights[col][row] += size;
592 				}
593 			}
594 		} else if (bw_height != avail_height) {
595 			/* proportionally distribute error */
596 			extent = avail_height;
597 			applied = 0;
598 			for (row = 0; row < bw->rows; row++) {
599 				if (row == bw->rows - 1) {
600 					/* Last cell, use up remainder */
601 					heights[col][row] += extent - applied;
602 					heights[col][row] =
603 							heights[col][row] < 0 ?
604 							0 : heights[col][row];
605 				} else {
606 					/* Find size of cell adjustment */
607 					size = (heights[col][row] * extent) /
608 							(bw_height - extent);
609 					/* Modify cell */
610 					heights[col][row] += size;
611 					applied += size;
612 				}
613 			}
614 		}
615 	}
616 
617 	/* position frames and calculate children */
618 	for (row = 0; row < bw->rows; row++) {
619 		x = 0;
620 		for (col = 0; col < bw->cols; col++) {
621 			index = (row * bw->cols) + col;
622 			window = &bw->children[index];
623 
624 			y = 0;
625 			for (row2 = 0; row2 < row; row2++)
626 				y+= heights[col][row2];
627 
628 			window->x = x;
629 			window->y = y;
630 
631 			new_width = widths[col][row] - 1;
632 			new_height = heights[col][row] - 1;
633 
634 			if (window->width != new_width ||
635 					window->height != new_height) {
636 				/* Change in frame size */
637 				browser_window_reformat(window, false,
638 						new_width * bw->scale,
639 						new_height * bw->scale);
640 				window->width = new_width;
641 				window->height = new_height;
642 
643 				browser_window_handle_scrollbars(window);
644 			}
645 
646 			x += widths[col][row];
647 
648 			if (window->children)
649 				browser_window_recalculate_frameset(window);
650 		}
651 	}
652 }
653 
654 
655 /**
656  * Resize a browser window that is a frame.
657  *
658  * \param bw The browser window to resize
659  * \param x The new width to set.
660  * \param y The new height to set.
661  */
662 
browser_window_resize_frame(struct browser_window * bw,int x,int y)663 void browser_window_resize_frame(struct browser_window *bw, int x, int y)
664 {
665 	struct browser_window *parent;
666 	struct browser_window *sibling;
667 	int col = -1, row = -1, i;
668 	bool change = false;
669 
670 	parent = bw->parent;
671 	assert(parent);
672 
673 	/* get frame location */
674 	for (i = 0; i < (parent->cols * parent->rows); i++) {
675 		if (&parent->children[i] == bw) {
676 			col = i % parent->cols;
677 			row = i / parent->cols;
678 		 }
679 	}
680 	assert((col >= 0) && (row >= 0));
681 
682 	sibling = NULL;
683 	if (bw->drag.resize_left) {
684 		sibling = &parent->children[row * parent->cols + (col - 1)];
685 	} else if (bw->drag.resize_right) {
686 		sibling = &parent->children[row * parent->cols + (col + 1)];
687 	}
688 	if (sibling) {
689 		change |= browser_window_resolve_frame_dimension(bw, sibling,
690 				x, y, true, false);
691 	}
692 
693 	sibling = NULL;
694 	if (bw->drag.resize_up) {
695 		sibling = &parent->children[(row - 1) * parent->cols + col];
696 	} else if (bw->drag.resize_down) {
697 		sibling = &parent->children[(row + 1) * parent->cols + col];
698 	}
699 
700 	if (sibling) {
701 		change |= browser_window_resolve_frame_dimension(bw, sibling,
702 				x, y, false, true);
703 	}
704 
705 	if (change) {
706 		browser_window_recalculate_frameset(parent);
707 	}
708 }
709 
710 
browser_window_resolve_frame_dimension(struct browser_window * bw,struct browser_window * sibling,int x,int y,bool width,bool height)711 bool browser_window_resolve_frame_dimension(struct browser_window *bw,
712 		struct browser_window *sibling,
713 		int x, int y, bool width, bool height)
714 {
715 	int bw_dimension, sibling_dimension;
716 	int bw_pixels, sibling_pixels;
717 	struct frame_dimension *bw_d, *sibling_d;
718 	float total_new;
719 	int frame_size;
720 
721 	assert(!(width && height));
722 
723 	/* extend/shrink the box to the pointer */
724 	if (width) {
725 		if (bw->drag.resize_left) {
726 			bw_dimension = bw->x + bw->width - x;
727 		} else {
728 			bw_dimension = x - bw->x;
729 		}
730 		bw_pixels = bw->width;
731 		sibling_pixels = sibling->width;
732 		bw_d = &bw->frame_width;
733 		sibling_d = &sibling->frame_width;
734 		frame_size = bw->parent->width;
735 	} else {
736 		if (bw->drag.resize_up) {
737 			bw_dimension = bw->y + bw->height - y;
738 		} else {
739 			bw_dimension = y - bw->y;
740 		}
741 		bw_pixels = bw->height;
742 		sibling_pixels = sibling->height;
743 		bw_d = &bw->frame_height;
744 		sibling_d = &sibling->frame_height;
745 		frame_size = bw->parent->height;
746 	}
747 	sibling_dimension = bw_pixels + sibling_pixels - bw_dimension;
748 
749 	/* check for no change or no frame size*/
750 	if ((bw_dimension == bw_pixels) || (frame_size == 0))
751 		return false;
752 	/* check for both being 0 */
753 	total_new = bw_dimension + sibling_dimension;
754 	if ((bw_dimension + sibling_dimension) == 0)
755 		return false;
756 
757 	/* our frame dimensions are now known to be:
758 	 *
759 	 * <--		    frame_size		    --> [VISIBLE PIXELS]
760 	 * |<--  bw_pixels -->|<--  sibling_pixels -->|	[VISIBLE PIXELS, BEFORE RESIZE]
761 	 * |<-- bw_d->value-->|<-- sibling_d->value-->| [SPECIFIED UNITS, BEFORE RESIZE]
762 	 * |<--bw_dimension-->|<--sibling_dimension-->|	[VISIBLE PIXELS, AFTER RESIZE]
763 	 * |<--		     total_new		   -->|	[VISIBLE PIXELS, AFTER RESIZE]
764 	 *
765 	 * when we resize, we must retain the original unit specification such that any
766 	 * subsequent resizing of the parent window will recalculate the page as the
767 	 * author specified.
768 	 *
769 	 * if the units of both frames are the same then we can resize the values simply
770 	 * by updating the values to be a percentage of the original widths.
771 	 */
772 	if (bw_d->unit == sibling_d->unit) {
773 		float total_specified = bw_d->value + sibling_d->value;
774 		bw_d->value = (total_specified * bw_dimension) / total_new;
775 		sibling_d->value = total_specified - bw_d->value;
776 		return true;
777 	}
778 
779 	/* if one of the sizes is relative then we don't alter the relative width and
780 	 * just let it reflow across. the non-relative (pixel/percentage) value can
781 	 * simply be resolved to the specified width that will result in the required
782 	 * dimension.
783 	 */
784 	if (bw_d->unit == FRAME_DIMENSION_RELATIVE) {
785 		if ((sibling_pixels == 0) && (bw_dimension == 0))
786 			return false;
787 		if (fabs(sibling_d->value) < 0.0001)
788 			bw_d->value = 1;
789 		if (sibling_pixels == 0)
790 			sibling_d->value = (sibling_d->value * bw_pixels) / bw_dimension;
791 		else
792 			sibling_d->value =
793 					(sibling_d->value * sibling_dimension) / sibling_pixels;
794 
795 		/* todo: the availble resize may have changed, update the drag box */
796 		return true;
797 	} else if (sibling_d->unit == FRAME_DIMENSION_RELATIVE) {
798 		if ((bw_pixels == 0) && (sibling_dimension == 0))
799 			return false;
800 		if (fabs(bw_d->value) < 0.0001)
801 			bw_d->value = 1;
802 		if (bw_pixels == 0)
803 			bw_d->value = (bw_d->value * sibling_pixels) / sibling_dimension;
804 		else
805 			bw_d->value = (bw_d->value * bw_dimension) / bw_pixels;
806 
807 		/* todo: the availble resize may have changed, update the drag box */
808 		return true;
809 	}
810 
811 	/* finally we have a pixel/percentage mix. unlike relative values, percentages
812 	 * can easily be backwards-calculated as they can simply be scaled like pixel
813 	 * values
814 	 */
815 	if (bw_d->unit == FRAME_DIMENSION_PIXELS) {
816 		float total_specified = bw_d->value + frame_size * sibling_d->value / 100;
817 		bw_d->value = (total_specified * bw_dimension) / total_new;
818 		sibling_d->value = (total_specified - bw_d->value) * 100 / frame_size;
819 		return true;
820 	} else if (sibling_d->unit == FRAME_DIMENSION_PIXELS) {
821 		float total_specified = bw_d->value * frame_size / 100 + sibling_d->value;
822 		sibling_d->value = (total_specified * sibling_dimension) / total_new;
823 		bw_d->value = (total_specified - sibling_d->value) * 100 / frame_size;
824 		return true;
825 	}
826 	assert(!"Invalid frame dimension unit");
827 	return false;
828 }
829 
830 
browser_window_resize_frames(struct browser_window * bw,browser_mouse_state mouse,int x,int y,browser_pointer_shape * pointer)831 static bool browser_window_resize_frames(struct browser_window *bw,
832 		browser_mouse_state mouse, int x, int y,
833 		browser_pointer_shape *pointer)
834 {
835 	struct browser_window *parent;
836 	bool left, right, up, down;
837 	int i, resize_margin;
838 
839 	if ((x < bw->x) || (x > bw->x + bw->width) ||
840 			(y < bw->y) || (y > bw->y + bw->height))
841 		return false;
842 
843 	parent = bw->parent;
844 	if ((!bw->no_resize) && parent) {
845 		resize_margin = FRAME_RESIZE;
846 		if (resize_margin * 2 > bw->width)
847 			resize_margin = bw->width / 2;
848 		left = (x < bw->x + resize_margin);
849 		right = (x > bw->x + bw->width - resize_margin);
850 		resize_margin = FRAME_RESIZE;
851 		if (resize_margin * 2 > bw->height)
852 			resize_margin = bw->height / 2;
853 		up = (y < bw->y + resize_margin);
854 		down = (y > bw->y + bw-> height - resize_margin);
855 
856 		/* check if the edges can actually be moved */
857 		if (left || right || up || down) {
858 			int row = -1, col = -1;
859 			switch (bw->browser_window_type) {
860 				case BROWSER_WINDOW_NORMAL:
861 				case BROWSER_WINDOW_IFRAME:
862 					assert(0);
863 					break;
864 				case BROWSER_WINDOW_FRAME:
865 				case BROWSER_WINDOW_FRAMESET:
866 					break;
867 			}
868 			for (i = 0; i < (parent->cols * parent->rows); i++) {
869 				if (&parent->children[i] == bw) {
870 					col = i % parent->cols;
871 					row = i / parent->cols;
872 					break;
873 				}
874 			}
875 			assert((row >= 0) && (col >= 0));
876 
877 			/* check the sibling frame is within bounds */
878 			left &= (col > 0);
879 			right &= (col < parent->cols - 1);
880 			up &= (row > 0);
881 			down &= (row < parent->rows - 1);
882 
883 			/* check the sibling frames can be resized */
884 			if (left)
885 				left &= !parent->children[row *
886 						parent->cols + (col - 1)].
887 						no_resize;
888 			if (right)
889 				right &= !parent->children[row *
890 						parent->cols + (col + 1)].
891 						no_resize;
892 			if (up)
893 				up &= !parent->children[(row - 1) *
894 						parent->cols + col].
895 						no_resize;
896 			if (down)
897 				down &= !parent->children[(row + 1) *
898 						parent->cols + col].
899 						no_resize;
900 
901 			/* can't have opposite directions simultaneously */
902 			if (up)
903 				down = false;
904 			if (left)
905 				right = false;
906 		}
907 
908 		if (left || right || up || down) {
909 			if (left) {
910 				if (down)
911 					*pointer = BROWSER_POINTER_LD;
912 				else if (up)
913 					*pointer = BROWSER_POINTER_LU;
914 				else
915 					*pointer = BROWSER_POINTER_LEFT;
916 			} else if (right) {
917 				if (down)
918 					*pointer = BROWSER_POINTER_RD;
919 				else if (up)
920 					*pointer = BROWSER_POINTER_RU;
921 				else
922 					*pointer = BROWSER_POINTER_RIGHT;
923 			} else if (up) {
924 				*pointer = BROWSER_POINTER_UP;
925 			} else {
926 				*pointer = BROWSER_POINTER_DOWN;
927 			}
928 			if (mouse & (BROWSER_MOUSE_DRAG_1 |
929 					BROWSER_MOUSE_DRAG_2)) {
930 
931 				/* TODO: Pass appropriate rectangle to allow
932 				 *	 front end to clamp pointer range */
933 				browser_window_set_drag_type(bw,
934 						DRAGGING_FRAME, NULL);
935 				bw->drag.start_x = x;
936 				bw->drag.start_y = y;
937 				bw->drag.resize_left = left;
938 				bw->drag.resize_right = right;
939 				bw->drag.resize_up = up;
940 				bw->drag.resize_down = down;
941 			}
942 			return true;
943 		}
944 	}
945 
946 	if (bw->children) {
947 		for (i = 0; i < (bw->cols * bw->rows); i++)
948 			if (browser_window_resize_frames(&bw->children[i],
949 					mouse, x, y, pointer))
950 				return true;
951 	}
952 	if (bw->iframes) {
953 		for (i = 0; i < bw->iframe_count; i++)
954 			if (browser_window_resize_frames(&bw->iframes[i],
955 					mouse, x, y, pointer))
956 				return true;
957 	}
958 	return false;
959 }
960 
961 
browser_window_frame_resize_start(struct browser_window * bw,browser_mouse_state mouse,int x,int y,browser_pointer_shape * pointer)962 bool browser_window_frame_resize_start(struct browser_window *bw,
963 		browser_mouse_state mouse, int x, int y,
964 		browser_pointer_shape *pointer)
965 {
966 	struct browser_window *root = browser_window_get_root(bw);
967 	int offx, offy;
968 
969 	browser_window_get_position(bw, true, &offx, &offy);
970 
971 	return browser_window_resize_frames(root, mouse,
972 			x + offx, y + offy, pointer);
973 }
974