2006-10-21 Nikolas Zimmermann <zimmermann@kde.org>
[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 "FrameQt.h"
38 #include "FrameView.h"
39 #include "TypingCommand.h"
40 #include "KeyboardCodes.h"
41 #include "GraphicsContext.h"
42 #include "SelectionController.h"
43 #include "PlatformMouseEvent.h"
44 #include "PlatformKeyboardEvent.h"
45
46 #include <QPainter>
47 #include <QPaintEvent>
48 #include <QMouseEvent>
49
50 namespace WebCore {
51
52 ScrollViewCanvasQt::ScrollViewCanvasQt(ScrollView* frameView, QWidget* parent)
53     : QWidget(parent)
54     , m_frameView(frameView)
55 {
56     setMouseTracking(true);
57     setFocusPolicy(Qt::StrongFocus);
58 }
59
60 void ScrollViewCanvasQt::paintEvent(QPaintEvent* ev)
61 {
62     FrameView* fv = static_cast<FrameView*>(m_frameView);
63     if (!fv || !fv->frame())
64         return;
65
66     QRect clip = ev->rect();
67
68     QPainter p(this);
69     GraphicsContext ctx(&p);
70
71     fv->layout();
72     fv->frame()->paint(&ctx, clip);
73 }
74
75 QSize ScrollViewCanvasQt::sizeHint() const
76 {
77     return QSize(1024, 768);
78 }
79
80 void ScrollViewCanvasQt::mouseMoveEvent(QMouseEvent* ev)
81 {
82     FrameView* fv = static_cast<FrameView*>(m_frameView);
83     if (!fv || !fv->frame())
84         return;
85
86     fv->handleMouseMoveEvent(PlatformMouseEvent(ev, 0));
87 }
88
89 void ScrollViewCanvasQt::mousePressEvent(QMouseEvent* ev)
90 {
91     FrameView* fv = static_cast<FrameView*>(m_frameView);
92     if (!fv || !fv->frame())
93         return;
94
95     fv->handleMousePressEvent(PlatformMouseEvent(ev, 1));
96 }
97
98 void ScrollViewCanvasQt::mouseReleaseEvent(QMouseEvent* ev)
99 {
100     FrameView* fv = static_cast<FrameView*>(m_frameView);
101     if (!fv || !fv->frame())
102         return;
103
104     fv->handleMouseReleaseEvent(PlatformMouseEvent(ev, 0));
105 }
106
107 void ScrollViewCanvasQt::keyPressEvent(QKeyEvent* ev)
108 {
109     handleKeyEvent(ev, false);
110 }
111
112 void ScrollViewCanvasQt::keyReleaseEvent(QKeyEvent* ev)
113 {
114     handleKeyEvent(ev, true);
115 }
116
117 void ScrollViewCanvasQt::handleKeyEvent(QKeyEvent* ev, bool isKeyUp)
118 {
119     PlatformKeyboardEvent kevent(ev, isKeyUp);
120
121     FrameView* fv = static_cast<FrameView*>(m_frameView);
122     FrameQt* frame = (fv ? static_cast<FrameQt*>(fv->frame()) : 0);
123     if (!frame)
124         return;
125
126     bool handled = frame->keyEvent(kevent);
127
128     if (!handled && !kevent.isKeyUp()) {
129         Node* start = frame->selectionController()->start().node();
130         if (start && start->isContentEditable()) {
131             switch(kevent.WindowsKeyCode()) {
132                 case VK_BACK:
133                     TypingCommand::deleteKeyPressed(frame->document());
134                     break;
135                 case VK_DELETE:
136                     TypingCommand::forwardDeleteKeyPressed(frame->document());
137                     break;
138                 case VK_LEFT:
139                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::LEFT, CharacterGranularity);
140                     break;
141                 case VK_RIGHT:
142                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::RIGHT, CharacterGranularity);
143                     break;
144                 case VK_UP:
145                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::BACKWARD, ParagraphGranularity);
146                     break;
147                 case VK_DOWN:
148                     frame->selectionController()->modify(SelectionController::MOVE, SelectionController::FORWARD, ParagraphGranularity);
149                     break;
150                 default:
151                     TypingCommand::insertText(frame->document(), kevent.text(), false);
152
153             }
154
155             handled = true;
156         }
157         
158         // FIXME: doScroll stuff()!
159     }
160 }
161
162 }
163
164 // vim: ts=4 sw=4 et