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 // All the below connection objects are needed because of a bug in some
195 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
196 // to these connections we avoid a segfault upon startup, and also at exit.
199 static boost::signals::connection timecon;
202 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
203 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
204 cursor_visible_(false), cursor_timeout_(400),
205 need_resize_(false), schedule_redraw_(false),
208 buffer.workAreaManager().add(this);
210 timecon = cursor_timeout_.timeout
211 .connect(boost::bind(&GuiWorkArea::toggleCursor, this));
213 cursor_timeout_.start();
215 screen_ = QPixmap(viewport()->width(), viewport()->height());
216 cursor_ = new frontend::CursorWidget();
219 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
220 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
221 setAcceptDrops(true);
222 setMouseTracking(true);
223 setMinimumSize(100, 70);
226 viewport()->setAutoFillBackground(false);
227 // We don't need double-buffering nor SystemBackground on
228 // the viewport because we have our own backing pixmap.
229 viewport()->setAttribute(Qt::WA_NoSystemBackground);
231 setFocusPolicy(Qt::WheelFocus);
233 viewport()->setCursor(Qt::IBeamCursor);
235 synthetic_mouse_event_.timeout.timeout.connect(
236 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
239 // Initialize the vertical Scroll Bar
240 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
241 this, SLOT(adjustViewWithScrollBar(int)));
243 // disable context menu for the scrollbar
244 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
246 // PageStep only depends on the viewport height.
247 verticalScrollBar()->setPageStep(viewport()->height());
249 LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION
250 << "\n viewport width\t" << viewport()->width()
251 << "\n viewport height\t" << viewport()->height());
253 // Enables input methods for asian languages.
254 // Must be set when creating custom text editing widgets.
255 setAttribute(Qt::WA_InputMethodEnabled, true);
260 GuiWorkArea::~GuiWorkArea()
262 buffer_view_->buffer().workAreaManager().remove(this);
267 void GuiWorkArea::close()
269 lyx_view_->removeWorkArea(this);
273 BufferView & GuiWorkArea::bufferView()
275 return *buffer_view_;
279 BufferView const & GuiWorkArea::bufferView() const
281 return *buffer_view_;
285 void GuiWorkArea::stopBlinkingCursor()
287 cursor_timeout_.stop();
292 void GuiWorkArea::startBlinkingCursor()
295 cursor_timeout_.restart();
299 void GuiWorkArea::redraw()
302 // No need to redraw in this case.
305 // No need to do anything if this is the current view. The BufferView
306 // metrics are already up to date.
307 if (lyx_view_ != theApp()->currentView()) {
308 // FIXME: it would be nice to optimize for the off-screen case.
309 buffer_view_->updateMetrics();
310 buffer_view_->cursor().fixIfBroken();
315 // update cursor position, because otherwise it has to wait until
316 // the blinking interval is over
317 if (cursor_visible_) {
322 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
324 update(0, 0, viewport()->width(), viewport()->height());
326 if (lyxerr.debugging(Debug::WORKAREA))
327 buffer_view_->coordCache().dump();
331 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
333 // In order to avoid bad surprise in the middle of an operation,
334 // we better stop the blinking cursor.
335 stopBlinkingCursor();
337 theLyXFunc().setLyXView(lyx_view_);
338 theLyXFunc().processKeySym(key, mod);
342 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
345 if (cmd0.action == LFUN_FILE_OPEN) {
346 lyx_view_->dispatch(cmd0);
350 theLyXFunc().setLyXView(lyx_view_);
354 if (cmd0.action == LFUN_MOUSE_PRESS) {
355 if (mod == ShiftModifier)
356 cmd = FuncRequest(cmd0, "region-select");
357 else if (mod == ControlModifier)
358 cmd = FuncRequest(cmd0, "paragraph-select");
365 // In order to avoid bad surprise in the middle of an operation, we better stop
366 // the blinking cursor.
367 if (!(cmd.action == LFUN_MOUSE_MOTION
368 && cmd.button() == mouse_button::none))
369 stopBlinkingCursor();
371 buffer_view_->mouseEventDispatch(cmd);
373 // Skip these when selecting
374 if (cmd.action != LFUN_MOUSE_MOTION) {
375 lyx_view_->updateLayoutChoice(false);
376 lyx_view_->updateToolbars();
379 // GUI tweaks except with mouse motion with no button pressed.
380 if (!(cmd.action == LFUN_MOUSE_MOTION
381 && cmd.button() == mouse_button::none)) {
382 // Slight hack: this is only called currently when we
383 // clicked somewhere, so we force through the display
384 // of the new status here.
385 lyx_view_->clearMessage();
387 // Show the cursor immediately after any operation.
388 startBlinkingCursor();
393 void GuiWorkArea::resizeBufferView()
395 // WARNING: Please don't put any code that will trigger a repaint here!
396 // We are already inside a paint event.
397 lyx_view_->setBusy(true);
398 buffer_view_->resize(viewport()->width(), viewport()->height());
399 lyx_view_->updateLayoutChoice(false);
400 lyx_view_->setBusy(false);
404 void GuiWorkArea::showCursor()
409 CursorShape shape = BAR_SHAPE;
411 Font const & realfont = buffer_view_->cursor().real_current_font;
412 BufferParams const & bp = buffer_view_->buffer().params();
413 bool const samelang = realfont.language() == bp.language;
414 bool const isrtl = realfont.isVisibleRightToLeft();
416 if (!samelang || isrtl != bp.language->rightToLeft()) {
419 shape = REVERSED_L_SHAPE;
422 // The ERT language hack needs fixing up
423 if (realfont.language() == latex_language)
426 Font const font = buffer_view_->cursor().getFont();
427 FontMetrics const & fm = theFontMetrics(font);
428 int const asc = fm.maxAscent();
429 int const des = fm.maxDescent();
433 buffer_view_->cursor().getPos(x, y);
436 // if it doesn't touch the screen, don't try to show it
437 if (y + h < 0 || y >= viewport()->height())
440 cursor_visible_ = true;
441 showCursor(x, y, h, shape);
445 void GuiWorkArea::hideCursor()
447 if (!cursor_visible_)
450 cursor_visible_ = false;
455 void GuiWorkArea::toggleCursor()
462 // Use this opportunity to deal with any child processes that
463 // have finished but are waiting to communicate this fact
464 // to the rest of LyX.
465 ForkedcallsController & fcc = ForkedcallsController::get();
466 fcc.handleCompletedProcesses();
468 cursor_timeout_.restart();
472 void GuiWorkArea::updateScrollbar()
474 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
475 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
476 verticalScrollBar()->setTracking(false);
478 buffer_view_->updateScrollbar();
479 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
481 // do what cursor movement does (some grey)
482 int const h = scroll_.height + viewport()->height() / 4;
483 int scroll_max_ = std::max(0, h - viewport()->height());
485 verticalScrollBar()->setRange(0, scroll_max_);
486 verticalScrollBar()->setSliderPosition(scroll_.position);
487 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
488 verticalScrollBar()->setValue(scroll_.position);
490 verticalScrollBar()->setTracking(true);
494 void GuiWorkArea::adjustViewWithScrollBar(int action)
496 stopBlinkingCursor();
497 if (action == QAbstractSlider::SliderPageStepAdd)
498 buffer_view_->scrollDown(viewport()->height());
499 else if (action == QAbstractSlider::SliderPageStepSub)
500 buffer_view_->scrollUp(viewport()->height());
502 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
504 if (lyxrc.cursor_follows_scrollbar) {
505 buffer_view_->setCursorFromScrollbar();
506 lyx_view_->updateLayoutChoice(false);
508 // Show the cursor immediately after any operation.
509 startBlinkingCursor();
510 QApplication::syncX();
514 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
516 // Repaint the whole screen.
517 // Note: this is different from redraw() as only the backing pixmap
518 // will be redrawn, which is cheap.
519 viewport()->repaint();
521 startBlinkingCursor();
525 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
527 stopBlinkingCursor();
531 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
533 if (dc_event_.active && dc_event_ == *e) {
534 dc_event_.active = false;
535 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
536 q_button_state(e->button()));
541 inputContext()->reset();
543 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
544 q_button_state(e->button()));
545 dispatch(cmd, q_key_state(e->modifiers()));
549 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
551 if (synthetic_mouse_event_.timeout.running())
552 synthetic_mouse_event_.timeout.stop();
554 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
555 q_button_state(e->button()));
560 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
562 // we kill the triple click if we move
563 doubleClickTimeout();
564 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
565 q_motion_state(e->buttons()));
567 // If we're above or below the work area...
568 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
569 // Make sure only a synthetic event can cause a page scroll,
570 // so they come at a steady rate:
572 // _Force_ a scroll up:
575 cmd.y = viewport()->height();
576 // Store the event, to be handled when the timeout expires.
577 synthetic_mouse_event_.cmd = cmd;
579 if (synthetic_mouse_event_.timeout.running())
580 // Discard the event. Note that it _may_ be handled
581 // when the timeout expires if
582 // synthetic_mouse_event_.cmd has not been overwritten.
583 // Ie, when the timeout expires, we handle the
584 // most recent event but discard all others that
585 // occurred after the one used to start the timeout
586 // in the first place.
589 synthetic_mouse_event_.restart_timeout = true;
590 synthetic_mouse_event_.timeout.start();
591 // Fall through to handle this event...
594 } else if (synthetic_mouse_event_.timeout.running()) {
595 // Store the event, to be possibly handled when the timeout
597 // Once the timeout has expired, normal control is returned
598 // to mouseMoveEvent (restart_timeout = false).
599 // This results in a much smoother 'feel' when moving the
600 // mouse back into the work area.
601 synthetic_mouse_event_.cmd = cmd;
602 synthetic_mouse_event_.restart_timeout = false;
606 // Has anything changed on-screen since the last QMouseEvent
608 double const scrollbar_value = verticalScrollBar()->value();
609 if (e->x() != synthetic_mouse_event_.x_old ||
610 e->y() != synthetic_mouse_event_.y_old ||
611 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
612 // Yes it has. Store the params used to check this.
613 synthetic_mouse_event_.x_old = e->x();
614 synthetic_mouse_event_.y_old = e->y();
615 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
617 // ... and dispatch the event to the LyX core.
623 void GuiWorkArea::wheelEvent(QWheelEvent * e)
625 // Wheel rotation by one notch results in a delta() of 120 (see
626 // documentation of QWheelEvent)
627 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
628 verticalScrollBar()->setValue(verticalScrollBar()->value() -
629 lines * verticalScrollBar()->singleStep());
630 adjustViewWithScrollBar();
634 void GuiWorkArea::generateSyntheticMouseEvent()
636 // Set things off to generate the _next_ 'pseudo' event.
637 if (synthetic_mouse_event_.restart_timeout)
638 synthetic_mouse_event_.timeout.start();
640 // Has anything changed on-screen since the last timeout signal
642 double const scrollbar_value = verticalScrollBar()->value();
643 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
644 // Yes it has. Store the params used to check this.
645 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
647 // ... and dispatch the event to the LyX core.
648 dispatch(synthetic_mouse_event_.cmd);
653 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
655 // do nothing if there are other events
656 // (the auto repeated events come too fast)
657 // \todo FIXME: remove hard coded Qt keys, process the key binding
659 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
660 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
661 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
662 << "\nsystem is busy: scroll key event ignored");
668 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
669 << " count=" << ev->count()
670 << " text=" << fromqstr(ev->text())
671 << " isAutoRepeat=" << ev->isAutoRepeat()
672 << " key=" << ev->key());
675 setKeySymbol(&sym, ev);
676 processKeySym(sym, q_key_state(ev->modifiers()));
680 void GuiWorkArea::doubleClickTimeout()
682 dc_event_.active = false;
686 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
688 dc_event_ = DoubleClick(ev);
689 QTimer::singleShot(QApplication::doubleClickInterval(), this,
690 SLOT(doubleClickTimeout()));
691 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
693 q_button_state(ev->button()));
698 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
700 QAbstractScrollArea::resizeEvent(ev);
705 void GuiWorkArea::update(int x, int y, int w, int h)
707 viewport()->repaint(x, y, w, h);
711 void GuiWorkArea::paintEvent(QPaintEvent * ev)
713 QRect const rc = ev->rect();
715 LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
717 << " w: " << rc.width()
718 << " h: " << rc.height() << endl;
722 verticalScrollBar()->setPageStep(viewport()->height());
723 screen_ = QPixmap(viewport()->width(), viewport()->height());
728 need_resize_ = false;
731 QPainter pain(viewport());
732 pain.drawPixmap(rc, screen_, rc);
737 void GuiWorkArea::updateScreen()
739 GuiPainter pain(&screen_);
740 verticalScrollBar()->show();
741 buffer_view_->draw(pain);
745 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
747 if (schedule_redraw_) {
748 buffer_view_->updateMetrics();
750 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
751 schedule_redraw_ = false;
752 // Show the cursor immediately after the update.
758 cursor_->update(x, y, h, shape);
760 viewport()->update(cursor_->rect());
764 void GuiWorkArea::removeCursor()
767 //if (!qApp->focusWidget())
768 viewport()->update(cursor_->rect());
772 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
774 QString const & commit_string = e->commitString();
775 docstring const & preedit_string
776 = qstring_to_ucs4(e->preeditString());
778 if (!commit_string.isEmpty()) {
780 LYXERR(Debug::KEY, BOOST_CURRENT_FUNCTION
781 << " preeditString =" << fromqstr(e->preeditString())
782 << " commitString =" << fromqstr(e->commitString()));
786 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
787 for (int i = 0; i < commit_string.size(); ++i) {
788 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
793 // Hide the cursor during the kana-kanji transformation.
794 if (preedit_string.empty())
795 startBlinkingCursor();
797 stopBlinkingCursor();
799 // last_width : for checking if last preedit string was/wasn't empty.
800 static bool last_width = false;
801 if (!last_width && preedit_string.empty()) {
802 // if last_width is last length of preedit string.
807 GuiPainter pain(&screen_);
808 buffer_view_->updateMetrics();
809 buffer_view_->draw(pain);
810 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
811 FontMetrics const & fm = theFontMetrics(font);
812 int height = fm.maxHeight();
813 int cur_x = cursor_->rect().left();
814 int cur_y = cursor_->rect().bottom();
816 // redraw area of preedit string.
817 update(0, cur_y - height, viewport()->width(),
818 (height + 1) * preedit_lines_);
820 if (preedit_string.empty()) {
828 // att : stores an IM attribute.
829 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
831 // get attributes of input method cursor.
832 // cursor_pos : cursor position in preedit string.
833 size_t cursor_pos = 0;
834 bool cursor_is_visible = false;
835 for (int i = 0; i < att.size(); ++i) {
836 if (att.at(i).type == QInputMethodEvent::Cursor) {
837 cursor_pos = att.at(i).start;
838 cursor_is_visible = att.at(i).length != 0;
843 size_t preedit_length = preedit_string.length();
845 // get position of selection in input method.
846 // FIXME: isn't there a way to do this simplier?
847 // rStart : cursor position in selected string in IM.
849 // rLength : selected string length in IM.
851 if (cursor_pos < preedit_length) {
852 for (int i = 0; i < att.size(); ++i) {
853 if (att.at(i).type == QInputMethodEvent::TextFormat) {
854 if (att.at(i).start <= int(cursor_pos)
855 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
856 rStart = att.at(i).start;
857 rLength = att.at(i).length;
858 if (!cursor_is_visible)
859 cursor_pos += rLength;
870 int const right_margin = rightMargin();
871 Painter::preedit_style ps;
872 // Most often there would be only one line:
874 for (size_t pos = 0; pos != preedit_length; ++pos) {
875 char_type const typed_char = preedit_string[pos];
876 // reset preedit string style
877 ps = Painter::preedit_default;
879 // if we reached the right extremity of the screen, go to next line.
880 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
881 cur_x = right_margin;
885 // preedit strings are displayed with dashed underline
886 // and partial strings are displayed white on black indicating
887 // that we are in selecting mode in the input method.
888 // FIXME: rLength == preedit_length is not a changing condition
889 // FIXME: should be put out of the loop.
891 && pos < rStart + rLength
892 && !(cursor_pos < rLength && rLength == preedit_length))
893 ps = Painter::preedit_selecting;
895 if (pos == cursor_pos
896 && (cursor_pos < rLength && rLength == preedit_length))
897 ps = Painter::preedit_cursor;
899 // draw one character and update cur_x.
900 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
903 // update the preedit string screen area.
904 update(0, cur_y - preedit_lines_*height, viewport()->width(),
905 (height + 1) * preedit_lines_);
907 // Don't forget to accept the event!
912 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
914 QRect cur_r(0,0,0,0);
916 // this is the CJK-specific composition window position.
917 case Qt::ImMicroFocus:
918 cur_r = cursor_->rect();
919 if (preedit_lines_ != 1)
921 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
922 // return lower right of cursor in LyX.
925 return QWidget::inputMethodQuery(query);
930 void GuiWorkArea::updateWindowTitle()
932 docstring maximize_title;
933 docstring minimize_title;
935 Buffer & buf = buffer_view_->buffer();
936 FileName const fileName = buf.fileName();
937 if (!fileName.empty()) {
938 maximize_title = fileName.displayName(30);
939 minimize_title = from_utf8(fileName.onlyFileName());
940 if (!buf.isClean()) {
941 maximize_title += _(" (changed)");
942 minimize_title += char_type('*');
944 if (buf.isReadonly())
945 maximize_title += _(" (read only)");
948 QString title = windowTitle();
949 QString new_title = toqstr(maximize_title);
950 if (title == new_title)
953 QWidget::setWindowTitle(new_title);
954 QWidget::setWindowIconText(toqstr(minimize_title));
959 void GuiWorkArea::setReadOnly(bool)
962 if (this == lyx_view_->currentWorkArea())
963 lyx_view_->updateBufferDependent(false);
967 ////////////////////////////////////////////////////////////////////
971 ////////////////////////////////////////////////////////////////////
973 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
975 QPalette pal = palette();
976 pal.setColor(QPalette::Active, QPalette::Button,
977 pal.color(QPalette::Active, QPalette::Window));
978 pal.setColor(QPalette::Disabled, QPalette::Button,
979 pal.color(QPalette::Disabled, QPalette::Window));
980 pal.setColor(QPalette::Inactive, QPalette::Button,
981 pal.color(QPalette::Inactive, QPalette::Window));
983 QToolButton * closeTabButton = new QToolButton(this);
984 closeTabButton->setPalette(pal);
985 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
986 closeTabButton->setText("Close");
987 closeTabButton->setAutoRaise(true);
988 closeTabButton->setCursor(Qt::ArrowCursor);
989 closeTabButton->setToolTip(tr("Close tab"));
990 closeTabButton->setEnabled(true);
992 QObject::connect(this, SIGNAL(currentChanged(int)),
993 this, SLOT(on_currentTabChanged(int)));
994 QObject::connect(closeTabButton, SIGNAL(clicked()),
995 this, SLOT(closeCurrentTab()));
997 setCornerWidget(closeTabButton);
998 setUsesScrollButtons(true);
1002 void TabWorkArea::showBar(bool show)
1004 tabBar()->setVisible(show);
1008 GuiWorkArea * TabWorkArea::currentWorkArea()
1013 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1019 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1021 for (int i = 0; i != count(); ++i) {
1022 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1024 if (&wa->bufferView().buffer() == &buffer)
1031 void TabWorkArea::closeAll()
1034 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1042 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1044 BOOST_ASSERT(work_area);
1045 int index = indexOf(work_area);
1049 if (index == currentIndex())
1050 // Make sure the work area is up to date.
1051 on_currentTabChanged(index);
1053 // Switch to the work area.
1054 setCurrentIndex(index);
1055 work_area->setFocus();
1061 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1063 BOOST_ASSERT(work_area);
1064 int index = indexOf(work_area);
1068 work_area->setUpdatesEnabled(false);
1073 // make sure the next work area is enabled.
1074 currentWidget()->setUpdatesEnabled(true);
1075 // Hide tabbar if there's only one tab.
1076 showBar(count() > 1);
1082 void TabWorkArea::on_currentTabChanged(int i)
1084 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1086 BufferView & bv = wa->bufferView();
1088 bv.cursor().fixIfBroken();
1089 wa->setUpdatesEnabled(true);
1093 currentWorkAreaChanged(wa);
1095 LYXERR(Debug::GUI, "currentTabChanged " << i
1096 << "File" << bv.buffer().absFileName());
1100 void TabWorkArea::closeCurrentTab()
1102 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1106 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1108 int const i = indexOf(wa);
1111 setTabText(i, wa->windowTitle());
1114 } // namespace frontend
1117 #include "GuiWorkArea_moc.cpp"