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 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 // PageStep only depends on the viewport height.
235 verticalScrollBar()->setPageStep(viewport()->height());
237 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
238 << " viewport height: " << viewport()->height());
240 // Enables input methods for asian languages.
241 // Must be set when creating custom text editing widgets.
242 setAttribute(Qt::WA_InputMethodEnabled, true);
247 GuiWorkArea::~GuiWorkArea()
249 buffer_view_->buffer().workAreaManager().remove(this);
255 void GuiWorkArea::close()
257 lyx_view_->removeWorkArea(this);
261 BufferView & GuiWorkArea::bufferView()
263 return *buffer_view_;
267 BufferView const & GuiWorkArea::bufferView() const
269 return *buffer_view_;
273 void GuiWorkArea::stopBlinkingCursor()
275 cursor_timeout_.stop();
280 void GuiWorkArea::startBlinkingCursor()
283 cursor_timeout_.start();
287 void GuiWorkArea::redraw()
290 // No need to redraw in this case.
293 // No need to do anything if this is the current view. The BufferView
294 // metrics are already up to date.
295 if (lyx_view_ != guiApp->currentView()) {
296 // FIXME: it would be nice to optimize for the off-screen case.
297 buffer_view_->updateMetrics();
298 buffer_view_->cursor().fixIfBroken();
301 // update cursor position, because otherwise it has to wait until
302 // the blinking interval is over
303 if (cursor_visible_) {
308 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
310 update(0, 0, viewport()->width(), viewport()->height());
312 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
313 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
315 lyx_view_->updateStatusBar();
317 if (lyxerr.debugging(Debug::WORKAREA))
318 buffer_view_->coordCache().dump();
322 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
324 // In order to avoid bad surprise in the middle of an operation,
325 // we better stop the blinking cursor.
326 stopBlinkingCursor();
328 theLyXFunc().setLyXView(lyx_view_);
329 theLyXFunc().processKeySym(key, mod);
333 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
336 if (cmd0.action == LFUN_FILE_OPEN) {
337 lyx_view_->dispatch(cmd0);
341 theLyXFunc().setLyXView(lyx_view_);
345 if (cmd0.action == LFUN_MOUSE_PRESS) {
346 if (mod == ShiftModifier)
347 cmd = FuncRequest(cmd0, "region-select");
348 else if (mod == ControlModifier)
349 cmd = FuncRequest(cmd0, "paragraph-select");
356 // In order to avoid bad surprise in the middle of an operation, we better stop
357 // the blinking cursor.
358 if (!(cmd.action == LFUN_MOUSE_MOTION
359 && cmd.button() == mouse_button::none))
360 stopBlinkingCursor();
362 buffer_view_->mouseEventDispatch(cmd);
364 // Skip these when selecting
365 if (cmd.action != LFUN_MOUSE_MOTION) {
366 lyx_view_->updateLayoutList();
367 lyx_view_->updateToolbars();
370 // GUI tweaks except with mouse motion with no button pressed.
371 if (!(cmd.action == LFUN_MOUSE_MOTION
372 && cmd.button() == mouse_button::none)) {
373 // Slight hack: this is only called currently when we
374 // clicked somewhere, so we force through the display
375 // of the new status here.
376 lyx_view_->clearMessage();
378 // Show the cursor immediately after any operation.
379 startBlinkingCursor();
384 void GuiWorkArea::resizeBufferView()
386 // WARNING: Please don't put any code that will trigger a repaint here!
387 // We are already inside a paint event.
388 lyx_view_->setBusy(true);
389 buffer_view_->resize(viewport()->width(), viewport()->height());
390 lyx_view_->updateLayoutList();
391 lyx_view_->setBusy(false);
392 need_resize_ = false;
396 void GuiWorkArea::showCursor()
401 CursorShape shape = BAR_SHAPE;
403 Font const & realfont = buffer_view_->cursor().real_current_font;
404 BufferParams const & bp = buffer_view_->buffer().params();
405 bool const samelang = realfont.language() == bp.language;
406 bool const isrtl = realfont.isVisibleRightToLeft();
408 if (!samelang || isrtl != bp.language->rightToLeft()) {
411 shape = REVERSED_L_SHAPE;
414 // The ERT language hack needs fixing up
415 if (realfont.language() == latex_language)
418 Font const font = buffer_view_->cursor().getFont();
419 FontMetrics const & fm = theFontMetrics(font);
420 int const asc = fm.maxAscent();
421 int const des = fm.maxDescent();
425 buffer_view_->cursor().getPos(x, y);
428 // if it doesn't touch the screen, don't try to show it
429 if (y + h < 0 || y >= viewport()->height())
432 cursor_visible_ = true;
433 showCursor(x, y, h, shape);
437 void GuiWorkArea::hideCursor()
439 if (!cursor_visible_)
442 cursor_visible_ = false;
447 void GuiWorkArea::toggleCursor()
454 // Use this opportunity to deal with any child processes that
455 // have finished but are waiting to communicate this fact
456 // to the rest of LyX.
457 ForkedCallsController::handleCompletedProcesses();
461 void GuiWorkArea::updateScrollbar()
463 verticalScrollBar()->setTracking(false);
465 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
467 // do what cursor movement does (some grey)
468 int const h = scroll_.height + viewport()->height() / 4;
469 int scroll_max_ = max(0, h - viewport()->height());
471 verticalScrollBar()->setRange(0, scroll_max_);
472 verticalScrollBar()->setSliderPosition(scroll_.position);
473 verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
474 verticalScrollBar()->setValue(scroll_.position);
476 verticalScrollBar()->setTracking(true);
480 void GuiWorkArea::adjustViewWithScrollBar(int action)
482 stopBlinkingCursor();
483 if (action == QAbstractSlider::SliderPageStepAdd)
484 buffer_view_->scrollDown(viewport()->height());
485 else if (action == QAbstractSlider::SliderPageStepSub)
486 buffer_view_->scrollUp(viewport()->height());
488 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
490 if (lyxrc.cursor_follows_scrollbar) {
491 buffer_view_->setCursorFromScrollbar();
492 lyx_view_->updateLayoutList();
494 // Show the cursor immediately after any operation.
495 startBlinkingCursor();
496 QApplication::syncX();
500 bool GuiWorkArea::event(QEvent * e)
502 if (e->type() == QEvent::ToolTip) {
503 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
504 if (!lyxrc.use_tooltip)
505 return QAbstractScrollArea::event(e);
506 QPoint pos = helpEvent->pos();
507 if (pos.x() < viewport()->width()) {
508 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
509 QToolTip::showText(helpEvent->globalPos(), s);
512 QToolTip::hideText();
514 return QAbstractScrollArea::event(e);
518 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
520 QPoint pos = e->pos();
521 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
523 QAbstractScrollArea::contextMenuEvent(e);
526 QMenu * menu = guiApp->menus().menu(toqstr(name));
528 QAbstractScrollArea::contextMenuEvent(e);
531 // Position the menu to the right.
532 // FIXME: menu position should be different for RTL text.
533 pos.rx() += menu->width();
534 // FIXME: correct vertical position of the menu WRT screen espace.
535 //pos.ry() += menu->height();
541 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
543 // Repaint the whole screen.
544 // Note: this is different from redraw() as only the backing pixmap
545 // will be redrawn, which is cheap.
546 viewport()->repaint();
548 startBlinkingCursor();
552 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
554 stopBlinkingCursor();
558 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
560 if (dc_event_.active && dc_event_ == *e) {
561 dc_event_.active = false;
562 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
563 q_button_state(e->button()));
568 inputContext()->reset();
570 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
571 q_button_state(e->button()));
572 dispatch(cmd, q_key_state(e->modifiers()));
576 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
578 if (synthetic_mouse_event_.timeout.running())
579 synthetic_mouse_event_.timeout.stop();
581 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
582 q_button_state(e->button()));
587 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
589 // we kill the triple click if we move
590 doubleClickTimeout();
591 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
592 q_motion_state(e->buttons()));
594 // If we're above or below the work area...
595 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
596 // Make sure only a synthetic event can cause a page scroll,
597 // so they come at a steady rate:
599 // _Force_ a scroll up:
602 cmd.y = viewport()->height();
603 // Store the event, to be handled when the timeout expires.
604 synthetic_mouse_event_.cmd = cmd;
606 if (synthetic_mouse_event_.timeout.running())
607 // Discard the event. Note that it _may_ be handled
608 // when the timeout expires if
609 // synthetic_mouse_event_.cmd has not been overwritten.
610 // Ie, when the timeout expires, we handle the
611 // most recent event but discard all others that
612 // occurred after the one used to start the timeout
613 // in the first place.
616 synthetic_mouse_event_.restart_timeout = true;
617 synthetic_mouse_event_.timeout.start();
618 // Fall through to handle this event...
621 } else if (synthetic_mouse_event_.timeout.running()) {
622 // Store the event, to be possibly handled when the timeout
624 // Once the timeout has expired, normal control is returned
625 // to mouseMoveEvent (restart_timeout = false).
626 // This results in a much smoother 'feel' when moving the
627 // mouse back into the work area.
628 synthetic_mouse_event_.cmd = cmd;
629 synthetic_mouse_event_.restart_timeout = false;
633 // Has anything changed on-screen since the last QMouseEvent
635 double const scrollbar_value = verticalScrollBar()->value();
636 if (e->x() != synthetic_mouse_event_.x_old ||
637 e->y() != synthetic_mouse_event_.y_old ||
638 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
639 // Yes it has. Store the params used to check this.
640 synthetic_mouse_event_.x_old = e->x();
641 synthetic_mouse_event_.y_old = e->y();
642 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
644 // ... and dispatch the event to the LyX core.
650 void GuiWorkArea::wheelEvent(QWheelEvent * e)
652 // Wheel rotation by one notch results in a delta() of 120 (see
653 // documentation of QWheelEvent)
654 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
655 verticalScrollBar()->setValue(verticalScrollBar()->value() -
656 lines * verticalScrollBar()->singleStep());
657 adjustViewWithScrollBar();
661 void GuiWorkArea::generateSyntheticMouseEvent()
663 // Set things off to generate the _next_ 'pseudo' event.
664 if (synthetic_mouse_event_.restart_timeout)
665 synthetic_mouse_event_.timeout.start();
667 // Has anything changed on-screen since the last timeout signal
669 double const scrollbar_value = verticalScrollBar()->value();
670 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
671 // Yes it has. Store the params used to check this.
672 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
674 // ... and dispatch the event to the LyX core.
675 dispatch(synthetic_mouse_event_.cmd);
680 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
682 // do nothing if there are other events
683 // (the auto repeated events come too fast)
684 // \todo FIXME: remove hard coded Qt keys, process the key binding
686 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
687 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
688 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
694 LYXERR(Debug::KEY, " count: " << ev->count()
695 << " text: " << fromqstr(ev->text())
696 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
699 setKeySymbol(&sym, ev);
700 processKeySym(sym, q_key_state(ev->modifiers()));
704 void GuiWorkArea::doubleClickTimeout()
706 dc_event_.active = false;
710 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
712 dc_event_ = DoubleClick(ev);
713 QTimer::singleShot(QApplication::doubleClickInterval(), this,
714 SLOT(doubleClickTimeout()));
715 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
717 q_button_state(ev->button()));
722 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
724 QAbstractScrollArea::resizeEvent(ev);
729 void GuiWorkArea::update(int x, int y, int w, int h)
731 viewport()->repaint(x, y, w, h);
735 void GuiWorkArea::paintEvent(QPaintEvent * ev)
737 QRect const rc = ev->rect();
738 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
739 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
742 verticalScrollBar()->setPageStep(viewport()->height());
743 screen_ = QPixmap(viewport()->width(), viewport()->height());
750 QPainter pain(viewport());
751 pain.drawPixmap(rc, screen_, rc);
756 void GuiWorkArea::updateScreen()
758 GuiPainter pain(&screen_);
759 buffer_view_->draw(pain);
763 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
765 if (schedule_redraw_) {
766 buffer_view_->updateMetrics();
768 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
769 schedule_redraw_ = false;
770 // Show the cursor immediately after the update.
776 cursor_->update(x, y, h, shape);
778 viewport()->update(cursor_->rect());
782 void GuiWorkArea::removeCursor()
785 //if (!qApp->focusWidget())
786 viewport()->update(cursor_->rect());
790 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
792 QString const & commit_string = e->commitString();
793 docstring const & preedit_string
794 = qstring_to_ucs4(e->preeditString());
796 if (!commit_string.isEmpty()) {
798 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
799 << " commitString: " << fromqstr(e->commitString()));
803 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
804 for (int i = 0; i != commit_string.size(); ++i) {
805 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
810 // Hide the cursor during the kana-kanji transformation.
811 if (preedit_string.empty())
812 startBlinkingCursor();
814 stopBlinkingCursor();
816 // last_width : for checking if last preedit string was/wasn't empty.
817 static bool last_width = false;
818 if (!last_width && preedit_string.empty()) {
819 // if last_width is last length of preedit string.
824 GuiPainter pain(&screen_);
825 buffer_view_->updateMetrics();
826 buffer_view_->draw(pain);
827 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
828 FontMetrics const & fm = theFontMetrics(font);
829 int height = fm.maxHeight();
830 int cur_x = cursor_->rect().left();
831 int cur_y = cursor_->rect().bottom();
833 // redraw area of preedit string.
834 update(0, cur_y - height, viewport()->width(),
835 (height + 1) * preedit_lines_);
837 if (preedit_string.empty()) {
845 // att : stores an IM attribute.
846 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
848 // get attributes of input method cursor.
849 // cursor_pos : cursor position in preedit string.
850 size_t cursor_pos = 0;
851 bool cursor_is_visible = false;
852 for (int i = 0; i != att.size(); ++i) {
853 if (att.at(i).type == QInputMethodEvent::Cursor) {
854 cursor_pos = att.at(i).start;
855 cursor_is_visible = att.at(i).length != 0;
860 size_t preedit_length = preedit_string.length();
862 // get position of selection in input method.
863 // FIXME: isn't there a way to do this simplier?
864 // rStart : cursor position in selected string in IM.
866 // rLength : selected string length in IM.
868 if (cursor_pos < preedit_length) {
869 for (int i = 0; i != att.size(); ++i) {
870 if (att.at(i).type == QInputMethodEvent::TextFormat) {
871 if (att.at(i).start <= int(cursor_pos)
872 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
873 rStart = att.at(i).start;
874 rLength = att.at(i).length;
875 if (!cursor_is_visible)
876 cursor_pos += rLength;
887 int const right_margin = rightMargin();
888 Painter::preedit_style ps;
889 // Most often there would be only one line:
891 for (size_t pos = 0; pos != preedit_length; ++pos) {
892 char_type const typed_char = preedit_string[pos];
893 // reset preedit string style
894 ps = Painter::preedit_default;
896 // if we reached the right extremity of the screen, go to next line.
897 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
898 cur_x = right_margin;
902 // preedit strings are displayed with dashed underline
903 // and partial strings are displayed white on black indicating
904 // that we are in selecting mode in the input method.
905 // FIXME: rLength == preedit_length is not a changing condition
906 // FIXME: should be put out of the loop.
908 && pos < rStart + rLength
909 && !(cursor_pos < rLength && rLength == preedit_length))
910 ps = Painter::preedit_selecting;
912 if (pos == cursor_pos
913 && (cursor_pos < rLength && rLength == preedit_length))
914 ps = Painter::preedit_cursor;
916 // draw one character and update cur_x.
917 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
920 // update the preedit string screen area.
921 update(0, cur_y - preedit_lines_*height, viewport()->width(),
922 (height + 1) * preedit_lines_);
924 // Don't forget to accept the event!
929 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
931 QRect cur_r(0,0,0,0);
933 // this is the CJK-specific composition window position.
934 case Qt::ImMicroFocus:
935 cur_r = cursor_->rect();
936 if (preedit_lines_ != 1)
938 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
939 // return lower right of cursor in LyX.
942 return QWidget::inputMethodQuery(query);
947 void GuiWorkArea::updateWindowTitle()
949 docstring maximize_title;
950 docstring minimize_title;
952 Buffer & buf = buffer_view_->buffer();
953 FileName const fileName = buf.fileName();
954 if (!fileName.empty()) {
955 maximize_title = fileName.displayName(30);
956 minimize_title = from_utf8(fileName.onlyFileName());
957 if (!buf.isClean()) {
958 maximize_title += _(" (changed)");
959 minimize_title += char_type('*');
961 if (buf.isReadonly())
962 maximize_title += _(" (read only)");
965 QString title = windowTitle();
966 QString new_title = toqstr(maximize_title);
967 if (title == new_title)
970 QWidget::setWindowTitle(new_title);
971 QWidget::setWindowIconText(toqstr(minimize_title));
976 void GuiWorkArea::setReadOnly(bool)
979 if (this == lyx_view_->currentWorkArea())
980 lyx_view_->updateBufferDependent(false);
984 ////////////////////////////////////////////////////////////////////
988 ////////////////////////////////////////////////////////////////////
990 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
992 QPalette pal = palette();
993 pal.setColor(QPalette::Active, QPalette::Button,
994 pal.color(QPalette::Active, QPalette::Window));
995 pal.setColor(QPalette::Disabled, QPalette::Button,
996 pal.color(QPalette::Disabled, QPalette::Window));
997 pal.setColor(QPalette::Inactive, QPalette::Button,
998 pal.color(QPalette::Inactive, QPalette::Window));
1000 QToolButton * closeTabButton = new QToolButton(this);
1001 closeTabButton->setPalette(pal);
1002 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1003 closeTabButton->setText("Close");
1004 closeTabButton->setAutoRaise(true);
1005 closeTabButton->setCursor(Qt::ArrowCursor);
1006 closeTabButton->setToolTip(tr("Close tab"));
1007 closeTabButton->setEnabled(true);
1009 QObject::connect(this, SIGNAL(currentChanged(int)),
1010 this, SLOT(on_currentTabChanged(int)));
1011 QObject::connect(closeTabButton, SIGNAL(clicked()),
1012 this, SLOT(closeCurrentTab()));
1014 setCornerWidget(closeTabButton);
1015 setUsesScrollButtons(true);
1019 void TabWorkArea::showBar(bool show)
1021 tabBar()->setEnabled(show);
1022 tabBar()->setVisible(show);
1026 GuiWorkArea * TabWorkArea::currentWorkArea()
1031 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1037 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1039 for (int i = 0; i != count(); ++i) {
1040 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1042 if (&wa->bufferView().buffer() == &buffer)
1049 void TabWorkArea::closeAll()
1052 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1060 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1062 BOOST_ASSERT(work_area);
1063 int index = indexOf(work_area);
1067 if (index == currentIndex())
1068 // Make sure the work area is up to date.
1069 on_currentTabChanged(index);
1071 // Switch to the work area.
1072 setCurrentIndex(index);
1073 work_area->setFocus();
1079 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1081 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1082 wa->setUpdatesEnabled(false);
1083 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1084 // when hiding it again below).
1085 showBar(count() > 0);
1086 addTab(wa, wa->windowTitle());
1087 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1088 this, SLOT(updateTabText(GuiWorkArea *)));
1089 // Hide tabbar if there's only one tab.
1090 showBar(count() > 1);
1095 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1097 BOOST_ASSERT(work_area);
1098 int index = indexOf(work_area);
1102 work_area->setUpdatesEnabled(false);
1107 // make sure the next work area is enabled.
1108 currentWidget()->setUpdatesEnabled(true);
1109 // Hide tabbar if there's only one tab.
1110 showBar(count() > 1);
1116 void TabWorkArea::on_currentTabChanged(int i)
1118 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1120 BufferView & bv = wa->bufferView();
1121 bv.cursor().fixIfBroken();
1123 wa->setUpdatesEnabled(true);
1127 currentWorkAreaChanged(wa);
1129 LYXERR(Debug::GUI, "currentTabChanged " << i
1130 << "File" << bv.buffer().absFileName());
1134 void TabWorkArea::closeCurrentTab()
1136 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1140 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1142 int const i = indexOf(wa);
1145 setTabText(i, wa->windowTitle());
1148 } // namespace frontend
1151 #include "GuiWorkArea_moc.cpp"