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
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderReplaced.h"
#include "GraphicsContext.h"
#include "RenderBlock.h"
#include "RenderLayer.h"
using namespace std;
namespace WebCore {
typedef WTF::HashMap<const RenderReplaced*, IntRect> OverflowRectMap;
static OverflowRectMap* gOverflowRectMap = 0;
const int cDefaultWidth = 300;
const int cDefaultHeight = 150;
RenderReplaced::RenderReplaced(Node* node)
: RenderBox(node)
, m_intrinsicSize(cDefaultWidth, cDefaultHeight)
, m_selectionState(SelectionNone)
, m_hasOverflow(false)
{
setReplaced(true);
}
RenderReplaced::RenderReplaced(Node* node, const IntSize& intrinsicSize)
: RenderBox(node)
, m_intrinsicSize(intrinsicSize)
, m_selectionState(SelectionNone)
, m_hasOverflow(false)
{
setReplaced(true);
}
RenderReplaced::~RenderReplaced()
{
if (m_hasOverflow)
gOverflowRectMap->remove(this);
}
void RenderReplaced::styleDidChange(RenderStyle::Diff diff, const RenderStyle* oldStyle)
{
RenderBox::styleDidChange(diff, oldStyle);
bool hadStyle = (oldStyle != 0);
float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
if (hadStyle && style() && style()->effectiveZoom() != oldZoom)
intrinsicSizeChanged();
}
void RenderReplaced::layout()
{
ASSERT(needsLayout());
IntRect oldBounds;
IntRect oldOutlineBox;
bool checkForRepaint = checkForRepaintDuringLayout();
if (checkForRepaint) {
oldBounds = absoluteClippedOverflowRect();
oldOutlineBox = absoluteOutlineBox();
}
m_height = minimumReplacedHeight();
calcWidth();
calcHeight();
adjustOverflowForBoxShadow();
if (checkForRepaint)
repaintAfterLayoutIfNeeded(oldBounds, oldOutlineBox);
setNeedsLayout(false);
}
void RenderReplaced::intrinsicSizeChanged()
{
int scaledWidth = static_cast<int>(cDefaultWidth * style()->effectiveZoom());
int scaledHeight = static_cast<int>(cDefaultHeight * style()->effectiveZoom());
m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
setNeedsLayoutAndPrefWidthsRecalc();
}
void RenderReplaced::paint(PaintInfo& paintInfo, int tx, int ty)
{
if (!shouldPaint(paintInfo, tx, ty))
return;
tx += m_x;
ty += m_y;
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, tx, ty);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, tx, ty);
return;
}
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth())
paintOutline(paintInfo.context, tx, ty, width(), height(), style());
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection)
return;
if (!shouldPaintWithinRoot(paintInfo))
return;
bool drawSelectionTint = selectionState() != SelectionNone && !document()->printing();
if (paintInfo.phase == PaintPhaseSelection) {
if (selectionState() == SelectionNone)
return;
drawSelectionTint = false;
}
paintReplaced(paintInfo, tx, ty);
if (drawSelectionTint)
paintInfo.context->fillRect(selectionRect(), selectionBackgroundColor());
}
bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, int& tx, int& ty)
{
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
&& paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask)
return false;
if (!shouldPaintWithinRoot(paintInfo))
return false;
// if we're invisible or haven't received a layout yet, then just bail.
if (style()->visibility() != VISIBLE)
return false;
int currentTX = tx + m_x;
int currentTY = ty + m_y;
// Early exit if the element touches the edges.
int top = currentTY + overflowTop();
int bottom = currentTY + overflowHeight();
if (isSelected() && m_inlineBoxWrapper) {
int selTop = ty + m_inlineBoxWrapper->root()->selectionTop();
int selBottom = ty + selTop + m_inlineBoxWrapper->root()->selectionHeight();
top = min(selTop, top);
bottom = max(selBottom, bottom);
}
int os = 2 * maximalOutlineSize(paintInfo.phase);
if (currentTX + overflowLeft() >= paintInfo.rect.right() + os || currentTX + overflowWidth() <= paintInfo.rect.x() - os)
return false;
if (top >= paintInfo.rect.bottom() + os || bottom <= paintInfo.rect.y() - os)
return false;
return true;
}
void RenderReplaced::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
int paddingAndBorders = paddingLeft() + paddingRight() + borderLeft() + borderRight();
int width = calcReplacedWidth(false) + paddingAndBorders;
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength)
width = min(width, style()->maxWidth().value() + (style()->boxSizing() == CONTENT_BOX ? paddingAndBorders : 0));
if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent())) {
m_minPrefWidth = 0;
m_maxPrefWidth = width;
} else
m_minPrefWidth = m_maxPrefWidth = width;
setPrefWidthsDirty(false);
}
int RenderReplaced::lineHeight(bool, bool) const
{
return height() + marginTop() + marginBottom();
}
int RenderReplaced::baselinePosition(bool, bool) const
{
return height() + marginTop() + marginBottom();
}
unsigned RenderReplaced::caretMaxRenderedOffset() const
{
return 1;
}
VisiblePosition RenderReplaced::positionForCoordinates(int x, int y)
{
InlineBox* box = inlineBoxWrapper();
if (!box)
return VisiblePosition(element(), 0, DOWNSTREAM);
// FIXME: This code is buggy if the replaced element is relative positioned.
RootInlineBox* root = box->root();
int top = root->topOverflow();
int bottom = root->nextRootBox() ? root->nextRootBox()->topOverflow() : root->bottomOverflow();
if (y + yPos() < top)
return VisiblePosition(element(), caretMinOffset(), DOWNSTREAM); // coordinates are above
if (y + yPos() >= bottom)
return VisiblePosition(element(), caretMaxOffset(), DOWNSTREAM); // coordinates are below
if (element()) {
if (x <= width() / 2)
return VisiblePosition(element(), 0, DOWNSTREAM);
return VisiblePosition(element(), 1, DOWNSTREAM);
}
return RenderBox::positionForCoordinates(x, y);
}
IntRect RenderReplaced::selectionRect(bool clipToVisibleContent)
{
ASSERT(!needsLayout());
if (!isSelected())
return IntRect();
if (!m_inlineBoxWrapper)
// We're a block-level replaced element. Just return our own dimensions.
return absoluteBoundingBoxRect();
RenderBlock* cb = containingBlock();
if (!cb)
return IntRect();
RootInlineBox* root = m_inlineBoxWrapper->root();
IntRect rect(0, root->selectionTop() - yPos(), width(), root->selectionHeight());
if (clipToVisibleContent)
computeAbsoluteRepaintRect(rect);
else {
int absx, absy;
absolutePositionForContent(absx, absy);
rect.move(absx, absy);
}
return rect;
}
void RenderReplaced::setSelectionState(SelectionState s)
{
m_selectionState = s;
if (m_inlineBoxWrapper) {
RootInlineBox* line = m_inlineBoxWrapper->root();
if (line)
line->setHasSelectedChildren(isSelected());
}
containingBlock()->setSelectionState(s);
}
bool RenderReplaced::isSelected() const
{
SelectionState s = selectionState();
if (s == SelectionNone)
return false;
if (s == SelectionInside)
return true;
int selectionStart, selectionEnd;
selectionStartEnd(selectionStart, selectionEnd);
if (s == SelectionStart)
return selectionStart == 0;
int end = element()->hasChildNodes() ? element()->childNodeCount() : 1;
if (s == SelectionEnd)
return selectionEnd == end;
if (s == SelectionBoth)
return selectionStart == 0 && selectionEnd == end;
ASSERT(0);
return false;
}
IntSize RenderReplaced::intrinsicSize() const
{
return m_intrinsicSize;
}
void RenderReplaced::setIntrinsicSize(const IntSize& size)
{
m_intrinsicSize = size;
}
void RenderReplaced::adjustOverflowForBoxShadow()
{
IntRect overflow;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
IntRect shadow = borderBox();
shadow.move(boxShadow->x, boxShadow->y);
shadow.inflate(boxShadow->blur);
overflow.unite(shadow);
}
if (!overflow.isEmpty()) {
if (!gOverflowRectMap)
gOverflowRectMap = new OverflowRectMap();
overflow.unite(borderBox());
gOverflowRectMap->set(this, overflow);
m_hasOverflow = true;
} else if (m_hasOverflow) {
gOverflowRectMap->remove(this);
m_hasOverflow = false;
}
}
int RenderReplaced::overflowHeight(bool includeInterior) const
{
if (m_hasOverflow) {
IntRect *r = &gOverflowRectMap->find(this)->second;
return r->height() + r->y();
}
return height();
}
int RenderReplaced::overflowWidth(bool includeInterior) const
{
if (m_hasOverflow) {
IntRect *r = &gOverflowRectMap->find(this)->second;
return r->width() + r->x();
}
return width();
}
int RenderReplaced::overflowLeft(bool includeInterior) const
{
if (m_hasOverflow)
return gOverflowRectMap->get(this).x();
return 0;
}
int RenderReplaced::overflowTop(bool includeInterior) const
{
if (m_hasOverflow)
return gOverflowRectMap->get(this).y();
return 0;
}
IntRect RenderReplaced::overflowRect(bool includeInterior) const
{
if (m_hasOverflow)
return gOverflowRectMap->find(this)->second;
return borderBox();
}
}
|