]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QContentPane.C
* Rename src/frontends/qt2 to src/frontends/qt3,
[lyx.git] / src / frontends / qt3 / QContentPane.C
1 /**
2  * \file QContentPane.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "frontends/LyXView.h"
15
16 // Qt defines a macro 'signals' that clashes with a boost namespace.
17 // All is well if the namespace is visible first.
18 #include "QWorkArea.h"
19
20 #include "QContentPane.h"
21 #include "QLyXKeySym.h"
22
23 #include <qapplication.h>
24 #include <qpainter.h>
25
26 #include <boost/bind.hpp>
27
28 namespace {
29
30 /// return the LyX key state from Qt's
31 key_modifier::state q_key_state(Qt::ButtonState state)
32 {
33         key_modifier::state k = key_modifier::none;
34         if (state & Qt::ControlButton)
35                 k |= key_modifier::ctrl;
36         if (state & Qt::ShiftButton)
37                 k |= key_modifier::shift;
38         if (state & Qt::AltButton)
39                 k |= key_modifier::alt;
40         return k;
41 }
42
43
44 /// return the LyX mouse button state from Qt's
45 mouse_button::state q_button_state(Qt::ButtonState button)
46 {
47         mouse_button::state b = mouse_button::none;
48         switch (button) {
49                 case Qt::LeftButton:
50                         b = mouse_button::button1;
51                         break;
52                 case Qt::MidButton:
53                         b = mouse_button::button2;
54                         break;
55                 case Qt::RightButton:
56                         b = mouse_button::button3;
57                         break;
58                 default:
59                         break;
60         }
61         return b;
62 }
63
64
65 /// return the LyX mouse button state from Qt's
66 mouse_button::state q_motion_state(Qt::ButtonState state)
67 {
68         mouse_button::state b = mouse_button::none;
69         if (state & Qt::LeftButton)
70                 b |= mouse_button::button1;
71         if (state & Qt::MidButton)
72                 b |= mouse_button::button2;
73         if (state & Qt::RightButton)
74                 b |= mouse_button::button3;
75         return b;
76 }
77
78 } // namespace anon
79
80
81 // This is a 'heartbeat' generating synthetic mouse move events when the
82 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
83 SyntheticMouseEvent::SyntheticMouseEvent()
84         : timeout(200), restart_timeout(true),
85           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
86 {}
87
88
89 QContentPane::QContentPane(QWorkArea * parent)
90         : QWidget(parent, "content_pane", WRepaintNoErase),
91           track_scrollbar_(true), wa_(parent)
92 {
93         synthetic_mouse_event_.timeout.timeout.connect(
94                 boost::bind(&QContentPane::generateSyntheticMouseEvent,
95                             this));
96
97         connect(&step_timer_, SIGNAL(timeout()), SLOT(keyeventTimeout()));
98
99         setFocusPolicy(QWidget::WheelFocus);
100         setFocus();
101         setCursor(ibeamCursor);
102 #if USE_INPUT_METHODS
103         // to make qt-immodule work
104         setInputMethodEnabled(true);
105 #endif
106
107         // stupid moc strikes again
108         connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
109                 this, SLOT(scrollBarChanged(int)));
110
111         // Start the timer, one-shot.
112         step_timer_.start(50, true);
113 }
114
115
116 #if USE_INPUT_METHODS
117 // to make qt-immodule work
118 void QContentPane::imStartEvent(QIMEvent *e)
119 {
120         e->accept();
121 }
122
123
124 void QContentPane::imComposeEvent(QIMEvent *e)
125 {
126         e->accept();
127 }
128
129
130 void QContentPane::imEndEvent(QIMEvent *e)
131 {
132         QString const text = e->text();
133         if (!text.isEmpty()) {
134                 int key = 0;
135                 // needed to make math superscript work on some systems
136                 // ideally, such special coding should not be necessary
137                 if (text == "^")
138                         key = Qt::Key_AsciiCircum;
139                 QKeyEvent ev(QEvent::KeyPress, key, *text.ascii(), 0, text);
140                 keyPressEvent(&ev);
141         }
142         e->accept();
143 }
144 #endif
145
146
147 void QContentPane::generateSyntheticMouseEvent()
148 {
149         // Set things off to generate the _next_ 'pseudo' event.
150         if (synthetic_mouse_event_.restart_timeout)
151                 synthetic_mouse_event_.timeout.start();
152
153         // Has anything changed on-screen since the last timeout signal
154         // was received?
155         double const scrollbar_value = wa_->scrollbar_->value();
156         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
157                 // Yes it has. Store the params used to check this.
158                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
159
160                 // ... and dispatch the event to the LyX core.
161                 wa_->view().view()->workAreaDispatch(synthetic_mouse_event_.cmd);
162         }
163 }
164
165
166 void QContentPane::scrollBarChanged(int val)
167 {
168         if (track_scrollbar_)
169                 wa_->view().view()->scrollDocView(val);
170 }
171
172
173 void QContentPane::mousePressEvent(QMouseEvent * e)
174 {
175         if (dc_event_.active && dc_event_ == *e) {
176                 dc_event_.active = false;
177                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
178                         dc_event_.x, dc_event_.y,
179                         q_button_state(dc_event_.state));
180                 wa_->view().view()->workAreaDispatch(cmd);
181                 return;
182         }
183
184         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
185                               q_button_state(e->button()));
186         wa_->view().view()->workAreaDispatch(cmd);
187 }
188
189
190 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
191 {
192         if (synthetic_mouse_event_.timeout.running())
193                 synthetic_mouse_event_.timeout.stop();
194
195         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
196                               q_button_state(e->button()));
197         wa_->view().view()->workAreaDispatch(cmd);
198 }
199
200
201 void QContentPane::mouseMoveEvent(QMouseEvent * e)
202 {
203         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
204                               q_motion_state(e->state()));
205
206         // If we're above or below the work area...
207         if (e->y() <= 20 || e->y() >= height() - 20) {
208                 // Make sure only a synthetic event can cause a page scroll,
209                 // so they come at a steady rate:
210                 if (e->y() <= 20)
211                         // _Force_ a scroll up:
212                         cmd.y = -40;
213                 else
214                         cmd.y = height();
215                 // Store the event, to be handled when the timeout expires.
216                 synthetic_mouse_event_.cmd = cmd;
217
218                 if (synthetic_mouse_event_.timeout.running())
219                         // Discard the event. Note that it _may_ be handled
220                         // when the timeout expires if
221                         // synthetic_mouse_event_.cmd has not been overwritten.
222                         // Ie, when the timeout expires, we handle the
223                         // most recent event but discard all others that
224                         // occurred after the one used to start the timeout
225                         // in the first place.
226                         return;
227                 else {
228                         synthetic_mouse_event_.restart_timeout = true;
229                         synthetic_mouse_event_.timeout.start();
230                         // Fall through to handle this event...
231                 }
232
233         } else if (synthetic_mouse_event_.timeout.running()) {
234                 // Store the event, to be possibly handled when the timeout
235                 // expires.
236                 // Once the timeout has expired, normal control is returned
237                 // to mouseMoveEvent (restart_timeout = false).
238                 // This results in a much smoother 'feel' when moving the
239                 // mouse back into the work area.
240                 synthetic_mouse_event_.cmd = cmd;
241                 synthetic_mouse_event_.restart_timeout = false;
242                 return;
243         }
244
245         // Has anything changed on-screen since the last QMouseEvent
246         // was received?
247         double const scrollbar_value = wa_->scrollbar_->value();
248         if (e->x() != synthetic_mouse_event_.x_old ||
249             e->y() != synthetic_mouse_event_.y_old ||
250             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
251                 // Yes it has. Store the params used to check this.
252                 synthetic_mouse_event_.x_old = e->x();
253                 synthetic_mouse_event_.y_old = e->y();
254                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
255
256                 // ... and dispatch the event to the LyX core.
257                 wa_->view().view()->workAreaDispatch(cmd);
258         }
259 }
260
261
262 void QContentPane::wheelEvent(QWheelEvent * e)
263 {
264         // Wheel rotation by one notch results in a delta() of 120 (see
265         // documentation of QWheelEvent)
266         int const lines = QApplication::wheelScrollLines() * e->delta() / 120;
267         wa_->scrollbar_->setValue(wa_->scrollbar_->value() -
268                                   lines *  wa_->scrollbar_->lineStep());
269 }
270
271
272 void QContentPane::keyPressEvent(QKeyEvent * e)
273 {
274         keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
275 }
276
277
278 void QContentPane::keyeventTimeout()
279 {
280         bool handle_autos = true;
281
282         while (!keyeventQueue_.empty()) {
283                 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
284
285                 // We never handle more than one auto repeated
286                 // char in a list of queued up events.
287                 if (!handle_autos && ev->isAutoRepeat()) {
288                         keyeventQueue_.pop();
289                         continue;
290                 }
291
292         boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
293                 sym->set(ev.get());
294
295                 wa_->view().view()->workAreaKeyPress(sym, q_key_state(ev->state()));
296                 keyeventQueue_.pop();
297
298                 handle_autos = false;
299         }
300
301         // Restart the timer.
302         step_timer_.start(25, true);
303 }
304
305
306 void QContentPane::doubleClickTimeout()
307 {
308         if (!dc_event_.active)
309                 return;
310
311         dc_event_.active = false;
312
313         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
314                 dc_event_.x, dc_event_.y,
315                 q_button_state(dc_event_.state));
316         wa_->view().view()->workAreaDispatch(cmd);
317 }
318
319
320 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
321 {
322         dc_event_ = double_click(e);
323
324         // doubleClickInterval() is just too long.
325         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
326                 this, SLOT(doubleClickTimeout()));
327 }
328
329
330 void QContentPane::resizeEvent(QResizeEvent *)
331 {
332         if (!pixmap_.get()) {
333                 pixmap_.reset(new QPixmap(width(), height()));
334         }
335
336         pixmap_->resize(width(), height());
337         wa_->view().view()->workAreaResize();
338 }
339
340
341 void QContentPane::paintEvent(QPaintEvent * e)
342 {
343         if (!pixmap_.get()) {
344                 pixmap_.reset(new QPixmap(width(), height()));
345                 wa_->view().view()->workAreaResize();
346                 return;
347         }
348
349         QRect r(e->rect());
350
351         QPainter q(this);
352         q.drawPixmap(QPoint(r.x(), r.y()),
353                 *pixmap_.get(), r);
354 }
355
356
357 void QContentPane::trackScrollbar(bool track_on)
358 {
359         track_scrollbar_ = track_on;
360 }