1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2009, 2010, 2013 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef RenderWidget_h
23#define RenderWidget_h
24
25#include "HTMLFrameOwnerElement.h"
26#include "OverlapTestRequestClient.h"
27#include "RenderReplaced.h"
28#include "Widget.h"
29#include <wtf/WeakPtr.h>
30
31namespace WebCore {
32
33class WidgetHierarchyUpdatesSuspensionScope {
34public:
35    WidgetHierarchyUpdatesSuspensionScope()
36    {
37        s_widgetHierarchyUpdateSuspendCount++;
38    }
39    ~WidgetHierarchyUpdatesSuspensionScope()
40    {
41        ASSERT(s_widgetHierarchyUpdateSuspendCount);
42        if (s_widgetHierarchyUpdateSuspendCount == 1)
43            moveWidgets();
44        s_widgetHierarchyUpdateSuspendCount--;
45    }
46
47    static bool isSuspended() { return s_widgetHierarchyUpdateSuspendCount; }
48    static void scheduleWidgetToMove(Widget* widget, FrameView* frame) { widgetNewParentMap().set(widget, frame); }
49
50private:
51    typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
52    static WidgetToParentMap& widgetNewParentMap();
53
54    void moveWidgets();
55
56    static unsigned s_widgetHierarchyUpdateSuspendCount;
57};
58
59class RenderWidget : public RenderReplaced, private OverlapTestRequestClient {
60public:
61    virtual ~RenderWidget();
62
63    HTMLFrameOwnerElement& frameOwnerElement() const { return toHTMLFrameOwnerElement(nodeForNonAnonymous()); }
64
65    Widget* widget() const { return m_widget.get(); }
66    void setWidget(PassRefPtr<Widget>);
67
68    static RenderWidget* find(const Widget*);
69
70    void updateWidgetPosition();
71    IntRect windowClipRect() const;
72
73    bool requiresAcceleratedCompositing() const;
74
75    WeakPtr<RenderWidget> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
76
77protected:
78    RenderWidget(HTMLFrameOwnerElement&, PassRef<RenderStyle>);
79
80    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override final;
81    virtual void layout() override;
82    virtual void paint(PaintInfo&, const LayoutPoint&) override;
83    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
84    virtual void paintContents(PaintInfo&, const LayoutPoint&);
85    virtual bool requiresLayer() const override;
86
87private:
88    void element() const = delete;
89
90    virtual bool isWidget() const override final { return true; }
91
92    virtual bool needsPreferredWidthsRecalculation() const override final;
93    virtual RenderBox* embeddedContentBox() const override final;
94
95    virtual void willBeDestroyed() override final;
96    virtual void setSelectionState(SelectionState) override final;
97    virtual void setOverlapTestResult(bool) override final;
98
99    bool setWidgetGeometry(const LayoutRect&);
100    bool updateWidgetGeometry();
101
102    WeakPtrFactory<RenderWidget> m_weakPtrFactory;
103    RefPtr<Widget> m_widget;
104    IntRect m_clipRect; // The rectangle needs to remain correct after scrolling, so it is stored in content view coordinates, and not clipped to window.
105};
106
107RENDER_OBJECT_TYPE_CASTS(RenderWidget, isWidget())
108
109} // namespace WebCore
110
111#endif // RenderWidget_h
112