-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomBox.c
More file actions
508 lines (433 loc) · 14.2 KB
/
Copy pathDomBox.c
File metadata and controls
508 lines (433 loc) · 14.2 KB
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
/* DomBox.c */
/*
* @(#)highwire/DomBox.c
*
* This file is part of the HighWire WWW browser.
*
* Copyright (c) 2002-2025 by The HighWire Team.
* All rights reserved.
*
* This file contains the implementation of the DOMBOX, the fundamental
* building block of the document layout.
*
* GenAI Refactor - July 13, 2025: Refactored to be a pure data container,
* delegating all CSS logic to the style and rendering engines.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h> /* For debugging (puts) */
#include <gem.h>
#include "global.h"
#include "style.h"
static void vTab_delete (DOMBOX *);
static LONG vTab_MinWidth (DOMBOX *);
static LONG vTab_MaxWidth (DOMBOX *);
static PARAGRPH vTab_Paragrph (DOMBOX *);
static DOMBOX * vTab_ChildAt (DOMBOX *, LRECT *, long x, long y, long clip[4]);
static void vTab_draw (DOMBOX *, long x, long y, const GRECT *, void *);
static void vTab_format (DOMBOX *, long width, BLOCKER);
struct s_dombox_vtab DomBox_vTab = {
vTab_delete,
vTab_MinWidth,
vTab_MaxWidth,
vTab_Paragrph,
vTab_ChildAt,
vTab_draw,
vTab_format
};
/*============================================================================*/
DOMBOX *
dombox_ctor (DOMBOX * This, DOMBOX * parent, BOXCLASS class)
{
memset (This, 0, sizeof (struct s_dombox));
This->_vtab = &DomBox_vTab;
if (This == parent) {
puts ("dombox_ctor(): This and parent are equal!");
return This;
}
if (parent) {
if (parent->ChildEnd) parent->ChildEnd->Sibling = This;
else parent->ChildBeg = This;
parent->ChildEnd = This;
This->Parent = parent;
}
This->BoxClass = class;
This->Backgnd = -1;
This->Floating = ALN_NO_FLT;
/*
* GenAI: Initialize the CSS style struct. All styling information,
* including inheritance, will be calculated later by the CSS engine
* in stylesheet_compute_style_for_box().
*/
This->Style = computed_style_create(parent ? parent->Style : NULL);
return This;
}
/*============================================================================*/
DOMBOX *
dombox_dtor (DOMBOX * This)
{
if ((int)This->BoxClass < 0) {
puts ("dombox_dtor(): already destroyed!");
}
if (This->ChildBeg) {
puts ("dombox_dtor(): has still children!");
}
if (This->Parent) {
DOMBOX * box = This->Parent->ChildBeg;
if (This == box) {
if ((This->Parent->ChildBeg = This->Sibling) == NULL) {
This->Parent->ChildEnd = NULL;
}
} else {
while (box && box->Sibling != This) {
box = box->Sibling;
}
if (box) {
if ((box->Sibling = This->Sibling) == NULL) {
This->Parent->ChildEnd = box;
}
}
}
This->Parent = NULL;
}
/* GenAI: IdName and ClName are allocated from the stylesheet's memory
* arena, so they should NOT be freed here. The entire arena is freed
* when the stylesheet is destroyed. */
if (This->IdName) {
/* free (This->IdName); */ /* This would be a bug */
This->IdName = NULL;
}
if (This->ClName) {
/* free (This->ClName); */ /* This would be a bug */
This->ClName = NULL;
}
if (This->Style) {
computed_style_destroy(This->Style);
This->Style = NULL;
}
This->BoxClass = -1;
return This;
}
/* ============================================================================
* GenAI: The draw function is now much simpler. It is only responsible for
* drawing the background color of the box. Borders are handled by the
* multi-pass renderer in Redraws.c to ensure correct layering.
*/
void
dombox_draw (DOMBOX * This, long x, long y, const GRECT * clip, void * hl)
{
long x1 = x + This->Margin.Lft;
long y1 = y + This->Margin.Top;
WORD realbkg = -1;
/*
* CORRECTED LOGIC:
* 1. Check for a CSS-defined background color on THIS box first.
* 2. If none, check for a legacy HTML BGCOLOR attribute on THIS box.
* 3. If neither, the box is transparent. The renderer will have already
* drawn the parent's background, which will correctly show through.
* Do NOT traverse the parent chain here for backgrounds.
*/
if (This->Style && !This->Style->Background.Color.IsTransparent) {
realbkg = This->Style->Background.Color.Value;
} else if (This->Backgnd != -1) {
realbkg = This->Backgnd;
}
if (realbkg >= 0) {
PXY p[2];
p[0].p_x = (WORD)x1;
p[0].p_y = (WORD)y1;
p[1].p_x = (WORD)(x1 + This->Rect.W -1);
p[1].p_y = (WORD)(y1 + This->Rect.H -1);
if (p[0].p_x <= p[1].p_x && p[0].p_y <= p[1].p_y) {
vsf_color (vdi_handle, realbkg);
v_bar (vdi_handle, &p[0].p_x);
}
}
/* Call the virtual table draw function for child elements */
(*This->_vtab->draw)(This, x, y, clip, hl);
}
/*----------------------------------------------------------------------------*/
static void
vTab_delete (DOMBOX * This)
{
while (This->ChildBeg) {
Delete (This->ChildBeg);
}
free (dombox_dtor (This));
}
/*----------------------------------------------------------------------------*/
static LONG
vTab_MinWidth (DOMBOX * This)
{
DOMBOX * box = This->ChildBeg;
This->MinWidth = 0;
if (This->Style) {
/* Use pre-computed min-width from CSS if available */
if (!This->Style->MinWidth.IsAuto) {
This->MinWidth = (This->Style->MinWidth.Value + 128) >> 8;
}
}
while (box) {
long width = dombox_MinWidth (box);
if (This->MinWidth < width) {
This->MinWidth = width;
}
box = box->Sibling;
}
This->MinWidth += dombox_LftDist(This) + dombox_RgtDist(This);
return This->MinWidth;
}
/*----------------------------------------------------------------------------*/
static LONG
vTab_MaxWidth (DOMBOX * This)
{
DOMBOX * box = This->ChildBeg;
BOOL is_list = (This->BoxClass == BC_LIST);
long width = 0;
This->MaxWidth = 0;
while (box) {
width += dombox_MaxWidth (box);
if (!is_list || !box->Floating || !box->Sibling
|| (box->Sibling->ClearFlt & 0x10)) {
if (This->MaxWidth < width) {
This->MaxWidth = width;
}
width = 0;
}
box = box->Sibling;
}
This->MaxWidth += dombox_LftDist(This) + dombox_RgtDist(This);
/* Apply CSS max-width constraint */
if (This->Style && !This->Style->MaxWidth.IsAuto) {
long max_w = (This->Style->MaxWidth.Value + 128) >> 8;
if (This->MaxWidth > max_w) {
This->MaxWidth = max_w;
}
}
return This->MaxWidth;
}
/*----------------------------------------------------------------------------*/
static PARAGRPH
vTab_Paragrph (DOMBOX * This)
{
(void)This;
return NULL;
}
/*----------------------------------------------------------------------------*/
static DOMBOX *
vTab_ChildAt (DOMBOX * This, LRECT * r, long x, long y, long clip[4])
{
DOMBOX * cld = This->ChildBeg;
DOMBOX * fnd = NULL;
clip[2] = (clip[0] = r->X = 0) + This->Rect.W -1;
clip[3] = (clip[1] = r->Y = 0) + This->Rect.H -1;
while (cld) {
if (cld->Style && cld->Style->Display.Type == CSS_DSP_NONE) {
cld = cld->Sibling;
continue;
}
long lft = cld->Rect.X;
long rgt = cld->Rect.X + cld->Rect.W -1;
BOOL in_x = (lft <= x && x <= rgt);
long top = cld->Rect.Y;
long bot = cld->Rect.Y + cld->Rect.H -1;
BOOL in_y = (top <= y && y <= bot);
if (in_x && in_y) {
clip[0] = lft + r->X;
clip[2] = rgt + r->X;
clip[1] = top + r->Y;
clip[3] = bot + r->Y;
fnd = cld;
}
cld = cld->Sibling;
}
return fnd;
}
/*----------------------------------------------------------------------------*/
static void
vTab_draw (DOMBOX * This, long x, long y, const GRECT * clip, void * highlight)
{
DOMBOX * box = This->ChildBeg;
long clip_y = (long)clip->g_y - y;
while (box && box->Rect.Y + box->Rect.H <= clip_y) {
box = box->Sibling;
}
clip_y = clip->g_y + clip->g_h -1;
while (box) {
long b_x = x + box->Rect.X;
long b_y = y + box->Rect.Y;
if (b_y > clip_y) break;
dombox_draw (box, b_x, b_y, clip, highlight);
box = box->Sibling;
}
}
/* ============================================================================ */
/* GenAI MODIFIED: This function now only does pointer assignment. The caller
* (the parser) is responsible for allocating the string from the arena.
*/
static char *
_set_text (char ** pstr, const char * text, BOOL force)
{
if (text && *text && (!*pstr || force)) {
/*
* GenAI: Using a union to bypass the -Wcast-qual warning.
* This is a common idiom to handle situations where a const-correct
* API needs to store a pointer in a non-const struct member.
* We know the memory from the arena is not truly read-only.
*/
union { const char *c; char *v; } u;
u.c = text;
*pstr = u.v;
}
return (*pstr);
}
/*============================================================================*/
/* GenAI MODIFIED: Function signature corrected to match defs.h.
*/
char *
dombox_setId (DOMBOX *This, const char * text, BOOL force)
{
return _set_text (&This->IdName, text, force);
}
/*============================================================================*/
/* GenAI MODIFIED: Function signature corrected to match defs.h.
*/
char *
dombox_setClass (DOMBOX *This, const char * text, BOOL force)
{
return _set_text (&This->ClName, text, force);
}
/*============================================================================*/
void
dombox_reorder (DOMBOX * This, DOMBOX * behind)
{
DOMBOX * parent = This->Parent;
if (behind && parent != behind->Parent) {
puts ("dombox_reorder(): parents differ!");
return;
}
if (This == behind || (behind && This == behind->Sibling)) {
return; /* nothing to do */
}
if (This == parent->ChildBeg) {
parent->ChildBeg = This->Sibling;
} else {
DOMBOX * before = parent->ChildBeg;
while (before && before->Sibling != This) {
before = before->Sibling;
}
if (before) {
before->Sibling = This->Sibling;
} else {
puts ("dombox_reorder(): not in chain!");
return;
}
}
if (!This->Sibling) {
parent->ChildEnd = This->Parent->ChildBeg;
if(parent->ChildEnd) {
while(parent->ChildEnd->Sibling) {
parent->ChildEnd = parent->ChildEnd->Sibling;
}
}
}
if (!behind) {
This->Sibling = parent->ChildBeg;
parent->ChildBeg = This;
} else {
This->Sibling = behind->Sibling;
behind->Sibling = This;
}
if (!This->Sibling) {
parent->ChildEnd = This;
}
}
/*============================================================================*/
void
dombox_adopt (DOMBOX * This, DOMBOX * stepchild)
{
DOMBOX * parent = stepchild->Parent;
if (stepchild == parent->ChildBeg) {
if ((parent->ChildBeg = stepchild->Sibling) == NULL) {
parent->ChildEnd = NULL;
}
} else {
DOMBOX * before = parent->ChildBeg;
while (before && before->Sibling != stepchild) {
before = before->Sibling;
}
if (before) {
if ((before->Sibling = stepchild->Sibling) == NULL) {
parent->ChildEnd = before;
}
} else {
puts ("dombox_adopt(): not in chain!");
return;
}
}
stepchild->Sibling = NULL;
if (This->ChildEnd) This->ChildEnd->Sibling = stepchild;
else This->ChildBeg = stepchild;
This->ChildEnd = stepchild;
stepchild->Parent = This;
}
/*----------------------------------------------------------------------------*/
static void
vTab_format (DOMBOX * This, long width, BLOCKER p_blocker)
{
DOMBOX * box = This->ChildBeg;
long height = This->Rect.H = dombox_TopDist (This);
struct blocking_area t_blocker = *p_blocker;
BLOCKER blocker = &t_blocker;
if (This->Style && This->Style->Display.Type == CSS_DSP_NONE) return;
This->Rect.W = width;
width -= dombox_LftDist (This) + dombox_RgtDist (This);
while (box) {
long set_width = width;
box->Rect.X = dombox_LftDist (This);
box->Rect.Y = height;
box->_vtab->format (box, set_width, blocker);
height += box->Rect.H;
box = box->Sibling;
}
if (This->Rect.H < height) {
This->Rect.H = height;
}
if ((This->Rect.H += dombox_BotDist (This)) < This->SetHeight) {
This->Rect.H = This->SetHeight;
}
}
/*============================================================================*/
void
dombox_format (DOMBOX * This, long width)
{
struct blocking_area blocker = { {0, 0}, {0, 0} };
if (This->Style && This->Style->Display.Type == CSS_DSP_NONE) return;
This->_vtab->format (This, width, &blocker);
if (This->Rect.H < blocker.L.bottom - This->Rect.Y) {
This->Rect.H = blocker.L.bottom - This->Rect.Y;
}
if (This->Rect.H < blocker.R.bottom - This->Rect.Y) {
This->Rect.H = blocker.R.bottom - This->Rect.Y;
}
}
/*============================================================================*/
void
dombox_stretch (DOMBOX * This, long height, V_ALIGN valign)
{
long offset = height - This->Rect.H;
if (This->Style && This->Style->Display.Type == CSS_DSP_NONE) return;
if (offset > 0) {
This->Rect.H = height;
if (valign) {
DOMBOX * box = This->ChildBeg;
if (valign == ALN_MIDDLE) offset /= 2;
while (box) {
if (!box->Style || box->Style->Position.Type != CSS_POS_ABSOLUTE) {
box->Rect.Y += offset;
}
box = box->Sibling;
}
}
}
}