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 || lyx_view_->currentWorkArea() != this) {
315 // FIXME: it would be nice to optimize for the off-screen case.
316 buffer_view_->updateMetrics();
317 buffer_view_->cursor().fixIfBroken();
320 // update cursor position, because otherwise it has to wait until
321 // the blinking interval is over
322 if (cursor_visible_) {
327 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
329 update(0, 0, viewport()->width(), viewport()->height());
331 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
332 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
334 lyx_view_->updateStatusBar();
336 if (lyxerr.debugging(Debug::WORKAREA))
337 buffer_view_->coordCache().dump();
341 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
343 // In order to avoid bad surprise in the middle of an operation,
344 // we better stop the blinking cursor...
345 // the cursor gets restarted in GuiView::restartCursor()
346 stopBlinkingCursor();
348 theLyXFunc().setLyXView(lyx_view_);
349 theLyXFunc().processKeySym(key, mod);
354 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
357 if (cmd0.action == LFUN_FILE_OPEN) {
358 lyx_view_->dispatch(cmd0);
362 theLyXFunc().setLyXView(lyx_view_);
366 if (cmd0.action == LFUN_MOUSE_PRESS) {
367 if (mod == ShiftModifier)
368 cmd = FuncRequest(cmd0, "region-select");
369 else if (mod == ControlModifier)
370 cmd = FuncRequest(cmd0, "paragraph-select");
377 bool const notJustMovingTheMouse =
378 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
380 // In order to avoid bad surprise in the middle of an operation, we better stop
381 // the blinking cursor.
382 if (notJustMovingTheMouse)
383 stopBlinkingCursor();
385 buffer_view_->mouseEventDispatch(cmd);
387 // Skip these when selecting
388 if (cmd.action != LFUN_MOUSE_MOTION) {
389 lyx_view_->updateLayoutList();
390 lyx_view_->updateToolbars();
393 // GUI tweaks except with mouse motion with no button pressed.
394 if (notJustMovingTheMouse) {
395 // Slight hack: this is only called currently when we
396 // clicked somewhere, so we force through the display
397 // of the new status here.
398 lyx_view_->clearMessage();
400 // Show the cursor immediately after any operation
401 startBlinkingCursor();
406 void GuiWorkArea::resizeBufferView()
408 // WARNING: Please don't put any code that will trigger a repaint here!
409 // We are already inside a paint event.
410 lyx_view_->setBusy(true);
411 buffer_view_->resize(viewport()->width(), viewport()->height());
414 // Update scrollbars which might have changed due different
415 // BufferView dimension. This is especially important when the
416 // BufferView goes from zero-size to the real-size for the first time,
417 // as the scrollbar paramters are then set for the first time.
420 lyx_view_->updateLayoutList();
421 lyx_view_->setBusy(false);
422 need_resize_ = false;
426 void GuiWorkArea::showCursor()
431 CursorShape shape = BAR_SHAPE;
433 Font const & realfont = buffer_view_->cursor().real_current_font;
434 BufferParams const & bp = buffer_view_->buffer().params();
435 bool const samelang = realfont.language() == bp.language;
436 bool const isrtl = realfont.isVisibleRightToLeft();
438 if (!samelang || isrtl != bp.language->rightToLeft()) {
441 shape = REVERSED_L_SHAPE;
444 // The ERT language hack needs fixing up
445 if (realfont.language() == latex_language)
448 Font const font = buffer_view_->cursor().getFont();
449 FontMetrics const & fm = theFontMetrics(font);
450 int const asc = fm.maxAscent();
451 int const des = fm.maxDescent();
455 buffer_view_->cursor().getPos(x, y);
458 // if it doesn't touch the screen, don't try to show it
459 if (y + h < 0 || y >= viewport()->height())
462 cursor_visible_ = true;
463 showCursor(x, y, h, shape);
467 void GuiWorkArea::hideCursor()
469 if (!cursor_visible_)
472 cursor_visible_ = false;
477 void GuiWorkArea::toggleCursor()
486 void GuiWorkArea::updateScrollbar()
488 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
490 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
491 verticalScrollBar()->setPageStep(scroll_.page_step);
492 verticalScrollBar()->setSingleStep(scroll_.single_step);
493 // Block the scrollbar signal to prevent recursive signal/slot calling.
494 verticalScrollBar()->blockSignals(true);
495 verticalScrollBar()->setValue(scroll_.position);
496 verticalScrollBar()->setSliderPosition(scroll_.position);
497 verticalScrollBar()->blockSignals(false);
501 void GuiWorkArea::scrollTo(int value)
503 stopBlinkingCursor();
504 buffer_view_->scrollDocView(value);
506 if (lyxrc.cursor_follows_scrollbar) {
507 buffer_view_->setCursorFromScrollbar();
508 lyx_view_->updateLayoutList();
510 // Show the cursor immediately after any operation.
511 startBlinkingCursor();
512 QApplication::syncX();
516 bool GuiWorkArea::event(QEvent * e)
519 case QEvent::ToolTip: {
520 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
521 if (lyxrc.use_tooltip) {
522 QPoint pos = helpEvent->pos();
523 if (pos.x() < viewport()->width()) {
524 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
525 QToolTip::showText(helpEvent->globalPos(), s);
528 QToolTip::hideText();
530 // Don't forget to accept the event!
535 case QEvent::ShortcutOverride: {
536 // We catch this event in order to catch the Tab or Shift+Tab key press
537 // which are otherwise reserved to focus switching between controls
539 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
540 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
541 || ke->modifiers() & Qt::ControlModifier)
542 return QAbstractScrollArea::event(e);
548 return QAbstractScrollArea::event(e);
554 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
556 QPoint pos = e->pos();
557 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
559 QAbstractScrollArea::contextMenuEvent(e);
562 QMenu * menu = guiApp->menus().menu(toqstr(name));
564 QAbstractScrollArea::contextMenuEvent(e);
567 // Position the menu to the right.
568 // FIXME: menu position should be different for RTL text.
569 menu->exec(e->globalPos());
574 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
576 lyx_view_->setCurrentWorkArea(this);
577 // Repaint the whole screen.
578 // Note: this is different from redraw() as only the backing pixmap
579 // will be redrawn, which is cheap.
580 viewport()->repaint();
582 startBlinkingCursor();
586 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
588 stopBlinkingCursor();
592 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
594 if (dc_event_.active && dc_event_ == *e) {
595 dc_event_.active = false;
596 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
597 q_button_state(e->button()));
602 inputContext()->reset();
604 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
605 q_button_state(e->button()));
606 dispatch(cmd, q_key_state(e->modifiers()));
610 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
612 if (synthetic_mouse_event_.timeout.running())
613 synthetic_mouse_event_.timeout.stop();
615 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
616 q_button_state(e->button()));
621 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
623 // we kill the triple click if we move
624 doubleClickTimeout();
625 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
626 q_motion_state(e->buttons()));
628 // If we're above or below the work area...
629 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
630 // Make sure only a synthetic event can cause a page scroll,
631 // so they come at a steady rate:
633 // _Force_ a scroll up:
636 cmd.y = viewport()->height();
637 // Store the event, to be handled when the timeout expires.
638 synthetic_mouse_event_.cmd = cmd;
640 if (synthetic_mouse_event_.timeout.running())
641 // Discard the event. Note that it _may_ be handled
642 // when the timeout expires if
643 // synthetic_mouse_event_.cmd has not been overwritten.
644 // Ie, when the timeout expires, we handle the
645 // most recent event but discard all others that
646 // occurred after the one used to start the timeout
647 // in the first place.
650 synthetic_mouse_event_.restart_timeout = true;
651 synthetic_mouse_event_.timeout.start();
652 // Fall through to handle this event...
654 } else if (synthetic_mouse_event_.timeout.running()) {
655 // Store the event, to be possibly handled when the timeout
657 // Once the timeout has expired, normal control is returned
658 // to mouseMoveEvent (restart_timeout = false).
659 // This results in a much smoother 'feel' when moving the
660 // mouse back into the work area.
661 synthetic_mouse_event_.cmd = cmd;
662 synthetic_mouse_event_.restart_timeout = false;
666 // Has anything changed on-screen since the last QMouseEvent
668 double const scrollbar_value = verticalScrollBar()->value();
669 if (e->x() == synthetic_mouse_event_.x_old
670 && e->y() == synthetic_mouse_event_.y_old
671 && scrollbar_value == synthetic_mouse_event_.scrollbar_value_old) {
672 // Nothing changed on-screen since the last QMouseEvent.
676 // Yes something has changed. Store the params used to check this.
677 synthetic_mouse_event_.x_old = e->x();
678 synthetic_mouse_event_.y_old = e->y();
679 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
681 // ... and dispatch the event to the LyX core.
686 void GuiWorkArea::wheelEvent(QWheelEvent * e)
688 // Wheel rotation by one notch results in a delta() of 120 (see
689 // documentation of QWheelEvent)
690 double const lines = qApp->wheelScrollLines()
691 * lyxrc.mouse_wheel_speed
692 * e->delta() / 120.0;
693 LYXERR(Debug::SCROLLING, "wheelScrollLines = " << qApp->wheelScrollLines()
694 << " delta = " << e->delta()
695 << " lines = " << lines);
696 verticalScrollBar()->setValue(verticalScrollBar()->value() -
697 int(lines * verticalScrollBar()->singleStep()));
701 void GuiWorkArea::generateSyntheticMouseEvent()
703 // Set things off to generate the _next_ 'pseudo' event.
704 if (synthetic_mouse_event_.restart_timeout)
705 synthetic_mouse_event_.timeout.start();
707 // Has anything changed on-screen since the last timeout signal
709 double const scrollbar_value = verticalScrollBar()->value();
710 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
711 // Yes it has. Store the params used to check this.
712 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
714 // ... and dispatch the event to the LyX core.
715 dispatch(synthetic_mouse_event_.cmd);
720 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
722 // do nothing if there are other events
723 // (the auto repeated events come too fast)
724 // \todo FIXME: remove hard coded Qt keys, process the key binding
726 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
727 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
728 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
734 LYXERR(Debug::KEY, " count: " << ev->count()
735 << " text: " << fromqstr(ev->text())
736 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
739 setKeySymbol(&sym, ev);
740 processKeySym(sym, q_key_state(ev->modifiers()));
745 void GuiWorkArea::doubleClickTimeout()
747 dc_event_.active = false;
751 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
753 dc_event_ = DoubleClick(ev);
754 QTimer::singleShot(QApplication::doubleClickInterval(), this,
755 SLOT(doubleClickTimeout()));
756 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
758 q_button_state(ev->button()));
763 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
765 QAbstractScrollArea::resizeEvent(ev);
770 void GuiWorkArea::update(int x, int y, int w, int h)
772 viewport()->repaint(x, y, w, h);
776 void GuiWorkArea::paintEvent(QPaintEvent * ev)
778 QRect const rc = ev->rect();
779 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
780 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
783 screen_ = QPixmap(viewport()->width(), viewport()->height());
789 QPainter pain(viewport());
790 pain.drawPixmap(rc, screen_, rc);
795 void GuiWorkArea::updateScreen()
797 GuiPainter pain(&screen_);
798 buffer_view_->draw(pain);
802 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
804 if (schedule_redraw_) {
805 buffer_view_->updateMetrics();
807 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
808 schedule_redraw_ = false;
809 // Show the cursor immediately after the update.
815 cursor_->update(x, y, h, shape);
817 viewport()->update(cursor_->rect());
821 void GuiWorkArea::removeCursor()
824 //if (!qApp->focusWidget())
825 viewport()->update(cursor_->rect());
829 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
831 QString const & commit_string = e->commitString();
832 docstring const & preedit_string
833 = qstring_to_ucs4(e->preeditString());
835 if (!commit_string.isEmpty()) {
837 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
838 << " commitString: " << fromqstr(e->commitString()));
842 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
843 for (int i = 0; i != commit_string.size(); ++i) {
844 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
849 // Hide the cursor during the kana-kanji transformation.
850 if (preedit_string.empty())
851 startBlinkingCursor();
853 stopBlinkingCursor();
855 // last_width : for checking if last preedit string was/wasn't empty.
856 static bool last_width = false;
857 if (!last_width && preedit_string.empty()) {
858 // if last_width is last length of preedit string.
863 GuiPainter pain(&screen_);
864 buffer_view_->updateMetrics();
865 buffer_view_->draw(pain);
866 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
867 FontMetrics const & fm = theFontMetrics(font);
868 int height = fm.maxHeight();
869 int cur_x = cursor_->rect().left();
870 int cur_y = cursor_->rect().bottom();
872 // redraw area of preedit string.
873 update(0, cur_y - height, viewport()->width(),
874 (height + 1) * preedit_lines_);
876 if (preedit_string.empty()) {
884 // att : stores an IM attribute.
885 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
887 // get attributes of input method cursor.
888 // cursor_pos : cursor position in preedit string.
889 size_t cursor_pos = 0;
890 bool cursor_is_visible = false;
891 for (int i = 0; i != att.size(); ++i) {
892 if (att.at(i).type == QInputMethodEvent::Cursor) {
893 cursor_pos = att.at(i).start;
894 cursor_is_visible = att.at(i).length != 0;
899 size_t preedit_length = preedit_string.length();
901 // get position of selection in input method.
902 // FIXME: isn't there a way to do this simplier?
903 // rStart : cursor position in selected string in IM.
905 // rLength : selected string length in IM.
907 if (cursor_pos < preedit_length) {
908 for (int i = 0; i != att.size(); ++i) {
909 if (att.at(i).type == QInputMethodEvent::TextFormat) {
910 if (att.at(i).start <= int(cursor_pos)
911 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
912 rStart = att.at(i).start;
913 rLength = att.at(i).length;
914 if (!cursor_is_visible)
915 cursor_pos += rLength;
926 int const right_margin = buffer_view_->rightMargin();
927 Painter::preedit_style ps;
928 // Most often there would be only one line:
930 for (size_t pos = 0; pos != preedit_length; ++pos) {
931 char_type const typed_char = preedit_string[pos];
932 // reset preedit string style
933 ps = Painter::preedit_default;
935 // if we reached the right extremity of the screen, go to next line.
936 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
937 cur_x = right_margin;
941 // preedit strings are displayed with dashed underline
942 // and partial strings are displayed white on black indicating
943 // that we are in selecting mode in the input method.
944 // FIXME: rLength == preedit_length is not a changing condition
945 // FIXME: should be put out of the loop.
947 && pos < rStart + rLength
948 && !(cursor_pos < rLength && rLength == preedit_length))
949 ps = Painter::preedit_selecting;
951 if (pos == cursor_pos
952 && (cursor_pos < rLength && rLength == preedit_length))
953 ps = Painter::preedit_cursor;
955 // draw one character and update cur_x.
956 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
959 // update the preedit string screen area.
960 update(0, cur_y - preedit_lines_*height, viewport()->width(),
961 (height + 1) * preedit_lines_);
963 // Don't forget to accept the event!
968 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
970 QRect cur_r(0,0,0,0);
972 // this is the CJK-specific composition window position.
973 case Qt::ImMicroFocus:
974 cur_r = cursor_->rect();
975 if (preedit_lines_ != 1)
977 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
978 // return lower right of cursor in LyX.
981 return QWidget::inputMethodQuery(query);
986 void GuiWorkArea::updateWindowTitle()
988 docstring maximize_title;
989 docstring minimize_title;
991 Buffer & buf = buffer_view_->buffer();
992 FileName const fileName = buf.fileName();
993 if (!fileName.empty()) {
994 maximize_title = fileName.displayName(30);
995 minimize_title = from_utf8(fileName.onlyFileName());
996 if (!buf.isClean()) {
997 maximize_title += _(" (changed)");
998 minimize_title += char_type('*');
1000 if (buf.isReadonly())
1001 maximize_title += _(" (read only)");
1004 QString title = windowTitle();
1005 QString new_title = toqstr(maximize_title);
1006 if (title == new_title)
1009 QWidget::setWindowTitle(new_title);
1010 QWidget::setWindowIconText(toqstr(minimize_title));
1015 void GuiWorkArea::setReadOnly(bool)
1017 updateWindowTitle();
1018 if (this == lyx_view_->currentWorkArea())
1019 lyx_view_->updateBufferDependent(false);
1023 ////////////////////////////////////////////////////////////////////
1027 ////////////////////////////////////////////////////////////////////
1029 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1031 QPalette pal = palette();
1032 pal.setColor(QPalette::Active, QPalette::Button,
1033 pal.color(QPalette::Active, QPalette::Window));
1034 pal.setColor(QPalette::Disabled, QPalette::Button,
1035 pal.color(QPalette::Disabled, QPalette::Window));
1036 pal.setColor(QPalette::Inactive, QPalette::Button,
1037 pal.color(QPalette::Inactive, QPalette::Window));
1039 QToolButton * closeTabButton = new QToolButton(this);
1040 closeTabButton->setPalette(pal);
1041 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1042 closeTabButton->setText("Close");
1043 closeTabButton->setAutoRaise(true);
1044 closeTabButton->setCursor(Qt::ArrowCursor);
1045 closeTabButton->setToolTip(tr("Close tab"));
1046 closeTabButton->setEnabled(true);
1048 QObject::connect(this, SIGNAL(currentChanged(int)),
1049 this, SLOT(on_currentTabChanged(int)));
1050 QObject::connect(closeTabButton, SIGNAL(clicked()),
1051 this, SLOT(closeCurrentTab()));
1053 setCornerWidget(closeTabButton);
1054 setUsesScrollButtons(true);
1058 void TabWorkArea::setFullScreen(bool full_screen)
1060 for (int i = 0; i != count(); ++i) {
1061 if (GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i)))
1062 wa->setFullScreen(full_screen);
1065 if (lyxrc.full_screen_tabbar)
1066 showBar(!full_screen && count()>1);
1070 void TabWorkArea::showBar(bool show)
1072 tabBar()->setEnabled(show);
1073 tabBar()->setVisible(show);
1077 GuiWorkArea * TabWorkArea::currentWorkArea()
1082 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1088 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1090 for (int i = 0; i != count(); ++i) {
1091 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1093 if (&wa->bufferView().buffer() == &buffer)
1100 void TabWorkArea::closeAll()
1103 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1111 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1113 BOOST_ASSERT(work_area);
1114 int index = indexOf(work_area);
1118 if (index == currentIndex())
1119 // Make sure the work area is up to date.
1120 on_currentTabChanged(index);
1122 // Switch to the work area.
1123 setCurrentIndex(index);
1124 work_area->setFocus();
1130 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1132 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1133 wa->setUpdatesEnabled(false);
1134 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1135 // when hiding it again below).
1136 showBar(count() > 0);
1137 addTab(wa, wa->windowTitle());
1138 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1139 this, SLOT(updateTabText(GuiWorkArea *)));
1140 // Hide tabbar if there's only one tab.
1141 showBar(count() > 1);
1146 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1148 BOOST_ASSERT(work_area);
1149 int index = indexOf(work_area);
1153 work_area->setUpdatesEnabled(false);
1158 // make sure the next work area is enabled.
1159 currentWidget()->setUpdatesEnabled(true);
1160 // Hide tabbar if there's only one tab.
1161 showBar(count() > 1);
1167 void TabWorkArea::on_currentTabChanged(int i)
1169 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1171 BufferView & bv = wa->bufferView();
1172 bv.cursor().fixIfBroken();
1174 wa->setUpdatesEnabled(true);
1178 currentWorkAreaChanged(wa);
1180 LYXERR(Debug::GUI, "currentTabChanged " << i
1181 << "File" << bv.buffer().absFileName());
1185 void TabWorkArea::closeCurrentTab()
1187 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1191 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1193 int const i = indexOf(wa);
1196 setTabText(i, wa->windowTitle());
1199 } // namespace frontend
1202 #include "GuiWorkArea_moc.cpp"