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;
80 using support::FileName;
83 /// return the LyX mouse button state from Qt's
84 static mouse_button::state q_button_state(Qt::MouseButton button)
86 mouse_button::state b = mouse_button::none;
89 b = mouse_button::button1;
92 b = mouse_button::button2;
95 b = mouse_button::button3;
104 /// return the LyX mouse button state from Qt's
105 mouse_button::state q_motion_state(Qt::MouseButtons state)
107 mouse_button::state b = mouse_button::none;
108 if (state & Qt::LeftButton)
109 b |= mouse_button::button1;
110 if (state & Qt::MidButton)
111 b |= mouse_button::button2;
112 if (state & Qt::RightButton)
113 b |= mouse_button::button3;
124 void draw(QPainter & painter)
126 if (show_ && rect_.isValid()) {
129 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
130 painter.setPen(color_);
131 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
132 rect_.bottomRight().x(), rect_.bottomLeft().y());
135 case REVERSED_L_SHAPE:
136 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
137 painter.setPen(color_);
138 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
139 rect_.bottomLeft().x(), rect_.bottomLeft().y());
143 painter.fillRect(rect_, color_);
149 void update(int x, int y, int h, CursorShape shape)
151 color_ = guiApp->colorCache().get(Color_cursor);
155 rect_ = QRect(x, y, CursorWidth + h / 3, h);
157 case REVERSED_L_SHAPE:
158 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
161 rect_ = QRect(x, y, CursorWidth, h);
166 void show(bool set_show = true) { show_ = set_show; }
167 void hide() { show_ = false; }
169 QRect const & rect() { return rect_; }
183 // This is a 'heartbeat' generating synthetic mouse move events when the
184 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
185 SyntheticMouseEvent::SyntheticMouseEvent()
186 : timeout(200), restart_timeout(true),
187 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
192 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
193 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
194 cursor_visible_(false),
195 need_resize_(false), schedule_redraw_(false),
198 buffer.workAreaManager().add(this);
200 cursor_timeout_.setInterval(400);
201 connect(&cursor_timeout_, SIGNAL(timeout()),
202 this, SLOT(toggleCursor()));
204 cursor_timeout_.start();
206 screen_ = QPixmap(viewport()->width(), viewport()->height());
207 cursor_ = new frontend::CursorWidget();
210 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
211 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212 setAcceptDrops(true);
213 setMouseTracking(true);
214 setMinimumSize(100, 70);
217 viewport()->setAutoFillBackground(false);
218 // We don't need double-buffering nor SystemBackground on
219 // the viewport because we have our own backing pixmap.
220 viewport()->setAttribute(Qt::WA_NoSystemBackground);
222 setFocusPolicy(Qt::WheelFocus);
224 viewport()->setCursor(Qt::IBeamCursor);
226 synthetic_mouse_event_.timeout.timeout.connect(
227 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
230 // Initialize the vertical Scroll Bar
231 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
232 this, SLOT(adjustViewWithScrollBar(int)));
234 // disable context menu for the scrollbar
235 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
237 // PageStep only depends on the viewport height.
238 verticalScrollBar()->setPageStep(viewport()->height());
240 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
241 << " viewport height: " << viewport()->height());
243 // Enables input methods for asian languages.
244 // Must be set when creating custom text editing widgets.
245 setAttribute(Qt::WA_InputMethodEnabled, true);
250 GuiWorkArea::~GuiWorkArea()
252 buffer_view_->buffer().workAreaManager().remove(this);
258 void GuiWorkArea::close()
260 lyx_view_->removeWorkArea(this);
264 BufferView & GuiWorkArea::bufferView()
266 return *buffer_view_;
270 BufferView const & GuiWorkArea::bufferView() const
272 return *buffer_view_;
276 void GuiWorkArea::stopBlinkingCursor()
278 cursor_timeout_.stop();
283 void GuiWorkArea::startBlinkingCursor()
286 cursor_timeout_.start();
290 void GuiWorkArea::redraw()
293 // No need to redraw in this case.
296 // No need to do anything if this is the current view. The BufferView
297 // metrics are already up to date.
298 if (lyx_view_ != guiApp->currentView()) {
299 // FIXME: it would be nice to optimize for the off-screen case.
300 buffer_view_->updateMetrics();
301 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 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 support::ForkedCallsController::handleCompletedProcesses();
463 void GuiWorkArea::updateScrollbar()
465 verticalScrollBar()->setTracking(false);
467 buffer_view_->updateScrollbar();
468 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
470 // do what cursor movement does (some grey)
471 int const h = scroll_.height + viewport()->height() / 4;
472 int scroll_max_ = std::max(0, h - viewport()->height());
474 verticalScrollBar()->setRange(0, scroll_max_);
475 verticalScrollBar()->setSliderPosition(scroll_.position);
476 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
477 verticalScrollBar()->setValue(scroll_.position);
479 verticalScrollBar()->setTracking(true);
483 void GuiWorkArea::adjustViewWithScrollBar(int action)
485 stopBlinkingCursor();
486 if (action == QAbstractSlider::SliderPageStepAdd)
487 buffer_view_->scrollDown(viewport()->height());
488 else if (action == QAbstractSlider::SliderPageStepSub)
489 buffer_view_->scrollUp(viewport()->height());
491 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
493 if (lyxrc.cursor_follows_scrollbar) {
494 buffer_view_->setCursorFromScrollbar();
495 lyx_view_->updateLayoutList();
497 // Show the cursor immediately after any operation.
498 startBlinkingCursor();
499 QApplication::syncX();
503 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
505 // Repaint the whole screen.
506 // Note: this is different from redraw() as only the backing pixmap
507 // will be redrawn, which is cheap.
508 viewport()->repaint();
510 startBlinkingCursor();
514 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
516 stopBlinkingCursor();
520 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
522 if (dc_event_.active && dc_event_ == *e) {
523 dc_event_.active = false;
524 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
525 q_button_state(e->button()));
530 inputContext()->reset();
532 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
533 q_button_state(e->button()));
534 dispatch(cmd, q_key_state(e->modifiers()));
538 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
540 if (synthetic_mouse_event_.timeout.running())
541 synthetic_mouse_event_.timeout.stop();
543 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
544 q_button_state(e->button()));
549 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
551 // we kill the triple click if we move
552 doubleClickTimeout();
553 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
554 q_motion_state(e->buttons()));
556 // If we're above or below the work area...
557 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
558 // Make sure only a synthetic event can cause a page scroll,
559 // so they come at a steady rate:
561 // _Force_ a scroll up:
564 cmd.y = viewport()->height();
565 // Store the event, to be handled when the timeout expires.
566 synthetic_mouse_event_.cmd = cmd;
568 if (synthetic_mouse_event_.timeout.running())
569 // Discard the event. Note that it _may_ be handled
570 // when the timeout expires if
571 // synthetic_mouse_event_.cmd has not been overwritten.
572 // Ie, when the timeout expires, we handle the
573 // most recent event but discard all others that
574 // occurred after the one used to start the timeout
575 // in the first place.
578 synthetic_mouse_event_.restart_timeout = true;
579 synthetic_mouse_event_.timeout.start();
580 // Fall through to handle this event...
583 } else if (synthetic_mouse_event_.timeout.running()) {
584 // Store the event, to be possibly handled when the timeout
586 // Once the timeout has expired, normal control is returned
587 // to mouseMoveEvent (restart_timeout = false).
588 // This results in a much smoother 'feel' when moving the
589 // mouse back into the work area.
590 synthetic_mouse_event_.cmd = cmd;
591 synthetic_mouse_event_.restart_timeout = false;
595 // Has anything changed on-screen since the last QMouseEvent
597 double const scrollbar_value = verticalScrollBar()->value();
598 if (e->x() != synthetic_mouse_event_.x_old ||
599 e->y() != synthetic_mouse_event_.y_old ||
600 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
601 // Yes it has. Store the params used to check this.
602 synthetic_mouse_event_.x_old = e->x();
603 synthetic_mouse_event_.y_old = e->y();
604 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
606 // ... and dispatch the event to the LyX core.
612 void GuiWorkArea::wheelEvent(QWheelEvent * e)
614 // Wheel rotation by one notch results in a delta() of 120 (see
615 // documentation of QWheelEvent)
616 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
617 verticalScrollBar()->setValue(verticalScrollBar()->value() -
618 lines * verticalScrollBar()->singleStep());
619 adjustViewWithScrollBar();
623 void GuiWorkArea::generateSyntheticMouseEvent()
625 // Set things off to generate the _next_ 'pseudo' event.
626 if (synthetic_mouse_event_.restart_timeout)
627 synthetic_mouse_event_.timeout.start();
629 // Has anything changed on-screen since the last timeout signal
631 double const scrollbar_value = verticalScrollBar()->value();
632 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
633 // Yes it has. Store the params used to check this.
634 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
636 // ... and dispatch the event to the LyX core.
637 dispatch(synthetic_mouse_event_.cmd);
642 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
644 // do nothing if there are other events
645 // (the auto repeated events come too fast)
646 // \todo FIXME: remove hard coded Qt keys, process the key binding
648 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
649 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
650 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
656 LYXERR(Debug::KEY, " count: " << ev->count()
657 << " text: " << fromqstr(ev->text())
658 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
661 setKeySymbol(&sym, ev);
662 processKeySym(sym, q_key_state(ev->modifiers()));
666 void GuiWorkArea::doubleClickTimeout()
668 dc_event_.active = false;
672 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
674 dc_event_ = DoubleClick(ev);
675 QTimer::singleShot(QApplication::doubleClickInterval(), this,
676 SLOT(doubleClickTimeout()));
677 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
679 q_button_state(ev->button()));
684 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
686 QAbstractScrollArea::resizeEvent(ev);
691 void GuiWorkArea::update(int x, int y, int w, int h)
693 viewport()->repaint(x, y, w, h);
697 void GuiWorkArea::paintEvent(QPaintEvent * ev)
699 QRect const rc = ev->rect();
700 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
701 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
704 verticalScrollBar()->setPageStep(viewport()->height());
705 screen_ = QPixmap(viewport()->width(), viewport()->height());
712 QPainter pain(viewport());
713 pain.drawPixmap(rc, screen_, rc);
718 void GuiWorkArea::updateScreen()
720 GuiPainter pain(&screen_);
721 buffer_view_->draw(pain);
725 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
727 if (schedule_redraw_) {
728 buffer_view_->updateMetrics();
730 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
731 schedule_redraw_ = false;
732 // Show the cursor immediately after the update.
738 cursor_->update(x, y, h, shape);
740 viewport()->update(cursor_->rect());
744 void GuiWorkArea::removeCursor()
747 //if (!qApp->focusWidget())
748 viewport()->update(cursor_->rect());
752 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
754 QString const & commit_string = e->commitString();
755 docstring const & preedit_string
756 = qstring_to_ucs4(e->preeditString());
758 if (!commit_string.isEmpty()) {
760 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
761 << " commitString: " << fromqstr(e->commitString()));
765 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
766 for (int i = 0; i != commit_string.size(); ++i) {
767 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
772 // Hide the cursor during the kana-kanji transformation.
773 if (preedit_string.empty())
774 startBlinkingCursor();
776 stopBlinkingCursor();
778 // last_width : for checking if last preedit string was/wasn't empty.
779 static bool last_width = false;
780 if (!last_width && preedit_string.empty()) {
781 // if last_width is last length of preedit string.
786 GuiPainter pain(&screen_);
787 buffer_view_->updateMetrics();
788 buffer_view_->draw(pain);
789 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
790 FontMetrics const & fm = theFontMetrics(font);
791 int height = fm.maxHeight();
792 int cur_x = cursor_->rect().left();
793 int cur_y = cursor_->rect().bottom();
795 // redraw area of preedit string.
796 update(0, cur_y - height, viewport()->width(),
797 (height + 1) * preedit_lines_);
799 if (preedit_string.empty()) {
807 // att : stores an IM attribute.
808 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
810 // get attributes of input method cursor.
811 // cursor_pos : cursor position in preedit string.
812 size_t cursor_pos = 0;
813 bool cursor_is_visible = false;
814 for (int i = 0; i != att.size(); ++i) {
815 if (att.at(i).type == QInputMethodEvent::Cursor) {
816 cursor_pos = att.at(i).start;
817 cursor_is_visible = att.at(i).length != 0;
822 size_t preedit_length = preedit_string.length();
824 // get position of selection in input method.
825 // FIXME: isn't there a way to do this simplier?
826 // rStart : cursor position in selected string in IM.
828 // rLength : selected string length in IM.
830 if (cursor_pos < preedit_length) {
831 for (int i = 0; i != att.size(); ++i) {
832 if (att.at(i).type == QInputMethodEvent::TextFormat) {
833 if (att.at(i).start <= int(cursor_pos)
834 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
835 rStart = att.at(i).start;
836 rLength = att.at(i).length;
837 if (!cursor_is_visible)
838 cursor_pos += rLength;
849 int const right_margin = rightMargin();
850 Painter::preedit_style ps;
851 // Most often there would be only one line:
853 for (size_t pos = 0; pos != preedit_length; ++pos) {
854 char_type const typed_char = preedit_string[pos];
855 // reset preedit string style
856 ps = Painter::preedit_default;
858 // if we reached the right extremity of the screen, go to next line.
859 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
860 cur_x = right_margin;
864 // preedit strings are displayed with dashed underline
865 // and partial strings are displayed white on black indicating
866 // that we are in selecting mode in the input method.
867 // FIXME: rLength == preedit_length is not a changing condition
868 // FIXME: should be put out of the loop.
870 && pos < rStart + rLength
871 && !(cursor_pos < rLength && rLength == preedit_length))
872 ps = Painter::preedit_selecting;
874 if (pos == cursor_pos
875 && (cursor_pos < rLength && rLength == preedit_length))
876 ps = Painter::preedit_cursor;
878 // draw one character and update cur_x.
879 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
882 // update the preedit string screen area.
883 update(0, cur_y - preedit_lines_*height, viewport()->width(),
884 (height + 1) * preedit_lines_);
886 // Don't forget to accept the event!
891 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
893 QRect cur_r(0,0,0,0);
895 // this is the CJK-specific composition window position.
896 case Qt::ImMicroFocus:
897 cur_r = cursor_->rect();
898 if (preedit_lines_ != 1)
900 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
901 // return lower right of cursor in LyX.
904 return QWidget::inputMethodQuery(query);
909 void GuiWorkArea::updateWindowTitle()
911 docstring maximize_title;
912 docstring minimize_title;
914 Buffer & buf = buffer_view_->buffer();
915 FileName const fileName = buf.fileName();
916 if (!fileName.empty()) {
917 maximize_title = fileName.displayName(30);
918 minimize_title = from_utf8(fileName.onlyFileName());
919 if (!buf.isClean()) {
920 maximize_title += _(" (changed)");
921 minimize_title += char_type('*');
923 if (buf.isReadonly())
924 maximize_title += _(" (read only)");
927 QString title = windowTitle();
928 QString new_title = toqstr(maximize_title);
929 if (title == new_title)
932 QWidget::setWindowTitle(new_title);
933 QWidget::setWindowIconText(toqstr(minimize_title));
938 void GuiWorkArea::setReadOnly(bool)
941 if (this == lyx_view_->currentWorkArea())
942 lyx_view_->updateBufferDependent(false);
946 ////////////////////////////////////////////////////////////////////
950 ////////////////////////////////////////////////////////////////////
952 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
954 QPalette pal = palette();
955 pal.setColor(QPalette::Active, QPalette::Button,
956 pal.color(QPalette::Active, QPalette::Window));
957 pal.setColor(QPalette::Disabled, QPalette::Button,
958 pal.color(QPalette::Disabled, QPalette::Window));
959 pal.setColor(QPalette::Inactive, QPalette::Button,
960 pal.color(QPalette::Inactive, QPalette::Window));
962 QToolButton * closeTabButton = new QToolButton(this);
963 closeTabButton->setPalette(pal);
964 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
965 closeTabButton->setText("Close");
966 closeTabButton->setAutoRaise(true);
967 closeTabButton->setCursor(Qt::ArrowCursor);
968 closeTabButton->setToolTip(tr("Close tab"));
969 closeTabButton->setEnabled(true);
971 QObject::connect(this, SIGNAL(currentChanged(int)),
972 this, SLOT(on_currentTabChanged(int)));
973 QObject::connect(closeTabButton, SIGNAL(clicked()),
974 this, SLOT(closeCurrentTab()));
976 setCornerWidget(closeTabButton);
977 setUsesScrollButtons(true);
981 void TabWorkArea::showBar(bool show)
983 tabBar()->setEnabled(show);
984 tabBar()->setVisible(show);
988 GuiWorkArea * TabWorkArea::currentWorkArea()
993 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
999 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1001 for (int i = 0; i != count(); ++i) {
1002 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1004 if (&wa->bufferView().buffer() == &buffer)
1011 void TabWorkArea::closeAll()
1014 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1022 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1024 BOOST_ASSERT(work_area);
1025 int index = indexOf(work_area);
1029 if (index == currentIndex())
1030 // Make sure the work area is up to date.
1031 on_currentTabChanged(index);
1033 // Switch to the work area.
1034 setCurrentIndex(index);
1035 work_area->setFocus();
1041 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1043 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1044 wa->setUpdatesEnabled(false);
1045 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1046 // when hiding it again below).
1047 showBar(count() > 0);
1048 addTab(wa, wa->windowTitle());
1049 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1050 this, SLOT(updateTabText(GuiWorkArea *)));
1051 // Hide tabbar if there's only one tab.
1052 showBar(count() > 1);
1057 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1059 BOOST_ASSERT(work_area);
1060 int index = indexOf(work_area);
1064 work_area->setUpdatesEnabled(false);
1069 // make sure the next work area is enabled.
1070 currentWidget()->setUpdatesEnabled(true);
1071 // Hide tabbar if there's only one tab.
1072 showBar(count() > 1);
1078 void TabWorkArea::on_currentTabChanged(int i)
1080 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1082 BufferView & bv = wa->bufferView();
1083 bv.cursor().fixIfBroken();
1085 wa->setUpdatesEnabled(true);
1089 currentWorkAreaChanged(wa);
1091 LYXERR(Debug::GUI, "currentTabChanged " << i
1092 << "File" << bv.buffer().absFileName());
1096 void TabWorkArea::closeCurrentTab()
1098 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1102 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1104 int const i = indexOf(wa);
1107 setTabText(i, wa->windowTitle());
1110 } // namespace frontend
1113 #include "GuiWorkArea_moc.cpp"