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"
27 #include "KeySymbol.h"
31 #include "MetricsInfo.h"
32 #include "qt_helpers.h"
35 #include "graphics/GraphicsImage.h"
36 #include "graphics/GraphicsLoader.h"
38 #include "support/debug.h"
39 #include "support/gettext.h"
40 #include "support/FileName.h"
41 #include "support/ForkedCalls.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>
61 extern "C" int XEventsQueued(Display *display, int mode);
65 int const CursorWidth = 2;
67 int const CursorWidth = 1;
74 using namespace lyx::support;
79 /// return the LyX mouse button state from Qt's
80 static mouse_button::state q_button_state(Qt::MouseButton button)
82 mouse_button::state b = mouse_button::none;
85 b = mouse_button::button1;
88 b = mouse_button::button2;
91 b = mouse_button::button3;
100 /// return the LyX mouse button state from Qt's
101 mouse_button::state q_motion_state(Qt::MouseButtons state)
103 mouse_button::state b = mouse_button::none;
104 if (state & Qt::LeftButton)
105 b |= mouse_button::button1;
106 if (state & Qt::MidButton)
107 b |= mouse_button::button2;
108 if (state & Qt::RightButton)
109 b |= mouse_button::button3;
120 void draw(QPainter & painter)
122 if (show_ && rect_.isValid()) {
125 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
126 painter.setPen(color_);
127 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
128 rect_.bottomRight().x(), rect_.bottomLeft().y());
131 case REVERSED_L_SHAPE:
132 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
133 painter.setPen(color_);
134 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
135 rect_.bottomLeft().x(), rect_.bottomLeft().y());
139 painter.fillRect(rect_, color_);
145 void update(int x, int y, int h, CursorShape shape)
147 color_ = guiApp->colorCache().get(Color_cursor);
151 rect_ = QRect(x, y, CursorWidth + h / 3, h);
153 case REVERSED_L_SHAPE:
154 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
157 rect_ = QRect(x, y, CursorWidth, h);
162 void show(bool set_show = true) { show_ = set_show; }
163 void hide() { show_ = false; }
165 QRect const & rect() { return rect_; }
179 // This is a 'heartbeat' generating synthetic mouse move events when the
180 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
181 SyntheticMouseEvent::SyntheticMouseEvent()
182 : timeout(200), restart_timeout(true),
183 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
188 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
189 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
190 cursor_visible_(false),
191 need_resize_(false), schedule_redraw_(false),
194 buffer.workAreaManager().add(this);
196 cursor_timeout_.setInterval(400);
197 connect(&cursor_timeout_, SIGNAL(timeout()),
198 this, SLOT(toggleCursor()));
200 cursor_timeout_.start();
202 screen_ = QPixmap(viewport()->width(), viewport()->height());
203 cursor_ = new frontend::CursorWidget();
206 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
207 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
208 setAcceptDrops(true);
209 setMouseTracking(true);
210 setMinimumSize(100, 70);
213 viewport()->setAutoFillBackground(false);
214 // We don't need double-buffering nor SystemBackground on
215 // the viewport because we have our own backing pixmap.
216 viewport()->setAttribute(Qt::WA_NoSystemBackground);
218 setFocusPolicy(Qt::WheelFocus);
220 viewport()->setCursor(Qt::IBeamCursor);
222 synthetic_mouse_event_.timeout.timeout.connect(
223 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
226 // Initialize the vertical Scroll Bar
227 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
228 this, SLOT(adjustViewWithScrollBar(int)));
230 // disable context menu for the scrollbar
231 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
233 // PageStep only depends on the viewport height.
234 verticalScrollBar()->setPageStep(viewport()->height());
236 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
237 << " viewport height: " << viewport()->height());
239 // Enables input methods for asian languages.
240 // Must be set when creating custom text editing widgets.
241 setAttribute(Qt::WA_InputMethodEnabled, true);
246 GuiWorkArea::~GuiWorkArea()
248 buffer_view_->buffer().workAreaManager().remove(this);
254 void GuiWorkArea::close()
256 lyx_view_->removeWorkArea(this);
260 BufferView & GuiWorkArea::bufferView()
262 return *buffer_view_;
266 BufferView const & GuiWorkArea::bufferView() const
268 return *buffer_view_;
272 void GuiWorkArea::stopBlinkingCursor()
274 cursor_timeout_.stop();
279 void GuiWorkArea::startBlinkingCursor()
282 cursor_timeout_.start();
286 void GuiWorkArea::redraw()
289 // No need to redraw in this case.
292 // No need to do anything if this is the current view. The BufferView
293 // metrics are already up to date.
294 if (lyx_view_ != guiApp->currentView()) {
295 // FIXME: it would be nice to optimize for the off-screen case.
296 buffer_view_->updateMetrics();
297 buffer_view_->cursor().fixIfBroken();
300 // update cursor position, because otherwise it has to wait until
301 // the blinking interval is over
302 if (cursor_visible_) {
307 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
309 update(0, 0, viewport()->width(), viewport()->height());
311 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
312 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
314 lyx_view_->updateStatusBar();
316 if (lyxerr.debugging(Debug::WORKAREA))
317 buffer_view_->coordCache().dump();
321 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
323 // In order to avoid bad surprise in the middle of an operation,
324 // we better stop the blinking cursor.
325 stopBlinkingCursor();
327 theLyXFunc().setLyXView(lyx_view_);
328 theLyXFunc().processKeySym(key, mod);
332 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
335 if (cmd0.action == LFUN_FILE_OPEN) {
336 lyx_view_->dispatch(cmd0);
340 theLyXFunc().setLyXView(lyx_view_);
344 if (cmd0.action == LFUN_MOUSE_PRESS) {
345 if (mod == ShiftModifier)
346 cmd = FuncRequest(cmd0, "region-select");
347 else if (mod == ControlModifier)
348 cmd = FuncRequest(cmd0, "paragraph-select");
355 // In order to avoid bad surprise in the middle of an operation, we better stop
356 // the blinking cursor.
357 if (!(cmd.action == LFUN_MOUSE_MOTION
358 && cmd.button() == mouse_button::none))
359 stopBlinkingCursor();
361 buffer_view_->mouseEventDispatch(cmd);
363 // Skip these when selecting
364 if (cmd.action != LFUN_MOUSE_MOTION) {
365 lyx_view_->updateLayoutList();
366 lyx_view_->updateToolbars();
369 // GUI tweaks except with mouse motion with no button pressed.
370 if (!(cmd.action == LFUN_MOUSE_MOTION
371 && cmd.button() == mouse_button::none)) {
372 // Slight hack: this is only called currently when we
373 // clicked somewhere, so we force through the display
374 // of the new status here.
375 lyx_view_->clearMessage();
377 // Show the cursor immediately after any operation.
378 startBlinkingCursor();
383 void GuiWorkArea::resizeBufferView()
385 // WARNING: Please don't put any code that will trigger a repaint here!
386 // We are already inside a paint event.
387 lyx_view_->setBusy(true);
388 buffer_view_->resize(viewport()->width(), viewport()->height());
389 lyx_view_->updateLayoutList();
390 lyx_view_->setBusy(false);
391 need_resize_ = false;
395 void GuiWorkArea::showCursor()
400 CursorShape shape = BAR_SHAPE;
402 Font const & realfont = buffer_view_->cursor().real_current_font;
403 BufferParams const & bp = buffer_view_->buffer().params();
404 bool const samelang = realfont.language() == bp.language;
405 bool const isrtl = realfont.isVisibleRightToLeft();
407 if (!samelang || isrtl != bp.language->rightToLeft()) {
410 shape = REVERSED_L_SHAPE;
413 // The ERT language hack needs fixing up
414 if (realfont.language() == latex_language)
417 Font const font = buffer_view_->cursor().getFont();
418 FontMetrics const & fm = theFontMetrics(font);
419 int const asc = fm.maxAscent();
420 int const des = fm.maxDescent();
424 buffer_view_->cursor().getPos(x, y);
427 // if it doesn't touch the screen, don't try to show it
428 if (y + h < 0 || y >= viewport()->height())
431 cursor_visible_ = true;
432 showCursor(x, y, h, shape);
436 void GuiWorkArea::hideCursor()
438 if (!cursor_visible_)
441 cursor_visible_ = false;
446 void GuiWorkArea::toggleCursor()
453 // Use this opportunity to deal with any child processes that
454 // have finished but are waiting to communicate this fact
455 // to the rest of LyX.
456 ForkedCallsController::handleCompletedProcesses();
460 void GuiWorkArea::updateScrollbar()
462 verticalScrollBar()->setTracking(false);
464 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
466 // do what cursor movement does (some grey)
467 int const h = scroll_.height + viewport()->height() / 4;
468 int scroll_max_ = max(0, h - viewport()->height());
470 verticalScrollBar()->setRange(0, scroll_max_);
471 verticalScrollBar()->setSliderPosition(scroll_.position);
472 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
473 verticalScrollBar()->setValue(scroll_.position);
475 verticalScrollBar()->setTracking(true);
479 void GuiWorkArea::adjustViewWithScrollBar(int action)
481 stopBlinkingCursor();
482 if (action == QAbstractSlider::SliderPageStepAdd)
483 buffer_view_->scrollDown(viewport()->height());
484 else if (action == QAbstractSlider::SliderPageStepSub)
485 buffer_view_->scrollUp(viewport()->height());
487 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
489 if (lyxrc.cursor_follows_scrollbar) {
490 buffer_view_->setCursorFromScrollbar();
491 lyx_view_->updateLayoutList();
493 // Show the cursor immediately after any operation.
494 startBlinkingCursor();
495 QApplication::syncX();
499 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
501 // Repaint the whole screen.
502 // Note: this is different from redraw() as only the backing pixmap
503 // will be redrawn, which is cheap.
504 viewport()->repaint();
506 startBlinkingCursor();
510 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
512 stopBlinkingCursor();
516 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
518 if (dc_event_.active && dc_event_ == *e) {
519 dc_event_.active = false;
520 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
521 q_button_state(e->button()));
526 inputContext()->reset();
528 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
529 q_button_state(e->button()));
530 dispatch(cmd, q_key_state(e->modifiers()));
534 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
536 if (synthetic_mouse_event_.timeout.running())
537 synthetic_mouse_event_.timeout.stop();
539 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
540 q_button_state(e->button()));
545 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
547 // we kill the triple click if we move
548 doubleClickTimeout();
549 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
550 q_motion_state(e->buttons()));
552 // If we're above or below the work area...
553 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
554 // Make sure only a synthetic event can cause a page scroll,
555 // so they come at a steady rate:
557 // _Force_ a scroll up:
560 cmd.y = viewport()->height();
561 // Store the event, to be handled when the timeout expires.
562 synthetic_mouse_event_.cmd = cmd;
564 if (synthetic_mouse_event_.timeout.running())
565 // Discard the event. Note that it _may_ be handled
566 // when the timeout expires if
567 // synthetic_mouse_event_.cmd has not been overwritten.
568 // Ie, when the timeout expires, we handle the
569 // most recent event but discard all others that
570 // occurred after the one used to start the timeout
571 // in the first place.
574 synthetic_mouse_event_.restart_timeout = true;
575 synthetic_mouse_event_.timeout.start();
576 // Fall through to handle this event...
579 } else if (synthetic_mouse_event_.timeout.running()) {
580 // Store the event, to be possibly handled when the timeout
582 // Once the timeout has expired, normal control is returned
583 // to mouseMoveEvent (restart_timeout = false).
584 // This results in a much smoother 'feel' when moving the
585 // mouse back into the work area.
586 synthetic_mouse_event_.cmd = cmd;
587 synthetic_mouse_event_.restart_timeout = false;
591 // Has anything changed on-screen since the last QMouseEvent
593 double const scrollbar_value = verticalScrollBar()->value();
594 if (e->x() != synthetic_mouse_event_.x_old ||
595 e->y() != synthetic_mouse_event_.y_old ||
596 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
597 // Yes it has. Store the params used to check this.
598 synthetic_mouse_event_.x_old = e->x();
599 synthetic_mouse_event_.y_old = e->y();
600 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
602 // ... and dispatch the event to the LyX core.
608 void GuiWorkArea::wheelEvent(QWheelEvent * e)
610 // Wheel rotation by one notch results in a delta() of 120 (see
611 // documentation of QWheelEvent)
612 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
613 verticalScrollBar()->setValue(verticalScrollBar()->value() -
614 lines * verticalScrollBar()->singleStep());
615 adjustViewWithScrollBar();
619 void GuiWorkArea::generateSyntheticMouseEvent()
621 // Set things off to generate the _next_ 'pseudo' event.
622 if (synthetic_mouse_event_.restart_timeout)
623 synthetic_mouse_event_.timeout.start();
625 // Has anything changed on-screen since the last timeout signal
627 double const scrollbar_value = verticalScrollBar()->value();
628 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
629 // Yes it has. Store the params used to check this.
630 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
632 // ... and dispatch the event to the LyX core.
633 dispatch(synthetic_mouse_event_.cmd);
638 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
640 // do nothing if there are other events
641 // (the auto repeated events come too fast)
642 // \todo FIXME: remove hard coded Qt keys, process the key binding
644 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
645 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
646 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
652 LYXERR(Debug::KEY, " count: " << ev->count()
653 << " text: " << fromqstr(ev->text())
654 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
657 setKeySymbol(&sym, ev);
658 processKeySym(sym, q_key_state(ev->modifiers()));
662 void GuiWorkArea::doubleClickTimeout()
664 dc_event_.active = false;
668 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
670 dc_event_ = DoubleClick(ev);
671 QTimer::singleShot(QApplication::doubleClickInterval(), this,
672 SLOT(doubleClickTimeout()));
673 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
675 q_button_state(ev->button()));
680 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
682 QAbstractScrollArea::resizeEvent(ev);
687 void GuiWorkArea::update(int x, int y, int w, int h)
689 viewport()->repaint(x, y, w, h);
693 void GuiWorkArea::paintEvent(QPaintEvent * ev)
695 QRect const rc = ev->rect();
696 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
697 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
700 verticalScrollBar()->setPageStep(viewport()->height());
701 screen_ = QPixmap(viewport()->width(), viewport()->height());
708 QPainter pain(viewport());
709 pain.drawPixmap(rc, screen_, rc);
714 void GuiWorkArea::updateScreen()
716 GuiPainter pain(&screen_);
717 buffer_view_->draw(pain);
721 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
723 if (schedule_redraw_) {
724 buffer_view_->updateMetrics();
726 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
727 schedule_redraw_ = false;
728 // Show the cursor immediately after the update.
734 cursor_->update(x, y, h, shape);
736 viewport()->update(cursor_->rect());
740 void GuiWorkArea::removeCursor()
743 //if (!qApp->focusWidget())
744 viewport()->update(cursor_->rect());
748 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
750 QString const & commit_string = e->commitString();
751 docstring const & preedit_string
752 = qstring_to_ucs4(e->preeditString());
754 if (!commit_string.isEmpty()) {
756 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
757 << " commitString: " << fromqstr(e->commitString()));
761 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
762 for (int i = 0; i != commit_string.size(); ++i) {
763 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
768 // Hide the cursor during the kana-kanji transformation.
769 if (preedit_string.empty())
770 startBlinkingCursor();
772 stopBlinkingCursor();
774 // last_width : for checking if last preedit string was/wasn't empty.
775 static bool last_width = false;
776 if (!last_width && preedit_string.empty()) {
777 // if last_width is last length of preedit string.
782 GuiPainter pain(&screen_);
783 buffer_view_->updateMetrics();
784 buffer_view_->draw(pain);
785 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
786 FontMetrics const & fm = theFontMetrics(font);
787 int height = fm.maxHeight();
788 int cur_x = cursor_->rect().left();
789 int cur_y = cursor_->rect().bottom();
791 // redraw area of preedit string.
792 update(0, cur_y - height, viewport()->width(),
793 (height + 1) * preedit_lines_);
795 if (preedit_string.empty()) {
803 // att : stores an IM attribute.
804 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
806 // get attributes of input method cursor.
807 // cursor_pos : cursor position in preedit string.
808 size_t cursor_pos = 0;
809 bool cursor_is_visible = false;
810 for (int i = 0; i != att.size(); ++i) {
811 if (att.at(i).type == QInputMethodEvent::Cursor) {
812 cursor_pos = att.at(i).start;
813 cursor_is_visible = att.at(i).length != 0;
818 size_t preedit_length = preedit_string.length();
820 // get position of selection in input method.
821 // FIXME: isn't there a way to do this simplier?
822 // rStart : cursor position in selected string in IM.
824 // rLength : selected string length in IM.
826 if (cursor_pos < preedit_length) {
827 for (int i = 0; i != att.size(); ++i) {
828 if (att.at(i).type == QInputMethodEvent::TextFormat) {
829 if (att.at(i).start <= int(cursor_pos)
830 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
831 rStart = att.at(i).start;
832 rLength = att.at(i).length;
833 if (!cursor_is_visible)
834 cursor_pos += rLength;
845 int const right_margin = rightMargin();
846 Painter::preedit_style ps;
847 // Most often there would be only one line:
849 for (size_t pos = 0; pos != preedit_length; ++pos) {
850 char_type const typed_char = preedit_string[pos];
851 // reset preedit string style
852 ps = Painter::preedit_default;
854 // if we reached the right extremity of the screen, go to next line.
855 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
856 cur_x = right_margin;
860 // preedit strings are displayed with dashed underline
861 // and partial strings are displayed white on black indicating
862 // that we are in selecting mode in the input method.
863 // FIXME: rLength == preedit_length is not a changing condition
864 // FIXME: should be put out of the loop.
866 && pos < rStart + rLength
867 && !(cursor_pos < rLength && rLength == preedit_length))
868 ps = Painter::preedit_selecting;
870 if (pos == cursor_pos
871 && (cursor_pos < rLength && rLength == preedit_length))
872 ps = Painter::preedit_cursor;
874 // draw one character and update cur_x.
875 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
878 // update the preedit string screen area.
879 update(0, cur_y - preedit_lines_*height, viewport()->width(),
880 (height + 1) * preedit_lines_);
882 // Don't forget to accept the event!
887 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
889 QRect cur_r(0,0,0,0);
891 // this is the CJK-specific composition window position.
892 case Qt::ImMicroFocus:
893 cur_r = cursor_->rect();
894 if (preedit_lines_ != 1)
896 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
897 // return lower right of cursor in LyX.
900 return QWidget::inputMethodQuery(query);
905 void GuiWorkArea::updateWindowTitle()
907 docstring maximize_title;
908 docstring minimize_title;
910 Buffer & buf = buffer_view_->buffer();
911 FileName const fileName = buf.fileName();
912 if (!fileName.empty()) {
913 maximize_title = fileName.displayName(30);
914 minimize_title = from_utf8(fileName.onlyFileName());
915 if (!buf.isClean()) {
916 maximize_title += _(" (changed)");
917 minimize_title += char_type('*');
919 if (buf.isReadonly())
920 maximize_title += _(" (read only)");
923 QString title = windowTitle();
924 QString new_title = toqstr(maximize_title);
925 if (title == new_title)
928 QWidget::setWindowTitle(new_title);
929 QWidget::setWindowIconText(toqstr(minimize_title));
934 void GuiWorkArea::setReadOnly(bool)
937 if (this == lyx_view_->currentWorkArea())
938 lyx_view_->updateBufferDependent(false);
942 ////////////////////////////////////////////////////////////////////
946 ////////////////////////////////////////////////////////////////////
948 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
950 QPalette pal = palette();
951 pal.setColor(QPalette::Active, QPalette::Button,
952 pal.color(QPalette::Active, QPalette::Window));
953 pal.setColor(QPalette::Disabled, QPalette::Button,
954 pal.color(QPalette::Disabled, QPalette::Window));
955 pal.setColor(QPalette::Inactive, QPalette::Button,
956 pal.color(QPalette::Inactive, QPalette::Window));
958 QToolButton * closeTabButton = new QToolButton(this);
959 closeTabButton->setPalette(pal);
960 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
961 closeTabButton->setText("Close");
962 closeTabButton->setAutoRaise(true);
963 closeTabButton->setCursor(Qt::ArrowCursor);
964 closeTabButton->setToolTip(tr("Close tab"));
965 closeTabButton->setEnabled(true);
967 QObject::connect(this, SIGNAL(currentChanged(int)),
968 this, SLOT(on_currentTabChanged(int)));
969 QObject::connect(closeTabButton, SIGNAL(clicked()),
970 this, SLOT(closeCurrentTab()));
972 setCornerWidget(closeTabButton);
973 setUsesScrollButtons(true);
977 void TabWorkArea::showBar(bool show)
979 tabBar()->setEnabled(show);
980 tabBar()->setVisible(show);
984 GuiWorkArea * TabWorkArea::currentWorkArea()
989 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
995 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
997 for (int i = 0; i != count(); ++i) {
998 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1000 if (&wa->bufferView().buffer() == &buffer)
1007 void TabWorkArea::closeAll()
1010 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1018 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1020 BOOST_ASSERT(work_area);
1021 int index = indexOf(work_area);
1025 if (index == currentIndex())
1026 // Make sure the work area is up to date.
1027 on_currentTabChanged(index);
1029 // Switch to the work area.
1030 setCurrentIndex(index);
1031 work_area->setFocus();
1037 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1039 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1040 wa->setUpdatesEnabled(false);
1041 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1042 // when hiding it again below).
1043 showBar(count() > 0);
1044 addTab(wa, wa->windowTitle());
1045 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1046 this, SLOT(updateTabText(GuiWorkArea *)));
1047 // Hide tabbar if there's only one tab.
1048 showBar(count() > 1);
1053 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1055 BOOST_ASSERT(work_area);
1056 int index = indexOf(work_area);
1060 work_area->setUpdatesEnabled(false);
1065 // make sure the next work area is enabled.
1066 currentWidget()->setUpdatesEnabled(true);
1067 // Hide tabbar if there's only one tab.
1068 showBar(count() > 1);
1074 void TabWorkArea::on_currentTabChanged(int i)
1076 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1078 BufferView & bv = wa->bufferView();
1079 bv.cursor().fixIfBroken();
1081 wa->setUpdatesEnabled(true);
1085 currentWorkAreaChanged(wa);
1087 LYXERR(Debug::GUI, "currentTabChanged " << i
1088 << "File" << bv.buffer().absFileName());
1092 void TabWorkArea::closeCurrentTab()
1094 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1098 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1100 int const i = indexOf(wa);
1103 setTabText(i, wa->windowTitle());
1106 } // namespace frontend
1109 #include "GuiWorkArea_moc.cpp"