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>
51 #include <QMainWindow>
57 #include <QToolButton>
60 #include <boost/bind.hpp>
64 extern "C" int XEventsQueued(Display *display, int mode);
68 int const CursorWidth = 2;
70 int const CursorWidth = 1;
77 using namespace lyx::support;
82 /// return the LyX mouse button state from Qt's
83 static mouse_button::state q_button_state(Qt::MouseButton button)
85 mouse_button::state b = mouse_button::none;
88 b = mouse_button::button1;
91 b = mouse_button::button2;
94 b = mouse_button::button3;
103 /// return the LyX mouse button state from Qt's
104 mouse_button::state q_motion_state(Qt::MouseButtons state)
106 mouse_button::state b = mouse_button::none;
107 if (state & Qt::LeftButton)
108 b |= mouse_button::button1;
109 if (state & Qt::MidButton)
110 b |= mouse_button::button2;
111 if (state & Qt::RightButton)
112 b |= mouse_button::button3;
123 void draw(QPainter & painter)
125 if (show_ && rect_.isValid()) {
128 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
129 painter.setPen(color_);
130 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
131 rect_.bottomRight().x(), rect_.bottomLeft().y());
134 case REVERSED_L_SHAPE:
135 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
136 painter.setPen(color_);
137 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
138 rect_.bottomLeft().x(), rect_.bottomLeft().y());
142 painter.fillRect(rect_, color_);
148 void update(int x, int y, int h, CursorShape shape)
150 color_ = guiApp->colorCache().get(Color_cursor);
154 rect_ = QRect(x, y, CursorWidth + h / 3, h);
156 case REVERSED_L_SHAPE:
157 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
160 rect_ = QRect(x, y, CursorWidth, h);
165 void show(bool set_show = true) { show_ = set_show; }
166 void hide() { show_ = false; }
168 QRect const & rect() { return rect_; }
182 // This is a 'heartbeat' generating synthetic mouse move events when the
183 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
184 SyntheticMouseEvent::SyntheticMouseEvent()
185 : timeout(200), restart_timeout(true),
186 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
191 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
192 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
193 cursor_visible_(false),
194 need_resize_(false), schedule_redraw_(false),
197 buffer.workAreaManager().add(this);
199 connect(&cursor_timeout_, SIGNAL(timeout()),
200 this, SLOT(toggleCursor()));
202 int const time = QApplication::cursorFlashTime() / 2;
204 cursor_timeout_.setInterval(time);
205 cursor_timeout_.start();
207 // let's initialize this just to be safe
208 cursor_timeout_.setInterval(500);
210 screen_ = QPixmap(viewport()->width(), viewport()->height());
211 cursor_ = new frontend::CursorWidget();
214 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
215 setAcceptDrops(true);
216 setMouseTracking(true);
217 setMinimumSize(100, 70);
220 viewport()->setAutoFillBackground(false);
221 // We don't need double-buffering nor SystemBackground on
222 // the viewport because we have our own backing pixmap.
223 viewport()->setAttribute(Qt::WA_NoSystemBackground);
225 setFocusPolicy(Qt::WheelFocus);
227 viewport()->setCursor(Qt::IBeamCursor);
229 synthetic_mouse_event_.timeout.timeout.connect(
230 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
233 // Initialize the vertical Scroll Bar
234 QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
235 this, SLOT(scrollTo(int)));
237 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
238 << " viewport height: " << viewport()->height());
240 // Enables input methods for asian languages.
241 // Must be set when creating custom text editing widgets.
242 setAttribute(Qt::WA_InputMethodEnabled, true);
247 GuiWorkArea::~GuiWorkArea()
249 buffer_view_->buffer().workAreaManager().remove(this);
255 void GuiWorkArea::close()
257 lyx_view_->removeWorkArea(this);
261 BufferView & GuiWorkArea::bufferView()
263 return *buffer_view_;
267 BufferView const & GuiWorkArea::bufferView() const
269 return *buffer_view_;
273 void GuiWorkArea::stopBlinkingCursor()
275 cursor_timeout_.stop();
280 void GuiWorkArea::startBlinkingCursor()
283 //we're not supposed to cache this value.
284 int const time = QApplication::cursorFlashTime() / 2;
287 cursor_timeout_.setInterval(time);
288 cursor_timeout_.start();
292 void GuiWorkArea::redraw()
295 // No need to redraw in this case.
298 // No need to do anything if this is the current view. The BufferView
299 // metrics are already up to date.
300 if (lyx_view_ != guiApp->currentView()) {
301 // FIXME: it would be nice to optimize for the off-screen case.
302 buffer_view_->updateMetrics();
303 buffer_view_->cursor().fixIfBroken();
306 // update cursor position, because otherwise it has to wait until
307 // the blinking interval is over
308 if (cursor_visible_) {
313 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
315 update(0, 0, viewport()->width(), viewport()->height());
317 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
318 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
320 lyx_view_->updateStatusBar();
322 if (lyxerr.debugging(Debug::WORKAREA))
323 buffer_view_->coordCache().dump();
327 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
329 // In order to avoid bad surprise in the middle of an operation,
330 // we better stop the blinking cursor...
331 // the cursor gets restarted in GuiView::restartCursor()
332 stopBlinkingCursor();
334 theLyXFunc().setLyXView(lyx_view_);
335 theLyXFunc().processKeySym(key, mod);
340 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
343 if (cmd0.action == LFUN_FILE_OPEN) {
344 lyx_view_->dispatch(cmd0);
348 theLyXFunc().setLyXView(lyx_view_);
352 if (cmd0.action == LFUN_MOUSE_PRESS) {
353 if (mod == ShiftModifier)
354 cmd = FuncRequest(cmd0, "region-select");
355 else if (mod == ControlModifier)
356 cmd = FuncRequest(cmd0, "paragraph-select");
363 bool const notJustMovingTheMouse =
364 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
366 // In order to avoid bad surprise in the middle of an operation, we better stop
367 // the blinking cursor.
368 if (notJustMovingTheMouse)
369 stopBlinkingCursor();
371 buffer_view_->mouseEventDispatch(cmd);
373 // Skip these when selecting
374 if (cmd.action != LFUN_MOUSE_MOTION) {
375 lyx_view_->updateLayoutList();
376 lyx_view_->updateToolbars();
379 // GUI tweaks except with mouse motion with no button pressed.
380 if (notJustMovingTheMouse) {
381 // Slight hack: this is only called currently when we
382 // clicked somewhere, so we force through the display
383 // of the new status here.
384 lyx_view_->clearMessage();
386 // Show the cursor immediately after any operation
387 startBlinkingCursor();
392 void GuiWorkArea::resizeBufferView()
394 // WARNING: Please don't put any code that will trigger a repaint here!
395 // We are already inside a paint event.
396 lyx_view_->setBusy(true);
397 buffer_view_->resize(viewport()->width(), viewport()->height());
398 lyx_view_->updateLayoutList();
399 lyx_view_->setBusy(false);
400 need_resize_ = false;
404 void GuiWorkArea::showCursor()
409 CursorShape shape = BAR_SHAPE;
411 Font const & realfont = buffer_view_->cursor().real_current_font;
412 BufferParams const & bp = buffer_view_->buffer().params();
413 bool const samelang = realfont.language() == bp.language;
414 bool const isrtl = realfont.isVisibleRightToLeft();
416 if (!samelang || isrtl != bp.language->rightToLeft()) {
419 shape = REVERSED_L_SHAPE;
422 // The ERT language hack needs fixing up
423 if (realfont.language() == latex_language)
426 Font const font = buffer_view_->cursor().getFont();
427 FontMetrics const & fm = theFontMetrics(font);
428 int const asc = fm.maxAscent();
429 int const des = fm.maxDescent();
433 buffer_view_->cursor().getPos(x, y);
436 // if it doesn't touch the screen, don't try to show it
437 if (y + h < 0 || y >= viewport()->height())
440 cursor_visible_ = true;
441 showCursor(x, y, h, shape);
445 void GuiWorkArea::hideCursor()
447 if (!cursor_visible_)
450 cursor_visible_ = false;
455 void GuiWorkArea::toggleCursor()
464 void GuiWorkArea::updateScrollbar()
466 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
468 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
469 verticalScrollBar()->setPageStep(scroll_.page_step);
470 verticalScrollBar()->setSingleStep(scroll_.single_step);
471 // Block the scrollbar signal to prevent recursive signal/slot calling.
472 verticalScrollBar()->blockSignals(true);
473 verticalScrollBar()->setValue(scroll_.position);
474 verticalScrollBar()->setSliderPosition(scroll_.position);
475 verticalScrollBar()->blockSignals(false);
479 void GuiWorkArea::scrollTo(int value)
481 stopBlinkingCursor();
482 buffer_view_->scrollDocView(value);
484 if (lyxrc.cursor_follows_scrollbar) {
485 buffer_view_->setCursorFromScrollbar();
486 lyx_view_->updateLayoutList();
488 // Show the cursor immediately after any operation.
489 startBlinkingCursor();
490 QApplication::syncX();
494 bool GuiWorkArea::event(QEvent * e)
497 case QEvent::ToolTip: {
498 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
499 if (lyxrc.use_tooltip) {
500 QPoint pos = helpEvent->pos();
501 if (pos.x() < viewport()->width()) {
502 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
503 QToolTip::showText(helpEvent->globalPos(), s);
506 QToolTip::hideText();
508 // Don't forget to accept the event!
513 case QEvent::ShortcutOverride: {
514 // We catch this event in order to catch the Tab or Shift+Tab key press
515 // which are otherwise reserved to focus switching between controls
517 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
518 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
519 || ke->modifiers() & Qt::ControlModifier)
520 return QAbstractScrollArea::event(e);
526 return QAbstractScrollArea::event(e);
532 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
534 QPoint pos = e->pos();
535 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
537 QAbstractScrollArea::contextMenuEvent(e);
540 QMenu * menu = guiApp->menus().menu(toqstr(name));
542 QAbstractScrollArea::contextMenuEvent(e);
545 // Position the menu to the right.
546 // FIXME: menu position should be different for RTL text.
547 pos.rx() += menu->width();
548 // FIXME: correct vertical position of the menu WRT screen espace.
549 //pos.ry() += menu->height();
555 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
557 // Repaint the whole screen.
558 // Note: this is different from redraw() as only the backing pixmap
559 // will be redrawn, which is cheap.
560 viewport()->repaint();
562 startBlinkingCursor();
566 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
568 stopBlinkingCursor();
572 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
574 if (dc_event_.active && dc_event_ == *e) {
575 dc_event_.active = false;
576 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
577 q_button_state(e->button()));
582 inputContext()->reset();
584 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
585 q_button_state(e->button()));
586 dispatch(cmd, q_key_state(e->modifiers()));
590 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
592 if (synthetic_mouse_event_.timeout.running())
593 synthetic_mouse_event_.timeout.stop();
595 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
596 q_button_state(e->button()));
601 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
603 // we kill the triple click if we move
604 doubleClickTimeout();
605 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
606 q_motion_state(e->buttons()));
608 // If we're above or below the work area...
609 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
610 // Make sure only a synthetic event can cause a page scroll,
611 // so they come at a steady rate:
613 // _Force_ a scroll up:
616 cmd.y = viewport()->height();
617 // Store the event, to be handled when the timeout expires.
618 synthetic_mouse_event_.cmd = cmd;
620 if (synthetic_mouse_event_.timeout.running())
621 // Discard the event. Note that it _may_ be handled
622 // when the timeout expires if
623 // synthetic_mouse_event_.cmd has not been overwritten.
624 // Ie, when the timeout expires, we handle the
625 // most recent event but discard all others that
626 // occurred after the one used to start the timeout
627 // in the first place.
630 synthetic_mouse_event_.restart_timeout = true;
631 synthetic_mouse_event_.timeout.start();
632 // Fall through to handle this event...
635 } else if (synthetic_mouse_event_.timeout.running()) {
636 // Store the event, to be possibly handled when the timeout
638 // Once the timeout has expired, normal control is returned
639 // to mouseMoveEvent (restart_timeout = false).
640 // This results in a much smoother 'feel' when moving the
641 // mouse back into the work area.
642 synthetic_mouse_event_.cmd = cmd;
643 synthetic_mouse_event_.restart_timeout = false;
647 // Has anything changed on-screen since the last QMouseEvent
649 double const scrollbar_value = verticalScrollBar()->value();
650 if (e->x() != synthetic_mouse_event_.x_old ||
651 e->y() != synthetic_mouse_event_.y_old ||
652 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
653 // Yes it has. Store the params used to check this.
654 synthetic_mouse_event_.x_old = e->x();
655 synthetic_mouse_event_.y_old = e->y();
656 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
658 // ... and dispatch the event to the LyX core.
664 void GuiWorkArea::wheelEvent(QWheelEvent * e)
666 // Wheel rotation by one notch results in a delta() of 120 (see
667 // documentation of QWheelEvent)
668 double const lines = qApp->wheelScrollLines()
669 * lyxrc.mouse_wheel_speed
670 * e->delta() / 120.0;
671 lyxerr << "wheelScrollLines = " << qApp->wheelScrollLines()
672 << " delta = " << e->delta()
673 << " lines = " << lines
675 verticalScrollBar()->setValue(verticalScrollBar()->value() -
676 int(lines * verticalScrollBar()->singleStep()));
680 void GuiWorkArea::generateSyntheticMouseEvent()
682 // Set things off to generate the _next_ 'pseudo' event.
683 if (synthetic_mouse_event_.restart_timeout)
684 synthetic_mouse_event_.timeout.start();
686 // Has anything changed on-screen since the last timeout signal
688 double const scrollbar_value = verticalScrollBar()->value();
689 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
690 // Yes it has. Store the params used to check this.
691 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
693 // ... and dispatch the event to the LyX core.
694 dispatch(synthetic_mouse_event_.cmd);
699 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
701 // do nothing if there are other events
702 // (the auto repeated events come too fast)
703 // \todo FIXME: remove hard coded Qt keys, process the key binding
705 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
706 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
707 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
713 LYXERR(Debug::KEY, " count: " << ev->count()
714 << " text: " << fromqstr(ev->text())
715 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
718 setKeySymbol(&sym, ev);
719 processKeySym(sym, q_key_state(ev->modifiers()));
724 void GuiWorkArea::doubleClickTimeout()
726 dc_event_.active = false;
730 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
732 dc_event_ = DoubleClick(ev);
733 QTimer::singleShot(QApplication::doubleClickInterval(), this,
734 SLOT(doubleClickTimeout()));
735 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
737 q_button_state(ev->button()));
742 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
744 QAbstractScrollArea::resizeEvent(ev);
749 void GuiWorkArea::update(int x, int y, int w, int h)
751 viewport()->repaint(x, y, w, h);
755 void GuiWorkArea::paintEvent(QPaintEvent * ev)
757 QRect const rc = ev->rect();
758 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
759 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
762 screen_ = QPixmap(viewport()->width(), viewport()->height());
765 // Update scrollbars which might have changed due different
766 // BufferView dimension. This is especially important when the
767 // BufferView goes from zero-size to the real-size for the first time,
768 // as the scrollbar paramters are then set for the first time.
770 BOOST_ASSERT(need_resize_ == false);
777 QPainter pain(viewport());
778 pain.drawPixmap(rc, screen_, rc);
783 void GuiWorkArea::updateScreen()
785 GuiPainter pain(&screen_);
786 buffer_view_->draw(pain);
790 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
792 if (schedule_redraw_) {
793 buffer_view_->updateMetrics();
795 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
796 schedule_redraw_ = false;
797 // Show the cursor immediately after the update.
803 cursor_->update(x, y, h, shape);
805 viewport()->update(cursor_->rect());
809 void GuiWorkArea::removeCursor()
812 //if (!qApp->focusWidget())
813 viewport()->update(cursor_->rect());
817 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
819 QString const & commit_string = e->commitString();
820 docstring const & preedit_string
821 = qstring_to_ucs4(e->preeditString());
823 if (!commit_string.isEmpty()) {
825 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
826 << " commitString: " << fromqstr(e->commitString()));
830 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
831 for (int i = 0; i != commit_string.size(); ++i) {
832 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
837 // Hide the cursor during the kana-kanji transformation.
838 if (preedit_string.empty())
839 startBlinkingCursor();
841 stopBlinkingCursor();
843 // last_width : for checking if last preedit string was/wasn't empty.
844 static bool last_width = false;
845 if (!last_width && preedit_string.empty()) {
846 // if last_width is last length of preedit string.
851 GuiPainter pain(&screen_);
852 buffer_view_->updateMetrics();
853 buffer_view_->draw(pain);
854 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
855 FontMetrics const & fm = theFontMetrics(font);
856 int height = fm.maxHeight();
857 int cur_x = cursor_->rect().left();
858 int cur_y = cursor_->rect().bottom();
860 // redraw area of preedit string.
861 update(0, cur_y - height, viewport()->width(),
862 (height + 1) * preedit_lines_);
864 if (preedit_string.empty()) {
872 // att : stores an IM attribute.
873 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
875 // get attributes of input method cursor.
876 // cursor_pos : cursor position in preedit string.
877 size_t cursor_pos = 0;
878 bool cursor_is_visible = false;
879 for (int i = 0; i != att.size(); ++i) {
880 if (att.at(i).type == QInputMethodEvent::Cursor) {
881 cursor_pos = att.at(i).start;
882 cursor_is_visible = att.at(i).length != 0;
887 size_t preedit_length = preedit_string.length();
889 // get position of selection in input method.
890 // FIXME: isn't there a way to do this simplier?
891 // rStart : cursor position in selected string in IM.
893 // rLength : selected string length in IM.
895 if (cursor_pos < preedit_length) {
896 for (int i = 0; i != att.size(); ++i) {
897 if (att.at(i).type == QInputMethodEvent::TextFormat) {
898 if (att.at(i).start <= int(cursor_pos)
899 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
900 rStart = att.at(i).start;
901 rLength = att.at(i).length;
902 if (!cursor_is_visible)
903 cursor_pos += rLength;
914 int const right_margin = rightMargin();
915 Painter::preedit_style ps;
916 // Most often there would be only one line:
918 for (size_t pos = 0; pos != preedit_length; ++pos) {
919 char_type const typed_char = preedit_string[pos];
920 // reset preedit string style
921 ps = Painter::preedit_default;
923 // if we reached the right extremity of the screen, go to next line.
924 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
925 cur_x = right_margin;
929 // preedit strings are displayed with dashed underline
930 // and partial strings are displayed white on black indicating
931 // that we are in selecting mode in the input method.
932 // FIXME: rLength == preedit_length is not a changing condition
933 // FIXME: should be put out of the loop.
935 && pos < rStart + rLength
936 && !(cursor_pos < rLength && rLength == preedit_length))
937 ps = Painter::preedit_selecting;
939 if (pos == cursor_pos
940 && (cursor_pos < rLength && rLength == preedit_length))
941 ps = Painter::preedit_cursor;
943 // draw one character and update cur_x.
944 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
947 // update the preedit string screen area.
948 update(0, cur_y - preedit_lines_*height, viewport()->width(),
949 (height + 1) * preedit_lines_);
951 // Don't forget to accept the event!
956 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
958 QRect cur_r(0,0,0,0);
960 // this is the CJK-specific composition window position.
961 case Qt::ImMicroFocus:
962 cur_r = cursor_->rect();
963 if (preedit_lines_ != 1)
965 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
966 // return lower right of cursor in LyX.
969 return QWidget::inputMethodQuery(query);
974 void GuiWorkArea::updateWindowTitle()
976 docstring maximize_title;
977 docstring minimize_title;
979 Buffer & buf = buffer_view_->buffer();
980 FileName const fileName = buf.fileName();
981 if (!fileName.empty()) {
982 maximize_title = fileName.displayName(30);
983 minimize_title = from_utf8(fileName.onlyFileName());
984 if (!buf.isClean()) {
985 maximize_title += _(" (changed)");
986 minimize_title += char_type('*');
988 if (buf.isReadonly())
989 maximize_title += _(" (read only)");
992 QString title = windowTitle();
993 QString new_title = toqstr(maximize_title);
994 if (title == new_title)
997 QWidget::setWindowTitle(new_title);
998 QWidget::setWindowIconText(toqstr(minimize_title));
1003 void GuiWorkArea::setReadOnly(bool)
1005 updateWindowTitle();
1006 if (this == lyx_view_->currentWorkArea())
1007 lyx_view_->updateBufferDependent(false);
1011 ////////////////////////////////////////////////////////////////////
1015 ////////////////////////////////////////////////////////////////////
1017 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1019 QPalette pal = palette();
1020 pal.setColor(QPalette::Active, QPalette::Button,
1021 pal.color(QPalette::Active, QPalette::Window));
1022 pal.setColor(QPalette::Disabled, QPalette::Button,
1023 pal.color(QPalette::Disabled, QPalette::Window));
1024 pal.setColor(QPalette::Inactive, QPalette::Button,
1025 pal.color(QPalette::Inactive, QPalette::Window));
1027 QToolButton * closeTabButton = new QToolButton(this);
1028 closeTabButton->setPalette(pal);
1029 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1030 closeTabButton->setText("Close");
1031 closeTabButton->setAutoRaise(true);
1032 closeTabButton->setCursor(Qt::ArrowCursor);
1033 closeTabButton->setToolTip(tr("Close tab"));
1034 closeTabButton->setEnabled(true);
1036 QObject::connect(this, SIGNAL(currentChanged(int)),
1037 this, SLOT(on_currentTabChanged(int)));
1038 QObject::connect(closeTabButton, SIGNAL(clicked()),
1039 this, SLOT(closeCurrentTab()));
1041 setCornerWidget(closeTabButton);
1042 setUsesScrollButtons(true);
1046 void TabWorkArea::showBar(bool show)
1048 tabBar()->setEnabled(show);
1049 tabBar()->setVisible(show);
1053 GuiWorkArea * TabWorkArea::currentWorkArea()
1058 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1064 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1066 for (int i = 0; i != count(); ++i) {
1067 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1069 if (&wa->bufferView().buffer() == &buffer)
1076 void TabWorkArea::closeAll()
1079 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1087 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1089 BOOST_ASSERT(work_area);
1090 int index = indexOf(work_area);
1094 if (index == currentIndex())
1095 // Make sure the work area is up to date.
1096 on_currentTabChanged(index);
1098 // Switch to the work area.
1099 setCurrentIndex(index);
1100 work_area->setFocus();
1106 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1108 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1109 wa->setUpdatesEnabled(false);
1110 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1111 // when hiding it again below).
1112 showBar(count() > 0);
1113 addTab(wa, wa->windowTitle());
1114 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1115 this, SLOT(updateTabText(GuiWorkArea *)));
1116 // Hide tabbar if there's only one tab.
1117 showBar(count() > 1);
1122 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1124 BOOST_ASSERT(work_area);
1125 int index = indexOf(work_area);
1129 work_area->setUpdatesEnabled(false);
1134 // make sure the next work area is enabled.
1135 currentWidget()->setUpdatesEnabled(true);
1136 // Hide tabbar if there's only one tab.
1137 showBar(count() > 1);
1143 void TabWorkArea::on_currentTabChanged(int i)
1145 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1147 BufferView & bv = wa->bufferView();
1148 bv.cursor().fixIfBroken();
1150 wa->setUpdatesEnabled(true);
1154 currentWorkAreaChanged(wa);
1156 LYXERR(Debug::GUI, "currentTabChanged " << i
1157 << "File" << bv.buffer().absFileName());
1161 void TabWorkArea::closeCurrentTab()
1163 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1167 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1169 int const i = indexOf(wa);
1172 setTabText(i, wa->windowTitle());
1175 } // namespace frontend
1178 #include "GuiWorkArea_moc.cpp"