]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWorkArea.cpp
* update cursor position on resize _after_ drawing. Because before
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
1 /**
2  * \file GuiWorkArea.cpp
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 "GuiWorkArea.h"
15
16 #include "GuiApplication.h"
17 #include "QLPainter.h"
18 #include "QKeySymbol.h"
19 #include "qt_helpers.h"
20
21 #include "LyXView.h"
22
23 #include "BufferView.h"
24 #include "rowpainter.h"
25 #include "debug.h"
26 #include "FuncRequest.h"
27 #include "Color.h"
28 #include "version.h"
29 #include "LyXRC.h"
30
31 #include "support/filetools.h" // LibFileSearch
32 #include "support/os.h"
33 #include "support/convert.h"
34
35 #include "graphics/GraphicsImage.h"
36 #include "graphics/GraphicsLoader.h"
37
38 #include <QLayout>
39 #include <QMainWindow>
40 #include <QMimeData>
41 #include <QUrl>
42 #include <QDragEnterEvent>
43 #include <QPainter>
44 #include <QScrollBar>
45 #include <QTimer>
46 #include <QInputContext>
47
48 #include <boost/bind.hpp>
49 #include <boost/current_function.hpp>
50
51 #ifdef Q_WS_X11
52 #include <QX11Info>
53 extern "C" int XEventsQueued(Display *display, int mode);
54 #endif
55
56 #ifdef Q_WS_WIN
57 int const CursorWidth = 2;
58 #else
59 int const CursorWidth = 1;
60 #endif
61
62
63 using std::endl;
64 using std::string;
65
66 namespace os = lyx::support::os;
67
68
69 namespace lyx {
70
71 using support::FileName;
72
73 /// return the LyX key state from Qt's
74 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
75 {
76         key_modifier::state k = key_modifier::none;
77         if (state & Qt::ControlModifier)
78                 k |= key_modifier::ctrl;
79         if (state & Qt::ShiftModifier)
80                 k |= key_modifier::shift;
81         if (state & Qt::AltModifier || state & Qt::MetaModifier)
82                 k |= key_modifier::alt;
83         return k;
84 }
85
86
87 /// return the LyX mouse button state from Qt's
88 static mouse_button::state q_button_state(Qt::MouseButton button)
89 {
90         mouse_button::state b = mouse_button::none;
91         switch (button) {
92                 case Qt::LeftButton:
93                         b = mouse_button::button1;
94                         break;
95                 case Qt::MidButton:
96                         b = mouse_button::button2;
97                         break;
98                 case Qt::RightButton:
99                         b = mouse_button::button3;
100                         break;
101                 default:
102                         break;
103         }
104         return b;
105 }
106
107
108 /// return the LyX mouse button state from Qt's
109 mouse_button::state q_motion_state(Qt::MouseButtons state)
110 {
111         mouse_button::state b = mouse_button::none;
112         if (state & Qt::LeftButton)
113                 b |= mouse_button::button1;
114         if (state & Qt::MidButton)
115                 b |= mouse_button::button2;
116         if (state & Qt::RightButton)
117                 b |= mouse_button::button3;
118         return b;
119 }
120
121
122 namespace frontend {
123
124 class CursorWidget {
125 public:
126         CursorWidget() {}
127
128         void draw(QPainter & painter)
129         {
130                 if (show_ && rect_.isValid()) {
131                         switch (shape_) {
132                         case L_SHAPE:
133                                 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
134                                 painter.setPen(color_);
135                                 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
136                                                                                                  rect_.bottomRight().x(), rect_.bottomLeft().y());
137                                 break;
138                         
139                         case REVERSED_L_SHAPE:
140                                 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
141                                 painter.setPen(color_);
142                                 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
143                                                                                                          rect_.bottomLeft().x(), rect_.bottomLeft().y());
144                                 break;
145                                         
146                         default:
147                                 painter.fillRect(rect_, color_);
148                                 break;
149                         }
150                 }
151         }
152
153         void update(int x, int y, int h, CursorShape shape)
154         {
155                 color_ = guiApp->colorCache().get(Color::cursor);
156                 shape_ = shape;
157                 switch (shape) {
158                 case L_SHAPE:
159                         rect_ = QRect(x, y, CursorWidth + h / 3, h);
160                         break;
161                 case REVERSED_L_SHAPE:
162                         rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
163                         break;
164                 default: 
165                         rect_ = QRect(x, y, CursorWidth, h);
166                         break;
167                 }
168         }
169
170         void show(bool set_show = true) { show_ = set_show; }
171         void hide() { show_ = false; }
172
173         QRect const & rect() { return rect_; }
174
175 private:
176         ///
177         CursorShape shape_;
178         ///
179         bool show_;
180         ///
181         QColor color_;
182         ///
183         QRect rect_;
184 };
185
186
187 // This is a 'heartbeat' generating synthetic mouse move events when the
188 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
189 SyntheticMouseEvent::SyntheticMouseEvent()
190         : timeout(200), restart_timeout(true),
191           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
192 {}
193
194
195 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
196         : WorkArea(id, lyx_view), need_resize_(false), schedule_redraw_(false),
197           preedit_lines_(1)
198 {
199         screen_ = QPixmap(viewport()->width(), viewport()->height());
200         cursor_ = new frontend::CursorWidget();
201         cursor_->hide();
202
203         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
204         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
205         setAcceptDrops(true);
206         setMouseTracking(true);
207         setMinimumSize(100, 70);
208
209         viewport()->setAutoFillBackground(false);
210         // We don't need double-buffering nor SystemBackground on
211         // the viewport because we have our own backing pixmap.
212         viewport()->setAttribute(Qt::WA_NoSystemBackground);
213
214         setFocusPolicy(Qt::WheelFocus);
215
216         viewport()->setCursor(Qt::IBeamCursor);
217
218         resize(w, h);
219
220         synthetic_mouse_event_.timeout.timeout.connect(
221                 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
222                             this));
223
224         // Initialize the vertical Scroll Bar
225         QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
226                 this, SLOT(adjustViewWithScrollBar(int)));
227
228         // disable context menu for the scrollbar
229         verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
230
231         // PageStep only depends on the viewport height.
232         verticalScrollBar()->setPageStep(viewport()->height());
233
234         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
235                 << "\n Area width\t" << width()
236                 << "\n Area height\t" << height()
237                 << "\n viewport width\t" << viewport()->width()
238                 << "\n viewport height\t" << viewport()->height()
239                 << endl;
240
241         // Enables input methods for asian languages.
242         // Must be set when creating custom text editing widgets.
243         setAttribute(Qt::WA_InputMethodEnabled, true);
244 }
245
246
247 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
248 {
249         if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
250                 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
251
252         verticalScrollBar()->setTracking(false);
253
254         // do what cursor movement does (some grey)
255         h += height() / 4;
256         int scroll_max_ = std::max(0, h - height());
257
258         verticalScrollBar()->setRange(0, scroll_max_);
259         verticalScrollBar()->setSliderPosition(scroll_pos);
260         verticalScrollBar()->setSingleStep(scroll_line_step);
261         verticalScrollBar()->setValue(scroll_pos);
262
263         verticalScrollBar()->setTracking(true);
264 }
265
266
267 void GuiWorkArea::adjustViewWithScrollBar(int)
268 {
269         scrollBufferView(verticalScrollBar()->sliderPosition());
270         QApplication::syncX();
271 }
272
273
274 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
275 {
276         if (event->mimeData()->hasUrls())
277                 event->accept();
278         /// \todo Ask lyx-devel is this is enough:
279         /// if (event->mimeData()->hasFormat("text/plain"))
280         ///     event->acceptProposedAction();
281 }
282
283
284 void GuiWorkArea::dropEvent(QDropEvent* event)
285 {
286         QList<QUrl> files = event->mimeData()->urls();
287         if (files.isEmpty())
288                 return;
289
290         LYXERR(Debug::GUI) << "GuiWorkArea::dropEvent: got URIs!" << endl;
291         for (int i = 0; i!=files.size(); ++i) {
292                 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
293                 if (!file.empty())
294                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
295         }
296 }
297
298
299 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
300 {
301         // No need to do anything if we didn't change views...
302 //      if (theApp() == 0 || &lyx_view_ == theApp()->currentView())
303 //              return;
304
305         theApp()->setCurrentView(lyx_view_);
306
307         // Repaint the whole screen.
308         // Note: this is different from redraw() as only the backing pixmap
309         // will be redrawn, which is cheap.
310         viewport()->repaint();
311
312         // FIXME: it would be better to send a signal "newBuffer()"
313         // in BufferList that could be connected to the different tabbars.
314         lyx_view_.updateTab();
315
316         startBlinkingCursor();
317 }
318
319
320 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
321 {
322         stopBlinkingCursor();
323 }
324
325
326 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
327 {
328         if (dc_event_.active && dc_event_ == *e) {
329                 dc_event_.active = false;
330                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
331                         e->x(), e->y(),
332                         q_button_state(e->button()));
333                 dispatch(cmd);
334                 return;
335         }
336
337         inputContext()->reset();
338
339         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
340                 q_button_state(e->button()));
341         dispatch(cmd, q_key_state(e->modifiers()));
342 }
343
344
345 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
346 {
347         if (synthetic_mouse_event_.timeout.running())
348                 synthetic_mouse_event_.timeout.stop();
349
350         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
351                               q_button_state(e->button()));
352         dispatch(cmd);
353 }
354
355
356 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
357 {
358         // we kill the triple click if we move
359         doubleClickTimeout();
360         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
361                               q_motion_state(e->buttons()));
362
363         // If we're above or below the work area...
364         if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
365                 // Make sure only a synthetic event can cause a page scroll,
366                 // so they come at a steady rate:
367                 if (e->y() <= 20)
368                         // _Force_ a scroll up:
369                         cmd.y = -40;
370                 else
371                         cmd.y = viewport()->height();
372                 // Store the event, to be handled when the timeout expires.
373                 synthetic_mouse_event_.cmd = cmd;
374
375                 if (synthetic_mouse_event_.timeout.running())
376                         // Discard the event. Note that it _may_ be handled
377                         // when the timeout expires if
378                         // synthetic_mouse_event_.cmd has not been overwritten.
379                         // Ie, when the timeout expires, we handle the
380                         // most recent event but discard all others that
381                         // occurred after the one used to start the timeout
382                         // in the first place.
383                         return;
384                 else {
385                         synthetic_mouse_event_.restart_timeout = true;
386                         synthetic_mouse_event_.timeout.start();
387                         // Fall through to handle this event...
388                 }
389
390         } else if (synthetic_mouse_event_.timeout.running()) {
391                 // Store the event, to be possibly handled when the timeout
392                 // expires.
393                 // Once the timeout has expired, normal control is returned
394                 // to mouseMoveEvent (restart_timeout = false).
395                 // This results in a much smoother 'feel' when moving the
396                 // mouse back into the work area.
397                 synthetic_mouse_event_.cmd = cmd;
398                 synthetic_mouse_event_.restart_timeout = false;
399                 return;
400         }
401
402         // Has anything changed on-screen since the last QMouseEvent
403         // was received?
404         double const scrollbar_value = verticalScrollBar()->value();
405         if (e->x() != synthetic_mouse_event_.x_old ||
406             e->y() != synthetic_mouse_event_.y_old ||
407             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
408                 // Yes it has. Store the params used to check this.
409                 synthetic_mouse_event_.x_old = e->x();
410                 synthetic_mouse_event_.y_old = e->y();
411                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
412
413                 // ... and dispatch the event to the LyX core.
414                 dispatch(cmd);
415         }
416 }
417
418
419 void GuiWorkArea::wheelEvent(QWheelEvent * e)
420 {
421         // Wheel rotation by one notch results in a delta() of 120 (see
422         // documentation of QWheelEvent)
423         int const lines = qApp->wheelScrollLines() * e->delta() / 120;
424         verticalScrollBar()->setValue(verticalScrollBar()->value() -
425                         lines *  verticalScrollBar()->singleStep());
426         adjustViewWithScrollBar();
427 }
428
429
430 void GuiWorkArea::generateSyntheticMouseEvent()
431 {
432 // Set things off to generate the _next_ 'pseudo' event.
433         if (synthetic_mouse_event_.restart_timeout)
434                 synthetic_mouse_event_.timeout.start();
435
436         // Has anything changed on-screen since the last timeout signal
437         // was received?
438         double const scrollbar_value = verticalScrollBar()->value();
439         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
440                 // Yes it has. Store the params used to check this.
441                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
442
443                 // ... and dispatch the event to the LyX core.
444                 dispatch(synthetic_mouse_event_.cmd);
445         }
446 }
447
448
449 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
450 {
451         // do nothing if there are other events
452         // (the auto repeated events come too fast)
453         // \todo FIXME: remove hard coded Qt keys, process the key binding
454 #ifdef Q_WS_X11
455         if (XEventsQueued(QX11Info::display(), 0) > 1 && e->isAutoRepeat() 
456                         && (Qt::Key_PageDown || Qt::Key_PageUp)) {
457                 LYXERR(Debug::KEY)      
458                         << BOOST_CURRENT_FUNCTION << endl
459                         << "system is busy: scroll key event ignored" << endl;
460                 e->ignore();
461                 return;
462         }
463 #endif
464
465         LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
466                 << " count=" << e->count()
467                 << " text=" << fromqstr(e->text())
468                 << " isAutoRepeat=" << e->isAutoRepeat()
469                 << " key=" << e->key()
470                 << endl;
471
472         boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
473         sym->set(e);
474         processKeySym(sym, q_key_state(e->modifiers()));
475 }
476
477 void GuiWorkArea::doubleClickTimeout() {
478         dc_event_.active = false;
479 }
480
481 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
482 {
483         dc_event_ = double_click(e);
484         QTimer::singleShot(QApplication::doubleClickInterval(), this,
485                            SLOT(doubleClickTimeout()));
486         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
487                         e->x(), e->y(),
488                         q_button_state(e->button()));
489         dispatch(cmd);
490 }
491
492
493 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
494 {
495         QAbstractScrollArea::resizeEvent(ev);
496         need_resize_ = true;
497 }
498
499
500 void GuiWorkArea::update(int x, int y, int w, int h)
501 {
502         viewport()->repaint(x, y, w, h);
503 }
504
505
506 void GuiWorkArea::doGreyOut(QLPainter & pain)
507 {
508         pain.fillRectangle(0, 0, width(), height(),
509                 Color::bottomarea);
510
511         //if (!lyxrc.show_banner)
512         //      return;
513         LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
514         /// The text to be written on top of the pixmap
515         QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
516         FileName const file = support::libFileSearch("images", "banner", "png");
517         if (file.empty())
518                 return;
519
520         QPixmap pm(toqstr(file.absFilename()));
521         if (!pm) {
522                 lyxerr << "could not load splash screen: '" << file << "'" << endl;
523                 return;
524         }
525
526         QFont font;
527         // The font used to display the version info
528         font.setStyleHint(QFont::SansSerif);
529         font.setWeight(QFont::Bold);
530         font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
531
532         int const w = pm.width();
533         int const h = pm.height();
534
535         int x = (width() - w) / 2;
536         int y = (height() - h) / 2;
537
538         pain.drawPixmap(x, y, pm);
539
540         x += 260;
541         y += 270;
542
543         pain.setPen(QColor(255, 255, 0));
544         pain.setFont(font);
545         pain.drawText(x, y, text);
546 }
547
548
549 void GuiWorkArea::paintEvent(QPaintEvent * ev)
550 {
551         QRect const rc = ev->rect();
552         /*
553         LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
554                 << " y: " << rc.y()
555                 << " w: " << rc.width()
556                 << " h: " << rc.height() << endl;
557         */
558
559         if (need_resize_) {
560                 verticalScrollBar()->setPageStep(viewport()->height());
561                 screen_ = QPixmap(viewport()->width(), viewport()->height());
562                 resizeBufferView();
563                 updateScreen();
564                 WorkArea::hideCursor();
565                 WorkArea::showCursor();
566                 need_resize_ = false;
567         }
568
569         QPainter pain(viewport());
570         pain.drawPixmap(rc, screen_, rc);
571         cursor_->draw(pain);
572 }
573
574
575 void GuiWorkArea::expose(int x, int y, int w, int h)
576 {
577         updateScreen();
578         update(x, y, w, h);
579 }
580
581
582 void GuiWorkArea::updateScreen()
583 {
584         QLPainter pain(&screen_);
585
586         if (greyed_out_) {
587                 LYXERR(Debug::GUI) << "splash screen requested" << endl;
588                 verticalScrollBar()->hide();
589                 doGreyOut(pain);
590                 return;
591         }
592
593         verticalScrollBar()->show();
594         paintText(*buffer_view_, pain);
595 }
596
597
598 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
599 {
600         if (schedule_redraw_) {
601                 if (buffer_view_ && buffer_view_->buffer()) {
602                         buffer_view_->update(Update::Force);
603                         updateScreen();
604                         viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
605                 }
606                 schedule_redraw_ = false;
607                 // Show the cursor immediately after the update.
608                 hideCursor();
609                 toggleCursor();
610                 return;
611         }
612
613         cursor_->update(x, y, h, shape);
614         cursor_->show();
615         viewport()->update(cursor_->rect());
616 }
617
618
619 void GuiWorkArea::removeCursor()
620 {
621         cursor_->hide();
622         //if (!qApp->focusWidget())
623                 viewport()->update(cursor_->rect());
624 }
625
626
627 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
628 {
629         QString const & commit_string = e->commitString();
630         docstring const & preedit_string
631                 = qstring_to_ucs4(e->preeditString());
632
633         if(greyed_out_) {
634                 e->ignore();
635                 return;
636         }
637
638         if (!commit_string.isEmpty()) {
639
640                 LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
641                         << " preeditString =" << fromqstr(e->preeditString())
642                         << " commitString  =" << fromqstr(e->commitString())
643                         << endl;
644
645                 int key = 0;
646
647                 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
648                 for (int i = 0; i < commit_string.size(); ++i) {
649                         QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
650                         keyPressEvent(&ev);
651                 }
652         }
653
654         // Hide the cursor during the kana-kanji transformation.
655         if (preedit_string.empty())
656                 startBlinkingCursor();
657         else
658                 stopBlinkingCursor();
659
660         // last_width : for checking if last preedit string was/wasn't empty.
661         static bool last_width = false;
662         if (!last_width && preedit_string.empty()) {
663                 // if last_width is last length of preedit string.
664                 e->accept();
665                 return;
666         }
667
668         QLPainter pain(&screen_);
669         buffer_view_->updateMetrics(false);
670         paintText(*buffer_view_, pain);
671         Font font = buffer_view_->cursor().getFont();
672         FontMetrics const & fm = theFontMetrics(font);
673         int height = fm.maxHeight();
674         int cur_x = cursor_->rect().left();
675         int cur_y = cursor_->rect().bottom();
676
677         // redraw area of preedit string.
678         update(0, cur_y - height, GuiWorkArea::width(),
679                 (height + 1) * preedit_lines_);
680
681         if (preedit_string.empty()) {
682                 last_width = false;
683                 preedit_lines_ = 1;
684                 e->accept();
685                 return;
686         }
687         last_width = true;
688
689         // att : stores an IM attribute.
690         QList<QInputMethodEvent::Attribute> const & att = e->attributes();
691
692         // get attributes of input method cursor.
693         // cursor_pos : cursor position in preedit string.
694         size_t cursor_pos = 0;
695         bool cursor_is_visible = false;
696         for (int i = 0; i < att.size(); ++i) {
697                 if (att.at(i).type == QInputMethodEvent::Cursor) {
698                         cursor_pos = att.at(i).start;
699                         cursor_is_visible = att.at(i).length != 0;
700                         break;
701                 }
702         }
703
704         size_t preedit_length = preedit_string.length();
705
706         // get position of selection in input method.
707         // FIXME: isn't there a way to do this simplier?
708         // rStart : cursor position in selected string in IM.
709         size_t rStart = 0;
710         // rLength : selected string length in IM.
711         size_t rLength = 0;
712         if (cursor_pos < preedit_length) {
713                 for (int i = 0; i < att.size(); ++i) {
714                         if (att.at(i).type == QInputMethodEvent::TextFormat) {
715                                 if (att.at(i).start <= int(cursor_pos)
716                                         && int(cursor_pos) < att.at(i).start + att.at(i).length) {
717                                                 rStart = att.at(i).start;
718                                                 rLength = att.at(i).length;
719                                                 if (!cursor_is_visible)
720                                                         cursor_pos += rLength;
721                                                 break;
722                                 }
723                         }
724                 }
725         }
726         else {
727                 rStart = cursor_pos;
728                 rLength = 0;
729         }
730
731         int const right_margin = rightMargin();
732         Painter::preedit_style ps;
733         // Most often there would be only one line:
734         preedit_lines_ = 1;
735         for (size_t pos = 0; pos != preedit_length; ++pos) {
736                 char_type const typed_char = preedit_string[pos];
737                 // reset preedit string style
738                 ps = Painter::preedit_default;
739
740                 // if we reached the right extremity of the screen, go to next line.
741                 if (cur_x + fm.width(typed_char) > GuiWorkArea::width() - right_margin) {
742                         cur_x = right_margin;
743                         cur_y += height + 1;
744                         ++preedit_lines_;
745                 }
746                 // preedit strings are displayed with dashed underline
747                 // and partial strings are displayed white on black indicating
748                 // that we are in selecting mode in the input method.
749                 // FIXME: rLength == preedit_length is not a changing condition
750                 // FIXME: should be put out of the loop.
751                 if (pos >= rStart
752                         && pos < rStart + rLength
753                         && !(cursor_pos < rLength && rLength == preedit_length))
754                         ps = Painter::preedit_selecting;
755
756                 if (pos == cursor_pos
757                         && (cursor_pos < rLength && rLength == preedit_length))
758                         ps = Painter::preedit_cursor;
759
760                 // draw one character and update cur_x.
761                 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
762         }
763
764         // update the preedit string screen area.
765         update(0, cur_y - preedit_lines_*height, GuiWorkArea::width(),
766                 (height + 1) * preedit_lines_);
767
768         // Don't forget to accept the event!
769         e->accept();
770 }
771
772
773 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
774 {
775         QRect cur_r(0,0,0,0);
776         switch (query) {
777                 // this is the CJK-specific composition window position.
778                 case Qt::ImMicroFocus:
779                         cur_r = cursor_->rect();
780                         if (preedit_lines_ != 1)
781                                 cur_r.moveLeft(10);
782                         cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
783                         // return lower right of cursor in LyX.
784                         return cur_r;
785                 default:
786                         return QWidget::inputMethodQuery(query);
787         }
788 }
789
790 } // namespace frontend
791 } // namespace lyx
792
793 #include "GuiWorkArea_moc.cpp"