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"
42 #include "support/ForkedCalls.h"
44 #include "frontends/Application.h"
45 #include "frontends/FontMetrics.h"
46 #include "frontends/WorkAreaManager.h"
48 #include <QContextMenuEvent>
49 #include <QInputContext>
52 #include <QMainWindow>
58 #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 cursor_timeout_.setInterval(400);
200 connect(&cursor_timeout_, SIGNAL(timeout()),
201 this, SLOT(toggleCursor()));
203 cursor_timeout_.start();
205 screen_ = QPixmap(viewport()->width(), viewport()->height());
206 cursor_ = new frontend::CursorWidget();
209 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
210 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
211 setAcceptDrops(true);
212 setMouseTracking(true);
213 setMinimumSize(100, 70);
216 viewport()->setAutoFillBackground(false);
217 // We don't need double-buffering nor SystemBackground on
218 // the viewport because we have our own backing pixmap.
219 viewport()->setAttribute(Qt::WA_NoSystemBackground);
221 setFocusPolicy(Qt::WheelFocus);
223 viewport()->setCursor(Qt::IBeamCursor);
225 synthetic_mouse_event_.timeout.timeout.connect(
226 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
229 // Initialize the vertical Scroll Bar
230 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
231 this, SLOT(adjustViewWithScrollBar(int)));
233 // disable context menu for the scrollbar
234 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
236 // PageStep only depends on the viewport height.
237 verticalScrollBar()->setPageStep(viewport()->height());
239 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
240 << " viewport height: " << viewport()->height());
242 // Enables input methods for asian languages.
243 // Must be set when creating custom text editing widgets.
244 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 BufferView & GuiWorkArea::bufferView()
265 return *buffer_view_;
269 BufferView const & GuiWorkArea::bufferView() const
271 return *buffer_view_;
275 void GuiWorkArea::stopBlinkingCursor()
277 cursor_timeout_.stop();
282 void GuiWorkArea::startBlinkingCursor()
285 cursor_timeout_.start();
289 void GuiWorkArea::redraw()
292 // No need to redraw in this case.
295 // No need to do anything if this is the current view. The BufferView
296 // metrics are already up to date.
297 if (lyx_view_ != guiApp->currentView()) {
298 // FIXME: it would be nice to optimize for the off-screen case.
299 buffer_view_->updateMetrics();
300 buffer_view_->cursor().fixIfBroken();
303 // update cursor position, because otherwise it has to wait until
304 // the blinking interval is over
305 if (cursor_visible_) {
310 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
312 update(0, 0, viewport()->width(), viewport()->height());
314 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
315 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
317 lyx_view_->updateStatusBar();
319 if (lyxerr.debugging(Debug::WORKAREA))
320 buffer_view_->coordCache().dump();
324 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
326 // In order to avoid bad surprise in the middle of an operation,
327 // we better stop the blinking cursor.
328 stopBlinkingCursor();
330 theLyXFunc().setLyXView(lyx_view_);
331 theLyXFunc().processKeySym(key, mod);
335 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
338 if (cmd0.action == LFUN_FILE_OPEN) {
339 lyx_view_->dispatch(cmd0);
343 theLyXFunc().setLyXView(lyx_view_);
347 if (cmd0.action == LFUN_MOUSE_PRESS) {
348 if (mod == ShiftModifier)
349 cmd = FuncRequest(cmd0, "region-select");
350 else if (mod == ControlModifier)
351 cmd = FuncRequest(cmd0, "paragraph-select");
358 // In order to avoid bad surprise in the middle of an operation, we better stop
359 // the blinking cursor.
360 if (!(cmd.action == LFUN_MOUSE_MOTION
361 && cmd.button() == mouse_button::none))
362 stopBlinkingCursor();
364 buffer_view_->mouseEventDispatch(cmd);
366 // Skip these when selecting
367 if (cmd.action != LFUN_MOUSE_MOTION) {
368 lyx_view_->updateLayoutList();
369 lyx_view_->updateToolbars();
372 // GUI tweaks except with mouse motion with no button pressed.
373 if (!(cmd.action == LFUN_MOUSE_MOTION
374 && cmd.button() == mouse_button::none)) {
375 // Slight hack: this is only called currently when we
376 // clicked somewhere, so we force through the display
377 // of the new status here.
378 lyx_view_->clearMessage();
380 // Show the cursor immediately after any operation.
381 startBlinkingCursor();
386 void GuiWorkArea::resizeBufferView()
388 // WARNING: Please don't put any code that will trigger a repaint here!
389 // We are already inside a paint event.
390 lyx_view_->setBusy(true);
391 buffer_view_->resize(viewport()->width(), viewport()->height());
392 lyx_view_->updateLayoutList();
393 lyx_view_->setBusy(false);
394 need_resize_ = false;
398 void GuiWorkArea::showCursor()
403 CursorShape shape = BAR_SHAPE;
405 Font const & realfont = buffer_view_->cursor().real_current_font;
406 BufferParams const & bp = buffer_view_->buffer().params();
407 bool const samelang = realfont.language() == bp.language;
408 bool const isrtl = realfont.isVisibleRightToLeft();
410 if (!samelang || isrtl != bp.language->rightToLeft()) {
413 shape = REVERSED_L_SHAPE;
416 // The ERT language hack needs fixing up
417 if (realfont.language() == latex_language)
420 Font const font = buffer_view_->cursor().getFont();
421 FontMetrics const & fm = theFontMetrics(font);
422 int const asc = fm.maxAscent();
423 int const des = fm.maxDescent();
427 buffer_view_->cursor().getPos(x, y);
430 // if it doesn't touch the screen, don't try to show it
431 if (y + h < 0 || y >= viewport()->height())
434 cursor_visible_ = true;
435 showCursor(x, y, h, shape);
439 void GuiWorkArea::hideCursor()
441 if (!cursor_visible_)
444 cursor_visible_ = false;
449 void GuiWorkArea::toggleCursor()
456 // Use this opportunity to deal with any child processes that
457 // have finished but are waiting to communicate this fact
458 // to the rest of LyX.
459 ForkedCallsController::handleCompletedProcesses();
463 void GuiWorkArea::updateScrollbar()
465 verticalScrollBar()->setTracking(false);
467 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
469 // do what cursor movement does (some grey)
470 int const h = scroll_.height + viewport()->height() / 4;
471 int scroll_max_ = max(0, h - viewport()->height());
473 verticalScrollBar()->setRange(0, scroll_max_);
474 verticalScrollBar()->setSliderPosition(scroll_.position);
475 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
476 verticalScrollBar()->setValue(scroll_.position);
478 verticalScrollBar()->setTracking(true);
482 void GuiWorkArea::adjustViewWithScrollBar(int action)
484 stopBlinkingCursor();
485 if (action == QAbstractSlider::SliderPageStepAdd)
486 buffer_view_->scrollDown(viewport()->height());
487 else if (action == QAbstractSlider::SliderPageStepSub)
488 buffer_view_->scrollUp(viewport()->height());
490 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
492 if (lyxrc.cursor_follows_scrollbar) {
493 buffer_view_->setCursorFromScrollbar();
494 lyx_view_->updateLayoutList();
496 // Show the cursor immediately after any operation.
497 startBlinkingCursor();
498 QApplication::syncX();
502 bool GuiWorkArea::event(QEvent * e)
504 if (e->type() == QEvent::ToolTip) {
505 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
506 QPoint pos = helpEvent->pos();
507 if (pos.x() < viewport()->width()) {
508 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
509 QToolTip::showText(helpEvent->globalPos(), s);
512 QToolTip::hideText();
514 return QAbstractScrollArea::event(e);
518 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
520 QPoint pos = e->pos();
521 Menu const & menu = buffer_view_->contextMenu(pos.x(), pos.y());
522 LYXERR(Debug::GUI, "context menu resquested: " << menu.name());
524 // FIXME: do something with GuiPopupMenu and MenuBackend.
525 // Some cleanups of GuiPopupMenu and GuiMenubar are needed
527 QAbstractScrollArea::contextMenuEvent(e);
531 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
533 // Repaint the whole screen.
534 // Note: this is different from redraw() as only the backing pixmap
535 // will be redrawn, which is cheap.
536 viewport()->repaint();
538 startBlinkingCursor();
542 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
544 stopBlinkingCursor();
548 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
550 if (dc_event_.active && dc_event_ == *e) {
551 dc_event_.active = false;
552 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
553 q_button_state(e->button()));
558 inputContext()->reset();
560 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
561 q_button_state(e->button()));
562 dispatch(cmd, q_key_state(e->modifiers()));
566 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
568 if (synthetic_mouse_event_.timeout.running())
569 synthetic_mouse_event_.timeout.stop();
571 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
572 q_button_state(e->button()));
577 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
579 // we kill the triple click if we move
580 doubleClickTimeout();
581 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
582 q_motion_state(e->buttons()));
584 // If we're above or below the work area...
585 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
586 // Make sure only a synthetic event can cause a page scroll,
587 // so they come at a steady rate:
589 // _Force_ a scroll up:
592 cmd.y = viewport()->height();
593 // Store the event, to be handled when the timeout expires.
594 synthetic_mouse_event_.cmd = cmd;
596 if (synthetic_mouse_event_.timeout.running())
597 // Discard the event. Note that it _may_ be handled
598 // when the timeout expires if
599 // synthetic_mouse_event_.cmd has not been overwritten.
600 // Ie, when the timeout expires, we handle the
601 // most recent event but discard all others that
602 // occurred after the one used to start the timeout
603 // in the first place.
606 synthetic_mouse_event_.restart_timeout = true;
607 synthetic_mouse_event_.timeout.start();
608 // Fall through to handle this event...
611 } else if (synthetic_mouse_event_.timeout.running()) {
612 // Store the event, to be possibly handled when the timeout
614 // Once the timeout has expired, normal control is returned
615 // to mouseMoveEvent (restart_timeout = false).
616 // This results in a much smoother 'feel' when moving the
617 // mouse back into the work area.
618 synthetic_mouse_event_.cmd = cmd;
619 synthetic_mouse_event_.restart_timeout = false;
623 // Has anything changed on-screen since the last QMouseEvent
625 double const scrollbar_value = verticalScrollBar()->value();
626 if (e->x() != synthetic_mouse_event_.x_old ||
627 e->y() != synthetic_mouse_event_.y_old ||
628 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
629 // Yes it has. Store the params used to check this.
630 synthetic_mouse_event_.x_old = e->x();
631 synthetic_mouse_event_.y_old = e->y();
632 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
634 // ... and dispatch the event to the LyX core.
640 void GuiWorkArea::wheelEvent(QWheelEvent * e)
642 // Wheel rotation by one notch results in a delta() of 120 (see
643 // documentation of QWheelEvent)
644 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
645 verticalScrollBar()->setValue(verticalScrollBar()->value() -
646 lines * verticalScrollBar()->singleStep());
647 adjustViewWithScrollBar();
651 void GuiWorkArea::generateSyntheticMouseEvent()
653 // Set things off to generate the _next_ 'pseudo' event.
654 if (synthetic_mouse_event_.restart_timeout)
655 synthetic_mouse_event_.timeout.start();
657 // Has anything changed on-screen since the last timeout signal
659 double const scrollbar_value = verticalScrollBar()->value();
660 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
661 // Yes it has. Store the params used to check this.
662 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
664 // ... and dispatch the event to the LyX core.
665 dispatch(synthetic_mouse_event_.cmd);
670 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
672 // do nothing if there are other events
673 // (the auto repeated events come too fast)
674 // \todo FIXME: remove hard coded Qt keys, process the key binding
676 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
677 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
678 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
684 LYXERR(Debug::KEY, " count: " << ev->count()
685 << " text: " << fromqstr(ev->text())
686 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
689 setKeySymbol(&sym, ev);
690 processKeySym(sym, q_key_state(ev->modifiers()));
694 void GuiWorkArea::doubleClickTimeout()
696 dc_event_.active = false;
700 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
702 dc_event_ = DoubleClick(ev);
703 QTimer::singleShot(QApplication::doubleClickInterval(), this,
704 SLOT(doubleClickTimeout()));
705 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
707 q_button_state(ev->button()));
712 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
714 QAbstractScrollArea::resizeEvent(ev);
719 void GuiWorkArea::update(int x, int y, int w, int h)
721 viewport()->repaint(x, y, w, h);
725 void GuiWorkArea::paintEvent(QPaintEvent * ev)
727 QRect const rc = ev->rect();
728 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
729 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
732 verticalScrollBar()->setPageStep(viewport()->height());
733 screen_ = QPixmap(viewport()->width(), viewport()->height());
740 QPainter pain(viewport());
741 pain.drawPixmap(rc, screen_, rc);
746 void GuiWorkArea::updateScreen()
748 GuiPainter pain(&screen_);
749 buffer_view_->draw(pain);
753 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
755 if (schedule_redraw_) {
756 buffer_view_->updateMetrics();
758 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
759 schedule_redraw_ = false;
760 // Show the cursor immediately after the update.
766 cursor_->update(x, y, h, shape);
768 viewport()->update(cursor_->rect());
772 void GuiWorkArea::removeCursor()
775 //if (!qApp->focusWidget())
776 viewport()->update(cursor_->rect());
780 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
782 QString const & commit_string = e->commitString();
783 docstring const & preedit_string
784 = qstring_to_ucs4(e->preeditString());
786 if (!commit_string.isEmpty()) {
788 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
789 << " commitString: " << fromqstr(e->commitString()));
793 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
794 for (int i = 0; i != commit_string.size(); ++i) {
795 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
800 // Hide the cursor during the kana-kanji transformation.
801 if (preedit_string.empty())
802 startBlinkingCursor();
804 stopBlinkingCursor();
806 // last_width : for checking if last preedit string was/wasn't empty.
807 static bool last_width = false;
808 if (!last_width && preedit_string.empty()) {
809 // if last_width is last length of preedit string.
814 GuiPainter pain(&screen_);
815 buffer_view_->updateMetrics();
816 buffer_view_->draw(pain);
817 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
818 FontMetrics const & fm = theFontMetrics(font);
819 int height = fm.maxHeight();
820 int cur_x = cursor_->rect().left();
821 int cur_y = cursor_->rect().bottom();
823 // redraw area of preedit string.
824 update(0, cur_y - height, viewport()->width(),
825 (height + 1) * preedit_lines_);
827 if (preedit_string.empty()) {
835 // att : stores an IM attribute.
836 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
838 // get attributes of input method cursor.
839 // cursor_pos : cursor position in preedit string.
840 size_t cursor_pos = 0;
841 bool cursor_is_visible = false;
842 for (int i = 0; i != att.size(); ++i) {
843 if (att.at(i).type == QInputMethodEvent::Cursor) {
844 cursor_pos = att.at(i).start;
845 cursor_is_visible = att.at(i).length != 0;
850 size_t preedit_length = preedit_string.length();
852 // get position of selection in input method.
853 // FIXME: isn't there a way to do this simplier?
854 // rStart : cursor position in selected string in IM.
856 // rLength : selected string length in IM.
858 if (cursor_pos < preedit_length) {
859 for (int i = 0; i != att.size(); ++i) {
860 if (att.at(i).type == QInputMethodEvent::TextFormat) {
861 if (att.at(i).start <= int(cursor_pos)
862 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
863 rStart = att.at(i).start;
864 rLength = att.at(i).length;
865 if (!cursor_is_visible)
866 cursor_pos += rLength;
877 int const right_margin = rightMargin();
878 Painter::preedit_style ps;
879 // Most often there would be only one line:
881 for (size_t pos = 0; pos != preedit_length; ++pos) {
882 char_type const typed_char = preedit_string[pos];
883 // reset preedit string style
884 ps = Painter::preedit_default;
886 // if we reached the right extremity of the screen, go to next line.
887 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
888 cur_x = right_margin;
892 // preedit strings are displayed with dashed underline
893 // and partial strings are displayed white on black indicating
894 // that we are in selecting mode in the input method.
895 // FIXME: rLength == preedit_length is not a changing condition
896 // FIXME: should be put out of the loop.
898 && pos < rStart + rLength
899 && !(cursor_pos < rLength && rLength == preedit_length))
900 ps = Painter::preedit_selecting;
902 if (pos == cursor_pos
903 && (cursor_pos < rLength && rLength == preedit_length))
904 ps = Painter::preedit_cursor;
906 // draw one character and update cur_x.
907 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
910 // update the preedit string screen area.
911 update(0, cur_y - preedit_lines_*height, viewport()->width(),
912 (height + 1) * preedit_lines_);
914 // Don't forget to accept the event!
919 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
921 QRect cur_r(0,0,0,0);
923 // this is the CJK-specific composition window position.
924 case Qt::ImMicroFocus:
925 cur_r = cursor_->rect();
926 if (preedit_lines_ != 1)
928 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
929 // return lower right of cursor in LyX.
932 return QWidget::inputMethodQuery(query);
937 void GuiWorkArea::updateWindowTitle()
939 docstring maximize_title;
940 docstring minimize_title;
942 Buffer & buf = buffer_view_->buffer();
943 FileName const fileName = buf.fileName();
944 if (!fileName.empty()) {
945 maximize_title = fileName.displayName(30);
946 minimize_title = from_utf8(fileName.onlyFileName());
947 if (!buf.isClean()) {
948 maximize_title += _(" (changed)");
949 minimize_title += char_type('*');
951 if (buf.isReadonly())
952 maximize_title += _(" (read only)");
955 QString title = windowTitle();
956 QString new_title = toqstr(maximize_title);
957 if (title == new_title)
960 QWidget::setWindowTitle(new_title);
961 QWidget::setWindowIconText(toqstr(minimize_title));
966 void GuiWorkArea::setReadOnly(bool)
969 if (this == lyx_view_->currentWorkArea())
970 lyx_view_->updateBufferDependent(false);
974 ////////////////////////////////////////////////////////////////////
978 ////////////////////////////////////////////////////////////////////
980 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
982 QPalette pal = palette();
983 pal.setColor(QPalette::Active, QPalette::Button,
984 pal.color(QPalette::Active, QPalette::Window));
985 pal.setColor(QPalette::Disabled, QPalette::Button,
986 pal.color(QPalette::Disabled, QPalette::Window));
987 pal.setColor(QPalette::Inactive, QPalette::Button,
988 pal.color(QPalette::Inactive, QPalette::Window));
990 QToolButton * closeTabButton = new QToolButton(this);
991 closeTabButton->setPalette(pal);
992 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
993 closeTabButton->setText("Close");
994 closeTabButton->setAutoRaise(true);
995 closeTabButton->setCursor(Qt::ArrowCursor);
996 closeTabButton->setToolTip(tr("Close tab"));
997 closeTabButton->setEnabled(true);
999 QObject::connect(this, SIGNAL(currentChanged(int)),
1000 this, SLOT(on_currentTabChanged(int)));
1001 QObject::connect(closeTabButton, SIGNAL(clicked()),
1002 this, SLOT(closeCurrentTab()));
1004 setCornerWidget(closeTabButton);
1005 setUsesScrollButtons(true);
1009 void TabWorkArea::showBar(bool show)
1011 tabBar()->setEnabled(show);
1012 tabBar()->setVisible(show);
1016 GuiWorkArea * TabWorkArea::currentWorkArea()
1021 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1027 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1029 for (int i = 0; i != count(); ++i) {
1030 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1032 if (&wa->bufferView().buffer() == &buffer)
1039 void TabWorkArea::closeAll()
1042 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1050 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1052 BOOST_ASSERT(work_area);
1053 int index = indexOf(work_area);
1057 if (index == currentIndex())
1058 // Make sure the work area is up to date.
1059 on_currentTabChanged(index);
1061 // Switch to the work area.
1062 setCurrentIndex(index);
1063 work_area->setFocus();
1069 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1071 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1072 wa->setUpdatesEnabled(false);
1073 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1074 // when hiding it again below).
1075 showBar(count() > 0);
1076 addTab(wa, wa->windowTitle());
1077 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1078 this, SLOT(updateTabText(GuiWorkArea *)));
1079 // Hide tabbar if there's only one tab.
1080 showBar(count() > 1);
1085 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1087 BOOST_ASSERT(work_area);
1088 int index = indexOf(work_area);
1092 work_area->setUpdatesEnabled(false);
1097 // make sure the next work area is enabled.
1098 currentWidget()->setUpdatesEnabled(true);
1099 // Hide tabbar if there's only one tab.
1100 showBar(count() > 1);
1106 void TabWorkArea::on_currentTabChanged(int i)
1108 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1110 BufferView & bv = wa->bufferView();
1111 bv.cursor().fixIfBroken();
1113 wa->setUpdatesEnabled(true);
1117 currentWorkAreaChanged(wa);
1119 LYXERR(Debug::GUI, "currentTabChanged " << i
1120 << "File" << bv.buffer().absFileName());
1124 void TabWorkArea::closeCurrentTab()
1126 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1130 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1132 int const i = indexOf(wa);
1135 setTabText(i, wa->windowTitle());
1138 } // namespace frontend
1141 #include "GuiWorkArea_moc.cpp"