3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include <boost/current_function.hpp>
16 // This include must be declared before everything else because
17 // of boost/Qt/LyX clash...
20 #include "GuiWorkArea.h"
21 #include "QLPainter.h"
22 #include "QLyXKeySym.h"
24 #include "ColorCache.h"
25 #include "qt_helpers.h"
26 #include "Application.h"
27 #include "BufferView.h"
29 #include "funcrequest.h"
31 #include "support/os.h"
34 #include <QMainWindow>
37 #include <QDragEnterEvent>
42 #include <boost/bind.hpp>
44 // Abdel (26/06/2006):
45 // On windows-XP the UserGuide PageDown scroll test is faster without event pruning (16 s)
46 // than with it (23 s).
48 #define USE_EVENT_PRUNING 0
50 #define USE_EVENT_PRUNING 0
56 namespace os = lyx::support::os;
60 /// return the LyX key state from Qt's
61 key_modifier::state q_key_state(Qt::ButtonState state)
63 key_modifier::state k = key_modifier::none;
64 if (state & Qt::ControlModifier)
65 k |= key_modifier::ctrl;
66 if (state & Qt::ShiftModifier)
67 k |= key_modifier::shift;
68 if (state & Qt::AltModifier)
69 k |= key_modifier::alt;
74 /// return the LyX mouse button state from Qt's
75 mouse_button::state q_button_state(Qt::ButtonState button)
77 mouse_button::state b = mouse_button::none;
80 b = mouse_button::button1;
83 b = mouse_button::button2;
86 b = mouse_button::button3;
95 /// return the LyX mouse button state from Qt's
96 mouse_button::state q_motion_state(Qt::ButtonState state)
98 mouse_button::state b = mouse_button::none;
99 if (state & Qt::LeftButton)
100 b |= mouse_button::button1;
101 if (state & Qt::MidButton)
102 b |= mouse_button::button2;
103 if (state & Qt::RightButton)
104 b |= mouse_button::button3;
113 // This is a 'heartbeat' generating synthetic mouse move events when the
114 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
115 SyntheticMouseEvent::SyntheticMouseEvent()
116 : timeout(200), restart_timeout(true),
117 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
121 GuiWorkArea::GuiWorkArea(int w, int h, QWidget * parent, BufferView * buffer_view)
122 : QAbstractScrollArea(parent), WorkArea(buffer_view), painter_(this)
124 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
125 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
127 setAcceptDrops(true);
129 setMinimumSize(100, 70);
131 viewport()->setAutoFillBackground(false);
132 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
134 viewport()->setFocusPolicy(Qt::WheelFocus);
135 viewport()->setFocus();
136 setFocusPolicy(Qt::WheelFocus);
138 viewport()->setCursor(Qt::IBeamCursor);
145 synthetic_mouse_event_.timeout.timeout.connect(
146 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
149 // Initialize the vertical Scroll Bar
150 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
151 this, SLOT(adjustViewWithScrollBar(int)));
153 // PageStep only depends on the viewport height.
154 verticalScrollBar()->setPageStep(workHeight_);
156 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
157 << "\n Area width\t" << width()
158 << "\n Area height\t" << height()
159 << "\n viewport width\t" << viewport()->width()
160 << "\n viewport height\t" << viewport()->height()
163 if (USE_EVENT_PRUNING) {
164 // This is the keyboard buffering stuff...
165 // I don't see any need for this under windows. The keyboard is reactive
168 if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
169 this, SLOT(keyeventTimeout())) )
170 lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
172 // Start the timer, one-shot.
173 step_timer_.start(50, true);
176 // Enables input methods for asian languages.
177 // Must be set when creating custom text editing widgets.
178 setAttribute(Qt::WA_InputMethodEnabled, true);
181 GuiWorkArea::~GuiWorkArea()
186 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
188 // do what cursor movement does (some grey)
190 int scroll_max_ = std::max(0, h - height());
192 verticalScrollBar()->setRange(0, scroll_max_);
193 verticalScrollBar()->setSliderPosition(scroll_pos);
194 verticalScrollBar()->setLineStep(scroll_line_step);
198 void GuiWorkArea::adjustViewWithScrollBar(int)
201 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
202 << " verticalScrollBar val=" << verticalScrollBar()->value()
203 << " verticalScrollBar pos=" << verticalScrollBar()->sliderPosition()
204 << " min=" << verticalScrollBar()->minimum()
205 << " max=" << verticalScrollBar()->maximum()
206 << " pagestep=" << verticalScrollBar()->pageStep()
207 << " linestep=" << verticalScrollBar()->lineStep()
210 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
214 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
216 if (event->mimeData()->hasUrls())
218 /// \todo Ask lyx-devel is this is enough:
219 /// if (event->mimeData()->hasFormat("text/plain"))
220 /// event->acceptProposedAction();
224 void GuiWorkArea::dropEvent(QDropEvent* event)
226 QList<QUrl> files = event->mimeData()->urls();
230 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
231 for (int i = 0; i!=files.size(); ++i) {
232 string const file = os::internal_path(fromqstr(files.at(i).toString()));
234 buffer_view_->workAreaDispatch(FuncRequest(LFUN_FILE_OPEN, file));
239 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
241 if (dc_event_.active && dc_event_ == *e) {
242 dc_event_.active = false;
243 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
244 dc_event_.x, dc_event_.y,
245 q_button_state(dc_event_.state));
246 buffer_view_->workAreaDispatch(cmd);
250 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
251 q_button_state(e->button()));
252 buffer_view_->workAreaDispatch(cmd);
256 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
258 if (synthetic_mouse_event_.timeout.running())
259 synthetic_mouse_event_.timeout.stop();
261 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
262 q_button_state(e->button()));
263 buffer_view_->workAreaDispatch(cmd);
267 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
269 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
270 q_motion_state(e->state()));
272 // If we're above or below the work area...
273 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
274 // Make sure only a synthetic event can cause a page scroll,
275 // so they come at a steady rate:
277 // _Force_ a scroll up:
280 cmd.y = viewport()->height();
281 // Store the event, to be handled when the timeout expires.
282 synthetic_mouse_event_.cmd = cmd;
284 if (synthetic_mouse_event_.timeout.running())
285 // Discard the event. Note that it _may_ be handled
286 // when the timeout expires if
287 // synthetic_mouse_event_.cmd has not been overwritten.
288 // Ie, when the timeout expires, we handle the
289 // most recent event but discard all others that
290 // occurred after the one used to start the timeout
291 // in the first place.
294 synthetic_mouse_event_.restart_timeout = true;
295 synthetic_mouse_event_.timeout.start();
296 // Fall through to handle this event...
299 } else if (synthetic_mouse_event_.timeout.running()) {
300 // Store the event, to be possibly handled when the timeout
302 // Once the timeout has expired, normal control is returned
303 // to mouseMoveEvent (restart_timeout = false).
304 // This results in a much smoother 'feel' when moving the
305 // mouse back into the work area.
306 synthetic_mouse_event_.cmd = cmd;
307 synthetic_mouse_event_.restart_timeout = false;
311 // Has anything changed on-screen since the last QMouseEvent
313 double const scrollbar_value = verticalScrollBar()->value();
314 if (e->x() != synthetic_mouse_event_.x_old ||
315 e->y() != synthetic_mouse_event_.y_old ||
316 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
317 // Yes it has. Store the params used to check this.
318 synthetic_mouse_event_.x_old = e->x();
319 synthetic_mouse_event_.y_old = e->y();
320 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
322 // ... and dispatch the event to the LyX core.
323 buffer_view_->workAreaDispatch(cmd);
328 void GuiWorkArea::wheelEvent(QWheelEvent * e)
330 // Wheel rotation by one notch results in a delta() of 120 (see
331 // documentation of QWheelEvent)
332 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
333 verticalScrollBar()->setValue(verticalScrollBar()->value() -
334 lines * verticalScrollBar()->lineStep());
335 adjustViewWithScrollBar();
339 void GuiWorkArea::generateSyntheticMouseEvent()
341 // Set things off to generate the _next_ 'pseudo' event.
342 if (synthetic_mouse_event_.restart_timeout)
343 synthetic_mouse_event_.timeout.start();
345 // Has anything changed on-screen since the last timeout signal
347 double const scrollbar_value = verticalScrollBar()->value();
348 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
349 // Yes it has. Store the params used to check this.
350 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
352 // ... and dispatch the event to the LyX core.
353 buffer_view_->workAreaDispatch(synthetic_mouse_event_.cmd);
358 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
360 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
361 << " count=" << e->count()
362 << " text=" << (const char *) e->text()
363 << " isAutoRepeat=" << e->isAutoRepeat()
364 << " key=" << e->key()
367 if (USE_EVENT_PRUNING) {
368 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
371 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
373 processKeySym(sym, q_key_state(e->state()));
378 // This is used only if USE_EVENT_PRUNING is defined...
379 void GuiWorkArea::keyeventTimeout()
381 bool handle_autos = true;
383 while (!keyeventQueue_.empty()) {
384 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
386 // We never handle more than one auto repeated
387 // char in a list of queued up events.
388 if (!handle_autos && ev->isAutoRepeat()) {
389 keyeventQueue_.pop();
393 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
396 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
397 << " count=" << ev->count()
398 << " text=" << (const char *) ev->text()
399 << " isAutoRepeat=" << ev->isAutoRepeat()
400 << " key=" << ev->key()
403 processKeySym(sym, q_key_state(ev->state()));
404 keyeventQueue_.pop();
406 handle_autos = false;
409 // Restart the timer.
410 step_timer_.start(25, true);
414 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
416 dc_event_ = double_click(e);
418 if (!dc_event_.active)
421 dc_event_.active = false;
423 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
424 dc_event_.x, dc_event_.y,
425 q_button_state(dc_event_.state));
426 buffer_view_->workAreaDispatch(cmd);
430 void GuiWorkArea::resizeEvent(QResizeEvent *)
432 workWidth_ = viewport()->width();
433 workHeight_ = viewport()->height();
435 verticalScrollBar()->setPageStep(viewport()->height());
437 // screen_device_ = QPixmap(viewport()->width(), viewport()->height());
438 // paint_device_ = QImage(viewport()->width(), viewport()->height(), QImage::Format_RGB32);
439 paint_device_ = QPixmap(viewport()->width(), viewport()->height());
441 buffer_view_->workAreaResize(viewport()->width(), viewport()->height());
444 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
445 << "\n QWidget width\t" << this->QWidget::width()
446 << "\n QWidget height\t" << this->QWidget::height()
447 << "\n viewport width\t" << viewport()->width()
448 << "\n viewport height\t" << viewport()->height()
449 << "\n QResizeEvent rect left\t" << rect().left()
450 << "\n QResizeEvent rect right\t" << rect().right()
456 void GuiWorkArea::update(int x, int y, int w, int h)
458 //screen_device_.fromImage(paint_device_);
459 //QPainter q(&screen_device_);
460 //q.drawImage(x, y, paint_device_.copy(x, y, w, h));
462 viewport()->update(x, y, w, h);
466 void GuiWorkArea::paintEvent(QPaintEvent * e)
469 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
470 << "\n QWidget width\t" << this->width()
471 << "\n QWidget height\t" << this->height()
472 << "\n viewport width\t" << viewport()->width()
473 << "\n viewport height\t" << viewport()->height()
474 << "\n pixmap width\t" << pixmap_->width()
475 << "\n pixmap height\t" << pixmap_->height()
476 << "\n QPaintEvent x\t" << e->rect().x()
477 << "\n QPaintEvent y\t" << e->rect().y()
478 << "\n QPaintEvent w\t" << e->rect().width()
479 << "\n QPaintEvent h\t" << e->rect().height()
482 QPainter q(viewport());
483 q.drawPixmap(e->rect(), paint_device_, e->rect());
486 q.drawPixmap(cursor_x_, cursor_y_, vcursor_);
489 q.drawPixmap(cursor_x_, cursor_y_ + cursor_h_ - 1, hcursor_);
491 buffer_view_->updateScrollbar();
493 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
495 verticalScrollBar()->setTracking(false);
496 setScrollbarParams(scroll_.height, scroll_.position,
497 scroll_.lineScrollHeight);
498 verticalScrollBar()->setTracking(true);
502 QPixmap GuiWorkArea::copyScreen(int x, int y, int w, int h) const
504 return paint_device_.copy(x, y, w, h);
508 void GuiWorkArea::drawScreen(int x, int y, QPixmap pixmap)
510 QPainter q(&paint_device_);
511 q.drawPixmap(x, y, pixmap);
512 update(x, y, pixmap.width(), pixmap.height());
516 void GuiWorkArea::expose(int x, int y, int w, int h)
519 if (x == 0 && y == 0 && w == viewport()->width() && h == viewport()->height()) {
520 viewport()->repaint(x, y, w, h);
529 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
531 if (!qApp->focusWidget())
534 show_vcursor_ = true;
536 QColor const & required_color = lcolorcache.get(LColor::cursor);
538 if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_
539 && cursor_color_ == required_color
540 && cursor_shape_ == shape) {
541 show_hcursor_ = lshape_cursor_;
542 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
546 // Cache the dimensions of the cursor.
550 cursor_color_ = required_color;
551 cursor_shape_ = shape;
553 switch (cursor_shape_) {
555 // FIXME the cursor width shouldn't be hard-coded!
557 lshape_cursor_ = false;
560 cursor_w_ = cursor_h_ / 3;
561 lshape_cursor_ = true;
563 case REVERSED_L_SHAPE:
564 cursor_w_ = cursor_h_ / 3;
565 cursor_x_ -= cursor_w_ - 1;
566 lshape_cursor_ = true;
570 // We cache two pixmaps:
571 // 1 the vertical line of the cursor.
572 // 2 the horizontal line of the L-shaped cursor (if necessary).
574 // Draw the new (vertical) cursor.
575 vcursor_ = QPixmap(cursor_w_, cursor_h_);
576 vcursor_.fill(cursor_color_);
578 // Draw the new (horizontal) cursor if necessary.
579 if (lshape_cursor_) {
580 hcursor_ = QPixmap(cursor_w_, 1);
581 hcursor_.fill(cursor_color_);
582 show_hcursor_ = true;
585 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
589 void GuiWorkArea::removeCursor()
591 show_vcursor_ = false;
592 show_hcursor_ = false;
594 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
598 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
600 QString const & text = e->commitString();
601 if (!text.isEmpty()) {
603 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
604 << " preeditString =" << (const char *) e->preeditString()
605 << " commitString =" << (const char *) e->commitString()
609 // needed to make math superscript work on some systems
610 // ideally, such special coding should not be necessary
612 key = Qt::Key_AsciiCircum;
613 QKeyEvent ev(QEvent::KeyPress, key, *text.ascii(), 0, text);
620 } // namespace frontend
623 #include "GuiWorkArea_moc.cpp"