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"
23 #include "FuncRequest.h"
25 #include "GuiApplication.h"
26 #include "GuiKeySymbol.h"
27 #include "GuiPainter.h"
29 #include "KeySymbol.h"
33 #include "MetricsInfo.h"
34 #include "qt_helpers.h"
37 #include "graphics/GraphicsImage.h"
38 #include "graphics/GraphicsLoader.h"
40 #include "support/FileName.h"
41 #include "support/ForkedcallsController.h"
43 #include "frontends/Application.h"
44 #include "frontends/FontMetrics.h"
45 #include "frontends/WorkAreaManager.h"
47 #include <QInputContext>
49 #include <QMainWindow>
51 #include <QToolButton>
57 #include <boost/bind.hpp>
58 #include <boost/current_function.hpp>
62 extern "C" int XEventsQueued(Display *display, int mode);
66 int const CursorWidth = 2;
68 int const CursorWidth = 1;
81 using support::FileName;
82 using support::ForkedcallsController;
85 /// return the LyX mouse button state from Qt's
86 static mouse_button::state q_button_state(Qt::MouseButton button)
88 mouse_button::state b = mouse_button::none;
91 b = mouse_button::button1;
94 b = mouse_button::button2;
97 b = mouse_button::button3;
106 /// return the LyX mouse button state from Qt's
107 mouse_button::state q_motion_state(Qt::MouseButtons state)
109 mouse_button::state b = mouse_button::none;
110 if (state & Qt::LeftButton)
111 b |= mouse_button::button1;
112 if (state & Qt::MidButton)
113 b |= mouse_button::button2;
114 if (state & Qt::RightButton)
115 b |= mouse_button::button3;
126 void draw(QPainter & painter)
128 if (show_ && rect_.isValid()) {
131 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
132 painter.setPen(color_);
133 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
134 rect_.bottomRight().x(), rect_.bottomLeft().y());
137 case REVERSED_L_SHAPE:
138 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
139 painter.setPen(color_);
140 painter.drawLine(rect_.bottomRight().x() - CursorWidth, 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),
200 buffer.workAreaManager().add(this);
202 cursor_timeout_.setInterval(400);
203 connect(&cursor_timeout_, SIGNAL(timeout()),
204 this, SLOT(toggleCursor()));
206 cursor_timeout_.start();
208 screen_ = QPixmap(viewport()->width(), viewport()->height());
209 cursor_ = new frontend::CursorWidget();
212 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
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(actionTriggered(int)),
234 this, SLOT(adjustViewWithScrollBar(int)));
236 // disable context menu for the scrollbar
237 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
239 // PageStep only depends on the viewport height.
240 verticalScrollBar()->setPageStep(viewport()->height());
242 LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION
243 << "\n viewport width\t" << viewport()->width()
244 << "\n viewport height\t" << viewport()->height());
246 // Enables input methods for asian languages.
247 // Must be set when creating custom text editing widgets.
248 setAttribute(Qt::WA_InputMethodEnabled, true);
253 GuiWorkArea::~GuiWorkArea()
255 buffer_view_->buffer().workAreaManager().remove(this);
260 void GuiWorkArea::close()
262 lyx_view_->removeWorkArea(this);
266 BufferView & GuiWorkArea::bufferView()
268 return *buffer_view_;
272 BufferView const & GuiWorkArea::bufferView() const
274 return *buffer_view_;
278 void GuiWorkArea::stopBlinkingCursor()
280 cursor_timeout_.stop();
285 void GuiWorkArea::startBlinkingCursor()
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();
308 // update cursor position, because otherwise it has to wait until
309 // the blinking interval is over
310 if (cursor_visible_) {
315 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
317 update(0, 0, viewport()->width(), viewport()->height());
319 lyx_view_->updateStatusBar();
321 if (lyxerr.debugging(Debug::WORKAREA))
322 buffer_view_->coordCache().dump();
326 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
328 // In order to avoid bad surprise in the middle of an operation,
329 // we better stop the blinking cursor.
330 stopBlinkingCursor();
332 theLyXFunc().setLyXView(lyx_view_);
333 theLyXFunc().processKeySym(key, mod);
337 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
340 if (cmd0.action == LFUN_FILE_OPEN) {
341 lyx_view_->dispatch(cmd0);
345 theLyXFunc().setLyXView(lyx_view_);
349 if (cmd0.action == LFUN_MOUSE_PRESS) {
350 if (mod == ShiftModifier)
351 cmd = FuncRequest(cmd0, "region-select");
352 else if (mod == ControlModifier)
353 cmd = FuncRequest(cmd0, "paragraph-select");
360 // In order to avoid bad surprise in the middle of an operation, we better stop
361 // the blinking cursor.
362 if (!(cmd.action == LFUN_MOUSE_MOTION
363 && cmd.button() == mouse_button::none))
364 stopBlinkingCursor();
366 buffer_view_->mouseEventDispatch(cmd);
368 // Skip these when selecting
369 if (cmd.action != LFUN_MOUSE_MOTION) {
370 lyx_view_->updateLayoutList();
371 lyx_view_->updateToolbars();
374 // GUI tweaks except with mouse motion with no button pressed.
375 if (!(cmd.action == LFUN_MOUSE_MOTION
376 && cmd.button() == mouse_button::none)) {
377 // Slight hack: this is only called currently when we
378 // clicked somewhere, so we force through the display
379 // of the new status here.
380 lyx_view_->clearMessage();
382 // Show the cursor immediately after any operation.
383 startBlinkingCursor();
388 void GuiWorkArea::resizeBufferView()
390 // WARNING: Please don't put any code that will trigger a repaint here!
391 // We are already inside a paint event.
392 lyx_view_->setBusy(true);
393 buffer_view_->resize(viewport()->width(), viewport()->height());
394 lyx_view_->updateLayoutList();
395 lyx_view_->setBusy(false);
396 need_resize_ = false;
400 void GuiWorkArea::showCursor()
405 CursorShape shape = BAR_SHAPE;
407 Font const & realfont = buffer_view_->cursor().real_current_font;
408 BufferParams const & bp = buffer_view_->buffer().params();
409 bool const samelang = realfont.language() == bp.language;
410 bool const isrtl = realfont.isVisibleRightToLeft();
412 if (!samelang || isrtl != bp.language->rightToLeft()) {
415 shape = REVERSED_L_SHAPE;
418 // The ERT language hack needs fixing up
419 if (realfont.language() == latex_language)
422 Font const font = buffer_view_->cursor().getFont();
423 FontMetrics const & fm = theFontMetrics(font);
424 int const asc = fm.maxAscent();
425 int const des = fm.maxDescent();
429 buffer_view_->cursor().getPos(x, y);
432 // if it doesn't touch the screen, don't try to show it
433 if (y + h < 0 || y >= viewport()->height())
436 cursor_visible_ = true;
437 showCursor(x, y, h, shape);
441 void GuiWorkArea::hideCursor()
443 if (!cursor_visible_)
446 cursor_visible_ = false;
451 void GuiWorkArea::toggleCursor()
458 // Use this opportunity to deal with any child processes that
459 // have finished but are waiting to communicate this fact
460 // to the rest of LyX.
461 ForkedcallsController & fcc = ForkedcallsController::get();
462 fcc.handleCompletedProcesses();
466 void GuiWorkArea::updateScrollbar()
468 verticalScrollBar()->setTracking(false);
470 buffer_view_->updateScrollbar();
471 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
473 // do what cursor movement does (some grey)
474 int const h = scroll_.height + viewport()->height() / 4;
475 int scroll_max_ = std::max(0, h - viewport()->height());
477 verticalScrollBar()->setRange(0, scroll_max_);
478 verticalScrollBar()->setSliderPosition(scroll_.position);
479 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
480 verticalScrollBar()->setValue(scroll_.position);
482 verticalScrollBar()->setTracking(true);
486 void GuiWorkArea::adjustViewWithScrollBar(int action)
488 stopBlinkingCursor();
489 if (action == QAbstractSlider::SliderPageStepAdd)
490 buffer_view_->scrollDown(viewport()->height());
491 else if (action == QAbstractSlider::SliderPageStepSub)
492 buffer_view_->scrollUp(viewport()->height());
494 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
496 if (lyxrc.cursor_follows_scrollbar) {
497 buffer_view_->setCursorFromScrollbar();
498 lyx_view_->updateLayoutList();
500 // Show the cursor immediately after any operation.
501 startBlinkingCursor();
502 QApplication::syncX();
506 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
508 // Repaint the whole screen.
509 // Note: this is different from redraw() as only the backing pixmap
510 // will be redrawn, which is cheap.
511 viewport()->repaint();
513 startBlinkingCursor();
517 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
519 stopBlinkingCursor();
523 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
525 if (dc_event_.active && dc_event_ == *e) {
526 dc_event_.active = false;
527 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
528 q_button_state(e->button()));
533 inputContext()->reset();
535 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
536 q_button_state(e->button()));
537 dispatch(cmd, q_key_state(e->modifiers()));
541 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
543 if (synthetic_mouse_event_.timeout.running())
544 synthetic_mouse_event_.timeout.stop();
546 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
547 q_button_state(e->button()));
552 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
554 // we kill the triple click if we move
555 doubleClickTimeout();
556 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
557 q_motion_state(e->buttons()));
559 // If we're above or below the work area...
560 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
561 // Make sure only a synthetic event can cause a page scroll,
562 // so they come at a steady rate:
564 // _Force_ a scroll up:
567 cmd.y = viewport()->height();
568 // Store the event, to be handled when the timeout expires.
569 synthetic_mouse_event_.cmd = cmd;
571 if (synthetic_mouse_event_.timeout.running())
572 // Discard the event. Note that it _may_ be handled
573 // when the timeout expires if
574 // synthetic_mouse_event_.cmd has not been overwritten.
575 // Ie, when the timeout expires, we handle the
576 // most recent event but discard all others that
577 // occurred after the one used to start the timeout
578 // in the first place.
581 synthetic_mouse_event_.restart_timeout = true;
582 synthetic_mouse_event_.timeout.start();
583 // Fall through to handle this event...
586 } else if (synthetic_mouse_event_.timeout.running()) {
587 // Store the event, to be possibly handled when the timeout
589 // Once the timeout has expired, normal control is returned
590 // to mouseMoveEvent (restart_timeout = false).
591 // This results in a much smoother 'feel' when moving the
592 // mouse back into the work area.
593 synthetic_mouse_event_.cmd = cmd;
594 synthetic_mouse_event_.restart_timeout = false;
598 // Has anything changed on-screen since the last QMouseEvent
600 double const scrollbar_value = verticalScrollBar()->value();
601 if (e->x() != synthetic_mouse_event_.x_old ||
602 e->y() != synthetic_mouse_event_.y_old ||
603 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
604 // Yes it has. Store the params used to check this.
605 synthetic_mouse_event_.x_old = e->x();
606 synthetic_mouse_event_.y_old = e->y();
607 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
609 // ... and dispatch the event to the LyX core.
615 void GuiWorkArea::wheelEvent(QWheelEvent * e)
617 // Wheel rotation by one notch results in a delta() of 120 (see
618 // documentation of QWheelEvent)
619 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
620 verticalScrollBar()->setValue(verticalScrollBar()->value() -
621 lines * verticalScrollBar()->singleStep());
622 adjustViewWithScrollBar();
626 void GuiWorkArea::generateSyntheticMouseEvent()
628 // Set things off to generate the _next_ 'pseudo' event.
629 if (synthetic_mouse_event_.restart_timeout)
630 synthetic_mouse_event_.timeout.start();
632 // Has anything changed on-screen since the last timeout signal
634 double const scrollbar_value = verticalScrollBar()->value();
635 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
636 // Yes it has. Store the params used to check this.
637 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
639 // ... and dispatch the event to the LyX core.
640 dispatch(synthetic_mouse_event_.cmd);
645 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
647 // do nothing if there are other events
648 // (the auto repeated events come too fast)
649 // \todo FIXME: remove hard coded Qt keys, process the key binding
651 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
652 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
653 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
654 << "\nsystem is busy: scroll key event ignored");
660 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
661 << " count=" << ev->count()
662 << " text=" << fromqstr(ev->text())
663 << " isAutoRepeat=" << ev->isAutoRepeat()
664 << " key=" << ev->key());
667 setKeySymbol(&sym, ev);
668 processKeySym(sym, q_key_state(ev->modifiers()));
672 void GuiWorkArea::doubleClickTimeout()
674 dc_event_.active = false;
678 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
680 dc_event_ = DoubleClick(ev);
681 QTimer::singleShot(QApplication::doubleClickInterval(), this,
682 SLOT(doubleClickTimeout()));
683 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
685 q_button_state(ev->button()));
690 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
692 QAbstractScrollArea::resizeEvent(ev);
697 void GuiWorkArea::update(int x, int y, int w, int h)
699 viewport()->repaint(x, y, w, h);
703 void GuiWorkArea::paintEvent(QPaintEvent * ev)
705 QRect const rc = ev->rect();
707 LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
709 << " w: " << rc.width()
710 << " h: " << rc.height() << endl;
714 verticalScrollBar()->setPageStep(viewport()->height());
715 screen_ = QPixmap(viewport()->width(), viewport()->height());
722 QPainter pain(viewport());
723 pain.drawPixmap(rc, screen_, rc);
728 void GuiWorkArea::updateScreen()
730 GuiPainter pain(&screen_);
731 buffer_view_->draw(pain);
735 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
737 if (schedule_redraw_) {
738 buffer_view_->updateMetrics();
740 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
741 schedule_redraw_ = false;
742 // Show the cursor immediately after the update.
748 cursor_->update(x, y, h, shape);
750 viewport()->update(cursor_->rect());
754 void GuiWorkArea::removeCursor()
757 //if (!qApp->focusWidget())
758 viewport()->update(cursor_->rect());
762 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
764 QString const & commit_string = e->commitString();
765 docstring const & preedit_string
766 = qstring_to_ucs4(e->preeditString());
768 if (!commit_string.isEmpty()) {
770 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
771 << " preeditString =" << fromqstr(e->preeditString())
772 << " commitString =" << fromqstr(e->commitString()));
776 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
777 for (int i = 0; i < commit_string.size(); ++i) {
778 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
783 // Hide the cursor during the kana-kanji transformation.
784 if (preedit_string.empty())
785 startBlinkingCursor();
787 stopBlinkingCursor();
789 // last_width : for checking if last preedit string was/wasn't empty.
790 static bool last_width = false;
791 if (!last_width && preedit_string.empty()) {
792 // if last_width is last length of preedit string.
797 GuiPainter pain(&screen_);
798 buffer_view_->updateMetrics();
799 buffer_view_->draw(pain);
800 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
801 FontMetrics const & fm = theFontMetrics(font);
802 int height = fm.maxHeight();
803 int cur_x = cursor_->rect().left();
804 int cur_y = cursor_->rect().bottom();
806 // redraw area of preedit string.
807 update(0, cur_y - height, viewport()->width(),
808 (height + 1) * preedit_lines_);
810 if (preedit_string.empty()) {
818 // att : stores an IM attribute.
819 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
821 // get attributes of input method cursor.
822 // cursor_pos : cursor position in preedit string.
823 size_t cursor_pos = 0;
824 bool cursor_is_visible = false;
825 for (int i = 0; i < att.size(); ++i) {
826 if (att.at(i).type == QInputMethodEvent::Cursor) {
827 cursor_pos = att.at(i).start;
828 cursor_is_visible = att.at(i).length != 0;
833 size_t preedit_length = preedit_string.length();
835 // get position of selection in input method.
836 // FIXME: isn't there a way to do this simplier?
837 // rStart : cursor position in selected string in IM.
839 // rLength : selected string length in IM.
841 if (cursor_pos < preedit_length) {
842 for (int i = 0; i < att.size(); ++i) {
843 if (att.at(i).type == QInputMethodEvent::TextFormat) {
844 if (att.at(i).start <= int(cursor_pos)
845 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
846 rStart = att.at(i).start;
847 rLength = att.at(i).length;
848 if (!cursor_is_visible)
849 cursor_pos += rLength;
860 int const right_margin = rightMargin();
861 Painter::preedit_style ps;
862 // Most often there would be only one line:
864 for (size_t pos = 0; pos != preedit_length; ++pos) {
865 char_type const typed_char = preedit_string[pos];
866 // reset preedit string style
867 ps = Painter::preedit_default;
869 // if we reached the right extremity of the screen, go to next line.
870 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
871 cur_x = right_margin;
875 // preedit strings are displayed with dashed underline
876 // and partial strings are displayed white on black indicating
877 // that we are in selecting mode in the input method.
878 // FIXME: rLength == preedit_length is not a changing condition
879 // FIXME: should be put out of the loop.
881 && pos < rStart + rLength
882 && !(cursor_pos < rLength && rLength == preedit_length))
883 ps = Painter::preedit_selecting;
885 if (pos == cursor_pos
886 && (cursor_pos < rLength && rLength == preedit_length))
887 ps = Painter::preedit_cursor;
889 // draw one character and update cur_x.
890 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
893 // update the preedit string screen area.
894 update(0, cur_y - preedit_lines_*height, viewport()->width(),
895 (height + 1) * preedit_lines_);
897 // Don't forget to accept the event!
902 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
904 QRect cur_r(0,0,0,0);
906 // this is the CJK-specific composition window position.
907 case Qt::ImMicroFocus:
908 cur_r = cursor_->rect();
909 if (preedit_lines_ != 1)
911 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
912 // return lower right of cursor in LyX.
915 return QWidget::inputMethodQuery(query);
920 void GuiWorkArea::updateWindowTitle()
922 docstring maximize_title;
923 docstring minimize_title;
925 Buffer & buf = buffer_view_->buffer();
926 FileName const fileName = buf.fileName();
927 if (!fileName.empty()) {
928 maximize_title = fileName.displayName(30);
929 minimize_title = from_utf8(fileName.onlyFileName());
930 if (!buf.isClean()) {
931 maximize_title += _(" (changed)");
932 minimize_title += char_type('*');
934 if (buf.isReadonly())
935 maximize_title += _(" (read only)");
938 QString title = windowTitle();
939 QString new_title = toqstr(maximize_title);
940 if (title == new_title)
943 QWidget::setWindowTitle(new_title);
944 QWidget::setWindowIconText(toqstr(minimize_title));
949 void GuiWorkArea::setReadOnly(bool)
952 if (this == lyx_view_->currentWorkArea())
953 lyx_view_->updateBufferDependent(false);
957 ////////////////////////////////////////////////////////////////////
961 ////////////////////////////////////////////////////////////////////
963 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
965 QPalette pal = palette();
966 pal.setColor(QPalette::Active, QPalette::Button,
967 pal.color(QPalette::Active, QPalette::Window));
968 pal.setColor(QPalette::Disabled, QPalette::Button,
969 pal.color(QPalette::Disabled, QPalette::Window));
970 pal.setColor(QPalette::Inactive, QPalette::Button,
971 pal.color(QPalette::Inactive, QPalette::Window));
973 QToolButton * closeTabButton = new QToolButton(this);
974 closeTabButton->setPalette(pal);
975 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
976 closeTabButton->setText("Close");
977 closeTabButton->setAutoRaise(true);
978 closeTabButton->setCursor(Qt::ArrowCursor);
979 closeTabButton->setToolTip(tr("Close tab"));
980 closeTabButton->setEnabled(true);
982 QObject::connect(this, SIGNAL(currentChanged(int)),
983 this, SLOT(on_currentTabChanged(int)));
984 QObject::connect(closeTabButton, SIGNAL(clicked()),
985 this, SLOT(closeCurrentTab()));
987 setCornerWidget(closeTabButton);
988 setUsesScrollButtons(true);
992 void TabWorkArea::showBar(bool show)
994 tabBar()->setEnabled(show);
995 tabBar()->setVisible(show);
999 GuiWorkArea * TabWorkArea::currentWorkArea()
1004 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1010 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1012 for (int i = 0; i != count(); ++i) {
1013 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1015 if (&wa->bufferView().buffer() == &buffer)
1022 void TabWorkArea::closeAll()
1025 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1033 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1035 BOOST_ASSERT(work_area);
1036 int index = indexOf(work_area);
1040 if (index == currentIndex())
1041 // Make sure the work area is up to date.
1042 on_currentTabChanged(index);
1044 // Switch to the work area.
1045 setCurrentIndex(index);
1046 work_area->setFocus();
1052 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1054 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1055 wa->setUpdatesEnabled(false);
1056 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1057 // when hiding it again below).
1058 showBar(count() > 0);
1059 addTab(wa, wa->windowTitle());
1060 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1061 this, SLOT(updateTabText(GuiWorkArea *)));
1062 // Hide tabbar if there's only one tab.
1063 showBar(count() > 1);
1068 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1070 BOOST_ASSERT(work_area);
1071 int index = indexOf(work_area);
1075 work_area->setUpdatesEnabled(false);
1080 // make sure the next work area is enabled.
1081 currentWidget()->setUpdatesEnabled(true);
1082 // Hide tabbar if there's only one tab.
1083 showBar(count() > 1);
1089 void TabWorkArea::on_currentTabChanged(int i)
1091 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1093 BufferView & bv = wa->bufferView();
1094 bv.cursor().fixIfBroken();
1096 wa->setUpdatesEnabled(true);
1100 currentWorkAreaChanged(wa);
1102 LYXERR(Debug::GUI, "currentTabChanged " << i
1103 << "File" << bv.buffer().absFileName());
1107 void TabWorkArea::closeCurrentTab()
1109 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1113 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1115 int const i = indexOf(wa);
1118 setTabText(i, wa->windowTitle());
1121 } // namespace frontend
1124 #include "GuiWorkArea_moc.cpp"