File: htmlrelayout.c

package info (click to toggle)
claws-mail-extra-plugins 3.5.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 35,040 kB
  • ctags: 10,917
  • sloc: ansic: 86,223; sh: 15,775; makefile: 4,400; perl: 983; yacc: 219; lex: 168
file content (353 lines) | stat: -rw-r--r-- 9,716 bytes parent folder | download | duplicates (6)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
   Copyright (C) 2000 CodeFactory AB
   Copyright (C) 2000 Jonas Borgstrm <jonas@codefactory.se>
   Copyright (C) 2000 Anders Carlsson <andersca@codefactory.se>
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <glib.h>
#include "htmlbox.h"
#include "htmlrelayout.h"
#include "htmlboxroot.h"

/*
 * FIXME: All these left_margin, max_width functions aren't complete untill we have a box->is_relayout flag that is working. / jb
 */


HtmlRelayout *
html_relayout_new (void)
{
	HtmlRelayout *relayout;

	relayout = g_new0 (HtmlRelayout, 1);

	return relayout;
}

void
html_relayout_destroy (HtmlRelayout *relayout)
{
	g_return_if_fail (relayout != NULL);

	g_free (relayout);
}

/*
 * This function should return the next y offset (box->y) where the
 * block should try to fit the box. It should also return -1 if there
 * aren't any more places to try */
static gint
html_relayout_next_float_offset_real (HtmlRelayout *relayout, HtmlBox *box, gint y, gint width, gint height, GSList *list)
{
	gint best = G_MAXINT;
	gint boxx, boxy;

	if (list == NULL)
		return -1;

	boxx = html_box_get_absolute_x (box) + html_box_left_mbp_sum (box, -1);
	boxy = html_box_get_absolute_y (box) + html_box_top_mbp_sum (box, -1);

	while (list) {
		HtmlBox *Float;
		gint Floatx, Floaty;

		Float = (HtmlBox *)list->data;

		/* If we have found our self in this list, then stop. Because a floating box doesn't
		   intercept the layout of boxes before itself */
		if (Float->is_relayouted == FALSE) {
			list = list->next;

			continue;
		}
		Floatx = html_box_get_absolute_x (Float);
		Floaty = html_box_get_absolute_y (Float);

		if ((boxy + y + height) > Floaty && (Floaty + Float->height) > (boxy + y) &&
		    (boxx + width) > Floatx && boxx < Floatx + Float->width && 
		    Floaty + Float->height < best)
			best = Floaty + Float->height;

		list = g_slist_next (list);
	}

	if (best == G_MAXINT)
		return -1;
	else
		return best - boxy;
}

/*
 * This function should return the next y offset (box->y) where the
 * block should try to fit the box. It should also return -1 if there
 * aren't any more places to try */
gint
html_relayout_next_float_offset (HtmlRelayout *relayout, HtmlBox *box, gint y, gint width, gint height)
{
	gint left, right;

	left  = html_relayout_next_float_offset_real (relayout, box, y, width, height, html_box_root_get_float_left_list  (HTML_BOX_ROOT (relayout->root)));
	right = html_relayout_next_float_offset_real (relayout, box, y, width, height, html_box_root_get_float_right_list (HTML_BOX_ROOT (relayout->root)));

	if (left == -1 && right == -1)
		return -1;
	else {
		if (left == -1)
			left = G_MAXINT;
		if (right == -1)
			right = G_MAXINT;
		return MIN (left, right);
	}
}

static gboolean
float_in_float (HtmlBox *box, HtmlBox *parent)
{
	box = box->parent;

	while (box) {

		if (box == parent)
			return FALSE;
		if (HTML_BOX_GET_STYLE (box)->Float != HTML_FLOAT_NONE)
			return TRUE;

		box = box->parent;
	}
	return FALSE;
}

gint 
html_relayout_get_left_margin (HtmlRelayout *relayout, HtmlBox *self, gint width, gint height, gint y)
{
	return html_relayout_get_left_margin_ignore (relayout, self, width, height, y, NULL);
}

gint 
html_relayout_get_left_margin_ignore (HtmlRelayout *relayout, HtmlBox *self, gint width, gint height, gint y, HtmlBox *ignore)
{
	GSList *list = html_box_root_get_float_left_list (HTML_BOX_ROOT (relayout->root));
	HtmlBox *Float;
	gint best, selfxwidth, x, boxx;
	gint Floatxwidth, Floatx, Floaty;

	if (list == NULL)
		return 0;

	best       = html_box_get_absolute_x (self);
	boxx       = best + html_box_left_mbp_sum (self, -1);
	selfxwidth = best + width;
#if 0
	selfxwidth = best + self->width - html_box_right_mbp_sum (self, -1);
#endif
	best      += html_box_left_mbp_sum (self, -1);
	x          = best;
	y          = html_box_get_absolute_y (self) + html_box_top_mbp_sum  (self, -1) + y;

	while (list) {
		Float = list->data;
		
		/* If we have found our self in this list, then stop. Because a floating box doesn't
		   intercept the layout of boxes before itself */
		if (Float->is_relayouted == FALSE) {
			list = list->next;

			continue;
		}
		if (Float == ignore)
			break;

		Floatx = html_box_get_absolute_x (Float);
		Floaty = html_box_get_absolute_y (Float);
		Floatxwidth = Floatx + Float->width;

		if ((y + height) > Floaty && (Floaty + Float->height) > y &&
		    selfxwidth > Floatx && boxx < Floatx + Float->width &&
		    best < Floatxwidth && !float_in_float (Float, self))
			best = Floatxwidth;

		list = list->next;
	}

	return (best - x) > 0 ? best - x : 0;
}

gint 
html_relayout_get_max_width (HtmlRelayout *relayout, HtmlBox *box, gint width, gint height, gint y)
{
	return html_relayout_get_max_width_ignore (relayout, box, width, height, y, NULL);
}
/*
 * This function returns the maximum width the box can have depending 
 * on the "flow:right" block boxes. If there aren't any "float:right"
 * boxes this function will return -1.
 */
gint 
html_relayout_get_max_width_ignore (HtmlRelayout *relayout, HtmlBox *box, gint width, gint height, gint y, HtmlBox *ignore)
{
	GSList *list = html_box_root_get_float_right_list (HTML_BOX_ROOT (relayout->root));
	HtmlBox *Float;
	gint best = G_MAXINT;
	gint boxx, boxy, Floatxwidth, Floatx, Floaty;
	gint boxy_box_height, x;

	if (list == NULL)
		return -1;

	boxx = x = html_box_get_absolute_x (box) + html_box_left_mbp_sum (box, -1);
	boxy = html_box_get_absolute_y (box) + html_box_top_mbp_sum (box, -1) + y;

	boxy_box_height = boxy + height;

	while (list) {
		Float = list->data;

		/* If we have found our self in this list, then stop. Because a floating box doesn't
		   intercept the layout of boxes before itself */
		if (Float->is_relayouted == FALSE) {
			list = list->next;

			continue;
		}
		if (Float == ignore)
			break;

		Floatx = html_box_get_absolute_x (Float);
		Floaty = html_box_get_absolute_y (Float);
		Floatxwidth = Floatx + Float->width;

		if (Floaty < boxy_box_height &&
		    boxy   < Floaty + Float->height &&
		    Floatx < boxx + width &&
		    Floatxwidth > boxx &&
		    Floatx <= best && !float_in_float (Float, box))
			best = Floatx;

		list = list->next;
	}
	if (best == G_MAXINT)
		return -1;

	return best - x >= 0 ? best - x : 0;
}

/* A helper function to position "float:left" boxes */
static gboolean 
html_relayout_will_fit_left (HtmlBox *parent, HtmlRelayout *relayout, HtmlBox *box, gint width, gint y)
{
	gint left_margin, max, real_max;

	left_margin = html_relayout_get_left_margin_ignore (relayout, parent, width, box->height, y, box);
	max = real_max = html_relayout_get_max_width_ignore (relayout, parent, width, box->height, y, box);
	if (max == -1)
		max = parent->width - html_box_horizontal_mbp_sum (parent);

	if (box->x < left_margin)
		return FALSE;
	
	if (max - left_margin < box->width && (real_max != -1 || left_margin != 0))
		return FALSE;
	
	if (max - left_margin >= box->width && (box->x + box->width > max))
		return FALSE;
	
	return TRUE;
}

/* A helper function to position "float:right" boxes */
static gboolean 
html_relayout_will_fit_right (HtmlBox *parent, HtmlRelayout *relayout, HtmlBox *box, gint width, gint y)
{
	gint max, real_max, left_margin;

	left_margin = html_relayout_get_left_margin (relayout, parent, width, box->height, y);
	max = real_max = html_relayout_get_max_width_ignore (relayout, parent, width, box->height, y, box);
	if (max == -1)
		max = parent->width - html_box_horizontal_mbp_sum (parent);

	if (real_max != -1 && box->x + box->width > max)
		return FALSE;

	if (left_margin > box->x && (left_margin > 0 || real_max != -1))
		return FALSE;

	return TRUE;
}

/* A helper function to position "float:left" boxes */
void
html_relayout_make_fit_left (HtmlBox *parent, HtmlRelayout *relayout, HtmlBox *box, gint width, gint y)
{
	while (html_relayout_will_fit_left (parent, relayout, box, width, y) == FALSE) {

		gint next_y;

		next_y = html_relayout_next_float_offset (relayout, parent, y, width, box->height);

		if (next_y != -1)
			y = next_y;
		else
			break;

		box->x = html_relayout_get_left_margin_ignore (relayout, parent, width, box->height, y, box);
	}
	box->y = y;
}


/* A helper function to position "float:right" boxes */
void
html_relayout_make_fit_right (HtmlBox *parent, HtmlRelayout *relayout, HtmlBox *box, gint width, gint y)
{
	while (html_relayout_will_fit_right (parent, relayout, box, width, y) == FALSE) {
		gint max_width, max_width2, next_y;
		
		next_y = html_relayout_next_float_offset (relayout, parent, y, width, box->height);
		
		if (next_y != -1)
			y = next_y;
		else
			break;
		
		max_width2 = max_width = html_relayout_get_max_width_ignore (relayout, parent, width, box->height, y, box);
		if (max_width == -1)
			max_width = parent->width - html_box_horizontal_mbp_sum (parent);

		box->x = max_width - box->width;
#if 0
		if (box->x < 0 && max_width2 == -1)
			box->x = 0;
#endif
	}
	box->y = y;
}