2 * \file GuiWorkArea.cpp
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 "GuiWorkArea.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CoordCache.h"
22 #include "FuncRequest.h"
23 #include "GuiApplication.h"
24 #include "GuiKeySymbol.h"
25 #include "GuiPainter.h"
26 #include "GuiPopupMenu.h"
28 #include "KeySymbol.h"
32 #include "MetricsInfo.h"
33 #include "qt_helpers.h"
36 #include "graphics/GraphicsImage.h"
37 #include "graphics/GraphicsLoader.h"
39 #include "support/debug.h"
40 #include "support/gettext.h"
41 #include "support/FileName.h"
43 #include "frontends/Application.h"
44 #include "frontends/FontMetrics.h"
45 #include "frontends/WorkAreaManager.h"
47 #include <QContextMenuEvent>
48 #include <QInputContext>
50 #include <QMainWindow>
56 #include <QToolButton>
59 #include <boost/bind.hpp>
63 extern "C" int XEventsQueued(Display *display, int mode);
67 int const CursorWidth = 2;
69 int const CursorWidth = 1;
76 using namespace lyx::support;
81 /// return the LyX mouse button state from Qt's
82 static mouse_button::state q_button_state(Qt::MouseButton button)
84 mouse_button::state b = mouse_button::none;
87 b = mouse_button::button1;
90 b = mouse_button::button2;
93 b = mouse_button::button3;
102 /// return the LyX mouse button state from Qt's
103 mouse_button::state q_motion_state(Qt::MouseButtons state)
105 mouse_button::state b = mouse_button::none;
106 if (state & Qt::LeftButton)
107 b |= mouse_button::button1;
108 if (state & Qt::MidButton)
109 b |= mouse_button::button2;
110 if (state & Qt::RightButton)
111 b |= mouse_button::button3;
122 void draw(QPainter & painter)
124 if (show_ && rect_.isValid()) {
127 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
128 painter.setPen(color_);
129 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
130 rect_.bottomRight().x(), rect_.bottomLeft().y());
133 case REVERSED_L_SHAPE:
134 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
135 painter.setPen(color_);
136 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
137 rect_.bottomLeft().x(), rect_.bottomLeft().y());
141 painter.fillRect(rect_, color_);
147 void update(int x, int y, int h, CursorShape shape)
149 color_ = guiApp->colorCache().get(Color_cursor);
153 rect_ = QRect(x, y, CursorWidth + h / 3, h);
155 case REVERSED_L_SHAPE:
156 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
159 rect_ = QRect(x, y, CursorWidth, h);
164 void show(bool set_show = true) { show_ = set_show; }
165 void hide() { show_ = false; }
167 QRect const & rect() { return rect_; }
181 // This is a 'heartbeat' generating synthetic mouse move events when the
182 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
183 SyntheticMouseEvent::SyntheticMouseEvent()
184 : timeout(200), restart_timeout(true),
185 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
190 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
191 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
192 cursor_visible_(false),
193 need_resize_(false), schedule_redraw_(false),
196 buffer.workAreaManager().add(this);
198 connect(&cursor_timeout_, SIGNAL(timeout()),
199 this, SLOT(toggleCursor()));
201 int const time = QApplication::cursorFlashTime() / 2;
203 cursor_timeout_.setInterval(time);
204 cursor_timeout_.start();
206 // let's initialize this just to be safe
207 cursor_timeout_.setInterval(500);
209 screen_ = QPixmap(viewport()->width(), viewport()->height());
210 cursor_ = new frontend::CursorWidget();
213 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 setAcceptDrops(true);
215 setMouseTracking(true);
216 setMinimumSize(100, 70);
219 viewport()->setAutoFillBackground(false);
220 // We don't need double-buffering nor SystemBackground on
221 // the viewport because we have our own backing pixmap.
222 viewport()->setAttribute(Qt::WA_NoSystemBackground);
224 setFocusPolicy(Qt::WheelFocus);
226 viewport()->setCursor(Qt::IBeamCursor);
228 synthetic_mouse_event_.timeout.timeout.connect(
229 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
232 // Initialize the vertical Scroll Bar
233 QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
234 this, SLOT(scrollTo(int)));
236 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
237 << " viewport height: " << viewport()->height());
239 // Enables input methods for asian languages.
240 // Must be set when creating custom text editing widgets.
241 setAttribute(Qt::WA_InputMethodEnabled, true);
246 GuiWorkArea::~GuiWorkArea()
248 buffer_view_->buffer().workAreaManager().remove(this);
254 void GuiWorkArea::close()
256 lyx_view_->removeWorkArea(this);
260 void GuiWorkArea::setFullScreen(bool full_screen)
262 buffer_view_->setFullScreen(full_screen);
264 setFrameStyle(QFrame::NoFrame);
265 if (lyxrc.full_screen_scrollbar)
266 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
268 setFrameStyle(QFrame::Box);
269 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
274 BufferView & GuiWorkArea::bufferView()
276 return *buffer_view_;
280 BufferView const & GuiWorkArea::bufferView() const
282 return *buffer_view_;
286 void GuiWorkArea::stopBlinkingCursor()
288 cursor_timeout_.stop();
293 void GuiWorkArea::startBlinkingCursor()
296 //we're not supposed to cache this value.
297 int const time = QApplication::cursorFlashTime() / 2;
300 cursor_timeout_.setInterval(time);
301 cursor_timeout_.start();
305 void GuiWorkArea::redraw()
308 // No need to redraw in this case.
311 // No need to do anything if this is the current view. The BufferView
312 // metrics are already up to date.
313 if (lyx_view_ != guiApp->currentView()) {
314 // FIXME: it would be nice to optimize for the off-screen case.
315 buffer_view_->updateMetrics();
316 buffer_view_->cursor().fixIfBroken();
319 // update cursor position, because otherwise it has to wait until
320 // the blinking interval is over
321 if (cursor_visible_) {
326 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
328 update(0, 0, viewport()->width(), viewport()->height());
330 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
331 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
333 lyx_view_->updateStatusBar();
335 if (lyxerr.debugging(Debug::WORKAREA))
336 buffer_view_->coordCache().dump();
340 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
342 // In order to avoid bad surprise in the middle of an operation,
343 // we better stop the blinking cursor...
344 // the cursor gets restarted in GuiView::restartCursor()
345 stopBlinkingCursor();
347 theLyXFunc().setLyXView(lyx_view_);
348 theLyXFunc().processKeySym(key, mod);
353 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
356 if (cmd0.action == LFUN_FILE_OPEN) {
357 lyx_view_->dispatch(cmd0);
361 theLyXFunc().setLyXView(lyx_view_);
365 if (cmd0.action == LFUN_MOUSE_PRESS) {
366 if (mod == ShiftModifier)
367 cmd = FuncRequest(cmd0, "region-select");
368 else if (mod == ControlModifier)
369 cmd = FuncRequest(cmd0, "paragraph-select");
376 bool const notJustMovingTheMouse =
377 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
379 // In order to avoid bad surprise in the middle of an operation, we better stop
380 // the blinking cursor.
381 if (notJustMovingTheMouse)
382 stopBlinkingCursor();
384 buffer_view_->mouseEventDispatch(cmd);
386 // Skip these when selecting
387 if (cmd.action != LFUN_MOUSE_MOTION) {
388 lyx_view_->updateLayoutList();
389 lyx_view_->updateToolbars();
392 // GUI tweaks except with mouse motion with no button pressed.
393 if (notJustMovingTheMouse) {
394 // Slight hack: this is only called currently when we
395 // clicked somewhere, so we force through the display
396 // of the new status here.
397 lyx_view_->clearMessage();
399 // Show the cursor immediately after any operation
400 startBlinkingCursor();
405 void GuiWorkArea::resizeBufferView()
407 // WARNING: Please don't put any code that will trigger a repaint here!
408 // We are already inside a paint event.
409 lyx_view_->setBusy(true);
410 buffer_view_->resize(viewport()->width(), viewport()->height());
413 // Update scrollbars which might have changed due different
414 // BufferView dimension. This is especially important when the
415 // BufferView goes from zero-size to the real-size for the first time,
416 // as the scrollbar paramters are then set for the first time.
419 lyx_view_->updateLayoutList();
420 lyx_view_->setBusy(false);
421 need_resize_ = false;
425 void GuiWorkArea::showCursor()
430 CursorShape shape = BAR_SHAPE;
432 Font const & realfont = buffer_view_->cursor().real_current_font;
433 BufferParams const & bp = buffer_view_->buffer().params();
434 bool const samelang = realfont.language() == bp.language;
435 bool const isrtl = realfont.isVisibleRightToLeft();
437 if (!samelang || isrtl != bp.language->rightToLeft()) {
440 shape = REVERSED_L_SHAPE;
443 // The ERT language hack needs fixing up
444 if (realfont.language() == latex_language)
447 Font const font = buffer_view_->cursor().getFont();
448 FontMetrics const & fm = theFontMetrics(font);
449 int const asc = fm.maxAscent();
450 int const des = fm.maxDescent();
454 buffer_view_->cursor().getPos(x, y);
457 // if it doesn't touch the screen, don't try to show it
458 if (y + h < 0 || y >= viewport()->height())
461 cursor_visible_ = true;
462 showCursor(x, y, h, shape);
466 void GuiWorkArea::hideCursor()
468 if (!cursor_visible_)
471 cursor_visible_ = false;
476 void GuiWorkArea::toggleCursor()
485 void GuiWorkArea::updateScrollbar()
487 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
489 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
490 verticalScrollBar()->setPageStep(scroll_.page_step);
491 verticalScrollBar()->setSingleStep(scroll_.single_step);
492 // Block the scrollbar signal to prevent recursive signal/slot calling.
493 verticalScrollBar()->blockSignals(true);
494 verticalScrollBar()->setValue(scroll_.position);
495 verticalScrollBar()->setSliderPosition(scroll_.position);
496 verticalScrollBar()->blockSignals(false);
500 void GuiWorkArea::scrollTo(int value)
502 stopBlinkingCursor();
503 buffer_view_->scrollDocView(value);
505 if (lyxrc.cursor_follows_scrollbar) {
506 buffer_view_->setCursorFromScrollbar();
507 lyx_view_->updateLayoutList();
509 // Show the cursor immediately after any operation.
510 startBlinkingCursor();
511 QApplication::syncX();
515 bool GuiWorkArea::event(QEvent * e)
518 case QEvent::ToolTip: {
519 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
520 if (lyxrc.use_tooltip) {
521 QPoint pos = helpEvent->pos();
522 if (pos.x() < viewport()->width()) {
523 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
524 QToolTip::showText(helpEvent->globalPos(), s);
527 QToolTip::hideText();
529 // Don't forget to accept the event!
534 case QEvent::ShortcutOverride: {
535 // We catch this event in order to catch the Tab or Shift+Tab key press
536 // which are otherwise reserved to focus switching between controls
538 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
539 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
540 || ke->modifiers() & Qt::ControlModifier)
541 return QAbstractScrollArea::event(e);
547 return QAbstractScrollArea::event(e);
553 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
555 QPoint pos = e->pos();
556 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
558 QAbstractScrollArea::contextMenuEvent(e);
561 QMenu * menu = guiApp->menus().menu(toqstr(name));
563 QAbstractScrollArea::contextMenuEvent(e);
566 // Position the menu to the right.
567 // FIXME: menu position should be different for RTL text.
568 menu->exec(e->globalPos());
573 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
575 // Repaint the whole screen.
576 // Note: this is different from redraw() as only the backing pixmap
577 // will be redrawn, which is cheap.
578 viewport()->repaint();
580 startBlinkingCursor();
584 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
586 stopBlinkingCursor();
590 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
592 if (dc_event_.active && dc_event_ == *e) {
593 dc_event_.active = false;
594 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
595 q_button_state(e->button()));
600 inputContext()->reset();
602 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
603 q_button_state(e->button()));
604 dispatch(cmd, q_key_state(e->modifiers()));
608 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
610 if (synthetic_mouse_event_.timeout.running())
611 synthetic_mouse_event_.timeout.stop();
613 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
614 q_button_state(e->button()));
619 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
621 // we kill the triple click if we move
622 doubleClickTimeout();
623 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
624 q_motion_state(e->buttons()));
626 // If we're above or below the work area...
627 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
628 // Make sure only a synthetic event can cause a page scroll,
629 // so they come at a steady rate:
631 // _Force_ a scroll up:
634 cmd.y = viewport()->height();
635 // Store the event, to be handled when the timeout expires.
636 synthetic_mouse_event_.cmd = cmd;
638 if (synthetic_mouse_event_.timeout.running())
639 // Discard the event. Note that it _may_ be handled
640 // when the timeout expires if
641 // synthetic_mouse_event_.cmd has not been overwritten.
642 // Ie, when the timeout expires, we handle the
643 // most recent event but discard all others that
644 // occurred after the one used to start the timeout
645 // in the first place.
648 synthetic_mouse_event_.restart_timeout = true;
649 synthetic_mouse_event_.timeout.start();
650 // Fall through to handle this event...
652 } else if (synthetic_mouse_event_.timeout.running()) {
653 // Store the event, to be possibly handled when the timeout
655 // Once the timeout has expired, normal control is returned
656 // to mouseMoveEvent (restart_timeout = false).
657 // This results in a much smoother 'feel' when moving the
658 // mouse back into the work area.
659 synthetic_mouse_event_.cmd = cmd;
660 synthetic_mouse_event_.restart_timeout = false;
664 // Has anything changed on-screen since the last QMouseEvent
666 double const scrollbar_value = verticalScrollBar()->value();
667 if (e->x() == synthetic_mouse_event_.x_old
668 && e->y() == synthetic_mouse_event_.y_old
669 && scrollbar_value == synthetic_mouse_event_.scrollbar_value_old) {
670 // Nothing changed on-screen since the last QMouseEvent.
674 // Yes something has changed. Store the params used to check this.
675 synthetic_mouse_event_.x_old = e->x();
676 synthetic_mouse_event_.y_old = e->y();
677 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
679 // ... and dispatch the event to the LyX core.
684 void GuiWorkArea::wheelEvent(QWheelEvent * e)
686 // Wheel rotation by one notch results in a delta() of 120 (see
687 // documentation of QWheelEvent)
688 double const lines = qApp->wheelScrollLines()
689 * lyxrc.mouse_wheel_speed
690 * e->delta() / 120.0;
691 LYXERR(Debug::SCROLLING, "wheelScrollLines = " << qApp->wheelScrollLines()
692 << " delta = " << e->delta()
693 << " lines = " << lines);
694 verticalScrollBar()->setValue(verticalScrollBar()->value() -
695 int(lines * verticalScrollBar()->singleStep()));
699 void GuiWorkArea::generateSyntheticMouseEvent()
701 // Set things off to generate the _next_ 'pseudo' event.
702 if (synthetic_mouse_event_.restart_timeout)
703 synthetic_mouse_event_.timeout.start();
705 // Has anything changed on-screen since the last timeout signal
707 double const scrollbar_value = verticalScrollBar()->value();
708 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
709 // Yes it has. Store the params used to check this.
710 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
712 // ... and dispatch the event to the LyX core.
713 dispatch(synthetic_mouse_event_.cmd);
718 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
720 // do nothing if there are other events
721 // (the auto repeated events come too fast)
722 // \todo FIXME: remove hard coded Qt keys, process the key binding
724 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
725 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
726 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
732 LYXERR(Debug::KEY, " count: " << ev->count()
733 << " text: " << fromqstr(ev->text())
734 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
737 setKeySymbol(&sym, ev);
738 processKeySym(sym, q_key_state(ev->modifiers()));
743 void GuiWorkArea::doubleClickTimeout()
745 dc_event_.active = false;
749 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
751 dc_event_ = DoubleClick(ev);
752 QTimer::singleShot(QApplication::doubleClickInterval(), this,
753 SLOT(doubleClickTimeout()));
754 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
756 q_button_state(ev->button()));
761 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
763 QAbstractScrollArea::resizeEvent(ev);
768 void GuiWorkArea::update(int x, int y, int w, int h)
770 viewport()->repaint(x, y, w, h);
774 void GuiWorkArea::paintEvent(QPaintEvent * ev)
776 QRect const rc = ev->rect();
777 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
778 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
781 screen_ = QPixmap(viewport()->width(), viewport()->height());
787 QPainter pain(viewport());
788 pain.drawPixmap(rc, screen_, rc);
793 void GuiWorkArea::updateScreen()
795 GuiPainter pain(&screen_);
796 buffer_view_->draw(pain);
800 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
802 if (schedule_redraw_) {
803 buffer_view_->updateMetrics();
805 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
806 schedule_redraw_ = false;
807 // Show the cursor immediately after the update.
813 cursor_->update(x, y, h, shape);
815 viewport()->update(cursor_->rect());
819 void GuiWorkArea::removeCursor()
822 //if (!qApp->focusWidget())
823 viewport()->update(cursor_->rect());
827 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
829 QString const & commit_string = e->commitString();
830 docstring const & preedit_string
831 = qstring_to_ucs4(e->preeditString());
833 if (!commit_string.isEmpty()) {
835 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
836 << " commitString: " << fromqstr(e->commitString()));
840 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
841 for (int i = 0; i != commit_string.size(); ++i) {
842 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
847 // Hide the cursor during the kana-kanji transformation.
848 if (preedit_string.empty())
849 startBlinkingCursor();
851 stopBlinkingCursor();
853 // last_width : for checking if last preedit string was/wasn't empty.
854 static bool last_width = false;
855 if (!last_width && preedit_string.empty()) {
856 // if last_width is last length of preedit string.
861 GuiPainter pain(&screen_);
862 buffer_view_->updateMetrics();
863 buffer_view_->draw(pain);
864 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
865 FontMetrics const & fm = theFontMetrics(font);
866 int height = fm.maxHeight();
867 int cur_x = cursor_->rect().left();
868 int cur_y = cursor_->rect().bottom();
870 // redraw area of preedit string.
871 update(0, cur_y - height, viewport()->width(),
872 (height + 1) * preedit_lines_);
874 if (preedit_string.empty()) {
882 // att : stores an IM attribute.
883 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
885 // get attributes of input method cursor.
886 // cursor_pos : cursor position in preedit string.
887 size_t cursor_pos = 0;
888 bool cursor_is_visible = false;
889 for (int i = 0; i != att.size(); ++i) {
890 if (att.at(i).type == QInputMethodEvent::Cursor) {
891 cursor_pos = att.at(i).start;
892 cursor_is_visible = att.at(i).length != 0;
897 size_t preedit_length = preedit_string.length();
899 // get position of selection in input method.
900 // FIXME: isn't there a way to do this simplier?
901 // rStart : cursor position in selected string in IM.
903 // rLength : selected string length in IM.
905 if (cursor_pos < preedit_length) {
906 for (int i = 0; i != att.size(); ++i) {
907 if (att.at(i).type == QInputMethodEvent::TextFormat) {
908 if (att.at(i).start <= int(cursor_pos)
909 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
910 rStart = att.at(i).start;
911 rLength = att.at(i).length;
912 if (!cursor_is_visible)
913 cursor_pos += rLength;
924 int const right_margin = buffer_view_->rightMargin();
925 Painter::preedit_style ps;
926 // Most often there would be only one line:
928 for (size_t pos = 0; pos != preedit_length; ++pos) {
929 char_type const typed_char = preedit_string[pos];
930 // reset preedit string style
931 ps = Painter::preedit_default;
933 // if we reached the right extremity of the screen, go to next line.
934 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
935 cur_x = right_margin;
939 // preedit strings are displayed with dashed underline
940 // and partial strings are displayed white on black indicating
941 // that we are in selecting mode in the input method.
942 // FIXME: rLength == preedit_length is not a changing condition
943 // FIXME: should be put out of the loop.
945 && pos < rStart + rLength
946 && !(cursor_pos < rLength && rLength == preedit_length))
947 ps = Painter::preedit_selecting;
949 if (pos == cursor_pos
950 && (cursor_pos < rLength && rLength == preedit_length))
951 ps = Painter::preedit_cursor;
953 // draw one character and update cur_x.
954 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
957 // update the preedit string screen area.
958 update(0, cur_y - preedit_lines_*height, viewport()->width(),
959 (height + 1) * preedit_lines_);
961 // Don't forget to accept the event!
966 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
968 QRect cur_r(0,0,0,0);
970 // this is the CJK-specific composition window position.
971 case Qt::ImMicroFocus:
972 cur_r = cursor_->rect();
973 if (preedit_lines_ != 1)
975 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
976 // return lower right of cursor in LyX.
979 return QWidget::inputMethodQuery(query);
984 void GuiWorkArea::updateWindowTitle()
986 docstring maximize_title;
987 docstring minimize_title;
989 Buffer & buf = buffer_view_->buffer();
990 FileName const fileName = buf.fileName();
991 if (!fileName.empty()) {
992 maximize_title = fileName.displayName(30);
993 minimize_title = from_utf8(fileName.onlyFileName());
994 if (!buf.isClean()) {
995 maximize_title += _(" (changed)");
996 minimize_title += char_type('*');
998 if (buf.isReadonly())
999 maximize_title += _(" (read only)");
1002 QString title = windowTitle();
1003 QString new_title = toqstr(maximize_title);
1004 if (title == new_title)
1007 QWidget::setWindowTitle(new_title);
1008 QWidget::setWindowIconText(toqstr(minimize_title));
1013 void GuiWorkArea::setReadOnly(bool)
1015 updateWindowTitle();
1016 if (this == lyx_view_->currentWorkArea())
1017 lyx_view_->updateBufferDependent(false);
1021 ////////////////////////////////////////////////////////////////////
1025 ////////////////////////////////////////////////////////////////////
1027 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1029 QPalette pal = palette();
1030 pal.setColor(QPalette::Active, QPalette::Button,
1031 pal.color(QPalette::Active, QPalette::Window));
1032 pal.setColor(QPalette::Disabled, QPalette::Button,
1033 pal.color(QPalette::Disabled, QPalette::Window));
1034 pal.setColor(QPalette::Inactive, QPalette::Button,
1035 pal.color(QPalette::Inactive, QPalette::Window));
1037 QToolButton * closeTabButton = new QToolButton(this);
1038 closeTabButton->setPalette(pal);
1039 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1040 closeTabButton->setText("Close");
1041 closeTabButton->setAutoRaise(true);
1042 closeTabButton->setCursor(Qt::ArrowCursor);
1043 closeTabButton->setToolTip(tr("Close tab"));
1044 closeTabButton->setEnabled(true);
1046 QObject::connect(this, SIGNAL(currentChanged(int)),
1047 this, SLOT(on_currentTabChanged(int)));
1048 QObject::connect(closeTabButton, SIGNAL(clicked()),
1049 this, SLOT(closeCurrentTab()));
1051 setCornerWidget(closeTabButton);
1052 setUsesScrollButtons(true);
1056 void TabWorkArea::setFullScreen(bool full_screen)
1058 for (int i = 0; i != count(); ++i) {
1059 if (GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i)))
1060 wa->setFullScreen(full_screen);
1063 if (lyxrc.full_screen_tabbar)
1064 showBar(!full_screen && count()>1);
1068 void TabWorkArea::showBar(bool show)
1070 tabBar()->setEnabled(show);
1071 tabBar()->setVisible(show);
1075 GuiWorkArea * TabWorkArea::currentWorkArea()
1080 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1086 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1088 for (int i = 0; i != count(); ++i) {
1089 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1091 if (&wa->bufferView().buffer() == &buffer)
1098 void TabWorkArea::closeAll()
1101 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1109 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1111 BOOST_ASSERT(work_area);
1112 int index = indexOf(work_area);
1116 if (index == currentIndex())
1117 // Make sure the work area is up to date.
1118 on_currentTabChanged(index);
1120 // Switch to the work area.
1121 setCurrentIndex(index);
1122 work_area->setFocus();
1128 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1130 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1131 wa->setUpdatesEnabled(false);
1132 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1133 // when hiding it again below).
1134 showBar(count() > 0);
1135 addTab(wa, wa->windowTitle());
1136 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1137 this, SLOT(updateTabText(GuiWorkArea *)));
1138 // Hide tabbar if there's only one tab.
1139 showBar(count() > 1);
1144 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1146 BOOST_ASSERT(work_area);
1147 int index = indexOf(work_area);
1151 work_area->setUpdatesEnabled(false);
1156 // make sure the next work area is enabled.
1157 currentWidget()->setUpdatesEnabled(true);
1158 // Hide tabbar if there's only one tab.
1159 showBar(count() > 1);
1165 void TabWorkArea::on_currentTabChanged(int i)
1167 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1169 BufferView & bv = wa->bufferView();
1170 bv.cursor().fixIfBroken();
1172 wa->setUpdatesEnabled(true);
1176 currentWorkAreaChanged(wa);
1178 LYXERR(Debug::GUI, "currentTabChanged " << i
1179 << "File" << bv.buffer().absFileName());
1183 void TabWorkArea::closeCurrentTab()
1185 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1189 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1191 int const i = indexOf(wa);
1194 setTabText(i, wa->windowTitle());
1197 } // namespace frontend
1200 #include "GuiWorkArea_moc.cpp"