2 * This file is part of the HTML widget for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "render_replaced.h"
26 #include "render_arena.h"
27 #include "render_canvas.h"
28 #include "render_line.h"
34 #include <qapplication.h>
36 #include "khtml_ext.h"
37 #include "khtmlview.h"
38 #include "xml/dom2_eventsimpl.h"
39 #include "khtml_part.h"
40 #include "xml/dom_docimpl.h" // ### remove dependency
41 #include "xml/dom_position.h"
44 using namespace khtml;
48 RenderReplaced::RenderReplaced(DOM::NodeImpl* node)
51 // init RenderObject attributes
54 m_intrinsicWidth = 300;
55 m_intrinsicHeight = 150;
56 m_selectionState = SelectionNone;
59 bool RenderReplaced::shouldPaint(PaintInfo& i, int& _tx, int& _ty)
61 if (i.phase != PaintActionForeground && i.phase != PaintActionOutline && i.phase != PaintActionSelection)
64 if (!shouldPaintWithinRoot(i))
67 // if we're invisible or haven't received a layout yet, then just bail.
68 if (style()->visibility() != VISIBLE || m_y <= -500000) return false;
73 // Early exit if the element touches the edges.
75 int bottom = ty + m_height;
76 if (m_selectionState != SelectionNone && m_inlineBoxWrapper) {
77 int selTop = _ty + m_inlineBoxWrapper->root()->selectionTop();
78 int selBottom = _ty + selTop + m_inlineBoxWrapper->root()->selectionHeight();
79 top = kMin(selTop, top);
80 bottom = kMax(selBottom, bottom);
83 int os = 2*maximalOutlineSize(i.phase);
84 if ((tx >= i.r.x() + i.r.width() + os) || (tx + m_width <= i.r.x() - os))
86 if ((top >= i.r.y() + i.r.height() + os) || (bottom <= i.r.y() - os))
92 void RenderReplaced::calcMinMaxWidth()
94 KHTMLAssert( !minMaxKnown());
97 kdDebug( 6040 ) << "RenderReplaced::calcMinMaxWidth() known=" << minMaxKnown() << endl;
100 int width = calcReplacedWidth() + paddingLeft() + paddingRight() + borderLeft() + borderRight();
101 if (style()->width().isPercent() || (style()->width().isVariable() && style()->height().isPercent())) {
106 m_minWidth = m_maxWidth = width;
111 short RenderReplaced::lineHeight( bool, bool ) const
113 return height()+marginTop()+marginBottom();
116 short RenderReplaced::baselinePosition( bool, bool ) const
118 return height()+marginTop()+marginBottom();
121 long RenderReplaced::caretMinOffset() const
126 // Returns 1 since a replaced element can have the caret positioned
127 // at its beginning (0), or at its end (1).
128 long RenderReplaced::caretMaxOffset() const
133 unsigned long RenderReplaced::caretMaxRenderedOffset() const
138 VisiblePosition RenderReplaced::positionForCoordinates(int _x, int _y)
140 InlineBox *box = inlineBoxWrapper();
142 return VisiblePosition(element(), 0, DOWNSTREAM);
144 RootInlineBox *root = box->root();
147 containingBlock()->absolutePosition(absx, absy);
149 int top = absy + root->topOverflow();
150 int bottom = root->nextRootBox() ? absy + root->nextRootBox()->topOverflow() : absy + root->bottomOverflow();
153 return VisiblePosition(element(), caretMinOffset(), DOWNSTREAM); // coordinates are above
156 return VisiblePosition(element(), caretMaxOffset(), DOWNSTREAM); // coordinates are below
159 if (_x <= absx + xPos() + (width() / 2))
160 return VisiblePosition(element(), 0, DOWNSTREAM);
162 return VisiblePosition(element(), 1, DOWNSTREAM);
165 return RenderBox::positionForCoordinates(_x, _y);
168 QRect RenderReplaced::selectionRect()
170 if (selectionState() == SelectionNone)
172 if (!m_inlineBoxWrapper)
173 // We're a block-level replaced element. Just return our own dimensions.
174 return absoluteBoundingBoxRect();
176 RenderBlock* cb = containingBlock();
180 RootInlineBox* root = m_inlineBoxWrapper->root();
181 int selectionTop = root->selectionTop();
182 int selectionHeight = root->selectionHeight();
183 int selectionLeft = xPos();
184 int selectionRight = xPos() + width();
187 cb->absolutePosition(absx, absy);
189 return QRect(selectionLeft + absx, selectionTop + absy, selectionRight - selectionLeft, selectionHeight);
192 void RenderReplaced::setSelectionState(SelectionState s)
194 m_selectionState = s;
195 if (m_inlineBoxWrapper) {
196 RootInlineBox* line = m_inlineBoxWrapper->root();
198 line->setHasSelectedChildren(s != SelectionNone);
201 containingBlock()->setSelectionState(s);
205 QColor RenderReplaced::selectionColor(QPainter *p) const
207 QColor color = RenderBox::selectionColor(p);
209 // Force a 60% alpha so that no user-specified selection color can obscure selected images.
210 if (qAlpha(color.rgb()) > 153)
211 color = QColor(qRgba(color.red(), color.green(), color.blue(), 153));
216 // -----------------------------------------------------------------------------
218 RenderWidget::RenderWidget(DOM::NodeImpl* node)
219 : RenderReplaced(node),
220 m_deleteWidget(false)
223 // a replaced element doesn't support being anonymous
225 m_view = node->getDocument()->view();
227 // this is no real reference counting, its just there
228 // to make sure that we're not deleted while we're recursed
229 // in an eventFilter of the widget
233 void RenderWidget::detach()
239 m_view->removeChild( m_widget );
241 m_widget->removeEventFilter( this );
242 m_widget->setMouseTracking( false );
245 RenderArena* arena = renderArena();
246 if (m_inlineBoxWrapper) {
247 if (!documentBeingDestroyed())
248 m_inlineBoxWrapper->remove();
249 m_inlineBoxWrapper->detach(arena);
250 m_inlineBoxWrapper = 0;
256 RenderWidget::~RenderWidget()
258 KHTMLAssert( refCount() <= 0 );
260 if (m_deleteWidget) {
265 void RenderWidget::resizeWidget( QWidget *widget, int w, int h )
268 // ugly hack to limit the maximum size of the widget (as X11 has problems if it's bigger)
273 if (element() && (widget->width() != w || widget->height() != h)) {
274 RenderArena *arena = ref();
276 widget->resize( w, h );
282 void RenderWidget::setQWidget(QWidget *widget, bool deleteWidget)
284 if (widget != m_widget)
287 m_widget->removeEventFilter(this);
288 disconnect( m_widget, SIGNAL( destroyed()), this, SLOT( slotWidgetDestructed()));
289 if (m_deleteWidget) {
296 connect( m_widget, SIGNAL( destroyed()), this, SLOT( slotWidgetDestructed()));
297 m_widget->installEventFilter(this);
298 // if we've already received a layout, apply the calculated space to the
299 // widget immediately, but we have to have really been full constructed (with a non-null
301 if (!needsLayout() && style()) {
302 resizeWidget( m_widget,
303 m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
304 m_height-borderLeft()-borderRight()-paddingLeft()-paddingRight() );
307 setPos(xPos(), -500000);
311 if (style()->visibility() != VISIBLE)
318 m_view->addChild( m_widget, -500000, 0 );
320 m_deleteWidget = deleteWidget;
323 void RenderWidget::layout( )
325 KHTMLAssert( needsLayout() );
326 KHTMLAssert( minMaxKnown() );
329 resizeWidget( m_widget,
330 m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
331 m_height-borderLeft()-borderRight()-paddingLeft()-paddingRight() );
335 setNeedsLayout(false);
339 void RenderWidget::sendConsumedMouseUp(const QPoint &mousePos, int button, int state)
341 RenderArena *arena = ref();
342 QMouseEvent e( QEvent::MouseButtonRelease, mousePos, button, state);
344 element()->dispatchMouseEvent(&e, EventImpl::MOUSEUP_EVENT, 0);
349 void RenderWidget::slotWidgetDestructed()
354 void RenderWidget::setStyle(RenderStyle *_style)
356 RenderReplaced::setStyle(_style);
359 m_widget->setFont(style()->font());
360 if (style()->visibility() != VISIBLE)
369 void RenderWidget::paint(PaintInfo& i, int _tx, int _ty)
371 if (!shouldPaint(i, _tx, _ty)) return;
376 if (shouldPaintBackgroundOrBorder() && i.phase != PaintActionOutline)
377 paintBoxDecorations(i, _tx, _ty);
380 if (!m_widget || !m_view || i.phase != PaintActionForeground ||
381 style()->visibility() != VISIBLE)
384 // Move the widget if necessary. We normally move and resize widgets during layout, but sometimes
385 // widgets can move without layout occurring (most notably when you scroll a document that
386 // contains fixed positioned elements).
387 int newX = _tx + borderLeft() + paddingLeft();
388 int newY = _ty + borderTop() + paddingTop();
389 if (m_view->childX(m_widget) != newX || m_view->childY(m_widget) != newY)
390 m_widget->move(newX, newY);
392 // Tell the widget to paint now. This is the only time the widget is allowed
393 // to paint itself. That way it will composite properly with z-indexed layers.
394 m_widget->paint(i.p, i.r);
396 // Paint a partially transparent wash over selected widgets.
397 if (m_selectionState != SelectionNone) {
398 QBrush brush(selectionColor(i.p));
399 QRect selRect(selectionRect());
400 i.p->fillRect(selRect.x(), selRect.y(), selRect.width(), selRect.height(), brush);
404 if (!m_widget || !m_view || i.phase != PaintActionForeground)
407 if (style()->visibility() != VISIBLE) {
412 int xPos = _tx+borderLeft()+paddingLeft();
413 int yPos = _ty+borderTop()+paddingTop();
415 int childw = m_widget->width();
416 int childh = m_widget->height();
417 if ( (childw == 2000 || childh == 3072) && m_widget->inherits( "KHTMLView" ) ) {
418 KHTMLView *vw = static_cast<KHTMLView *>(m_widget);
419 int cy = m_view->contentsY();
420 int ch = m_view->visibleHeight();
423 int childx = m_view->childX( m_widget );
424 int childy = m_view->childY( m_widget );
429 // qDebug("cy=%d, ch=%d, childy=%d, childh=%d", cy, ch, childy, childh );
430 if ( childh == 3072 ) {
431 if ( cy + ch > childy + childh ) {
432 yNew = cy + ( ch - childh )/2;
433 } else if ( cy < childy ) {
434 yNew = cy + ( ch - childh )/2;
436 // qDebug("calculated yNew=%d", yNew);
438 yNew = kMin( yNew, yPos + m_height - childh );
439 yNew = kMax( yNew, yPos );
440 if ( yNew != childy || xNew != childx ) {
441 if ( vw->contentsHeight() < yNew - yPos + childh )
442 vw->resizeContents( vw->contentsWidth(), yNew - yPos + childh );
443 vw->setContentsPos( xNew - xPos, yNew - yPos );
449 m_view->addChild(m_widget, xPos, yPos );
454 void RenderWidget::handleFocusOut()
458 bool RenderWidget::eventFilter(QObject* /*o*/, QEvent* e)
460 if ( !element() ) return true;
462 RenderArena *arena = ref();
463 DOM::NodeImpl *elem = element();
466 bool filtered = false;
468 //kdDebug() << "RenderWidget::eventFilter type=" << e->type() << endl;
470 case QEvent::FocusOut:
471 //static const char* const r[] = {"Mouse", "Tab", "Backtab", "ActiveWindow", "Popup", "Shortcut", "Other" };
472 //kdDebug() << "RenderFormElement::eventFilter FocusOut widget=" << m_widget << " reason:" << r[QFocusEvent::reason()] << endl;
473 // Don't count popup as a valid reason for losing the focus
474 // (example: opening the options of a select combobox shouldn't emit onblur)
475 if ( QFocusEvent::reason() != QFocusEvent::Popup )
477 //kdDebug(6000) << "RenderWidget::eventFilter captures FocusOut" << endl;
478 if (elem->getDocument()->focusNode() == elem)
479 elem->getDocument()->setFocusNode(0);
480 // if ( elem->isEditable() ) {
481 // KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>( elem->view->part()->browserExtension() );
482 // if ( ext ) ext->editableWidgetBlurred( m_widget );
487 case QEvent::FocusIn:
488 //kdDebug(6000) << "RenderWidget::eventFilter captures FocusIn" << endl;
489 elem->getDocument()->setFocusNode(elem);
490 // if ( isEditable() ) {
491 // KHTMLPartBrowserExtension *ext = static_cast<KHTMLPartBrowserExtension *>( elem->view->part()->browserExtension() );
492 // if ( ext ) ext->editableWidgetFocused( m_widget );
495 case QEvent::MouseButtonPress:
496 // handleMousePressed(static_cast<QMouseEvent*>(e));
498 case QEvent::MouseButtonRelease:
501 // absolutePosition(absX,absY);
502 // QMouseEvent* _e = static_cast<QMouseEvent*>(e);
503 // m_button = _e->button();
504 // m_state = _e->state();
505 // QMouseEvent e2(e->type(),QPoint(absX,absY)+_e->pos(),_e->button(),_e->state());
507 // elem->dispatchMouseEvent(&e2,EventImpl::MOUSEUP_EVENT,m_clickCount);
509 // if((m_mousePos - e2.pos()).manhattanLength() <= QApplication::startDragDistance()) {
510 // // DOM2 Events section 1.6.2 says that a click is if the mouse was pressed
511 // // and released in the "same screen location"
512 // // As people usually can't click on the same pixel, we're a bit tolerant here
513 // elem->dispatchMouseEvent(&e2,EventImpl::CLICK_EVENT,m_clickCount);
516 // if(!isRenderButton()) {
517 // // ### DOMActivate is also dispatched for thigs like selects & textareas -
518 // // not sure if this is correct
519 // elem->dispatchUIEvent(EventImpl::DOMACTIVATE_EVENT,m_isDoubleClick ? 2 : 1);
520 // elem->dispatchMouseEvent(&e2, m_isDoubleClick ? EventImpl::KHTML_DBLCLICK_EVENT : EventImpl::KHTML_CLICK_EVENT, m_clickCount);
521 // m_isDoubleClick = false;
524 // // save position for slotClicked - see below -
525 // m_mousePos = e2.pos();
528 case QEvent::MouseButtonDblClick:
530 // m_isDoubleClick = true;
531 // handleMousePressed(static_cast<QMouseEvent*>(e));
534 case QEvent::MouseMove:
537 // absolutePosition(absX,absY);
538 // QMouseEvent* _e = static_cast<QMouseEvent*>(e);
539 // QMouseEvent e2(e->type(),QPoint(absX,absY)+_e->pos(),_e->button(),_e->state());
540 // elem->dispatchMouseEvent(&e2);
541 // // ### change cursor like in KHTMLView?
544 case QEvent::KeyPress:
545 case QEvent::KeyRelease:
547 if (!elem->dispatchKeyEvent(static_cast<QKeyEvent*>(e)))
556 // stop processing if the widget gets deleted, but continue in all other cases
564 void RenderWidget::deref(RenderArena *arena)
568 arenaDelete(arena, this);
572 void RenderWidget::updateWidgetPositions()
577 int x, y, width, height;
578 absolutePosition(x,y);
579 x += borderLeft() + paddingLeft();
580 y += borderTop() + paddingTop();
581 width = m_width - borderLeft() - borderRight() - paddingLeft() - paddingRight();
582 height = m_height - borderTop() - borderBottom() - paddingTop() - paddingBottom();
583 QRect newBounds(x,y,width,height);
584 QRect oldBounds(m_widget->frameGeometry());
585 if (newBounds != oldBounds) {
586 // The widget changed positions. Update the frame geometry.
587 if (checkForRepaintDuringLayout()) {
588 RenderCanvas* c = canvas();
589 if (!c->printingMode()) {
590 c->repaintViewRectangle(oldBounds);
591 c->repaintViewRectangle(newBounds);
595 RenderArena *arena = ref();
597 m_widget->setFrameGeometry(newBounds);
604 void RenderWidget::setSelectionState(SelectionState s)
606 if (m_selectionState != s) {
607 RenderReplaced::setSelectionState(s);
608 m_selectionState = s;
610 m_widget->setIsSelected(m_selectionState != SelectionNone);
614 #include "render_replaced.moc"