summaryrefslogtreecommitdiff
path: root/desktop/frames.c
blob: 88d49f20cbacdaccdc4d5bd6bb2a9a529b9f9b1c (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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
/*
 * Copyright 2006 Richard Wilson <info@tinct.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Frame and frameset creation and manipulation (implementation).
 */

#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "utils/config.h"
#include "content/hlcache.h"
#include "desktop/browser_private.h"
#include "desktop/frames.h"
#include "desktop/history_core.h"
#include "desktop/gui.h"
#include "desktop/scrollbar.h"
#include "desktop/selection.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "render/html.h"
#include "render/box.h"

/** maximum frame resize margin */
#define FRAME_RESIZE 6

static bool browser_window_resolve_frame_dimension(struct browser_window *bw,
		struct browser_window *sibling, int x, int y, bool width,
		bool height);


/**
 * Callback for (i)frame scrollbars.
 */
void browser_window_scroll_callback(void *client_data,
		struct scrollbar_msg_data *scrollbar_data)
{
	struct browser_window *bw = client_data;

	switch(scrollbar_data->msg) {
	case SCROLLBAR_MSG_MOVED:
		if (bw->browser_window_type == BROWSER_WINDOW_IFRAME) {
			html_redraw_a_box(bw->parent->current_content, bw->box);
		} else {
			struct rect rect_x;
			struct rect rect_y;
			struct rect copyrect;
			int dest_x;
			int dest_y;
			int cur_x = scrollbar_get_offset(bw->scroll_x);
			int cur_y = scrollbar_get_offset(bw->scroll_y);
			
			rect_y.x0 = 0;				
			rect_y.x1 = bw->width;
			rect_x.y0 = 0;				
			rect_x.y1 = bw->height;
	
			if(cur_y > bw->prev_scroll_y) {
				copyrect.y0 = cur_y;
				copyrect.y1 = copyrect.y0 + bw->height - (cur_y - bw->prev_scroll_y);
				dest_y = bw->prev_scroll_y;
				
				rect_y.y0 = copyrect.y0;				
				rect_y.y1 = copyrect.y0 + bw->height - (cur_y - bw->prev_scroll_y);
				
				rect_x.y0 = 0;				
				rect_x.y1 = rect_y.y0;
			} else {
				copyrect.y0 = bw->prev_scroll_y;
				copyrect.y1 = copyrect.y0 + bw->height - (bw->prev_scroll_y - cur_y);
				dest_y = cur_y;

				rect_y.y0 = cur_y;
				rect_y.y1 = cur_y + bw->height - (bw->prev_scroll_y - cur_y);
				
				rect_x.y0 = rect_y.y1;				
				rect_x.y1 = bw->height;
			}

			if(cur_x > bw->prev_scroll_x) {
				copyrect.x0 = cur_x;
				copyrect.x1 = copyrect.x0 + bw->width - (cur_x - bw->prev_scroll_x);
				dest_x = bw->prev_scroll_x;
				
				rect_x.x0 = copyrect.x0;				
				rect_x.x1 = copyrect.x0 + bw->width - (cur_x - bw->prev_scroll_x);
			} else {
				copyrect.x0 = bw->prev_scroll_x;
				copyrect.x1 = copyrect.x0 + bw->width - (bw->prev_scroll_x - cur_x);
				dest_x = cur_x;

				rect_x.x0 = cur_x;
				rect_x.x1 = cur_x + bw->width - (bw->prev_scroll_x - cur_x);
			}
			
			browser_window_copy_box(bw, &copyrect, dest_x, dest_y);
			browser_window_update_box(bw, &rect_y);
			browser_window_update_box(bw, &rect_x);
			
			bw->prev_scroll_x = scrollbar_get_offset(bw->scroll_x);
			bw->prev_scroll_y = scrollbar_get_offset(bw->scroll_y);
		}
		break;
	case SCROLLBAR_MSG_SCROLL_START:
	{
		struct rect rect = {
			.x0 = scrollbar_data->x0,
			.y0 = scrollbar_data->y0,
			.x1 = scrollbar_data->x1,
			.y1 = scrollbar_data->y1
		};

		if (scrollbar_is_horizontal(scrollbar_data->scrollbar))
			browser_window_set_drag_type(bw, DRAGGING_SCR_X, &rect);
		else
			browser_window_set_drag_type(bw, DRAGGING_SCR_Y, &rect);
	}
		break;
	case SCROLLBAR_MSG_SCROLL_FINISHED:
		browser_window_set_drag_type(bw, DRAGGING_NONE, NULL);

		browser_window_set_pointer(bw, BROWSER_POINTER_DEFAULT);
		break;
	}
}

/* exported interface, documented in browser.h */
void browser_window_handle_scrollbars(struct browser_window *bw)
{
	hlcache_handle *h = bw->current_content;
	bool scroll_x;
	bool scroll_y;
	int c_width = 0;
	int c_height = 0;

	assert(!bw->window); /* Core-handled windows only */

	if (h != NULL) {
		c_width  = content_get_width(h);
		c_height = content_get_height(h);
	}

	if (bw->scrolling == SCROLLING_YES) {
		scroll_x = true;
		scroll_y = true;
	} else if (bw->scrolling == SCROLLING_AUTO &&
			bw->current_content) {
		int bw_width = bw->width;
		int bw_height = bw->height;

		/* subtract existing scrollbar width */
		bw_width -= bw->scroll_y ? SCROLLBAR_WIDTH : 0;
		bw_height -= bw->scroll_x ? SCROLLBAR_WIDTH : 0;

		scroll_y = (c_height > bw_height) ? true : false;
		scroll_x = (c_width > bw_width) ? true : false;
	} else {
		/* No scrollbars */
		scroll_x = false;
		scroll_y = false;
	}

	if (!scroll_x && bw->scroll_x != NULL) {
		scrollbar_destroy(bw->scroll_x);
		bw->scroll_x = NULL;
	}

	if (!scroll_y && bw->scroll_y != NULL) {
		scrollbar_destroy(bw->scroll_y);
		bw->scroll_y = NULL;
	}

	if (scroll_y) {
		int length = bw->height;
		int visible = bw->height - (scroll_x ? SCROLLBAR_WIDTH : 0);

		if (bw->scroll_y == NULL) {
			/* create vertical scrollbar */
			if (!scrollbar_create(false, length, c_height, visible,
					bw, browser_window_scroll_callback,
					&(bw->scroll_y)))
				return;
		} else {
			/* update vertical scrollbar */
			scrollbar_set_extents(bw->scroll_y, length,
					visible, c_height);
		}
	}

	if (scroll_x) {
		int length = bw->width - (scroll_y ? SCROLLBAR_WIDTH : 0);
		int visible = length;

		if (bw->scroll_x == NULL) {
			/* create horizontal scrollbar */
			if (!scrollbar_create(true, length, c_width, visible,
					bw, browser_window_scroll_callback,
					&(bw->scroll_x)))
				return;
		} else {
			/* update horizontal scrollbar */
			scrollbar_set_extents(bw->scroll_x, length,
					visible, c_width);
		}
	}

	if (scroll_x && scroll_y)
		scrollbar_make_pair(bw->scroll_x, bw->scroll_y);
}


/**
 * Create and open a iframes for a browser window.
 *
 * \param  bw	    The browser window to create iframes for
 * \param  iframe   The iframes to create
 */

void browser_window_create_iframes(struct browser_window *bw,
		struct content_html_iframe *iframe)
{
	struct browser_window *window;
	struct content_html_iframe *cur;
	struct rect rect;
	int iframes = 0;
	int index;

	for (cur = iframe; cur; cur = cur->next)
		iframes++;
	bw->iframes = calloc(iframes, sizeof(*bw));
	if (!bw->iframes)
		return;
	bw->iframe_count = iframes;

	index = 0;
	for (cur = iframe; cur; cur = cur->next) {
		window = &(bw->iframes[index++]);

		/* Initialise common parts */
		browser_window_initialise_common(window, NULL);

		/* window characteristics */
		window->browser_window_type = BROWSER_WINDOW_IFRAME;
		window->scrolling = cur->scrolling;
		window->border = cur->border;
		window->border_colour = cur->border_colour;
		window->no_resize = true;
		window->margin_width = cur->margin_width;
		window->margin_height = cur->margin_height;
		window->cur_sel = bw->cur_sel;
		window->scale = bw->scale;
		if (cur->name) {
			window->name = strdup(cur->name);
			if (!window->name)
				warn_user("NoMemory", 0);
		}

		/* linking */
		window->box = cur->box;
		window->parent = bw;
		window->box->iframe = window;

		/* iframe dimensions */
		box_bounds(window->box, &rect);

		browser_window_set_position(window, rect.x0, rect.y0);
		browser_window_set_dimensions(window, rect.x1 - rect.x0,
				rect.y1 - rect.y0);
	}

	/* calculate dimensions */
	browser_window_update_extent(bw);
	browser_window_recalculate_iframes(bw);

	index = 0;
	for (cur = iframe; cur; cur = cur->next) {
		window = &(bw->iframes[index++]);
		if (cur->url) {
			/* fetch iframe's content */
			browser_window_go_unverifiable(window,
					nsurl_access(cur->url),
					nsurl_access(hlcache_handle_get_url(
							bw->current_content)),
					false, bw->current_content);
		}
	}
}


/**
 * Recalculate iframe positions following a resize.
 *
 * \param  bw	    The browser window to reposition iframes for
 */

void browser_window_recalculate_iframes(struct browser_window *bw)
{
	struct browser_window *window;
	int index;

	for (index = 0; index < bw->iframe_count; index++) {
		window = &(bw->iframes[index]);

		if (window != NULL) {
			browser_window_handle_scrollbars(window);
		}
	}
}


/**
 * Create and open a frameset for a browser window.
 *
 * \param  bw	    The browser window to create the frameset for
 * \param  iframe   The frameset to create
 */

void browser_window_create_frameset(struct browser_window *bw,
		struct content_html_frames *frameset)
{
	int row, col, index;
	struct content_html_frames *frame;
	struct browser_window *window;
	hlcache_handle *parent;

	assert(bw && frameset);

	/* 1. Create children */
	assert(bw->children == NULL);
	assert(frameset->cols + frameset->rows != 0);

	bw->children = calloc((frameset->cols * frameset->rows), sizeof(*bw));
	if (!bw->children)
		return;
	bw->cols = frameset->cols;
	bw->rows = frameset->rows;
	for (row = 0; row < bw->rows; row++) {
		for (col = 0; col < bw->cols; col++) {
			index = (row * bw->cols) + col;
			frame = &frameset->children[index];
			window = &bw->children[index];

			/* Initialise common parts */
			browser_window_initialise_common(window, NULL);

			/* window characteristics */
			if (frame->children)
				window->browser_window_type =
						BROWSER_WINDOW_FRAMESET;
			else
				window->browser_window_type =
						BROWSER_WINDOW_FRAME;
			window->scrolling = frame->scrolling;
			window->border = frame->border;
			window->border_colour = frame->border_colour;
			window->no_resize = frame->no_resize;
			window->frame_width = frame->width;
			window->frame_height = frame->height;
			window->margin_width = frame->margin_width;
			window->margin_height = frame->margin_height;
			if (frame->name) {
				window->name = strdup(frame->name);
				if (!window->name)
					warn_user("NoMemory", 0);
			}

			window->cur_sel = bw->cur_sel;
			window->scale = bw->scale;

			/* linking */
			window->parent = bw;

			if (window->name)
				LOG(("Created frame '%s'", window->name));
			else
				LOG(("Created frame (unnamed)"));
		}
	}

	/* 2. Calculate dimensions */
	browser_window_update_extent(bw);
	browser_window_recalculate_frameset(bw);

	/* 3. Recurse for grandchildren */
	for (row = 0; row < bw->rows; row++) {
		for (col = 0; col < bw->cols; col++) {
			index = (row * bw->cols) + col;
			frame = &frameset->children[index];
			window = &bw->children[index];

			if (frame->children)
				browser_window_create_frameset(window, frame);
		}
	}

	/* Use the URL of the first ancestor window containing html content
	 * as the referer */
	for (window = bw; window->parent; window = window->parent) {
		if (window->current_content && 
				content_get_type(window->current_content) == 
				CONTENT_HTML)
			break;
	}

	parent = window->current_content;

	/* 4. Launch content */
	for (row = 0; row < bw->rows; row++) {
		for (col = 0; col < bw->cols; col++) {
			index = (row * bw->cols) + col;
			frame = &frameset->children[index];
			window = &bw->children[index];

			if (frame->url) {
				browser_window_go_unverifiable(window,
						nsurl_access(frame->url),
						nsurl_access(hlcache_handle_get_url(
								parent)),
						true,
						parent);
			}
		}
	}
}


/**
 * Recalculate frameset positions following a resize.
 *
 * \param  bw	    The browser window to reposition framesets for
 */

void browser_window_recalculate_frameset(struct browser_window *bw)
{
	int widths[bw->cols][bw->rows];
	int heights[bw->cols][bw->rows];
	int bw_width, bw_height;
	int avail_width, avail_height;
	int row, row2, col, index;
	struct browser_window *window;
	float relative;
	int size, extent, applied;
	int x, y;
	int new_width, new_height;

	assert(bw);

	/* window dimensions */
	if (!bw->parent) {
		browser_window_get_dimensions(bw, &bw_width, &bw_height, true);
		bw->x = 0;
		bw->y = 0;
		bw->width = bw_width;
		bw->height = bw_height;
	} else {
		bw_width = bw->width;
		bw_height = bw->height;
	}
	bw_width++;
	bw_height++;

	/* widths */
	for (row = 0; row < bw->rows; row++) {
		avail_width = bw_width;
		relative = 0;
		for (col = 0; col < bw->cols; col++) {
			index = (row * bw->cols) + col;
			window = &bw->children[index];

			switch (window->frame_width.unit) {
			case FRAME_DIMENSION_PIXELS:
				widths[col][row] = window->frame_width.value *
						window->scale;
				if (window->border) {
					if (col != 0)
						widths[col][row] += 1;
					if (col != bw->cols - 1)
						widths[col][row] += 1;
				}
				break;
			case FRAME_DIMENSION_PERCENT:
				widths[col][row] = bw_width *
						window->frame_width.value / 100;
				break;
			case FRAME_DIMENSION_RELATIVE:
				widths[col][row] = 0;
				relative += window->frame_width.value;
				break;
			default:
				/* unknown frame dimension unit */
				assert(window->frame_width.unit ==
						FRAME_DIMENSION_PIXELS ||
						window->frame_width.unit ==
						FRAME_DIMENSION_PERCENT ||
						window->frame_width.unit ==
						FRAME_DIMENSION_RELATIVE);
				break;
			}
			avail_width -= widths[col][row];
		}

		/* Redistribute to fit window */
		if ((relative > 0) && (avail_width > 0)) {
			/* Expand the relative sections to fill remainder */
			for (col = 0; col < bw->cols; col++) {
				index = (row * bw->cols) + col;
				window = &bw->children[index];

				if (window->frame_width.unit ==
						FRAME_DIMENSION_RELATIVE) {
					size = avail_width * window->
							frame_width.value /
							relative;
					avail_width -= size;
					relative -= window->frame_width.value;
					widths[col][row] += size;
				}
			}
		} else if (bw_width != avail_width) {
			/* proportionally distribute error */
			extent = avail_width;
			applied = 0;
			for (col = 0; col < bw->cols; col++) {
				if (col == bw->cols - 1) {
					/* Last cell, use up remainder */
					widths[col][row] += extent - applied;
					widths[col][row] =
							widths[col][row] < 0 ?
							0 : widths[col][row];
				} else {
					/* Find size of cell adjustment */
					size = (widths[col][row] * extent) /
							(bw_width - extent);
					/* Modify cell */
					widths[col][row] += size;
					applied += size;
				}
			}
		}
	}

	/* heights */
	for (col = 0; col < bw->cols; col++) {
		avail_height = bw_height;
		relative = 0;
		for (row = 0; row < bw->rows; row++) {
			index = (row * bw->cols) + col;
			window = &bw->children[index];

			switch (window->frame_height.unit) {
			case FRAME_DIMENSION_PIXELS:
				heights[col][row] = window->frame_height.value *
						window->scale;
				if (window->border) {
					if (row != 0)
						heights[col][row] += 1;
					if (row != bw->rows - 1)
						heights[col][row] += 1;
				}
				break;
			case FRAME_DIMENSION_PERCENT:
				heights[col][row] = bw_height *
						window->frame_height.value / 100;
				break;
			case FRAME_DIMENSION_RELATIVE:
				heights[col][row] = 0;
				relative += window->frame_height.value;
				break;
			default:
				/* unknown frame dimension unit */
				assert(window->frame_height.unit ==
						FRAME_DIMENSION_PIXELS ||
						window->frame_height.unit ==
						FRAME_DIMENSION_PERCENT ||
						window->frame_height.unit ==
						FRAME_DIMENSION_RELATIVE);
				break;
			}
			avail_height -= heights[col][row];
		}

		if (avail_height == 0)
			continue;

		/* Redistribute to fit window */
		if ((relative > 0) && (avail_height > 0)) {
			/* Expand the relative sections to fill remainder */
			for (row = 0; row < bw->rows; row++) {
				index = (row * bw->cols) + col;
				window = &bw->children[index];

				if (window->frame_height.unit ==
						FRAME_DIMENSION_RELATIVE) {
					size = avail_height * window->
							frame_height.value /
							relative;
					avail_height -= size;
					relative -= window->frame_height.value;
					heights[col][row] += size;
				}
			}
		} else if (bw_height != avail_height) {
			/* proportionally distribute error */
			extent = avail_height;
			applied = 0;
			for (row = 0; row < bw->rows; row++) {
				if (row == bw->rows - 1) {
					/* Last cell, use up remainder */
					heights[col][row] += extent - applied;
					heights[col][row] =
							heights[col][row] < 0 ?
							0 : heights[col][row];
				} else {
					/* Find size of cell adjustment */
					size = (heights[col][row] * extent) /
							(bw_height - extent);
					/* Modify cell */
					heights[col][row] += size;
					applied += size;
				}
			}
		}
	}

	/* position frames and calculate children */
	for (row = 0; row < bw->rows; row++) {
		x = 0;
		for (col = 0; col < bw->cols; col++) {
			index = (row * bw->cols) + col;
			window = &bw->children[index];

			y = 0;
			for (row2 = 0; row2 < row; row2++)
				y+= heights[col][row2];

			window->x = x;
			window->y = y;

			new_width = widths[col][row] - 1;
			new_height = heights[col][row] - 1;

			if (window->width != new_width ||
					window->height != new_height) {
				/* Change in frame size */
				browser_window_reformat(window, false,
						new_width * bw->scale,
						new_height * bw->scale);
				window->width = new_width;
				window->height = new_height;

				browser_window_handle_scrollbars(window);
			}

			x += widths[col][row];

			if (window->children)
				browser_window_recalculate_frameset(window);
		}
	}
}


/**
 * Resize a browser window that is a frame.
 *
 * \param  bw	    The browser window to resize
 */

void browser_window_resize_frame(struct browser_window *bw, int x, int y)
{
	struct browser_window *parent;
	struct browser_window *sibling;
	int col = -1, row = -1, i;
	bool change = false;

	parent = bw->parent;
	assert(parent);

	/* get frame location */
	for (i = 0; i < (parent->cols * parent->rows); i++) {
		if (&parent->children[i] == bw) {
			col = i % parent->cols;
			row = i / parent->cols;
		 }
	}
	assert((col >= 0) && (row >= 0));

	sibling = NULL;
	if (bw->drag_resize_left)
		sibling = &parent->children[row * parent->cols + (col - 1)];
	else if (bw->drag_resize_right)
		sibling = &parent->children[row * parent->cols + (col + 1)];
	if (sibling)
		change |= browser_window_resolve_frame_dimension(bw, sibling,
				x, y, true, false);

	sibling = NULL;
	if (bw->drag_resize_up)
		sibling = &parent->children[(row - 1) * parent->cols + col];
	else if (bw->drag_resize_down)
		sibling = &parent->children[(row + 1) * parent->cols + col];
	if (sibling)
		change |= browser_window_resolve_frame_dimension(bw, sibling,
				x, y, false, true);

	if (change)
		browser_window_recalculate_frameset(parent);
}


bool browser_window_resolve_frame_dimension(struct browser_window *bw,
		struct browser_window *sibling,
		int x, int y, bool width, bool height)
{
	int bw_dimension, sibling_dimension;
	int bw_pixels, sibling_pixels;
	struct frame_dimension *bw_d, *sibling_d;
	float total_new;
	int frame_size;

	assert(!(width && height));

	/* extend/shrink the box to the pointer */
	if (width) {
		if (bw->drag_resize_left)
			bw_dimension = bw->x + bw->width - x;
		else
			bw_dimension = x - bw->x;
		bw_pixels = bw->width;
		sibling_pixels = sibling->width;
		bw_d = &bw->frame_width;
		sibling_d = &sibling->frame_width;
		frame_size = bw->parent->width;
	} else {
		if (bw->drag_resize_up)
			bw_dimension = bw->y + bw->height - y;
		else
			bw_dimension = y - bw->y;
		bw_pixels = bw->height;
		sibling_pixels = sibling->height;
		bw_d = &bw->frame_height;
		sibling_d = &sibling->frame_height;
		frame_size = bw->parent->height;
	}
	sibling_dimension = bw_pixels + sibling_pixels - bw_dimension;

	/* check for no change or no frame size*/
	if ((bw_dimension == bw_pixels) || (frame_size == 0))
		return false;
	/* check for both being 0 */
	total_new = bw_dimension + sibling_dimension;
	if ((bw_dimension + sibling_dimension) == 0)
		return false;

	/* our frame dimensions are now known to be:
	 *
	 * <--		    frame_size		    --> [VISIBLE PIXELS]
	 * |<--  bw_pixels -->|<--  sibling_pixels -->|	[VISIBLE PIXELS, BEFORE RESIZE]
	 * |<-- bw_d->value-->|<-- sibling_d->value-->| [SPECIFIED UNITS, BEFORE RESIZE]
	 * |<--bw_dimension-->|<--sibling_dimension-->|	[VISIBLE PIXELS, AFTER RESIZE]
	 * |<--		     total_new		   -->|	[VISIBLE PIXELS, AFTER RESIZE]
	 *
	 * when we resize, we must retain the original unit specification such that any
	 * subsequent resizing of the parent window will recalculate the page as the
	 * author specified.
	 *
	 * if the units of both frames are the same then we can resize the values simply
	 * by updating the values to be a percentage of the original widths.
	 */
	if (bw_d->unit == sibling_d->unit) {
		float total_specified = bw_d->value + sibling_d->value;
		bw_d->value = (total_specified * bw_dimension) / total_new;
		sibling_d->value = total_specified - bw_d->value;
		return true;
	}

	/* if one of the sizes is relative then we don't alter the relative width and
	 * just let it reflow across. the non-relative (pixel/percentage) value can
	 * simply be resolved to the specified width that will result in the required
	 * dimension.
	 */
	if (bw_d->unit == FRAME_DIMENSION_RELATIVE) {
		if ((sibling_pixels == 0) && (bw_dimension == 0))
			return false;
		if (fabs(sibling_d->value) < 0.0001)
			bw_d->value = 1;
		if (sibling_pixels == 0)
			sibling_d->value = (sibling_d->value * bw_pixels) / bw_dimension;
		else
			sibling_d->value =
					(sibling_d->value * sibling_dimension) / sibling_pixels;

		/* todo: the availble resize may have changed, update the drag box */
		return true;
	} else if (sibling_d->unit == FRAME_DIMENSION_RELATIVE) {
		if ((bw_pixels == 0) && (sibling_dimension == 0))
			return false;
		if (fabs(bw_d->value) < 0.0001)
			bw_d->value = 1;
		if (bw_pixels == 0)
			bw_d->value = (bw_d->value * sibling_pixels) / sibling_dimension;
		else
			bw_d->value = (bw_d->value * bw_dimension) / bw_pixels;

		/* todo: the availble resize may have changed, update the drag box */
		return true;
	}

	/* finally we have a pixel/percentage mix. unlike relative values, percentages
	 * can easily be backwards-calculated as they can simply be scaled like pixel
	 * values
	 */
	if (bw_d->unit == FRAME_DIMENSION_PIXELS) {
		float total_specified = bw_d->value + frame_size * sibling_d->value / 100;
		bw_d->value = (total_specified * bw_dimension) / total_new;
		sibling_d->value = (total_specified - bw_d->value) * 100 / frame_size;
		return true;
	} else if (sibling_d->unit == FRAME_DIMENSION_PIXELS) {
		float total_specified = bw_d->value * frame_size / 100 + sibling_d->value;
		sibling_d->value = (total_specified * sibling_dimension) / total_new;
		bw_d->value = (total_specified - sibling_d->value) * 100 / frame_size;
		return true;
	}
	assert(!"Invalid frame dimension unit");
	return false;
}


static bool browser_window_resize_frames(struct browser_window *bw,
		browser_mouse_state mouse, int x, int y,
		browser_pointer_shape *pointer)
{
	struct browser_window *parent;
	bool left, right, up, down;
	int i, resize_margin;

	if ((x < bw->x) || (x > bw->x + bw->width) ||
			(y < bw->y) || (y > bw->y + bw->height))
		return false;

	parent = bw->parent;
	if ((!bw->no_resize) && parent) {
		resize_margin = FRAME_RESIZE;
		if (resize_margin * 2 > bw->width)
			resize_margin = bw->width / 2;
		left = (x < bw->x + resize_margin);
		right = (x > bw->x + bw->width - resize_margin);
		resize_margin = FRAME_RESIZE;
		if (resize_margin * 2 > bw->height)
			resize_margin = bw->height / 2;
		up = (y < bw->y + resize_margin);
		down = (y > bw->y + bw-> height - resize_margin);

		/* check if the edges can actually be moved */
		if (left || right || up || down) {
			int row = -1, col = -1;
			switch (bw->browser_window_type) {
				case BROWSER_WINDOW_NORMAL:
				case BROWSER_WINDOW_IFRAME:
					assert(0);
					break;
				case BROWSER_WINDOW_FRAME:
				case BROWSER_WINDOW_FRAMESET:
					break;
			}
			for (i = 0; i < (parent->cols * parent->rows); i++) {
				if (&parent->children[i] == bw) {
					col = i % parent->cols;
					row = i / parent->cols;
					break;
				}
			}
			assert((row >= 0) && (col >= 0));

			/* check the sibling frame is within bounds */
			left &= (col > 0);
			right &= (col < parent->cols - 1);
			up &= (row > 0);
			down &= (row < parent->rows - 1);

			/* check the sibling frames can be resized */
			if (left)
				left &= !parent->children[row *
						parent->cols + (col - 1)].
						no_resize;
			if (right)
				right &= !parent->children[row *
						parent->cols + (col + 1)].
						no_resize;
			if (up)
				up &= !parent->children[(row - 1) *
						parent->cols + col].
						no_resize;
			if (down)
				down &= !parent->children[(row + 1) *
						parent->cols + col].
						no_resize;

			/* can't have opposite directions simultaneously */
			if (up)
				down = false;
			if (left)
				right = false;
		}

		if (left || right || up || down) {
			if (left) {
				if (down)
					*pointer = BROWSER_POINTER_LD;
				else if (up)
					*pointer = BROWSER_POINTER_LU;
				else
					*pointer = BROWSER_POINTER_LEFT;
			} else if (right) {
				if (down)
					*pointer = BROWSER_POINTER_RD;
				else if (up)
					*pointer = BROWSER_POINTER_RU;
				else
					*pointer = BROWSER_POINTER_RIGHT;
			} else if (up) {
				*pointer = BROWSER_POINTER_UP;
			} else {
				*pointer = BROWSER_POINTER_DOWN;
			}
			if (mouse & (BROWSER_MOUSE_DRAG_1 |
					BROWSER_MOUSE_DRAG_2)) {

				/* TODO: Pass appropriate rectangle to allow
				 *	 front end to clamp pointer range */
				browser_window_set_drag_type(bw,
						DRAGGING_FRAME, NULL);
				bw->drag_start_x = x;
				bw->drag_start_y = y;
				bw->drag_resize_left = left;
				bw->drag_resize_right = right;
				bw->drag_resize_up = up;
				bw->drag_resize_down = down;
			}
			return true;
		}
	}

	if (bw->children) {
		for (i = 0; i < (bw->cols * bw->rows); i++)
			if (browser_window_resize_frames(&bw->children[i],
					mouse, x, y, pointer))
				return true;
	}
	if (bw->iframes) {
		for (i = 0; i < bw->iframe_count; i++)
			if (browser_window_resize_frames(&bw->iframes[i],
					mouse, x, y, pointer))
				return true;
	}
	return false;
}


bool browser_window_frame_resize_start(struct browser_window *bw,
		browser_mouse_state mouse, int x, int y,
		browser_pointer_shape *pointer)
{
	struct browser_window *root = browser_window_get_root(bw);
	int offx, offy;

	browser_window_get_position(bw, true, &offx, &offy);

	return browser_window_resize_frames(root, mouse,
			x + offx, y + offy, pointer);
}