2 * \file GuiWorkArea.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "GuiWorkArea.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CoordCache.h"
22 #include "FuncRequest.h"
23 #include "GuiApplication.h"
24 #include "GuiKeySymbol.h"
25 #include "GuiPainter.h"
26 #include "GuiPopupMenu.h"
28 #include "KeySymbol.h"
32 #include "MetricsInfo.h"
33 #include "qt_helpers.h"
36 #include "graphics/GraphicsImage.h"
37 #include "graphics/GraphicsLoader.h"
39 #include "support/debug.h"
40 #include "support/gettext.h"
41 #include "support/FileName.h"
42 #include "support/ForkedCalls.h"
44 #include "frontends/Application.h"
45 #include "frontends/FontMetrics.h"
46 #include "frontends/WorkAreaManager.h"
48 #include <QContextMenuEvent>
49 #include <QInputContext>
52 #include <QMainWindow>
58 #include <QToolButton>
61 #include <boost/bind.hpp>
65 extern "C" int XEventsQueued(Display *display, int mode);
69 int const CursorWidth = 2;
71 int const CursorWidth = 1;
78 using namespace lyx::support;
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 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
211 setAcceptDrops(true);
212 setMouseTracking(true);
213 setMinimumSize(100, 70);
216 viewport()->setAutoFillBackground(false);
217 // We don't need double-buffering nor SystemBackground on
218 // the viewport because we have our own backing pixmap.
219 viewport()->setAttribute(Qt::WA_NoSystemBackground);
221 setFocusPolicy(Qt::WheelFocus);
223 viewport()->setCursor(Qt::IBeamCursor);
225 synthetic_mouse_event_.timeout.timeout.connect(
226 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
229 // Initialize the vertical Scroll Bar
230 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
231 this, SLOT(adjustViewWithScrollBar(int)));
233 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
234 << " viewport height: " << viewport()->height());
236 // Enables input methods for asian languages.
237 // Must be set when creating custom text editing widgets.
238 setAttribute(Qt::WA_InputMethodEnabled, true);
243 GuiWorkArea::~GuiWorkArea()
245 buffer_view_->buffer().workAreaManager().remove(this);
251 void GuiWorkArea::close()
253 lyx_view_->removeWorkArea(this);
257 BufferView & GuiWorkArea::bufferView()
259 return *buffer_view_;
263 BufferView const & GuiWorkArea::bufferView() const
265 return *buffer_view_;
269 void GuiWorkArea::stopBlinkingCursor()
271 cursor_timeout_.stop();
276 void GuiWorkArea::startBlinkingCursor()
279 cursor_timeout_.start();
283 void GuiWorkArea::redraw()
286 // No need to redraw in this case.
289 // No need to do anything if this is the current view. The BufferView
290 // metrics are already up to date.
291 if (lyx_view_ != guiApp->currentView()) {
292 // FIXME: it would be nice to optimize for the off-screen case.
293 buffer_view_->updateMetrics();
294 buffer_view_->cursor().fixIfBroken();
297 // update cursor position, because otherwise it has to wait until
298 // the blinking interval is over
299 if (cursor_visible_) {
304 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
306 update(0, 0, viewport()->width(), viewport()->height());
308 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
309 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
311 lyx_view_->updateStatusBar();
313 if (lyxerr.debugging(Debug::WORKAREA))
314 buffer_view_->coordCache().dump();
318 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
320 // In order to avoid bad surprise in the middle of an operation,
321 // we better stop the blinking cursor.
322 stopBlinkingCursor();
324 theLyXFunc().setLyXView(lyx_view_);
325 theLyXFunc().processKeySym(key, mod);
329 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
332 if (cmd0.action == LFUN_FILE_OPEN) {
333 lyx_view_->dispatch(cmd0);
337 theLyXFunc().setLyXView(lyx_view_);
341 if (cmd0.action == LFUN_MOUSE_PRESS) {
342 if (mod == ShiftModifier)
343 cmd = FuncRequest(cmd0, "region-select");
344 else if (mod == ControlModifier)
345 cmd = FuncRequest(cmd0, "paragraph-select");
352 // In order to avoid bad surprise in the middle of an operation, we better stop
353 // the blinking cursor.
354 if (!(cmd.action == LFUN_MOUSE_MOTION
355 && cmd.button() == mouse_button::none))
356 stopBlinkingCursor();
358 buffer_view_->mouseEventDispatch(cmd);
360 // Skip these when selecting
361 if (cmd.action != LFUN_MOUSE_MOTION) {
362 lyx_view_->updateLayoutList();
363 lyx_view_->updateToolbars();
366 // GUI tweaks except with mouse motion with no button pressed.
367 if (!(cmd.action == LFUN_MOUSE_MOTION
368 && cmd.button() == mouse_button::none)) {
369 // Slight hack: this is only called currently when we
370 // clicked somewhere, so we force through the display
371 // of the new status here.
372 lyx_view_->clearMessage();
374 // Show the cursor immediately after any operation.
375 startBlinkingCursor();
380 void GuiWorkArea::resizeBufferView()
382 // WARNING: Please don't put any code that will trigger a repaint here!
383 // We are already inside a paint event.
384 lyx_view_->setBusy(true);
385 buffer_view_->resize(viewport()->width(), viewport()->height());
386 lyx_view_->updateLayoutList();
387 lyx_view_->setBusy(false);
388 need_resize_ = false;
392 void GuiWorkArea::showCursor()
397 CursorShape shape = BAR_SHAPE;
399 Font const & realfont = buffer_view_->cursor().real_current_font;
400 BufferParams const & bp = buffer_view_->buffer().params();
401 bool const samelang = realfont.language() == bp.language;
402 bool const isrtl = realfont.isVisibleRightToLeft();
404 if (!samelang || isrtl != bp.language->rightToLeft()) {
407 shape = REVERSED_L_SHAPE;
410 // The ERT language hack needs fixing up
411 if (realfont.language() == latex_language)
414 Font const font = buffer_view_->cursor().getFont();
415 FontMetrics const & fm = theFontMetrics(font);
416 int const asc = fm.maxAscent();
417 int const des = fm.maxDescent();
421 buffer_view_->cursor().getPos(x, y);
424 // if it doesn't touch the screen, don't try to show it
425 if (y + h < 0 || y >= viewport()->height())
428 cursor_visible_ = true;
429 showCursor(x, y, h, shape);
433 void GuiWorkArea::hideCursor()
435 if (!cursor_visible_)
438 cursor_visible_ = false;
443 void GuiWorkArea::toggleCursor()
450 // Use this opportunity to deal with any child processes that
451 // have finished but are waiting to communicate this fact
452 // to the rest of LyX.
453 ForkedCallsController::handleCompletedProcesses();
457 void GuiWorkArea::updateScrollbar()
459 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
461 verticalScrollBar()->setTracking(false);
463 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
464 verticalScrollBar()->setPageStep(scroll_.page_step);
465 verticalScrollBar()->setSingleStep(scroll_.single_step);
466 verticalScrollBar()->setValue(scroll_.position);
467 verticalScrollBar()->setSliderPosition(scroll_.position);
469 verticalScrollBar()->setTracking(true);
473 void GuiWorkArea::adjustViewWithScrollBar(int action)
475 stopBlinkingCursor();
476 if (action == QAbstractSlider::SliderPageStepAdd)
477 buffer_view_->scrollDown(viewport()->height());
478 else if (action == QAbstractSlider::SliderPageStepSub)
479 buffer_view_->scrollUp(viewport()->height());
481 buffer_view_->scrollDocView(verticalScrollBar()->value());
483 if (lyxrc.cursor_follows_scrollbar) {
484 buffer_view_->setCursorFromScrollbar();
485 lyx_view_->updateLayoutList();
487 // Show the cursor immediately after any operation.
488 startBlinkingCursor();
489 QApplication::syncX();
493 bool GuiWorkArea::event(QEvent * e)
496 case QEvent::ToolTip: {
497 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
498 if (lyxrc.use_tooltip) {
499 QPoint pos = helpEvent->pos();
500 if (pos.x() < viewport()->width()) {
501 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
502 QToolTip::showText(helpEvent->globalPos(), s);
505 QToolTip::hideText();
507 // Don't forget to accept the event!
512 case QEvent::ShortcutOverride: {
513 // We catch this event in order to catch the Tab or Shift+Tab key press
514 // which are otherwise reserved to focus switching between controls
516 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
517 if (ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
518 return QAbstractScrollArea::event(e);
524 return QAbstractScrollArea::event(e);
530 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
532 QPoint pos = e->pos();
533 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
535 QAbstractScrollArea::contextMenuEvent(e);
538 QMenu * menu = guiApp->menus().menu(toqstr(name));
540 QAbstractScrollArea::contextMenuEvent(e);
543 // Position the menu to the right.
544 // FIXME: menu position should be different for RTL text.
545 pos.rx() += menu->width();
546 // FIXME: correct vertical position of the menu WRT screen espace.
547 //pos.ry() += menu->height();
553 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
555 // Repaint the whole screen.
556 // Note: this is different from redraw() as only the backing pixmap
557 // will be redrawn, which is cheap.
558 viewport()->repaint();
560 startBlinkingCursor();
564 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
566 stopBlinkingCursor();
570 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
572 if (dc_event_.active && dc_event_ == *e) {
573 dc_event_.active = false;
574 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
575 q_button_state(e->button()));
580 inputContext()->reset();
582 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
583 q_button_state(e->button()));
584 dispatch(cmd, q_key_state(e->modifiers()));
588 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
590 if (synthetic_mouse_event_.timeout.running())
591 synthetic_mouse_event_.timeout.stop();
593 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
594 q_button_state(e->button()));
599 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
601 // we kill the triple click if we move
602 doubleClickTimeout();
603 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
604 q_motion_state(e->buttons()));
606 // If we're above or below the work area...
607 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
608 // Make sure only a synthetic event can cause a page scroll,
609 // so they come at a steady rate:
611 // _Force_ a scroll up:
614 cmd.y = viewport()->height();
615 // Store the event, to be handled when the timeout expires.
616 synthetic_mouse_event_.cmd = cmd;
618 if (synthetic_mouse_event_.timeout.running())
619 // Discard the event. Note that it _may_ be handled
620 // when the timeout expires if
621 // synthetic_mouse_event_.cmd has not been overwritten.
622 // Ie, when the timeout expires, we handle the
623 // most recent event but discard all others that
624 // occurred after the one used to start the timeout
625 // in the first place.
628 synthetic_mouse_event_.restart_timeout = true;
629 synthetic_mouse_event_.timeout.start();
630 // Fall through to handle this event...
633 } else if (synthetic_mouse_event_.timeout.running()) {
634 // Store the event, to be possibly handled when the timeout
636 // Once the timeout has expired, normal control is returned
637 // to mouseMoveEvent (restart_timeout = false).
638 // This results in a much smoother 'feel' when moving the
639 // mouse back into the work area.
640 synthetic_mouse_event_.cmd = cmd;
641 synthetic_mouse_event_.restart_timeout = false;
645 // Has anything changed on-screen since the last QMouseEvent
647 double const scrollbar_value = verticalScrollBar()->value();
648 if (e->x() != synthetic_mouse_event_.x_old ||
649 e->y() != synthetic_mouse_event_.y_old ||
650 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
651 // Yes it has. Store the params used to check this.
652 synthetic_mouse_event_.x_old = e->x();
653 synthetic_mouse_event_.y_old = e->y();
654 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
656 // ... and dispatch the event to the LyX core.
662 void GuiWorkArea::wheelEvent(QWheelEvent * e)
664 // Wheel rotation by one notch results in a delta() of 120 (see
665 // documentation of QWheelEvent)
666 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
667 verticalScrollBar()->setValue(verticalScrollBar()->value() -
668 lines * verticalScrollBar()->singleStep());
669 adjustViewWithScrollBar();
673 void GuiWorkArea::generateSyntheticMouseEvent()
675 // Set things off to generate the _next_ 'pseudo' event.
676 if (synthetic_mouse_event_.restart_timeout)
677 synthetic_mouse_event_.timeout.start();
679 // Has anything changed on-screen since the last timeout signal
681 double const scrollbar_value = verticalScrollBar()->value();
682 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
683 // Yes it has. Store the params used to check this.
684 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
686 // ... and dispatch the event to the LyX core.
687 dispatch(synthetic_mouse_event_.cmd);
692 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
694 // do nothing if there are other events
695 // (the auto repeated events come too fast)
696 // \todo FIXME: remove hard coded Qt keys, process the key binding
698 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
699 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
700 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
706 LYXERR(Debug::KEY, " count: " << ev->count()
707 << " text: " << fromqstr(ev->text())
708 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
711 setKeySymbol(&sym, ev);
712 processKeySym(sym, q_key_state(ev->modifiers()));
717 void GuiWorkArea::doubleClickTimeout()
719 dc_event_.active = false;
723 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
725 dc_event_ = DoubleClick(ev);
726 QTimer::singleShot(QApplication::doubleClickInterval(), this,
727 SLOT(doubleClickTimeout()));
728 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
730 q_button_state(ev->button()));
735 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
737 QAbstractScrollArea::resizeEvent(ev);
742 void GuiWorkArea::update(int x, int y, int w, int h)
744 viewport()->repaint(x, y, w, h);
748 void GuiWorkArea::paintEvent(QPaintEvent * ev)
750 QRect const rc = ev->rect();
751 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
752 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
755 screen_ = QPixmap(viewport()->width(), viewport()->height());
762 QPainter pain(viewport());
763 pain.drawPixmap(rc, screen_, rc);
768 void GuiWorkArea::updateScreen()
770 GuiPainter pain(&screen_);
771 buffer_view_->draw(pain);
775 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
777 if (schedule_redraw_) {
778 buffer_view_->updateMetrics();
780 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
781 schedule_redraw_ = false;
782 // Show the cursor immediately after the update.
788 cursor_->update(x, y, h, shape);
790 viewport()->update(cursor_->rect());
794 void GuiWorkArea::removeCursor()
797 //if (!qApp->focusWidget())
798 viewport()->update(cursor_->rect());
802 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
804 QString const & commit_string = e->commitString();
805 docstring const & preedit_string
806 = qstring_to_ucs4(e->preeditString());
808 if (!commit_string.isEmpty()) {
810 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
811 << " commitString: " << fromqstr(e->commitString()));
815 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
816 for (int i = 0; i != commit_string.size(); ++i) {
817 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
822 // Hide the cursor during the kana-kanji transformation.
823 if (preedit_string.empty())
824 startBlinkingCursor();
826 stopBlinkingCursor();
828 // last_width : for checking if last preedit string was/wasn't empty.
829 static bool last_width = false;
830 if (!last_width && preedit_string.empty()) {
831 // if last_width is last length of preedit string.
836 GuiPainter pain(&screen_);
837 buffer_view_->updateMetrics();
838 buffer_view_->draw(pain);
839 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
840 FontMetrics const & fm = theFontMetrics(font);
841 int height = fm.maxHeight();
842 int cur_x = cursor_->rect().left();
843 int cur_y = cursor_->rect().bottom();
845 // redraw area of preedit string.
846 update(0, cur_y - height, viewport()->width(),
847 (height + 1) * preedit_lines_);
849 if (preedit_string.empty()) {
857 // att : stores an IM attribute.
858 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
860 // get attributes of input method cursor.
861 // cursor_pos : cursor position in preedit string.
862 size_t cursor_pos = 0;
863 bool cursor_is_visible = false;
864 for (int i = 0; i != att.size(); ++i) {
865 if (att.at(i).type == QInputMethodEvent::Cursor) {
866 cursor_pos = att.at(i).start;
867 cursor_is_visible = att.at(i).length != 0;
872 size_t preedit_length = preedit_string.length();
874 // get position of selection in input method.
875 // FIXME: isn't there a way to do this simplier?
876 // rStart : cursor position in selected string in IM.
878 // rLength : selected string length in IM.
880 if (cursor_pos < preedit_length) {
881 for (int i = 0; i != att.size(); ++i) {
882 if (att.at(i).type == QInputMethodEvent::TextFormat) {
883 if (att.at(i).start <= int(cursor_pos)
884 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
885 rStart = att.at(i).start;
886 rLength = att.at(i).length;
887 if (!cursor_is_visible)
888 cursor_pos += rLength;
899 int const right_margin = rightMargin();
900 Painter::preedit_style ps;
901 // Most often there would be only one line:
903 for (size_t pos = 0; pos != preedit_length; ++pos) {
904 char_type const typed_char = preedit_string[pos];
905 // reset preedit string style
906 ps = Painter::preedit_default;
908 // if we reached the right extremity of the screen, go to next line.
909 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
910 cur_x = right_margin;
914 // preedit strings are displayed with dashed underline
915 // and partial strings are displayed white on black indicating
916 // that we are in selecting mode in the input method.
917 // FIXME: rLength == preedit_length is not a changing condition
918 // FIXME: should be put out of the loop.
920 && pos < rStart + rLength
921 && !(cursor_pos < rLength && rLength == preedit_length))
922 ps = Painter::preedit_selecting;
924 if (pos == cursor_pos
925 && (cursor_pos < rLength && rLength == preedit_length))
926 ps = Painter::preedit_cursor;
928 // draw one character and update cur_x.
929 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
932 // update the preedit string screen area.
933 update(0, cur_y - preedit_lines_*height, viewport()->width(),
934 (height + 1) * preedit_lines_);
936 // Don't forget to accept the event!
941 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
943 QRect cur_r(0,0,0,0);
945 // this is the CJK-specific composition window position.
946 case Qt::ImMicroFocus:
947 cur_r = cursor_->rect();
948 if (preedit_lines_ != 1)
950 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
951 // return lower right of cursor in LyX.
954 return QWidget::inputMethodQuery(query);
959 void GuiWorkArea::updateWindowTitle()
961 docstring maximize_title;
962 docstring minimize_title;
964 Buffer & buf = buffer_view_->buffer();
965 FileName const fileName = buf.fileName();
966 if (!fileName.empty()) {
967 maximize_title = fileName.displayName(30);
968 minimize_title = from_utf8(fileName.onlyFileName());
969 if (!buf.isClean()) {
970 maximize_title += _(" (changed)");
971 minimize_title += char_type('*');
973 if (buf.isReadonly())
974 maximize_title += _(" (read only)");
977 QString title = windowTitle();
978 QString new_title = toqstr(maximize_title);
979 if (title == new_title)
982 QWidget::setWindowTitle(new_title);
983 QWidget::setWindowIconText(toqstr(minimize_title));
988 void GuiWorkArea::setReadOnly(bool)
991 if (this == lyx_view_->currentWorkArea())
992 lyx_view_->updateBufferDependent(false);
996 ////////////////////////////////////////////////////////////////////
1000 ////////////////////////////////////////////////////////////////////
1002 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1004 QPalette pal = palette();
1005 pal.setColor(QPalette::Active, QPalette::Button,
1006 pal.color(QPalette::Active, QPalette::Window));
1007 pal.setColor(QPalette::Disabled, QPalette::Button,
1008 pal.color(QPalette::Disabled, QPalette::Window));
1009 pal.setColor(QPalette::Inactive, QPalette::Button,
1010 pal.color(QPalette::Inactive, QPalette::Window));
1012 QToolButton * closeTabButton = new QToolButton(this);
1013 closeTabButton->setPalette(pal);
1014 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1015 closeTabButton->setText("Close");
1016 closeTabButton->setAutoRaise(true);
1017 closeTabButton->setCursor(Qt::ArrowCursor);
1018 closeTabButton->setToolTip(tr("Close tab"));
1019 closeTabButton->setEnabled(true);
1021 QObject::connect(this, SIGNAL(currentChanged(int)),
1022 this, SLOT(on_currentTabChanged(int)));
1023 QObject::connect(closeTabButton, SIGNAL(clicked()),
1024 this, SLOT(closeCurrentTab()));
1026 setCornerWidget(closeTabButton);
1027 setUsesScrollButtons(true);
1031 void TabWorkArea::showBar(bool show)
1033 tabBar()->setEnabled(show);
1034 tabBar()->setVisible(show);
1038 GuiWorkArea * TabWorkArea::currentWorkArea()
1043 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1049 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1051 for (int i = 0; i != count(); ++i) {
1052 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1054 if (&wa->bufferView().buffer() == &buffer)
1061 void TabWorkArea::closeAll()
1064 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1072 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1074 BOOST_ASSERT(work_area);
1075 int index = indexOf(work_area);
1079 if (index == currentIndex())
1080 // Make sure the work area is up to date.
1081 on_currentTabChanged(index);
1083 // Switch to the work area.
1084 setCurrentIndex(index);
1085 work_area->setFocus();
1091 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1093 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1094 wa->setUpdatesEnabled(false);
1095 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1096 // when hiding it again below).
1097 showBar(count() > 0);
1098 addTab(wa, wa->windowTitle());
1099 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1100 this, SLOT(updateTabText(GuiWorkArea *)));
1101 // Hide tabbar if there's only one tab.
1102 showBar(count() > 1);
1107 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1109 BOOST_ASSERT(work_area);
1110 int index = indexOf(work_area);
1114 work_area->setUpdatesEnabled(false);
1119 // make sure the next work area is enabled.
1120 currentWidget()->setUpdatesEnabled(true);
1121 // Hide tabbar if there's only one tab.
1122 showBar(count() > 1);
1128 void TabWorkArea::on_currentTabChanged(int i)
1130 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1132 BufferView & bv = wa->bufferView();
1133 bv.cursor().fixIfBroken();
1135 wa->setUpdatesEnabled(true);
1139 currentWorkAreaChanged(wa);
1141 LYXERR(Debug::GUI, "currentTabChanged " << i
1142 << "File" << bv.buffer().absFileName());
1146 void TabWorkArea::closeCurrentTab()
1148 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1152 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1154 int const i = indexOf(wa);
1157 setTabText(i, wa->windowTitle());
1160 } // namespace frontend
1163 #include "GuiWorkArea_moc.cpp"