📄 render_replaced.cpp
字号:
/**
* This file is part of the HTML widget for KDE.
*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004 Apple Computer, Inc.
*
* 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 "render_replaced.h"
#include "render_arena.h"
#include "render_canvas.h"
#include "render_line.h"
#include <assert.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qevent.h>
#include <qapplication.h>
#include "khtml_ext.h"
#include "khtmlview.h"
#include "xml/dom2_eventsimpl.h"
#include "khtml_part.h"
#include "xml/dom_docimpl.h" // ### remove dependency
#include "xml/dom_position.h"
#include <kdebug.h>
using namespace khtml;
using namespace DOM;
RenderReplaced::RenderReplaced(DOM::NodeImpl* node)
: RenderBox(node)
{
// init RenderObject attributes
setReplaced(true);
m_intrinsicWidth = 200;
m_intrinsicHeight = 150;
m_selectionState = SelectionNone;
}
bool RenderReplaced::shouldPaint(PaintInfo& i, int& _tx, int& _ty)
{
if (i.phase != PaintActionForeground && i.phase != PaintActionOutline && i.phase != PaintActionSelection)
return false;
if (!shouldPaintWithinRoot(i))
return false;
// if we're invisible or haven't received a layout yet, then just bail.
if (style()->visibility() != VISIBLE || m_y <= -500000) return false;
int tx = _tx + m_x;
int ty = _ty + m_y;
// Early exit if the element touches the edges.
int top = ty;
int bottom = ty + m_height;
if (m_selectionState != SelectionNone && m_inlineBoxWrapper) {
int selTop = _ty + m_inlineBoxWrapper->root()->selectionTop();
int selBottom = _ty + selTop + m_inlineBoxWrapper->root()->selectionHeight();
top = kMin(selTop, top);
bottom = kMax(selBottom, bottom);
}
int os = 2*maximalOutlineSize(i.phase);
if ((tx >= i.r.x() + i.r.width() + os) || (tx + m_width <= i.r.x() - os))
return false;
if ((top >= i.r.y() + i.r.height() + os) || (bottom <= i.r.y() - os))
return false;
return true;
}
void RenderReplaced::calcMinMaxWidth()
{
KHTMLAssert( !minMaxKnown());
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << "RenderReplaced::calcMinMaxWidth() known=" << minMaxKnown() << endl;
#endif
int width = calcReplacedWidth() + paddingLeft() + paddingRight() + borderLeft() + borderRight();
if (style()->width().isPercent() || (style()->width().isVariable() && style()->height().isPercent())) {
m_minWidth = 0;
m_maxWidth = width;
}
else
m_minWidth = m_maxWidth = width;
setMinMaxKnown();
}
short RenderReplaced::lineHeight( bool, bool ) const
{
return height()+marginTop()+marginBottom();
}
short RenderReplaced::baselinePosition( bool, bool ) const
{
return height()+marginTop()+marginBottom();
}
long RenderReplaced::caretMinOffset() const
{
return 0;
}
// Returns 1 since a replaced element can have the caret positioned
// at its beginning (0), or at its end (1).
long RenderReplaced::caretMaxOffset() const
{
return 1;
}
unsigned long RenderReplaced::caretMaxRenderedOffset() const
{
return 1;
}
VisiblePosition RenderReplaced::positionForCoordinates(int _x, int _y)
{
InlineBox *box = inlineBoxWrapper();
if (!box)
return VisiblePosition(element(), 0, DOWNSTREAM);
RootInlineBox *root = box->root();
int absx, absy;
containingBlock()->absolutePosition(absx, absy);
int top = absy + root->topOverflow();
int bottom = root->nextRootBox() ? absy + root->nextRootBox()->topOverflow() : absy + root->bottomOverflow();
if (_y < top)
return VisiblePosition(element(), caretMinOffset(), DOWNSTREAM); // coordinates are above
if (_y >= bottom)
return VisiblePosition(element(), caretMaxOffset(), DOWNSTREAM); // coordinates are below
if (element()) {
if (_x <= absx + xPos() + (width() / 2))
return VisiblePosition(element(), 0, DOWNSTREAM);
return VisiblePosition(element(), 1, DOWNSTREAM);
}
return RenderBox::positionForCoordinates(_x, _y);
}
QRect RenderReplaced::selectionRect()
{
if (selectionState() == SelectionNone)
return QRect();
if (!m_inlineBoxWrapper)
// We're a block-level replaced element. Just return our own dimensions.
return absoluteBoundingBoxRect();
RenderBlock* cb = containingBlock();
if (!cb)
return QRect();
RootInlineBox* root = m_inlineBoxWrapper->root();
int selectionTop = root->selectionTop();
int selectionHeight = root->selectionHeight();
int selectionLeft = xPos();
int selectionRight = xPos() + width();
int absx, absy;
cb->absolutePosition(absx, absy);
return QRect(selectionLeft + absx, selectionTop + absy, selectionRight - selectionLeft, selectionHeight);
}
void RenderReplaced::setSelectionState(SelectionState s)
{
m_selectionState = s;
if (m_inlineBoxWrapper) {
RootInlineBox* line = m_inlineBoxWrapper->root();
if (line)
line->setHasSelectedChildren(s != SelectionNone);
}
containingBlock()->setSelectionState(s);
}
QColor RenderReplaced::selectionColor(QPainter *p) const
{
QColor color = RenderBox::selectionColor(p);
// Force a 60% alpha so that no user-specified selection color can obscure selected images.
if (qAlpha(color.rgb()) > 153)
color = QColor(qRgba(color.red(), color.green(), color.blue(), 153));
return color;
}
// -----------------------------------------------------------------------------
RenderWidget::RenderWidget(DOM::NodeImpl* node)
: RenderReplaced(node),
m_deleteWidget(false)
{
m_widget = 0;
// a replaced element doesn't support being anonymous
assert(node);
m_view = node->getDocument()->view();
// this is no real reference counting, its just there
// to make sure that we're not deleted while we're recursed
// in an eventFilter of the widget
ref();
}
void RenderWidget::detach()
{
remove();
if ( m_widget ) {
if ( m_view )
m_view->removeChild( m_widget );
m_widget->removeEventFilter( this );
m_widget->setMouseTracking( false );
}
RenderArena* arena = renderArena();
if (m_inlineBoxWrapper) {
if (!documentBeingDestroyed())
m_inlineBoxWrapper->remove();
m_inlineBoxWrapper->detach(arena);
m_inlineBoxWrapper = 0;
}
setNode(0);
deref(arena);
}
RenderWidget::~RenderWidget()
{
KHTMLAssert( refCount() <= 0 );
if (m_deleteWidget) {
delete m_widget;
}
}
void RenderWidget::resizeWidget( QWidget *widget, int w, int h )
{
#if !APPLE_CHANGES
// ugly hack to limit the maximum size of the widget (as X11 has problems if it's bigger)
h = QMIN( h, 3072 );
w = QMIN( w, 2000 );
#endif
if (element() && (widget->width() != w || widget->height() != h)) {
RenderArena *arena = ref();
element()->ref();
widget->resize( w, h );
element()->deref();
deref(arena);
}
}
void RenderWidget::setQWidget(QWidget *widget, bool deleteWidget)
{
if (widget != m_widget)
{
if (m_widget) {
m_widget->removeEventFilter(this);
disconnect( m_widget, SIGNAL( destroyed()), this, SLOT( slotWidgetDestructed()));
if (m_deleteWidget) {
delete m_widget;
}
m_widget = 0;
}
m_widget = widget;
if (m_widget) {
connect( m_widget, SIGNAL( destroyed()), this, SLOT( slotWidgetDestructed()));
m_widget->installEventFilter(this);
// if we've already received a layout, apply the calculated space to the
// widget immediately, but we have to have really been full constructed (with a non-null
// style pointer).
if (!needsLayout() && style()) {
resizeWidget( m_widget,
m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
m_height-borderLeft()-borderRight()-paddingLeft()-paddingRight() );
}
else
setPos(xPos(), -500000);
#if APPLE_CHANGES
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -