3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "BufferView.h"
14 #include "frontends/LyXView.h"
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"
20 #include "QContentPane.h"
21 #include "QLyXKeySym.h"
23 #include <qapplication.h>
26 #include <boost/bind.hpp>
30 /// return the LyX key state from Qt's
31 key_modifier::state q_key_state(Qt::ButtonState state)
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;
44 /// return the LyX mouse button state from Qt's
45 mouse_button::state q_button_state(Qt::ButtonState button)
47 mouse_button::state b = mouse_button::none;
50 b = mouse_button::button1;
53 b = mouse_button::button2;
56 b = mouse_button::button3;
65 /// return the LyX mouse button state from Qt's
66 mouse_button::state q_motion_state(Qt::ButtonState state)
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;
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)
89 QContentPane::QContentPane(QWorkArea * parent)
90 : QWidget(parent, "content_pane", WRepaintNoErase),
91 track_scrollbar_(true), wa_(parent)
93 synthetic_mouse_event_.timeout.timeout.connect(
94 boost::bind(&QContentPane::generateSyntheticMouseEvent,
97 connect(&step_timer_, SIGNAL(timeout()), SLOT(keyeventTimeout()));
99 setFocusPolicy(QWidget::WheelFocus);
101 setCursor(ibeamCursor);
102 #if USE_INPUT_METHODS
103 // to make qt-immodule work
104 setInputMethodEnabled(true);
107 // stupid moc strikes again
108 connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
109 this, SLOT(scrollBarChanged(int)));
111 // Start the timer, one-shot.
112 step_timer_.start(50, true);
116 #if USE_INPUT_METHODS
117 // to make qt-immodule work
118 void QContentPane::imStartEvent(QIMEvent *e)
124 void QContentPane::imComposeEvent(QIMEvent *e)
130 void QContentPane::imEndEvent(QIMEvent *e)
132 QString const text = e->text();
133 if (!text.isEmpty()) {
135 // needed to make math superscript work on some systems
136 // ideally, such special coding should not be necessary
138 key = Qt::Key_AsciiCircum;
139 QKeyEvent ev(QEvent::KeyPress, key, *text.ascii(), 0, text);
147 void QContentPane::generateSyntheticMouseEvent()
149 // Set things off to generate the _next_ 'pseudo' event.
150 if (synthetic_mouse_event_.restart_timeout)
151 synthetic_mouse_event_.timeout.start();
153 // Has anything changed on-screen since the last timeout signal
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;
160 // ... and dispatch the event to the LyX core.
161 wa_->view().view()->workAreaDispatch(synthetic_mouse_event_.cmd);
166 void QContentPane::scrollBarChanged(int val)
168 if (track_scrollbar_)
169 wa_->view().view()->scrollDocView(val);
173 void QContentPane::mousePressEvent(QMouseEvent * e)
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);
184 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
185 q_button_state(e->button()));
186 wa_->view().view()->workAreaDispatch(cmd);
190 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
192 if (synthetic_mouse_event_.timeout.running())
193 synthetic_mouse_event_.timeout.stop();
195 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
196 q_button_state(e->button()));
197 wa_->view().view()->workAreaDispatch(cmd);
201 void QContentPane::mouseMoveEvent(QMouseEvent * e)
203 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
204 q_motion_state(e->state()));
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:
211 // _Force_ a scroll up:
215 // Store the event, to be handled when the timeout expires.
216 synthetic_mouse_event_.cmd = cmd;
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.
228 synthetic_mouse_event_.restart_timeout = true;
229 synthetic_mouse_event_.timeout.start();
230 // Fall through to handle this event...
233 } else if (synthetic_mouse_event_.timeout.running()) {
234 // Store the event, to be possibly handled when the timeout
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;
245 // Has anything changed on-screen since the last QMouseEvent
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;
256 // ... and dispatch the event to the LyX core.
257 wa_->view().view()->workAreaDispatch(cmd);
262 void QContentPane::wheelEvent(QWheelEvent * e)
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());
272 void QContentPane::keyPressEvent(QKeyEvent * e)
274 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
278 void QContentPane::keyeventTimeout()
280 bool handle_autos = true;
282 while (!keyeventQueue_.empty()) {
283 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
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();
292 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
295 wa_->view().view()->workAreaKeyPress(sym, q_key_state(ev->state()));
296 keyeventQueue_.pop();
298 handle_autos = false;
301 // Restart the timer.
302 step_timer_.start(25, true);
306 void QContentPane::doubleClickTimeout()
308 if (!dc_event_.active)
311 dc_event_.active = false;
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);
320 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
322 dc_event_ = double_click(e);
324 // doubleClickInterval() is just too long.
325 QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
326 this, SLOT(doubleClickTimeout()));
330 void QContentPane::resizeEvent(QResizeEvent *)
332 if (!pixmap_.get()) {
333 pixmap_.reset(new QPixmap(width(), height()));
336 pixmap_->resize(width(), height());
337 wa_->view().view()->workAreaResize();
341 void QContentPane::paintEvent(QPaintEvent * e)
343 if (!pixmap_.get()) {
344 pixmap_.reset(new QPixmap(width(), height()));
345 wa_->view().view()->workAreaResize();
352 q.drawPixmap(QPoint(r.x(), r.y()),
357 void QContentPane::trackScrollbar(bool track_on)
359 track_scrollbar_ = track_on;