Reviewed by Mitz. Landed by Niko.
[WebKit-https.git] / WebCore / platform / qt / ScrollViewCanvasQt.cpp
1 /*
2  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
3  * Copyright (C) 2006 George Staikos <staikos@kde.org>
4  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
5  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
6  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33
34 #include "ScrollViewCanvasQt.h"
35 #include "ScrollViewCanvasQt.moc"
36
37 #include "EventHandler.h"
38 #include "FrameQt.h"
39 #include "FrameView.h"
40 #include "TypingCommand.h"
41 #include "KeyboardCodes.h"
42 #include "GraphicsContext.h"
43 #include "SelectionController.h"
44 #include "PlatformMouseEvent.h"
45 #include "PlatformKeyboardEvent.h"
46
47 #include <QPainter>
48 #include <QPaintEvent>
49 #include <QMouseEvent>
50
51
52 namespace WebCore {
53
54 ScrollViewCanvasQt::ScrollViewCanvasQt(ScrollView* frameView, QWidget* parent)
55     : QWidget(parent),
56       m_frameView(frameView)
57 {
58     setMouseTracking(true);
59     setFocusPolicy(Qt::StrongFocus);
60 }
61
62 void ScrollViewCanvasQt::paintEvent(QPaintEvent* ev)
63 {
64     FrameView* fv = static_cast<FrameView*>(m_frameView);
65     if (!fv || !fv->frame())
66         return;
67
68     QRect clip = ev->rect();
69
70     QPainter p(this);
71     GraphicsContext ctx(&p);
72
73     fv->layout();
74     fv->frame()->paint(&ctx, clip);
75 }
76
77 QSize ScrollViewCanvasQt::sizeHint() const
78 {
79     return QSize(1024, 768);
80 }
81
82 void ScrollViewCanvasQt::mouseMoveEvent(QMouseEvent* ev)
83 {
84     FrameView* fv = static_cast<FrameView*>(m_frameView);
85     if (!fv || !fv->frame())
86         return;
87
88     fv->handleMouseMoveEvent(PlatformMouseEvent(ev, 0));
89 }
90
91 void ScrollViewCanvasQt::mousePressEvent(QMouseEvent* ev)
92 {
93     FrameView* fv = static_cast<FrameView*>(m_frameView);
94     if (!fv || !fv->frame() || !fv->frame()->eventHandler())
95         return;
96
97     fv->frame()->eventHandler()->handleMousePressEvent(PlatformMouseEvent(ev, 1));
98 }
99
100 void ScrollViewCanvasQt::mouseReleaseEvent(QMouseEvent* ev)
101 {
102     FrameView* fv = static_cast<FrameView*>(m_frameView);
103     if (!fv || !fv->frame())
104         return;
105
106     fv->handleMouseReleaseEvent(PlatformMouseEvent(ev, 0));
107 }
108
109 void ScrollViewCanvasQt::keyPressEvent(QKeyEvent* ev)
110 {
111     handleKeyEvent(ev, false);
112 }
113
114 void ScrollViewCanvasQt::keyReleaseEvent(QKeyEvent* ev)
115 {
116     handleKeyEvent(ev, true);
117 }
118
119 void ScrollViewCanvasQt::handleKeyEvent(QKeyEvent* ev, bool isKeyUp)
120 {
121     PlatformKeyboardEvent kevent(ev, isKeyUp);
122
123     FrameView* fv = static_cast<FrameView*>(m_frameView);
124     FrameQt* frame = (fv ? static_cast<FrameQt*>(fv->frame()) : 0);
125     if (!frame)
126         return;
127
128     bool handled = frame->keyEvent(kevent);
129
130     if (!handled && !kevent.isKeyUp()) {
131         Node* start = frame->selectionController()->start().node();
132         if (start && start->isContentEditable()) {
133             switch(kevent.WindowsKeyCode()) {
134                 case VK_BACK:
135                     TypingCommand::deleteKeyPressed(frame->document());
136                     break;
137                 case VK_DELETE:
138                     TypingCommand::forwardDeleteKeyPressed(frame->document());
139                     break;
140                 case VK_LEFT:
141                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::LEFT, CharacterGranularity);
142                     break;
143                 case VK_RIGHT:
144                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::RIGHT, CharacterGranularity);
145                     break;
146                 case VK_UP:
147                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::BACKWARD, ParagraphGranularity);
148                     break;
149                 case VK_DOWN:
150                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::FORWARD, ParagraphGranularity);
151                     break;
152                 default:
153                     TypingCommand::insertText(frame->document(), kevent.text(), false);
154
155             }
156
157             handled = true;
158         }
159
160         // FIXME: doScroll stuff()!
161     }
162 }
163
164 }
165
166 // vim: ts=4 sw=4 et