render_replaced.cpp [plain text]
#include "render_replaced.h"
#include "render_canvas.h"
#include "render_arena.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 <kdebug.h>
using namespace khtml;
using namespace DOM;
RenderReplaced::RenderReplaced(DOM::NodeImpl* node)
: RenderBox(node)
{
setReplaced(true);
m_intrinsicWidth = 200;
m_intrinsicHeight = 150;
}
void RenderReplaced::paint(QPainter *p, int _x, int _y, int _w, int _h,
int _tx, int _ty, PaintAction paintAction)
{
if (paintAction != PaintActionForeground && paintAction != PaintActionOutline && paintAction != PaintActionSelection)
return;
if (style()->visibility() != VISIBLE || m_y <= -500000) return;
_tx += m_x;
_ty += m_y;
int os = 2*maximalOutlineSize(paintAction);
if((_tx >= _x + _w + os) || (_tx + m_width <= _x - os))
return;
if((_ty >= _y + _h + os) || (_ty + m_height <= _y - os))
return;
if(shouldPaintBackgroundOrBorder() && paintAction != PaintActionOutline)
paintBoxDecorations(p, _x, _y, _w, _h, _tx, _ty);
paintObject(p, _x, _y, _w, _h, _tx, _ty, paintAction);
}
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()->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();
}
bool RenderReplaced::canHaveChildren() const
{
return false;
}
RenderWidget::RenderWidget(DOM::NodeImpl* node)
: RenderReplaced(node),
m_deleteWidget(false)
{
m_widget = 0;
assert(node);
m_view = node->getDocument()->view();
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();
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
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 (!needsLayout() && style()) {
resizeWidget( m_widget,
m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
m_height-borderLeft()-borderRight()-paddingLeft()-paddingRight() );
}
else
setPos(xPos(), -500000);
#if APPLE_CHANGES
if (style()) {
if (style()->visibility() != VISIBLE)
m_widget->hide();
else
m_widget->show();
}
#endif
}
m_view->addChild( m_widget, -500000, 0 );
}
m_deleteWidget = deleteWidget;
}
void RenderWidget::layout( )
{
KHTMLAssert( needsLayout() );
KHTMLAssert( minMaxKnown() );
#if !APPLE_CHANGES
if ( m_widget ) {
resizeWidget( m_widget,
m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
m_height-borderLeft()-borderRight()-paddingLeft()-paddingRight() );
}
#endif
setNeedsLayout(false);
}
#if APPLE_CHANGES
void RenderWidget::sendConsumedMouseUp(const QPoint &mousePos, int button, int state)
{
RenderArena *arena = ref();
QMouseEvent e( QEvent::MouseButtonRelease, mousePos, button, state);
element()->dispatchMouseEvent(&e, EventImpl::MOUSEUP_EVENT, 0);
deref(arena);
}
#endif
void RenderWidget::slotWidgetDestructed()
{
m_widget = 0;
}
void RenderWidget::setStyle(RenderStyle *_style)
{
RenderReplaced::setStyle(_style);
if (m_widget)
{
m_widget->setFont(style()->font());
if (style()->visibility() != VISIBLE)
m_widget->hide();
#if APPLE_CHANGES
else
m_widget->show();
#endif
}
}
void RenderWidget::paintObject(QPainter *p, int x, int y, int width, int height, int _tx, int _ty,
PaintAction paintAction)
{
#if APPLE_CHANGES
if (!m_widget || !m_view || paintAction != PaintActionForeground ||
style()->visibility() != VISIBLE)
return;
int newX = _tx + borderLeft() + paddingLeft();
int newY = _ty + borderTop() + paddingTop();
if (m_view->childX(m_widget) != newX || m_view->childY(m_widget) != newY)
m_widget->move(newX, newY);
m_widget->paint(p, QRect(x, y, width, height));
#else
if (!m_widget || !m_view || paintAction != PaintActionForeground)
return;
if (style()->visibility() != VISIBLE) {
m_widget->hide();
return;
}
int xPos = _tx+borderLeft()+paddingLeft();
int yPos = _ty+borderTop()+paddingTop();
int childw = m_widget->width();
int childh = m_widget->height();
if ( (childw == 2000 || childh == 3072) && m_widget->inherits( "KHTMLView" ) ) {
KHTMLView *vw = static_cast<KHTMLView *>(m_widget);
int cy = m_view->contentsY();
int ch = m_view->visibleHeight();
int childx = m_view->childX( m_widget );
int childy = m_view->childY( m_widget );
int xNew = xPos;
int yNew = childy;
if ( childh == 3072 ) {
if ( cy + ch > childy + childh ) {
yNew = cy + ( ch - childh )/2;
} else if ( cy < childy ) {
yNew = cy + ( ch - childh )/2;
}
}
yNew = QMIN( yNew, yPos + m_height - childh );
yNew = QMAX( yNew, yPos );
if ( yNew != childy || xNew != childx ) {
if ( vw->contentsHeight() < yNew - yPos + childh )
vw->resizeContents( vw->contentsWidth(), yNew - yPos + childh );
vw->setContentsPos( xNew - xPos, yNew - yPos );
}
xPos = xNew;
yPos = yNew;
}
m_view->addChild(m_widget, xPos, yPos );
m_widget->show();
#endif
}
void RenderWidget::handleFocusOut()
{
}
bool RenderWidget::eventFilter(QObject* , QEvent* e)
{
if ( !element() ) return true;
RenderArena *arena = ref();
DOM::NodeImpl *elem = element();
elem->ref();
bool filtered = false;
switch(e->type()) {
case QEvent::FocusOut:
if ( QFocusEvent::reason() != QFocusEvent::Popup )
{
if (elem->getDocument()->focusNode() == elem)
elem->getDocument()->setFocusNode(0);
handleFocusOut();
}
break;
case QEvent::FocusIn:
elem->getDocument()->setFocusNode(elem);
break;
case QEvent::MouseButtonPress:
break;
case QEvent::MouseButtonRelease:
break;
case QEvent::MouseButtonDblClick:
break;
case QEvent::MouseMove:
break;
case QEvent::KeyPress:
case QEvent::KeyRelease:
{
if (!elem->dispatchKeyEvent(static_cast<QKeyEvent*>(e)))
filtered = true;
break;
}
default: break;
};
elem->deref();
if (hasOneRef())
filtered = true;
deref(arena);
return filtered;
}
void RenderWidget::deref(RenderArena *arena)
{
if (_ref) _ref--;
if (!_ref)
arenaDelete(arena);
}
#if APPLE_CHANGES
void RenderWidget::updateWidgetPositions()
{
if (!m_widget)
return;
int x, y, width, height;
absolutePosition(x,y);
x += borderLeft() + paddingLeft();
y += borderTop() + paddingTop();
width = m_width - borderLeft() - borderRight() - paddingLeft() - paddingRight();
height = m_height - borderTop() - borderBottom() - paddingTop() - paddingBottom();
QRect newBounds(x,y,width,height);
if (newBounds != m_widget->frameGeometry()) {
RenderArena *arena = ref();
element()->ref();
m_widget->setFrameGeometry(newBounds);
element()->deref();
deref(arena);
}
}
#endif
#include "render_replaced.moc"