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(),
128 CursorWidth, rect_.height(), color_);
129 painter.setPen(color_);
130 painter.drawLine(rect_.bottomLeft().x() + CursorWidth,
131 rect_.bottomLeft().y(),
132 rect_.bottomRight().x(), rect_.bottomLeft().y());
135 case REVERSED_L_SHAPE:
136 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(),
137 CursorWidth, rect_.height(), color_);
138 painter.setPen(color_);
139 painter.drawLine(rect_.bottomRight().x() - CursorWidth,
140 rect_.bottomLeft().y(),
141 rect_.bottomLeft().x(), rect_.bottomLeft().y());
145 painter.fillRect(rect_, color_);
151 void update(int x, int y, int h, CursorShape shape)
153 color_ = guiApp->colorCache().get(Color_cursor);
157 rect_ = QRect(x, y, CursorWidth + h / 3, h);
159 case REVERSED_L_SHAPE:
160 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
163 rect_ = QRect(x, y, CursorWidth, h);
168 void show(bool set_show = true) { show_ = set_show; }
169 void hide() { show_ = false; }
171 QRect const & rect() { return rect_; }
185 // This is a 'heartbeat' generating synthetic mouse move events when the
186 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
187 SyntheticMouseEvent::SyntheticMouseEvent()
188 : timeout(200), restart_timeout(true),
189 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
194 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
195 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
196 cursor_visible_(false),
197 need_resize_(false), schedule_redraw_(false),
198 preedit_lines_(1), completer_(this)
200 buffer.workAreaManager().add(this);
202 connect(&cursor_timeout_, SIGNAL(timeout()),
203 this, SLOT(toggleCursor()));
205 int const time = QApplication::cursorFlashTime() / 2;
207 cursor_timeout_.setInterval(time);
208 cursor_timeout_.start();
210 // let's initialize this just to be safe
211 cursor_timeout_.setInterval(500);
213 screen_ = QPixmap(viewport()->width(), viewport()->height());
214 cursor_ = new frontend::CursorWidget();
217 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
218 setAcceptDrops(true);
219 setMouseTracking(true);
220 setMinimumSize(100, 70);
223 viewport()->setAutoFillBackground(false);
224 // We don't need double-buffering nor SystemBackground on
225 // the viewport because we have our own backing pixmap.
226 viewport()->setAttribute(Qt::WA_NoSystemBackground);
228 setFocusPolicy(Qt::WheelFocus);
230 viewport()->setCursor(Qt::IBeamCursor);
232 synthetic_mouse_event_.timeout.timeout.connect(
233 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
236 // Initialize the vertical Scroll Bar
237 QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
238 this, SLOT(scrollTo(int)));
240 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
241 << " viewport height: " << viewport()->height());
243 // Enables input methods for asian languages.
244 // Must be set when creating custom text editing widgets.
245 setAttribute(Qt::WA_InputMethodEnabled, true);
249 GuiWorkArea::~GuiWorkArea()
251 buffer_view_->buffer().workAreaManager().remove(this);
257 void GuiWorkArea::close()
259 lyx_view_->removeWorkArea(this);
263 void GuiWorkArea::setFullScreen(bool full_screen)
265 buffer_view_->setFullScreen(full_screen);
267 setFrameStyle(QFrame::NoFrame);
268 if (lyxrc.full_screen_scrollbar)
269 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
271 setFrameStyle(QFrame::Box);
272 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
277 BufferView & GuiWorkArea::bufferView()
279 return *buffer_view_;
283 BufferView const & GuiWorkArea::bufferView() const
285 return *buffer_view_;
289 void GuiWorkArea::stopBlinkingCursor()
291 cursor_timeout_.stop();
296 void GuiWorkArea::startBlinkingCursor()
299 //we're not supposed to cache this value.
300 int const time = QApplication::cursorFlashTime() / 2;
303 cursor_timeout_.setInterval(time);
304 cursor_timeout_.start();
308 void GuiWorkArea::redraw()
311 // No need to redraw in this case.
314 // No need to do anything if this is the current view. The BufferView
315 // metrics are already up to date.
316 if (lyx_view_ != guiApp->currentView()
317 || lyx_view_->currentWorkArea() != this) {
318 // FIXME: it would be nice to optimize for the off-screen case.
319 buffer_view_->updateMetrics();
320 buffer_view_->cursor().fixIfBroken();
323 // update cursor position, because otherwise it has to wait until
324 // the blinking interval is over
325 if (cursor_visible_) {
330 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
332 update(0, 0, viewport()->width(), viewport()->height());
334 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
335 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
337 lyx_view_->updateStatusBar();
339 if (lyxerr.debugging(Debug::WORKAREA))
340 buffer_view_->coordCache().dump();
344 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
346 // In order to avoid bad surprise in the middle of an operation,
347 // we better stop the blinking cursor...
348 // the cursor gets restarted in GuiView::restartCursor()
349 stopBlinkingCursor();
351 theLyXFunc().setLyXView(lyx_view_);
352 theLyXFunc().processKeySym(key, mod);
357 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
360 if (cmd0.action == LFUN_FILE_OPEN) {
361 lyx_view_->dispatch(cmd0);
365 theLyXFunc().setLyXView(lyx_view_);
369 if (cmd0.action == LFUN_MOUSE_PRESS) {
370 if (mod == ShiftModifier)
371 cmd = FuncRequest(cmd0, "region-select");
372 else if (mod == ControlModifier)
373 cmd = FuncRequest(cmd0, "paragraph-select");
380 bool const notJustMovingTheMouse =
381 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
383 // In order to avoid bad surprise in the middle of an operation, we better stop
384 // the blinking cursor.
385 if (notJustMovingTheMouse)
386 stopBlinkingCursor();
388 buffer_view_->mouseEventDispatch(cmd);
390 // Skip these when selecting
391 if (cmd.action != LFUN_MOUSE_MOTION) {
392 completer_.updateVisibility(false, false);
393 lyx_view_->updateLayoutList();
394 lyx_view_->updateToolbars();
397 // GUI tweaks except with mouse motion with no button pressed.
398 if (notJustMovingTheMouse) {
399 // Slight hack: this is only called currently when we
400 // clicked somewhere, so we force through the display
401 // of the new status here.
402 lyx_view_->clearMessage();
404 // Show the cursor immediately after any operation
405 startBlinkingCursor();
410 void GuiWorkArea::resizeBufferView()
412 // WARNING: Please don't put any code that will trigger a repaint here!
413 // We are already inside a paint event.
414 lyx_view_->setBusy(true);
415 buffer_view_->resize(viewport()->width(), viewport()->height());
418 // Update scrollbars which might have changed due different
419 // BufferView dimension. This is especially important when the
420 // BufferView goes from zero-size to the real-size for the first time,
421 // as the scrollbar paramters are then set for the first time.
424 lyx_view_->updateLayoutList();
425 lyx_view_->setBusy(false);
426 need_resize_ = false;
430 void GuiWorkArea::showCursor()
435 CursorShape shape = BAR_SHAPE;
437 Font const & realfont = buffer_view_->cursor().real_current_font;
438 BufferParams const & bp = buffer_view_->buffer().params();
439 bool const samelang = realfont.language() == bp.language;
440 bool const isrtl = realfont.isVisibleRightToLeft();
442 if (!samelang || isrtl != bp.language->rightToLeft()) {
445 shape = REVERSED_L_SHAPE;
448 // The ERT language hack needs fixing up
449 if (realfont.language() == latex_language)
452 Font const font = buffer_view_->cursor().getFont();
453 FontMetrics const & fm = theFontMetrics(font);
454 int const asc = fm.maxAscent();
455 int const des = fm.maxDescent();
459 Cursor & cur = buffer_view_->cursor();
463 // if it doesn't touch the screen, don't try to show it
464 bool cursorInView = true;
465 if (y + h < 0 || y >= viewport()->height())
466 cursorInView = false;
468 // show cursor on screen
470 cursor_visible_ = true;
471 showCursor(x, y, h, shape);
476 void GuiWorkArea::hideCursor()
478 if (!cursor_visible_)
481 cursor_visible_ = false;
486 void GuiWorkArea::toggleCursor()
495 void GuiWorkArea::updateScrollbar()
497 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
499 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
500 verticalScrollBar()->setPageStep(scroll_.page_step);
501 verticalScrollBar()->setSingleStep(scroll_.single_step);
502 // Block the scrollbar signal to prevent recursive signal/slot calling.
503 verticalScrollBar()->blockSignals(true);
504 verticalScrollBar()->setValue(scroll_.position);
505 verticalScrollBar()->setSliderPosition(scroll_.position);
506 verticalScrollBar()->blockSignals(false);
510 void GuiWorkArea::scrollTo(int value)
512 stopBlinkingCursor();
513 buffer_view_->scrollDocView(value);
515 if (lyxrc.cursor_follows_scrollbar) {
516 buffer_view_->setCursorFromScrollbar();
517 lyx_view_->updateLayoutList();
519 // Show the cursor immediately after any operation.
520 startBlinkingCursor();
521 QApplication::syncX();
525 bool GuiWorkArea::event(QEvent * e)
528 case QEvent::ToolTip: {
529 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
530 if (lyxrc.use_tooltip) {
531 QPoint pos = helpEvent->pos();
532 if (pos.x() < viewport()->width()) {
533 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
534 QToolTip::showText(helpEvent->globalPos(), s);
537 QToolTip::hideText();
539 // Don't forget to accept the event!
544 case QEvent::ShortcutOverride: {
545 // We catch this event in order to catch the Tab or Shift+Tab key press
546 // which are otherwise reserved to focus switching between controls
548 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
549 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
550 || ke->modifiers() & Qt::ControlModifier)
551 return QAbstractScrollArea::event(e);
557 return QAbstractScrollArea::event(e);
563 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
565 QPoint pos = e->pos();
566 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
568 QAbstractScrollArea::contextMenuEvent(e);
571 QMenu * menu = guiApp->menus().menu(toqstr(name));
573 QAbstractScrollArea::contextMenuEvent(e);
576 // Position the menu to the right.
577 // FIXME: menu position should be different for RTL text.
578 menu->exec(e->globalPos());
583 void GuiWorkArea::focusInEvent(QFocusEvent * e)
585 lyx_view_->setCurrentWorkArea(this);
586 // Repaint the whole screen.
587 // Note: this is different from redraw() as only the backing pixmap
588 // will be redrawn, which is cheap.
589 viewport()->repaint();
591 startBlinkingCursor();
592 QAbstractScrollArea::focusInEvent(e);
596 void GuiWorkArea::focusOutEvent(QFocusEvent * e)
598 stopBlinkingCursor();
599 QAbstractScrollArea::focusOutEvent(e);
603 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
605 if (dc_event_.active && dc_event_ == *e) {
606 dc_event_.active = false;
607 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
608 q_button_state(e->button()));
614 inputContext()->reset();
616 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
617 q_button_state(e->button()));
618 dispatch(cmd, q_key_state(e->modifiers()));
623 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
625 if (synthetic_mouse_event_.timeout.running())
626 synthetic_mouse_event_.timeout.stop();
628 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
629 q_button_state(e->button()));
635 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
637 // we kill the triple click if we move
638 doubleClickTimeout();
639 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
640 q_motion_state(e->buttons()));
644 // If we're above or below the work area...
645 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
646 // Make sure only a synthetic event can cause a page scroll,
647 // so they come at a steady rate:
649 // _Force_ a scroll up:
652 cmd.y = viewport()->height();
653 // Store the event, to be handled when the timeout expires.
654 synthetic_mouse_event_.cmd = cmd;
656 if (synthetic_mouse_event_.timeout.running())
657 // Discard the event. Note that it _may_ be handled
658 // when the timeout expires if
659 // synthetic_mouse_event_.cmd has not been overwritten.
660 // Ie, when the timeout expires, we handle the
661 // most recent event but discard all others that
662 // occurred after the one used to start the timeout
663 // in the first place.
666 synthetic_mouse_event_.restart_timeout = true;
667 synthetic_mouse_event_.timeout.start();
668 // Fall through to handle this event...
670 } else if (synthetic_mouse_event_.timeout.running()) {
671 // Store the event, to be possibly handled when the timeout
673 // Once the timeout has expired, normal control is returned
674 // to mouseMoveEvent (restart_timeout = false).
675 // This results in a much smoother 'feel' when moving the
676 // mouse back into the work area.
677 synthetic_mouse_event_.cmd = cmd;
678 synthetic_mouse_event_.restart_timeout = false;
682 // Has anything changed on-screen since the last QMouseEvent
684 double const scrollbar_value = verticalScrollBar()->value();
685 if (e->x() == synthetic_mouse_event_.x_old
686 && e->y() == synthetic_mouse_event_.y_old
687 && scrollbar_value == synthetic_mouse_event_.scrollbar_value_old) {
688 // Nothing changed on-screen since the last QMouseEvent.
692 // Yes something has changed. Store the params used to check this.
693 synthetic_mouse_event_.x_old = e->x();
694 synthetic_mouse_event_.y_old = e->y();
695 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
697 // ... and dispatch the event to the LyX core.
702 void GuiWorkArea::wheelEvent(QWheelEvent * e)
704 // Wheel rotation by one notch results in a delta() of 120 (see
705 // documentation of QWheelEvent)
706 double const lines = qApp->wheelScrollLines()
707 * lyxrc.mouse_wheel_speed
708 * e->delta() / 120.0;
709 LYXERR(Debug::SCROLLING, "wheelScrollLines = " << qApp->wheelScrollLines()
710 << " delta = " << e->delta()
711 << " lines = " << lines);
712 verticalScrollBar()->setValue(verticalScrollBar()->value() -
713 int(lines * verticalScrollBar()->singleStep()));
718 void GuiWorkArea::generateSyntheticMouseEvent()
720 // Set things off to generate the _next_ 'pseudo' event.
721 if (synthetic_mouse_event_.restart_timeout)
722 synthetic_mouse_event_.timeout.start();
724 // Has anything changed on-screen since the last timeout signal
726 double const scrollbar_value = verticalScrollBar()->value();
727 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
728 // Yes it has. Store the params used to check this.
729 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
731 // ... and dispatch the event to the LyX core.
732 dispatch(synthetic_mouse_event_.cmd);
737 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
739 // intercept some keys if completion popup is visible
740 if (completer_.popupVisible()) {
744 completer_.activate();
750 // intercept keys for the completion
751 if (ev->key() == Qt::Key_Tab) {
757 if (completer_.popupVisible() && ev->key() == Qt::Key_Escape) {
758 completer_.hidePopup();
763 if (completer_.inlineVisible() && ev->key() == Qt::Key_Escape) {
764 completer_.hideInline();
769 // do nothing if there are other events
770 // (the auto repeated events come too fast)
771 // \todo FIXME: remove hard coded Qt keys, process the key binding
773 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
774 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
775 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
781 LYXERR(Debug::KEY, " count: " << ev->count()
782 << " text: " << fromqstr(ev->text())
783 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
786 setKeySymbol(&sym, ev);
787 processKeySym(sym, q_key_state(ev->modifiers()));
792 void GuiWorkArea::doubleClickTimeout()
794 dc_event_.active = false;
798 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
800 dc_event_ = DoubleClick(ev);
801 QTimer::singleShot(QApplication::doubleClickInterval(), this,
802 SLOT(doubleClickTimeout()));
803 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
805 q_button_state(ev->button()));
811 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
813 QAbstractScrollArea::resizeEvent(ev);
819 void GuiWorkArea::update(int x, int y, int w, int h)
821 viewport()->repaint(x, y, w, h);
825 void GuiWorkArea::paintEvent(QPaintEvent * ev)
827 QRect const rc = ev->rect();
828 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
829 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
832 screen_ = QPixmap(viewport()->width(), viewport()->height());
838 QPainter pain(viewport());
839 pain.drawPixmap(rc, screen_, rc);
845 void GuiWorkArea::updateScreen()
847 GuiPainter pain(&screen_);
848 buffer_view_->draw(pain);
852 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
854 if (schedule_redraw_) {
855 buffer_view_->updateMetrics();
857 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
858 schedule_redraw_ = false;
859 // Show the cursor immediately after the update.
865 cursor_->update(x, y, h, shape);
867 viewport()->update(cursor_->rect());
871 void GuiWorkArea::removeCursor()
874 //if (!qApp->focusWidget())
875 viewport()->update(cursor_->rect());
879 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
881 QString const & commit_string = e->commitString();
882 docstring const & preedit_string
883 = qstring_to_ucs4(e->preeditString());
885 if (!commit_string.isEmpty()) {
887 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
888 << " commitString: " << fromqstr(e->commitString()));
892 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
893 for (int i = 0; i != commit_string.size(); ++i) {
894 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
899 // Hide the cursor during the kana-kanji transformation.
900 if (preedit_string.empty())
901 startBlinkingCursor();
903 stopBlinkingCursor();
905 // last_width : for checking if last preedit string was/wasn't empty.
906 static bool last_width = false;
907 if (!last_width && preedit_string.empty()) {
908 // if last_width is last length of preedit string.
913 GuiPainter pain(&screen_);
914 buffer_view_->updateMetrics();
915 buffer_view_->draw(pain);
916 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
917 FontMetrics const & fm = theFontMetrics(font);
918 int height = fm.maxHeight();
919 int cur_x = cursor_->rect().left();
920 int cur_y = cursor_->rect().bottom();
922 // redraw area of preedit string.
923 update(0, cur_y - height, viewport()->width(),
924 (height + 1) * preedit_lines_);
926 if (preedit_string.empty()) {
934 // att : stores an IM attribute.
935 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
937 // get attributes of input method cursor.
938 // cursor_pos : cursor position in preedit string.
939 size_t cursor_pos = 0;
940 bool cursor_is_visible = false;
941 for (int i = 0; i != att.size(); ++i) {
942 if (att.at(i).type == QInputMethodEvent::Cursor) {
943 cursor_pos = att.at(i).start;
944 cursor_is_visible = att.at(i).length != 0;
949 size_t preedit_length = preedit_string.length();
951 // get position of selection in input method.
952 // FIXME: isn't there a way to do this simplier?
953 // rStart : cursor position in selected string in IM.
955 // rLength : selected string length in IM.
957 if (cursor_pos < preedit_length) {
958 for (int i = 0; i != att.size(); ++i) {
959 if (att.at(i).type == QInputMethodEvent::TextFormat) {
960 if (att.at(i).start <= int(cursor_pos)
961 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
962 rStart = att.at(i).start;
963 rLength = att.at(i).length;
964 if (!cursor_is_visible)
965 cursor_pos += rLength;
976 int const right_margin = buffer_view_->rightMargin();
977 Painter::preedit_style ps;
978 // Most often there would be only one line:
980 for (size_t pos = 0; pos != preedit_length; ++pos) {
981 char_type const typed_char = preedit_string[pos];
982 // reset preedit string style
983 ps = Painter::preedit_default;
985 // if we reached the right extremity of the screen, go to next line.
986 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
987 cur_x = right_margin;
991 // preedit strings are displayed with dashed underline
992 // and partial strings are displayed white on black indicating
993 // that we are in selecting mode in the input method.
994 // FIXME: rLength == preedit_length is not a changing condition
995 // FIXME: should be put out of the loop.
997 && pos < rStart + rLength
998 && !(cursor_pos < rLength && rLength == preedit_length))
999 ps = Painter::preedit_selecting;
1001 if (pos == cursor_pos
1002 && (cursor_pos < rLength && rLength == preedit_length))
1003 ps = Painter::preedit_cursor;
1005 // draw one character and update cur_x.
1006 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
1009 // update the preedit string screen area.
1010 update(0, cur_y - preedit_lines_*height, viewport()->width(),
1011 (height + 1) * preedit_lines_);
1013 // Don't forget to accept the event!
1018 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
1020 QRect cur_r(0,0,0,0);
1022 // this is the CJK-specific composition window position.
1023 case Qt::ImMicroFocus:
1024 cur_r = cursor_->rect();
1025 if (preedit_lines_ != 1)
1027 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
1028 // return lower right of cursor in LyX.
1031 return QWidget::inputMethodQuery(query);
1036 void GuiWorkArea::updateWindowTitle()
1038 docstring maximize_title;
1039 docstring minimize_title;
1041 Buffer & buf = buffer_view_->buffer();
1042 FileName const fileName = buf.fileName();
1043 if (!fileName.empty()) {
1044 maximize_title = fileName.displayName(30);
1045 minimize_title = from_utf8(fileName.onlyFileName());
1046 if (!buf.isClean()) {
1047 maximize_title += _(" (changed)");
1048 minimize_title += char_type('*');
1050 if (buf.isReadonly())
1051 maximize_title += _(" (read only)");
1054 QString title = windowTitle();
1055 QString new_title = toqstr(maximize_title);
1056 if (title == new_title)
1059 QWidget::setWindowTitle(new_title);
1060 QWidget::setWindowIconText(toqstr(minimize_title));
1065 void GuiWorkArea::setReadOnly(bool)
1067 updateWindowTitle();
1068 if (this == lyx_view_->currentWorkArea())
1069 lyx_view_->updateBufferDependent(false);
1073 bool GuiWorkArea::isFullScreen()
1075 return lyx_view_ && lyx_view_->isFullScreen();
1079 ////////////////////////////////////////////////////////////////////
1083 ////////////////////////////////////////////////////////////////////
1085 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1087 QPalette pal = palette();
1088 pal.setColor(QPalette::Active, QPalette::Button,
1089 pal.color(QPalette::Active, QPalette::Window));
1090 pal.setColor(QPalette::Disabled, QPalette::Button,
1091 pal.color(QPalette::Disabled, QPalette::Window));
1092 pal.setColor(QPalette::Inactive, QPalette::Button,
1093 pal.color(QPalette::Inactive, QPalette::Window));
1095 QObject::connect(this, SIGNAL(currentChanged(int)),
1096 this, SLOT(on_currentTabChanged(int)));
1098 QToolButton * closeBufferButton = new QToolButton(this);
1099 closeBufferButton->setPalette(pal);
1100 // FIXME: rename the icon to closebuffer.png
1101 closeBufferButton->setIcon(QIcon(":/images/closetab.png"));
1102 closeBufferButton->setText("Close File");
1103 closeBufferButton->setAutoRaise(true);
1104 closeBufferButton->setCursor(Qt::ArrowCursor);
1105 closeBufferButton->setToolTip(qt_("Close File"));
1106 closeBufferButton->setEnabled(true);
1107 QObject::connect(closeBufferButton, SIGNAL(clicked()),
1108 this, SLOT(closeCurrentBuffer()));
1109 setCornerWidget(closeBufferButton, Qt::TopRightCorner);
1111 QToolButton * closeTabButton = new QToolButton(this);
1112 closeTabButton->setPalette(pal);
1113 closeTabButton->setIcon(QIcon(":/images/hidetab.png"));
1114 closeTabButton->setText("Hide tab");
1115 closeTabButton->setAutoRaise(true);
1116 closeTabButton->setCursor(Qt::ArrowCursor);
1117 closeTabButton->setToolTip(qt_("Hide tab"));
1118 closeTabButton->setEnabled(true);
1119 QObject::connect(closeTabButton, SIGNAL(clicked()),
1120 this, SLOT(closeCurrentTab()));
1121 setCornerWidget(closeTabButton, Qt::TopLeftCorner);
1123 setUsesScrollButtons(true);
1127 void TabWorkArea::setFullScreen(bool full_screen)
1129 for (int i = 0; i != count(); ++i) {
1130 if (GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i)))
1131 wa->setFullScreen(full_screen);
1134 if (lyxrc.full_screen_tabbar)
1135 showBar(!full_screen && count()>1);
1139 void TabWorkArea::showBar(bool show)
1141 tabBar()->setEnabled(show);
1142 tabBar()->setVisible(show);
1146 GuiWorkArea * TabWorkArea::currentWorkArea()
1151 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1157 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1159 for (int i = 0; i != count(); ++i) {
1160 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1162 if (&wa->bufferView().buffer() == &buffer)
1169 void TabWorkArea::closeAll()
1172 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1180 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1182 BOOST_ASSERT(work_area);
1183 int index = indexOf(work_area);
1187 if (index == currentIndex())
1188 // Make sure the work area is up to date.
1189 on_currentTabChanged(index);
1191 // Switch to the work area.
1192 setCurrentIndex(index);
1193 work_area->setFocus();
1199 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1201 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1202 wa->setUpdatesEnabled(false);
1203 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1204 // when hiding it again below).
1205 if (!(currentWorkArea() && currentWorkArea()->isFullScreen()))
1206 showBar(count() > 0);
1207 addTab(wa, wa->windowTitle());
1208 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1209 this, SLOT(updateTabText(GuiWorkArea *)));
1210 if (currentWorkArea() && currentWorkArea()->isFullScreen())
1211 setFullScreen(true);
1213 // Hide tabbar if there's only one tab.
1214 showBar(count() > 1);
1220 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1222 BOOST_ASSERT(work_area);
1223 int index = indexOf(work_area);
1227 work_area->setUpdatesEnabled(false);
1232 // make sure the next work area is enabled.
1233 currentWidget()->setUpdatesEnabled(true);
1234 if ((currentWorkArea() && currentWorkArea()->isFullScreen()))
1235 setFullScreen(true);
1237 // Hide tabbar if there's only one tab.
1238 showBar(count() > 1);
1244 void TabWorkArea::on_currentTabChanged(int i)
1246 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1248 BufferView & bv = wa->bufferView();
1249 bv.cursor().fixIfBroken();
1251 wa->setUpdatesEnabled(true);
1255 currentWorkAreaChanged(wa);
1257 LYXERR(Debug::GUI, "currentTabChanged " << i
1258 << "File" << bv.buffer().absFileName());
1262 void TabWorkArea::closeCurrentBuffer()
1264 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1268 void TabWorkArea::closeCurrentTab()
1270 removeWorkArea(currentWorkArea());
1274 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1276 int const i = indexOf(wa);
1279 setTabText(i, wa->windowTitle());
1282 } // namespace frontend
1285 #include "GuiWorkArea_moc.cpp"