]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QWorkArea.C
fix bug 1955
[lyx.git] / src / frontends / qt4 / QWorkArea.C
1 /**
2  * \file QWorkArea.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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QWorkArea.h"
15 #include "QLPainter.h"
16 #include "QLyXKeySym.h"
17
18 #include "lcolorcache.h"
19 #include "qt_helpers.h"
20
21 #include "debug.h"
22 #include "funcrequest.h"
23 #include "LColor.h"
24 #include "support/os.h"
25
26 #include <QApplication>
27 #include <QClipboard>
28 #include <QLayout>
29 #include <QMainWindow>
30 #include <Q3UriDrag>
31 #include <QDragEnterEvent>
32 #include <QPixmap>
33 #include <QPainter>
34 #include <QScrollBar>
35
36 #include <boost/bind.hpp>
37
38 #ifdef Q_WS_X11
39 #include <X11/Xlib.h>
40 #endif
41
42 #ifdef Q_WS_MACX
43 #include <Carbon/Carbon.h>
44 #endif
45
46 #ifdef Q_OS_MAC
47 #include <support/lstrings.h>
48
49 using lyx::support::subst;
50 #endif
51 using std::endl;
52 using std::string;
53
54 namespace os = lyx::support::os;
55
56 namespace {
57 QWorkArea const * wa_ptr = 0;
58
59 /// return the LyX key state from Qt's
60 key_modifier::state q_key_state(Qt::ButtonState state)
61 {
62         key_modifier::state k = key_modifier::none;
63         if (state & Qt::ControlModifier)
64                 k |= key_modifier::ctrl;
65         if (state & Qt::ShiftModifier)
66                 k |= key_modifier::shift;
67         if (state & Qt::AltModifier)
68                 k |= key_modifier::alt;
69         return k;
70 }
71
72
73 /// return the LyX mouse button state from Qt's
74 mouse_button::state q_button_state(Qt::ButtonState button)
75 {
76         mouse_button::state b = mouse_button::none;
77         switch (button) {
78                 case Qt::LeftButton:
79                         b = mouse_button::button1;
80                         break;
81                 case Qt::MidButton:
82                         b = mouse_button::button2;
83                         break;
84                 case Qt::RightButton:
85                         b = mouse_button::button3;
86                         break;
87                 default:
88                         break;
89         }
90         return b;
91 }
92
93
94 /// return the LyX mouse button state from Qt's
95 mouse_button::state q_motion_state(Qt::ButtonState state)
96 {
97         mouse_button::state b = mouse_button::none;
98         if (state & Qt::LeftButton)
99                 b |= mouse_button::button1;
100         if (state & Qt::MidButton)
101                 b |= mouse_button::button2;
102         if (state & Qt::RightButton)
103                 b |= mouse_button::button3;
104         return b;
105 }
106
107 } // namespace anon
108
109 // This is a 'heartbeat' generating synthetic mouse move events when the
110 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
111 SyntheticMouseEvent::SyntheticMouseEvent()
112         : timeout(200), restart_timeout(true),
113           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
114 {}
115
116
117
118 Painter & QWorkArea::getPainter() { return painter_; }
119
120 //      QLPainter & QWorkArea::getQLPainter() { return painter_; }
121
122 /// get the content pane widget
123 QWidget * QWorkArea::getContent() const { return viewport(); }
124
125 QWorkArea::QWorkArea(LyXView &, int w, int h)
126         : WorkArea(), QAbstractScrollArea(qApp->mainWidget()), painter_(this)
127 {
128         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
129         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
130
131         (static_cast<QMainWindow*>(qApp->mainWidget()))->setCentralWidget(this);
132
133         setAcceptDrops(true);
134
135         setMinimumSize(100, 70);
136
137 //      setBackgroundRole(lcolorcache.get(LColor::background));
138 //      viewport()->setBackgroundRole(QPalette::Window);
139         viewport()->setAutoFillBackground(false);
140         viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
141
142         viewport()->setFocusPolicy(Qt::WheelFocus);
143         viewport()->setFocus();
144 //      viewport()->grabKeyboard();
145         setFocusPolicy(Qt::WheelFocus);
146         setFocusProxy(viewport());
147         viewport()->setCursor(Qt::IBeamCursor);
148
149         resize(w, h);
150         show();
151
152         scrolled_with_mouse_=false;
153         scrolled_with_keyboard_ = false;
154
155         synthetic_mouse_event_.timeout.timeout.connect(
156                 boost::bind(&QWorkArea::generateSyntheticMouseEvent,
157                             this));
158 /*
159         if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
160                 this, SLOT(keyeventTimeout())) )
161                         lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
162 */
163
164         if ( !QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
165                 this, SLOT(adjustViewWithScrollBar(int))) )
166                         lyxerr[Debug::GUI] << "ERROR: adjustViewWithScrollBar cannot connect!" << endl;
167         
168 #if USE_INPUT_METHODS
169         // to make qt-immodule work
170         setInputMethodEnabled(true);
171 #endif
172 #ifdef Q_WS_X11
173         // doubleClickInterval() is 400 ms on X11 witch is just too long.
174         // On Windows and Mac OS X, the operating system's value is used.
175         // On Microsoft Windows, calling this function sets the double
176         // click interval for all applications. So we don't!
177         QApplication::setDoubleClickInterval(300);
178 #endif
179
180         // Start the timer, one-shot.
181         step_timer_.start(50, true);
182
183         //viewport()->resize(w, h);
184         pixmap_.reset(new QPixmap(viewport()->width(), viewport()->height()));
185
186         this->workAreaResize();
187         
188         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
189                 << "\n Area width\t" << width()
190                 << "\n Area height\t" << height()
191                 << "\n viewport width\t" << viewport()->width()
192                 << "\n viewport height\t" << viewport()->height()
193                 << endl;
194
195 //      this->QWidget::resize(w,h);
196
197 #ifdef Q_WS_MACX
198         wa_ptr = this;
199 #endif
200 }
201
202 QWorkArea::~QWorkArea()
203 {
204 }
205
206 void QWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
207 {
208         /*
209         if (scrolled_with_mouse_)
210         {
211                 scrolled_with_mouse_=false;
212                 return;
213         }
214         */
215
216         //if (scroll_pos_ == scroll_pos)
217         //{
218         //      verticalScrollBar()->triggerAction(QAbstractSlider::SliderMove);
219                 int scroll_pos_ = scroll_pos;
220         //}
221         int scroll_line_step_ = scroll_line_step;
222         int scroll_page_step_ = viewport()->height();
223
224         // do what cursor movement does (some grey)
225         h += height() / 4;
226         int scroll_max_ = std::max(0, h - height());
227
228         //scrolled_with_keyboard_=true;
229
230         verticalScrollBar()->setRange(0, scroll_max_);
231         verticalScrollBar()->setSliderPosition(scroll_pos_);
232         verticalScrollBar()->setLineStep(scroll_line_step_);
233         verticalScrollBar()->setPageStep(scroll_page_step_);
234 }
235
236 void QWorkArea::adjustViewWithScrollBar(int action)
237 {
238         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
239                 << " verticalScrollBar val=" << verticalScrollBar()->value()
240                 << " verticalScrollBar pos=" << verticalScrollBar()->sliderPosition()
241                 << " min=" << verticalScrollBar()->minimum()
242                 << " max=" << verticalScrollBar()->maximum()
243                 << " pagestep=" << verticalScrollBar()->pageStep()
244                 << " linestep=" << verticalScrollBar()->lineStep()
245                 << endl;
246
247         this->scrollDocView(verticalScrollBar()->sliderPosition());
248
249 //      scrolled_with_mouse_ = true;
250 }
251
252 #ifdef Q_WS_X11
253 bool lyxX11EventFilter(XEvent * xev)
254 {
255         switch (xev->type) {
256         case SelectionRequest:
257                 lyxerr[Debug::GUI] << "X requested selection." << endl;
258                 if (wa_ptr)
259                         wa_ptr->selectionRequested();
260                 break;
261         case SelectionClear:
262                 lyxerr[Debug::GUI] << "Lost selection." << endl;
263                 if (wa_ptr)
264                         wa_ptr->selectionLost();
265                 break;
266         }
267         return false;
268 }
269 #endif
270
271 #ifdef Q_WS_MACX
272 namespace{
273 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
274  {
275         DescType returnedType;
276         Size actualSize;
277         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
278                                       typeWildCard, &returnedType, nil, 0,
279                                       &actualSize);
280         switch (err) {
281         case errAEDescNotFound:
282                 return noErr;
283         case noErr:
284                 return errAEEventNotHandled;
285         default:
286                 return err;
287         }
288  }
289 }
290
291 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
292                                  AppleEvent* /*reply*/, long /*refCon*/)
293 {
294         QString s_arg;
295         AEDescList documentList;
296         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
297                                    &documentList);
298         if (err != noErr)
299                 return err;
300
301         err = checkAppleEventForMissingParams(*inEvent);
302         if (err == noErr) {
303                 long documentCount;
304                 err = AECountItems(&documentList, &documentCount);
305                 for (long documentIndex = 1;
306                      err == noErr && documentIndex <= documentCount;
307                      documentIndex++) {
308                         DescType returnedType;
309                         Size actualSize;
310                         AEKeyword keyword;
311                         FSRef ref;
312                         char qstr_buf[1024];
313                         err = AESizeOfNthItem(&documentList, documentIndex,
314                                               &returnedType, &actualSize);
315                         if (err == noErr) {
316                                 err = AEGetNthPtr(&documentList, documentIndex,
317                                                   typeFSRef, &keyword,
318                                                   &returnedType, (Ptr)&ref,
319                                                   sizeof(FSRef), &actualSize);
320                                 if (err == noErr) {
321                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
322                                                       1024);
323                                         s_arg=QString::fromUtf8(qstr_buf);
324                                         wa_ptr->dispatch(
325                                                 FuncRequest(LFUN_FILE_OPEN,
326                                                             fromqstr(s_arg)));
327                                         break;
328                                 }
329                         }
330                 } // for ...
331         }
332         AEDisposeDesc(&documentList);
333         return err;
334 }
335 #endif  // Q_WS_MACX
336
337 void QWorkArea::haveSelection(bool own) const
338 {
339         wa_ptr = this;
340
341         if (!QApplication::clipboard()->supportsSelection())
342                 return;
343
344         if (own) {
345                 QApplication::clipboard()->setText(QString(), QClipboard::Selection);
346         }
347         // We don't need to do anything if own = false, as this case is
348         // handled by QT.
349 }
350
351
352 string const QWorkArea::getClipboard() const
353 {
354         QString str = QApplication::clipboard()->text(QClipboard::Selection);
355         lyxerr[Debug::ACTION] << "getClipboard: " << (const char*) str << endl;
356         if (str.isNull())
357                 return string();
358 #ifdef Q_OS_MAC
359         // The MAC clipboard uses \r for lineendings, and we use \n
360         return subst(fromqstr(str), '\r', '\n');
361 #else
362         return fromqstr(str);
363 #endif
364 }
365
366
367 void QWorkArea::putClipboard(string const & str) const
368 {
369 #ifdef Q_OS_MAC
370         // The MAC clipboard uses \r for lineendings, and we use \n
371         QApplication::clipboard()->setText(toqstr(subst(str, '\n', '\r')),
372                                            QClipboard::Selection);
373 #else
374         QApplication::clipboard()->setText(toqstr(str), QClipboard::Selection);
375 #endif
376         lyxerr[Debug::ACTION] << "putClipboard: " << str << endl;
377 }
378
379
380 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
381 {
382         event->accept(Q3UriDrag::canDecode(event));
383
384         /// \todo Ask lyx-devel is this is enough:
385         /// if (event->mimeData()->hasFormat("text/plain"))
386         ///     event->acceptProposedAction();
387
388 }
389
390
391 void QWorkArea::dropEvent(QDropEvent* event)
392 {
393         QStringList files;
394
395         if (Q3UriDrag::decodeLocalFiles(event, files)) {
396                 lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!"
397                                    << endl;
398                 for (QStringList::Iterator i = files.begin();
399                      i!=files.end(); ++i) {
400                         string const file = os::internal_path(fromqstr(*i));
401                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
402                 }
403         }
404 }
405 /*
406 void QWorkArea::scrollContentsBy(int dx, int dy)
407 {       
408         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
409                 << " scroll by dx=" << dx
410                 << " dy=" << dy
411                 << " verticalScrollBar val=" << verticalScrollBar()->value()
412                 << " min=" << verticalScrollBar()->minimum()
413                 << " max=" << verticalScrollBar()->maximum()
414                 << " pagestep=" << verticalScrollBar()->pageStep()
415                 << " linestep=" << verticalScrollBar()->lineStep()
416                 << endl;
417
418         if (scrolled_with_keyboard_)
419         {
420                 scrolled_with_keyboard_=false;
421                 return;
422         }
423
424         this->scrollDocView(verticalScrollBar()->value());
425         scrolled_with_mouse_ = true;
426 }
427 */
428
429 #if USE_INPUT_METHODS
430 // to make qt-immodule work
431
432 void QWorkArea::inputMethodEvent(QInputMethodEvent * e) 
433 {
434         QString const text = e->text();
435         if (!text.isEmpty()) {
436                 int key = 0;
437                 // needed to make math superscript work on some systems
438                 // ideally, such special coding should not be necessary
439                 if (text == "^")
440                         key = Qt::Key_AsciiCircum;
441                 QKeyEvent ev(QEvent::KeyPress, key, *text.ascii(), 0, text);
442                 keyPressEvent(&ev);
443         }
444         e->accept();
445 }
446 #endif
447
448 void QWorkArea::mousePressEvent(QMouseEvent * e)
449 {
450         if (dc_event_.active && dc_event_ == *e) {
451                 dc_event_.active = false;
452                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
453                         dc_event_.x, dc_event_.y,
454                         q_button_state(dc_event_.state));
455                 this->dispatch(cmd);
456                 return;
457         }
458
459         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
460                               q_button_state(e->button()));
461         this->dispatch(cmd);
462 }
463
464
465 void QWorkArea::mouseReleaseEvent(QMouseEvent * e)
466 {
467         if (synthetic_mouse_event_.timeout.running())
468                 synthetic_mouse_event_.timeout.stop();
469
470         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
471                               q_button_state(e->button()));
472         this->dispatch(cmd);
473 }
474
475
476 void QWorkArea::mouseMoveEvent(QMouseEvent * e)
477 {
478         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
479                               q_motion_state(e->state()));
480
481         // If we're above or below the work area...
482         if (e->y() <= 20 || e->y() >= viewport()->QWidget::height() - 20) {
483                 // Make sure only a synthetic event can cause a page scroll,
484                 // so they come at a steady rate:
485                 if (e->y() <= 20)
486                         // _Force_ a scroll up:
487                         cmd.y = -40;
488                 else
489                         cmd.y = viewport()->QWidget::height();
490                 // Store the event, to be handled when the timeout expires.
491                 synthetic_mouse_event_.cmd = cmd;
492
493                 if (synthetic_mouse_event_.timeout.running())
494                         // Discard the event. Note that it _may_ be handled
495                         // when the timeout expires if
496                         // synthetic_mouse_event_.cmd has not been overwritten.
497                         // Ie, when the timeout expires, we handle the
498                         // most recent event but discard all others that
499                         // occurred after the one used to start the timeout
500                         // in the first place.
501                         return;
502                 else {
503                         synthetic_mouse_event_.restart_timeout = true;
504                         synthetic_mouse_event_.timeout.start();
505                         // Fall through to handle this event...
506                 }
507
508         } else if (synthetic_mouse_event_.timeout.running()) {
509                 // Store the event, to be possibly handled when the timeout
510                 // expires.
511                 // Once the timeout has expired, normal control is returned
512                 // to mouseMoveEvent (restart_timeout = false).
513                 // This results in a much smoother 'feel' when moving the
514                 // mouse back into the work area.
515                 synthetic_mouse_event_.cmd = cmd;
516                 synthetic_mouse_event_.restart_timeout = false;
517                 return;
518         }
519
520         // Has anything changed on-screen since the last QMouseEvent
521         // was received?
522         double const scrollbar_value = verticalScrollBar()->value();
523         if (e->x() != synthetic_mouse_event_.x_old ||
524             e->y() != synthetic_mouse_event_.y_old ||
525             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
526                 // Yes it has. Store the params used to check this.
527                 synthetic_mouse_event_.x_old = e->x();
528                 synthetic_mouse_event_.y_old = e->y();
529                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
530
531                 // ... and dispatch the event to the LyX core.
532                 this->dispatch(cmd);
533         }
534 }
535
536
537 void QWorkArea::wheelEvent(QWheelEvent * e)
538 {
539 //      verticalScrollBar()->setValue(verticalScrollBar()->value() - e->delta());
540 }
541
542
543 void QWorkArea::keyPressEvent(QKeyEvent * e)
544 {
545         lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
546                 << " count=" << e->count()
547                 << " text=" << (const char *) e->text()
548                 << " isAutoRepeat=" << e->isAutoRepeat()
549                 << " key=" << e->key()
550                 << endl;
551
552         //viewport()->grabKeyboard();
553 //      keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
554
555     boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
556     sym->set(e);
557     this->workAreaKeyPress(sym, q_key_state(e->state()));        
558  
559 }
560
561 void QWorkArea::generateSyntheticMouseEvent()
562 {
563         // Set things off to generate the _next_ 'pseudo' event.
564         if (synthetic_mouse_event_.restart_timeout)
565                 synthetic_mouse_event_.timeout.start();
566
567         // Has anything changed on-screen since the last timeout signal
568         // was received?
569         double const scrollbar_value = verticalScrollBar()->value();
570         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
571                 // Yes it has. Store the params used to check this.
572                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
573
574                 // ... and dispatch the event to the LyX core.
575                 this->dispatch(synthetic_mouse_event_.cmd);
576         }
577 }
578
579
580 void QWorkArea::keyeventTimeout()
581 {
582         bool handle_autos = true;
583
584         while (!keyeventQueue_.empty()) {
585                 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
586
587                 // We never handle more than one auto repeated
588                 // char in a list of queued up events.
589                 if (!handle_autos && ev->isAutoRepeat()) {
590                         keyeventQueue_.pop();
591                         continue;
592                 }
593
594         boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
595                 sym->set(ev.get());
596
597         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
598                 << " count=" << ev->count()
599                 << " text=" << (const char *) ev->text()
600                 << " isAutoRepeat=" << ev->isAutoRepeat()
601                 << " key=" << ev->key()
602                 << endl;
603
604                 this->workAreaKeyPress(sym, q_key_state(ev->state()));
605                 keyeventQueue_.pop();
606
607                 handle_autos = false;
608         }
609
610         // Restart the timer.
611         step_timer_.start(25, true);
612 }
613
614
615 void QWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
616 {
617         dc_event_ = double_click(e);
618
619         if (!dc_event_.active)
620                 return;
621
622         dc_event_.active = false;
623
624         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
625                 dc_event_.x, dc_event_.y,
626                 q_button_state(dc_event_.state));
627         this->dispatch(cmd);
628 }
629
630
631 void QWorkArea::resizeEvent(QResizeEvent * resizeEvent)
632 {
633         pixmap_.reset(new QPixmap(viewport()->width(), viewport()->height()));
634
635         scrolled_with_mouse_=false;
636         scrolled_with_keyboard_=false;
637
638         this->workAreaResize();
639
640         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
641                 << "\n QWidget width\t" << viewport()->width()
642                 << "\n QWidget height\t" << viewport()->height()
643                 << "\n QResizeEvent rect left\t" << rect().left()
644                 << "\n QResizeEvent rect right\t" << rect().right()
645                 << endl;
646 }
647
648 void QWorkArea::paintEvent(QPaintEvent * e)
649 {
650 /*
651         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
652                 << "\n QWidget width\t" << viewport()->width()
653                 << "\n QWidget height\t" << viewport()->height()
654                 << "\n QPaintEvent x\t" << e->rect().x()
655                 << "\n QPaintEvent y\t" << e->rect().x()
656                 << "\n QPaintEvent w\t" << e->rect().width()
657                 << "\n QPaintEvent h\t" << e->rect().height()
658                 << endl;
659 */
660         QPainter q(viewport());
661         q.drawPixmap(e->rect(), *pixmap_.get(), e->rect());
662
663 //      q.drawPixmap(QPoint(r.x(), r.y()),
664 //              *pixmap_.get(), r);
665 }