initial import
[vuplus_webkit] / Tools / WebKitTestRunner / qt / PlatformWebViewQt.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 University of Szeged. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28
29 #include "PlatformWebView.h"
30 #include "qdesktopwebview.h"
31 #include <QtDeclarative/qsgcanvas.h>
32 #include <QtGui>
33
34 namespace WTR {
35
36 class WebView : public QSGCanvas {
37 public:
38     WebView(WKContextRef, WKPageGroupRef);
39
40     QDesktopWebView* wkView() const { return m_item; }
41     WKPageRef pageRef() const { return m_item->pageRef(); }
42
43     virtual ~WebView() { delete m_item; }
44
45 private:
46     QDesktopWebView* m_item;
47 };
48
49 WebView::WebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
50     : m_item(new QDesktopWebView(contextRef, pageGroupRef, rootItem()))
51 {
52     m_item->setWidth(800);
53     m_item->setHeight(600);
54 }
55
56 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
57     : m_view(new WebView(contextRef, pageGroupRef))
58     , m_window(new QMainWindow())
59 {
60     m_view->setParent(m_window);
61     m_window->setCentralWidget(m_view);
62     m_window->setGeometry(0, 0, 800, 600);
63
64     QFocusEvent ev(QEvent::WindowActivate);
65     QApplication::sendEvent(m_view, &ev);
66     m_view->wkView()->setFocus(Qt::OtherFocusReason);
67 }
68
69 PlatformWebView::~PlatformWebView()
70 {
71     delete m_window;
72 }
73
74 void PlatformWebView::resizeTo(unsigned width, unsigned height)
75 {
76     m_window->resize(width, height);
77 }
78
79 WKPageRef PlatformWebView::page()
80 {
81     return m_view->pageRef();
82 }
83
84 void PlatformWebView::focus()
85 {
86     m_view->setFocus(Qt::OtherFocusReason);
87 }
88
89 WKRect PlatformWebView::windowFrame()
90 {
91     QRect windowRect = m_window->geometry();
92     WKRect wkFrame;
93     wkFrame.origin.x = windowRect.x();
94     wkFrame.origin.y = windowRect.y();
95     wkFrame.size.width = windowRect.size().width();
96     wkFrame.size.height = windowRect.size().height();
97     return wkFrame;
98 }
99
100 void PlatformWebView::setWindowFrame(WKRect wkRect)
101 {
102     m_window->setGeometry(wkRect.origin.x, wkRect.origin.y, wkRect.size.width, wkRect.size.height);
103 }
104
105 } // namespace WTR