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"
43 #include "frontends/Application.h"
44 #include "frontends/FontMetrics.h"
45 #include "frontends/WorkAreaManager.h"
47 #include <QContextMenuEvent>
48 #include <QInputContext>
51 #include <QMainWindow>
57 #include <QToolButton>
60 #include <boost/bind.hpp>
64 extern "C" int XEventsQueued(Display *display, int mode);
68 int const CursorWidth = 2;
70 int const CursorWidth = 1;
77 using namespace lyx::support;
82 /// return the LyX mouse button state from Qt's
83 static mouse_button::state q_button_state(Qt::MouseButton button)
85 mouse_button::state b = mouse_button::none;
88 b = mouse_button::button1;
91 b = mouse_button::button2;
94 b = mouse_button::button3;
103 /// return the LyX mouse button state from Qt's
104 mouse_button::state q_motion_state(Qt::MouseButtons state)
106 mouse_button::state b = mouse_button::none;
107 if (state & Qt::LeftButton)
108 b |= mouse_button::button1;
109 if (state & Qt::MidButton)
110 b |= mouse_button::button2;
111 if (state & Qt::RightButton)
112 b |= mouse_button::button3;
123 void draw(QPainter & painter)
125 if (show_ && rect_.isValid()) {
128 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
129 painter.setPen(color_);
130 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
131 rect_.bottomRight().x(), rect_.bottomLeft().y());
134 case REVERSED_L_SHAPE:
135 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
136 painter.setPen(color_);
137 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
138 rect_.bottomLeft().x(), rect_.bottomLeft().y());
142 painter.fillRect(rect_, color_);
148 void update(int x, int y, int h, CursorShape shape)
150 color_ = guiApp->colorCache().get(Color_cursor);
154 rect_ = QRect(x, y, CursorWidth + h / 3, h);
156 case REVERSED_L_SHAPE:
157 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
160 rect_ = QRect(x, y, CursorWidth, h);
165 void show(bool set_show = true) { show_ = set_show; }
166 void hide() { show_ = false; }
168 QRect const & rect() { return rect_; }
182 // This is a 'heartbeat' generating synthetic mouse move events when the
183 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
184 SyntheticMouseEvent::SyntheticMouseEvent()
185 : timeout(200), restart_timeout(true),
186 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
191 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
192 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
193 cursor_visible_(false),
194 need_resize_(false), schedule_redraw_(false),
197 buffer.workAreaManager().add(this);
199 connect(&cursor_timeout_, SIGNAL(timeout()),
200 this, SLOT(toggleCursor()));
202 int const time = QApplication::cursorFlashTime() / 2;
204 cursor_timeout_.setInterval(time);
205 cursor_timeout_.start();
207 // let's initialize this just to be safe
208 cursor_timeout_.setInterval(500);
210 screen_ = QPixmap(viewport()->width(), viewport()->height());
211 cursor_ = new frontend::CursorWidget();
214 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
215 setAcceptDrops(true);
216 setMouseTracking(true);
217 setMinimumSize(100, 70);
220 viewport()->setAutoFillBackground(false);
221 // We don't need double-buffering nor SystemBackground on
222 // the viewport because we have our own backing pixmap.
223 viewport()->setAttribute(Qt::WA_NoSystemBackground);
225 setFocusPolicy(Qt::WheelFocus);
227 viewport()->setCursor(Qt::IBeamCursor);
229 synthetic_mouse_event_.timeout.timeout.connect(
230 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
233 // Initialize the vertical Scroll Bar
234 QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
235 this, SLOT(scrollTo(int)));
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 //we're not supposed to cache this value.
284 int const time = QApplication::cursorFlashTime() / 2;
287 cursor_timeout_.setInterval(time);
288 cursor_timeout_.start();
292 void GuiWorkArea::redraw()
295 // No need to redraw in this case.
298 // No need to do anything if this is the current view. The BufferView
299 // metrics are already up to date.
300 if (lyx_view_ != guiApp->currentView()) {
301 // FIXME: it would be nice to optimize for the off-screen case.
302 buffer_view_->updateMetrics();
303 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 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
318 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
320 lyx_view_->updateStatusBar();
322 if (lyxerr.debugging(Debug::WORKAREA))
323 buffer_view_->coordCache().dump();
327 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
329 // In order to avoid bad surprise in the middle of an operation,
330 // we better stop the blinking cursor...
331 // the cursor gets restarted in GuiView::restartCursor()
332 stopBlinkingCursor();
334 theLyXFunc().setLyXView(lyx_view_);
335 theLyXFunc().processKeySym(key, mod);
340 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
343 if (cmd0.action == LFUN_FILE_OPEN) {
344 lyx_view_->dispatch(cmd0);
348 theLyXFunc().setLyXView(lyx_view_);
352 if (cmd0.action == LFUN_MOUSE_PRESS) {
353 if (mod == ShiftModifier)
354 cmd = FuncRequest(cmd0, "region-select");
355 else if (mod == ControlModifier)
356 cmd = FuncRequest(cmd0, "paragraph-select");
363 bool const notJustMovingTheMouse =
364 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
366 // In order to avoid bad surprise in the middle of an operation, we better stop
367 // the blinking cursor.
368 if (notJustMovingTheMouse)
369 stopBlinkingCursor();
371 buffer_view_->mouseEventDispatch(cmd);
373 // Skip these when selecting
374 if (cmd.action != LFUN_MOUSE_MOTION) {
375 lyx_view_->updateLayoutList();
376 lyx_view_->updateToolbars();
379 // GUI tweaks except with mouse motion with no button pressed.
380 if (notJustMovingTheMouse) {
381 // Slight hack: this is only called currently when we
382 // clicked somewhere, so we force through the display
383 // of the new status here.
384 lyx_view_->clearMessage();
386 // Show the cursor immediately after any operation
387 startBlinkingCursor();
392 void GuiWorkArea::resizeBufferView()
394 // WARNING: Please don't put any code that will trigger a repaint here!
395 // We are already inside a paint event.
396 lyx_view_->setBusy(true);
397 buffer_view_->resize(viewport()->width(), viewport()->height());
398 lyx_view_->updateLayoutList();
399 lyx_view_->setBusy(false);
400 need_resize_ = 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()
464 void GuiWorkArea::updateScrollbar()
466 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
468 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
469 verticalScrollBar()->setPageStep(scroll_.page_step);
470 verticalScrollBar()->setSingleStep(scroll_.single_step);
471 // Block the scrollbar signal to prevent recursive signal/slot calling.
472 verticalScrollBar()->blockSignals(true);
473 verticalScrollBar()->setValue(scroll_.position);
474 verticalScrollBar()->setSliderPosition(scroll_.position);
475 verticalScrollBar()->blockSignals(false);
479 void GuiWorkArea::scrollTo(int value)
481 stopBlinkingCursor();
482 buffer_view_->scrollDocView(value);
484 if (lyxrc.cursor_follows_scrollbar) {
485 buffer_view_->setCursorFromScrollbar();
486 lyx_view_->updateLayoutList();
488 // Show the cursor immediately after any operation.
489 startBlinkingCursor();
490 QApplication::syncX();
494 bool GuiWorkArea::event(QEvent * e)
497 case QEvent::ToolTip: {
498 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
499 if (lyxrc.use_tooltip) {
500 QPoint pos = helpEvent->pos();
501 if (pos.x() < viewport()->width()) {
502 QString s = toqstr(buffer_view_->toolTip(pos.x(), pos.y()));
503 QToolTip::showText(helpEvent->globalPos(), s);
506 QToolTip::hideText();
508 // Don't forget to accept the event!
513 case QEvent::ShortcutOverride: {
514 // We catch this event in order to catch the Tab or Shift+Tab key press
515 // which are otherwise reserved to focus switching between controls
517 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
518 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
519 || ke->modifiers() & Qt::ControlModifier)
520 return QAbstractScrollArea::event(e);
526 return QAbstractScrollArea::event(e);
532 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
534 QPoint pos = e->pos();
535 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
537 QAbstractScrollArea::contextMenuEvent(e);
540 QMenu * menu = guiApp->menus().menu(toqstr(name));
542 QAbstractScrollArea::contextMenuEvent(e);
545 // Position the menu to the right.
546 // FIXME: menu position should be different for RTL text.
547 pos.rx() += menu->width();
548 // FIXME: correct vertical position of the menu WRT screen espace.
549 //pos.ry() += menu->height();
555 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
557 // Repaint the whole screen.
558 // Note: this is different from redraw() as only the backing pixmap
559 // will be redrawn, which is cheap.
560 viewport()->repaint();
562 startBlinkingCursor();
566 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
568 stopBlinkingCursor();
572 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
574 if (dc_event_.active && dc_event_ == *e) {
575 dc_event_.active = false;
576 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
577 q_button_state(e->button()));
582 inputContext()->reset();
584 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
585 q_button_state(e->button()));
586 dispatch(cmd, q_key_state(e->modifiers()));
590 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
592 if (synthetic_mouse_event_.timeout.running())
593 synthetic_mouse_event_.timeout.stop();
595 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
596 q_button_state(e->button()));
601 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
603 // we kill the triple click if we move
604 doubleClickTimeout();
605 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
606 q_motion_state(e->buttons()));
608 // If we're above or below the work area...
609 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
610 // Make sure only a synthetic event can cause a page scroll,
611 // so they come at a steady rate:
613 // _Force_ a scroll up:
616 cmd.y = viewport()->height();
617 // Store the event, to be handled when the timeout expires.
618 synthetic_mouse_event_.cmd = cmd;
620 if (synthetic_mouse_event_.timeout.running())
621 // Discard the event. Note that it _may_ be handled
622 // when the timeout expires if
623 // synthetic_mouse_event_.cmd has not been overwritten.
624 // Ie, when the timeout expires, we handle the
625 // most recent event but discard all others that
626 // occurred after the one used to start the timeout
627 // in the first place.
630 synthetic_mouse_event_.restart_timeout = true;
631 synthetic_mouse_event_.timeout.start();
632 // Fall through to handle this event...
635 } else if (synthetic_mouse_event_.timeout.running()) {
636 // Store the event, to be possibly handled when the timeout
638 // Once the timeout has expired, normal control is returned
639 // to mouseMoveEvent (restart_timeout = false).
640 // This results in a much smoother 'feel' when moving the
641 // mouse back into the work area.
642 synthetic_mouse_event_.cmd = cmd;
643 synthetic_mouse_event_.restart_timeout = false;
647 // Has anything changed on-screen since the last QMouseEvent
649 double const scrollbar_value = verticalScrollBar()->value();
650 if (e->x() != synthetic_mouse_event_.x_old ||
651 e->y() != synthetic_mouse_event_.y_old ||
652 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
653 // Yes it has. Store the params used to check this.
654 synthetic_mouse_event_.x_old = e->x();
655 synthetic_mouse_event_.y_old = e->y();
656 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
658 // ... and dispatch the event to the LyX core.
664 void GuiWorkArea::wheelEvent(QWheelEvent * e)
666 // Wheel rotation by one notch results in a delta() of 120 (see
667 // documentation of QWheelEvent)
668 double const lines = qApp->wheelScrollLines()
669 * lyxrc.mouse_wheel_speed
670 * e->delta() / 120.0;
671 LYXERR(Debug::SCROLLING, "wheelScrollLines = " << qApp->wheelScrollLines()
672 << " delta = " << e->delta()
673 << " lines = " << lines);
674 verticalScrollBar()->setValue(verticalScrollBar()->value() -
675 int(lines * verticalScrollBar()->singleStep()));
679 void GuiWorkArea::generateSyntheticMouseEvent()
681 // Set things off to generate the _next_ 'pseudo' event.
682 if (synthetic_mouse_event_.restart_timeout)
683 synthetic_mouse_event_.timeout.start();
685 // Has anything changed on-screen since the last timeout signal
687 double const scrollbar_value = verticalScrollBar()->value();
688 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
689 // Yes it has. Store the params used to check this.
690 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
692 // ... and dispatch the event to the LyX core.
693 dispatch(synthetic_mouse_event_.cmd);
698 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
700 // do nothing if there are other events
701 // (the auto repeated events come too fast)
702 // \todo FIXME: remove hard coded Qt keys, process the key binding
704 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
705 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
706 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
712 LYXERR(Debug::KEY, " count: " << ev->count()
713 << " text: " << fromqstr(ev->text())
714 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
717 setKeySymbol(&sym, ev);
718 processKeySym(sym, q_key_state(ev->modifiers()));
723 void GuiWorkArea::doubleClickTimeout()
725 dc_event_.active = false;
729 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
731 dc_event_ = DoubleClick(ev);
732 QTimer::singleShot(QApplication::doubleClickInterval(), this,
733 SLOT(doubleClickTimeout()));
734 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
736 q_button_state(ev->button()));
741 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
743 QAbstractScrollArea::resizeEvent(ev);
748 void GuiWorkArea::update(int x, int y, int w, int h)
750 viewport()->repaint(x, y, w, h);
754 void GuiWorkArea::paintEvent(QPaintEvent * ev)
756 QRect const rc = ev->rect();
757 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
758 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
761 screen_ = QPixmap(viewport()->width(), viewport()->height());
764 // Update scrollbars which might have changed due different
765 // BufferView dimension. This is especially important when the
766 // BufferView goes from zero-size to the real-size for the first time,
767 // as the scrollbar paramters are then set for the first time.
769 BOOST_ASSERT(need_resize_ == false);
776 QPainter pain(viewport());
777 pain.drawPixmap(rc, screen_, rc);
782 void GuiWorkArea::updateScreen()
784 GuiPainter pain(&screen_);
785 buffer_view_->draw(pain);
789 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
791 if (schedule_redraw_) {
792 buffer_view_->updateMetrics();
794 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
795 schedule_redraw_ = false;
796 // Show the cursor immediately after the update.
802 cursor_->update(x, y, h, shape);
804 viewport()->update(cursor_->rect());
808 void GuiWorkArea::removeCursor()
811 //if (!qApp->focusWidget())
812 viewport()->update(cursor_->rect());
816 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
818 QString const & commit_string = e->commitString();
819 docstring const & preedit_string
820 = qstring_to_ucs4(e->preeditString());
822 if (!commit_string.isEmpty()) {
824 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
825 << " commitString: " << fromqstr(e->commitString()));
829 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
830 for (int i = 0; i != commit_string.size(); ++i) {
831 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
836 // Hide the cursor during the kana-kanji transformation.
837 if (preedit_string.empty())
838 startBlinkingCursor();
840 stopBlinkingCursor();
842 // last_width : for checking if last preedit string was/wasn't empty.
843 static bool last_width = false;
844 if (!last_width && preedit_string.empty()) {
845 // if last_width is last length of preedit string.
850 GuiPainter pain(&screen_);
851 buffer_view_->updateMetrics();
852 buffer_view_->draw(pain);
853 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
854 FontMetrics const & fm = theFontMetrics(font);
855 int height = fm.maxHeight();
856 int cur_x = cursor_->rect().left();
857 int cur_y = cursor_->rect().bottom();
859 // redraw area of preedit string.
860 update(0, cur_y - height, viewport()->width(),
861 (height + 1) * preedit_lines_);
863 if (preedit_string.empty()) {
871 // att : stores an IM attribute.
872 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
874 // get attributes of input method cursor.
875 // cursor_pos : cursor position in preedit string.
876 size_t cursor_pos = 0;
877 bool cursor_is_visible = false;
878 for (int i = 0; i != att.size(); ++i) {
879 if (att.at(i).type == QInputMethodEvent::Cursor) {
880 cursor_pos = att.at(i).start;
881 cursor_is_visible = att.at(i).length != 0;
886 size_t preedit_length = preedit_string.length();
888 // get position of selection in input method.
889 // FIXME: isn't there a way to do this simplier?
890 // rStart : cursor position in selected string in IM.
892 // rLength : selected string length in IM.
894 if (cursor_pos < preedit_length) {
895 for (int i = 0; i != att.size(); ++i) {
896 if (att.at(i).type == QInputMethodEvent::TextFormat) {
897 if (att.at(i).start <= int(cursor_pos)
898 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
899 rStart = att.at(i).start;
900 rLength = att.at(i).length;
901 if (!cursor_is_visible)
902 cursor_pos += rLength;
913 int const right_margin = rightMargin();
914 Painter::preedit_style ps;
915 // Most often there would be only one line:
917 for (size_t pos = 0; pos != preedit_length; ++pos) {
918 char_type const typed_char = preedit_string[pos];
919 // reset preedit string style
920 ps = Painter::preedit_default;
922 // if we reached the right extremity of the screen, go to next line.
923 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
924 cur_x = right_margin;
928 // preedit strings are displayed with dashed underline
929 // and partial strings are displayed white on black indicating
930 // that we are in selecting mode in the input method.
931 // FIXME: rLength == preedit_length is not a changing condition
932 // FIXME: should be put out of the loop.
934 && pos < rStart + rLength
935 && !(cursor_pos < rLength && rLength == preedit_length))
936 ps = Painter::preedit_selecting;
938 if (pos == cursor_pos
939 && (cursor_pos < rLength && rLength == preedit_length))
940 ps = Painter::preedit_cursor;
942 // draw one character and update cur_x.
943 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
946 // update the preedit string screen area.
947 update(0, cur_y - preedit_lines_*height, viewport()->width(),
948 (height + 1) * preedit_lines_);
950 // Don't forget to accept the event!
955 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
957 QRect cur_r(0,0,0,0);
959 // this is the CJK-specific composition window position.
960 case Qt::ImMicroFocus:
961 cur_r = cursor_->rect();
962 if (preedit_lines_ != 1)
964 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
965 // return lower right of cursor in LyX.
968 return QWidget::inputMethodQuery(query);
973 void GuiWorkArea::updateWindowTitle()
975 docstring maximize_title;
976 docstring minimize_title;
978 Buffer & buf = buffer_view_->buffer();
979 FileName const fileName = buf.fileName();
980 if (!fileName.empty()) {
981 maximize_title = fileName.displayName(30);
982 minimize_title = from_utf8(fileName.onlyFileName());
983 if (!buf.isClean()) {
984 maximize_title += _(" (changed)");
985 minimize_title += char_type('*');
987 if (buf.isReadonly())
988 maximize_title += _(" (read only)");
991 QString title = windowTitle();
992 QString new_title = toqstr(maximize_title);
993 if (title == new_title)
996 QWidget::setWindowTitle(new_title);
997 QWidget::setWindowIconText(toqstr(minimize_title));
1002 void GuiWorkArea::setReadOnly(bool)
1004 updateWindowTitle();
1005 if (this == lyx_view_->currentWorkArea())
1006 lyx_view_->updateBufferDependent(false);
1010 ////////////////////////////////////////////////////////////////////
1014 ////////////////////////////////////////////////////////////////////
1016 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1018 QPalette pal = palette();
1019 pal.setColor(QPalette::Active, QPalette::Button,
1020 pal.color(QPalette::Active, QPalette::Window));
1021 pal.setColor(QPalette::Disabled, QPalette::Button,
1022 pal.color(QPalette::Disabled, QPalette::Window));
1023 pal.setColor(QPalette::Inactive, QPalette::Button,
1024 pal.color(QPalette::Inactive, QPalette::Window));
1026 QToolButton * closeTabButton = new QToolButton(this);
1027 closeTabButton->setPalette(pal);
1028 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1029 closeTabButton->setText("Close");
1030 closeTabButton->setAutoRaise(true);
1031 closeTabButton->setCursor(Qt::ArrowCursor);
1032 closeTabButton->setToolTip(tr("Close tab"));
1033 closeTabButton->setEnabled(true);
1035 QObject::connect(this, SIGNAL(currentChanged(int)),
1036 this, SLOT(on_currentTabChanged(int)));
1037 QObject::connect(closeTabButton, SIGNAL(clicked()),
1038 this, SLOT(closeCurrentTab()));
1040 setCornerWidget(closeTabButton);
1041 setUsesScrollButtons(true);
1045 void TabWorkArea::showBar(bool show)
1047 tabBar()->setEnabled(show);
1048 tabBar()->setVisible(show);
1052 GuiWorkArea * TabWorkArea::currentWorkArea()
1057 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1063 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1065 for (int i = 0; i != count(); ++i) {
1066 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1068 if (&wa->bufferView().buffer() == &buffer)
1075 void TabWorkArea::closeAll()
1078 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1086 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1088 BOOST_ASSERT(work_area);
1089 int index = indexOf(work_area);
1093 if (index == currentIndex())
1094 // Make sure the work area is up to date.
1095 on_currentTabChanged(index);
1097 // Switch to the work area.
1098 setCurrentIndex(index);
1099 work_area->setFocus();
1105 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1107 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1108 wa->setUpdatesEnabled(false);
1109 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1110 // when hiding it again below).
1111 showBar(count() > 0);
1112 addTab(wa, wa->windowTitle());
1113 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1114 this, SLOT(updateTabText(GuiWorkArea *)));
1115 // Hide tabbar if there's only one tab.
1116 showBar(count() > 1);
1121 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1123 BOOST_ASSERT(work_area);
1124 int index = indexOf(work_area);
1128 work_area->setUpdatesEnabled(false);
1133 // make sure the next work area is enabled.
1134 currentWidget()->setUpdatesEnabled(true);
1135 // Hide tabbar if there's only one tab.
1136 showBar(count() > 1);
1142 void TabWorkArea::on_currentTabChanged(int i)
1144 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1146 BufferView & bv = wa->bufferView();
1147 bv.cursor().fixIfBroken();
1149 wa->setUpdatesEnabled(true);
1153 currentWorkAreaChanged(wa);
1155 LYXERR(Debug::GUI, "currentTabChanged " << i
1156 << "File" << bv.buffer().absFileName());
1160 void TabWorkArea::closeCurrentTab()
1162 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1166 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1168 int const i = indexOf(wa);
1171 setTabText(i, wa->windowTitle());
1174 } // namespace frontend
1177 #include "GuiWorkArea_moc.cpp"