2002-06-19 03:38:44 +00:00
|
|
|
/**
|
|
|
|
* \file QContentPane.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-06-19 03:38:44 +00:00
|
|
|
*
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-06-19 03:38:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
// Qt defines a macro 'signals' that clashes with a boost namespace.
|
|
|
|
// All is well if the namespace is visible first.
|
2002-06-19 03:38:44 +00:00
|
|
|
#include "QWorkArea.h"
|
2004-05-20 09:36:28 +00:00
|
|
|
|
2003-09-05 16:18:57 +00:00
|
|
|
#include "QContentPane.h"
|
2002-06-19 03:38:44 +00:00
|
|
|
#include "QLyXKeySym.h"
|
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
#include <qapplication.h>
|
2002-06-19 03:38:44 +00:00
|
|
|
#include <qpainter.h>
|
2002-08-29 01:25:41 +00:00
|
|
|
#include <qtimer.h>
|
2004-05-20 09:36:28 +00:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
namespace {
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
/// return the LyX key state from Qt's
|
|
|
|
key_modifier::state q_key_state(Qt::ButtonState state)
|
|
|
|
{
|
|
|
|
key_modifier::state k = key_modifier::none;
|
|
|
|
if (state & Qt::ControlButton)
|
|
|
|
k |= key_modifier::ctrl;
|
|
|
|
if (state & Qt::ShiftButton)
|
|
|
|
k |= key_modifier::shift;
|
|
|
|
if (state & Qt::AltButton)
|
|
|
|
k |= key_modifier::alt;
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
/// return the LyX mouse button state from Qt's
|
|
|
|
mouse_button::state q_button_state(Qt::ButtonState button)
|
|
|
|
{
|
|
|
|
mouse_button::state b = mouse_button::none;
|
|
|
|
switch (button) {
|
|
|
|
case Qt::LeftButton:
|
|
|
|
b = mouse_button::button1;
|
|
|
|
break;
|
|
|
|
case Qt::MidButton:
|
|
|
|
b = mouse_button::button2;
|
|
|
|
break;
|
|
|
|
case Qt::RightButton:
|
|
|
|
b = mouse_button::button3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
/// return the LyX mouse button state from Qt's
|
|
|
|
mouse_button::state q_motion_state(Qt::ButtonState state)
|
|
|
|
{
|
|
|
|
mouse_button::state b = mouse_button::none;
|
|
|
|
if (state & Qt::LeftButton)
|
|
|
|
b |= mouse_button::button1;
|
|
|
|
if (state & Qt::MidButton)
|
|
|
|
b |= mouse_button::button2;
|
|
|
|
if (state & Qt::RightButton)
|
|
|
|
b |= mouse_button::button3;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
|
2003-12-02 10:10:15 +00:00
|
|
|
SyntheticMouseEvent::SyntheticMouseEvent()
|
|
|
|
: timeout(200), restart_timeout(true),
|
|
|
|
x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
QContentPane::QContentPane(QWorkArea * parent)
|
2002-10-20 01:48:28 +00:00
|
|
|
: QWidget(parent, "content_pane", WRepaintNoErase),
|
2003-11-30 18:26:41 +00:00
|
|
|
track_scrollbar_(true), wa_(parent)
|
2002-06-19 03:38:44 +00:00
|
|
|
{
|
2003-12-02 10:10:15 +00:00
|
|
|
synthetic_mouse_event_.timeout.timeout.connect(
|
|
|
|
boost::bind(&QContentPane::generateSyntheticMouseEvent,
|
|
|
|
this));
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
setFocusPolicy(QWidget::WheelFocus);
|
|
|
|
setFocus();
|
2002-12-05 13:50:13 +00:00
|
|
|
setCursor(ibeamCursor);
|
2002-06-19 03:38:44 +00:00
|
|
|
|
|
|
|
// stupid moc strikes again
|
2002-10-20 01:48:28 +00:00
|
|
|
connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
|
2002-06-19 03:38:44 +00:00
|
|
|
this, SLOT(scrollBarChanged(int)));
|
2003-12-02 10:10:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2003-12-02 10:10:15 +00:00
|
|
|
void QContentPane::generateSyntheticMouseEvent()
|
|
|
|
{
|
|
|
|
// Set things off to generate the _next_ 'pseudo' event.
|
|
|
|
if (synthetic_mouse_event_.restart_timeout)
|
|
|
|
synthetic_mouse_event_.timeout.start();
|
|
|
|
|
|
|
|
// Has anything changed on-screen since the last timeout signal
|
|
|
|
// was received?
|
|
|
|
double const scrollbar_value = wa_->scrollbar_->value();
|
|
|
|
if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
|
|
|
|
// Yes it has. Store the params used to check this.
|
|
|
|
synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
|
|
|
|
|
|
|
|
// ... and dispatch the event to the LyX core.
|
|
|
|
wa_->dispatch(synthetic_mouse_event_.cmd);
|
|
|
|
}
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QContentPane::scrollBarChanged(int val)
|
|
|
|
{
|
2003-11-24 08:26:08 +00:00
|
|
|
if (track_scrollbar_)
|
|
|
|
wa_->scrollDocView(val);
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
void QContentPane::mousePressEvent(QMouseEvent * e)
|
|
|
|
{
|
2002-08-29 01:25:41 +00:00
|
|
|
if (dc_event_.active && dc_event_ == *e) {
|
|
|
|
dc_event_.active = false;
|
|
|
|
FuncRequest cmd(LFUN_MOUSE_TRIPLE,
|
|
|
|
dc_event_.x, dc_event_.y,
|
|
|
|
q_button_state(dc_event_.state));
|
|
|
|
wa_->dispatch(cmd);
|
|
|
|
return;
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2003-12-01 23:04:36 +00:00
|
|
|
FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
|
|
|
|
q_button_state(e->button()));
|
2002-08-28 08:30:27 +00:00
|
|
|
wa_->dispatch(cmd);
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QContentPane::mouseReleaseEvent(QMouseEvent * e)
|
|
|
|
{
|
2003-12-02 10:10:15 +00:00
|
|
|
if (synthetic_mouse_event_.timeout.running())
|
|
|
|
synthetic_mouse_event_.timeout.stop();
|
|
|
|
|
2003-12-01 23:04:36 +00:00
|
|
|
FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
|
|
|
|
q_button_state(e->button()));
|
2002-08-28 08:30:27 +00:00
|
|
|
wa_->dispatch(cmd);
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
void QContentPane::mouseMoveEvent(QMouseEvent * e)
|
|
|
|
{
|
2003-12-01 23:04:36 +00:00
|
|
|
FuncRequest const cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
|
|
|
|
q_motion_state(e->state()));
|
2003-12-02 10:10:15 +00:00
|
|
|
|
|
|
|
// If we're above or below the work area...
|
|
|
|
if (e->y() <= 0 || e->y() >= height()) {
|
|
|
|
// Store the event, to be handled when the timeout expires.
|
|
|
|
synthetic_mouse_event_.cmd = cmd;
|
|
|
|
|
|
|
|
if (synthetic_mouse_event_.timeout.running())
|
|
|
|
// Discard the event. Note that it _may_ be handled
|
|
|
|
// when the timeout expires if
|
|
|
|
// synthetic_mouse_event_.cmd has not been overwritten.
|
|
|
|
// Ie, when the timeout expires, we handle the
|
|
|
|
// most recent event but discard all others that
|
|
|
|
// occurred after the one used to start the timeout
|
|
|
|
// in the first place.
|
|
|
|
return;
|
|
|
|
else {
|
|
|
|
synthetic_mouse_event_.restart_timeout = true;
|
|
|
|
synthetic_mouse_event_.timeout.start();
|
|
|
|
// Fall through to handle this event...
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (synthetic_mouse_event_.timeout.running()) {
|
|
|
|
// Store the event, to be possibly handled when the timeout
|
|
|
|
// expires.
|
|
|
|
// Once the timeout has expired, normal control is returned
|
|
|
|
// to mouseMoveEvent (restart_timeout = false).
|
|
|
|
// This results in a much smoother 'feel' when moving the
|
|
|
|
// mouse back into the work area.
|
|
|
|
synthetic_mouse_event_.cmd = cmd;
|
|
|
|
synthetic_mouse_event_.restart_timeout = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has anything changed on-screen since the last QMouseEvent
|
|
|
|
// was received?
|
|
|
|
double const scrollbar_value = wa_->scrollbar_->value();
|
|
|
|
if (e->x() != synthetic_mouse_event_.x_old ||
|
|
|
|
e->y() != synthetic_mouse_event_.y_old ||
|
|
|
|
scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
|
|
|
|
// Yes it has. Store the params used to check this.
|
|
|
|
synthetic_mouse_event_.x_old = e->x();
|
|
|
|
synthetic_mouse_event_.y_old = e->y();
|
|
|
|
synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
|
|
|
|
|
|
|
|
// ... and dispatch the event to the LyX core.
|
|
|
|
wa_->dispatch(cmd);
|
|
|
|
}
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-21 04:46:26 +00:00
|
|
|
void QContentPane::wheelEvent(QWheelEvent * e)
|
|
|
|
{
|
|
|
|
wa_->scrollbar_->setValue(wa_->scrollbar_->value() - e->delta());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
void QContentPane::keyPressEvent(QKeyEvent * e)
|
|
|
|
{
|
2004-08-01 21:24:03 +00:00
|
|
|
boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
|
2002-08-25 05:41:42 +00:00
|
|
|
sym->set(e);
|
2004-08-01 21:24:03 +00:00
|
|
|
wa_->workAreaKeyPress(sym, q_key_state(e->state()));
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-29 01:25:41 +00:00
|
|
|
void QContentPane::doubleClickTimeout()
|
2002-06-19 03:38:44 +00:00
|
|
|
{
|
2002-08-29 01:25:41 +00:00
|
|
|
if (!dc_event_.active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dc_event_.active = false;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-29 01:25:41 +00:00
|
|
|
FuncRequest cmd(LFUN_MOUSE_DOUBLE,
|
|
|
|
dc_event_.x, dc_event_.y,
|
|
|
|
q_button_state(dc_event_.state));
|
2002-08-28 08:30:27 +00:00
|
|
|
wa_->dispatch(cmd);
|
2002-08-29 01:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
|
|
|
|
{
|
|
|
|
dc_event_ = double_click(e);
|
|
|
|
|
|
|
|
// doubleClickInterval() is just too long.
|
2002-09-11 15:56:07 +00:00
|
|
|
QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
|
2002-08-29 01:25:41 +00:00
|
|
|
this, SLOT(doubleClickTimeout()));
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
void QContentPane::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
|
|
|
if (!pixmap_.get()) {
|
|
|
|
pixmap_.reset(new QPixmap(width(), height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pixmap_->resize(width(), height());
|
2002-06-21 02:58:54 +00:00
|
|
|
wa_->workAreaResize();
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
void QContentPane::paintEvent(QPaintEvent * e)
|
|
|
|
{
|
|
|
|
if (!pixmap_.get()) {
|
|
|
|
pixmap_.reset(new QPixmap(width(), height()));
|
2002-06-21 02:58:54 +00:00
|
|
|
wa_->workAreaResize();
|
2002-06-19 03:38:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect r(e->rect());
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
QPainter q(this);
|
|
|
|
q.drawPixmap(QPoint(r.x(), r.y()),
|
2002-10-20 01:48:28 +00:00
|
|
|
*pixmap_.get(), r);
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
2003-11-24 08:26:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
void QContentPane::trackScrollbar(bool track_on)
|
|
|
|
{
|
|
|
|
track_scrollbar_ = track_on;
|
|
|
|
}
|