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>
50 #include <QMainWindow>
56 #include <QToolButton>
59 #include <boost/bind.hpp>
63 extern "C" int XEventsQueued(Display *display, int mode);
67 int const CursorWidth = 2;
69 int const CursorWidth = 1;
76 using namespace lyx::support;
81 /// return the LyX mouse button state from Qt's
82 static mouse_button::state q_button_state(Qt::MouseButton button)
84 mouse_button::state b = mouse_button::none;
87 b = mouse_button::button1;
90 b = mouse_button::button2;
93 b = mouse_button::button3;
102 /// return the LyX mouse button state from Qt's
103 mouse_button::state q_motion_state(Qt::MouseButtons state)
105 mouse_button::state b = mouse_button::none;
106 if (state & Qt::LeftButton)
107 b |= mouse_button::button1;
108 if (state & Qt::MidButton)
109 b |= mouse_button::button2;
110 if (state & Qt::RightButton)
111 b |= mouse_button::button3;
122 void draw(QPainter & painter)
124 if (show_ && rect_.isValid()) {
127 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
128 painter.setPen(color_);
129 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
130 rect_.bottomRight().x(), rect_.bottomLeft().y());
133 case REVERSED_L_SHAPE:
134 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
135 painter.setPen(color_);
136 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
137 rect_.bottomLeft().x(), rect_.bottomLeft().y());
141 painter.fillRect(rect_, color_);
147 void update(int x, int y, int h, CursorShape shape)
149 color_ = guiApp->colorCache().get(Color_cursor);
153 rect_ = QRect(x, y, CursorWidth + h / 3, h);
155 case REVERSED_L_SHAPE:
156 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
159 rect_ = QRect(x, y, CursorWidth, h);
164 void show(bool set_show = true) { show_ = set_show; }
165 void hide() { show_ = false; }
167 QRect const & rect() { return rect_; }
181 // This is a 'heartbeat' generating synthetic mouse move events when the
182 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
183 SyntheticMouseEvent::SyntheticMouseEvent()
184 : timeout(200), restart_timeout(true),
185 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
190 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
191 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
192 cursor_visible_(false),
193 need_resize_(false), schedule_redraw_(false),
196 buffer.workAreaManager().add(this);
198 connect(&cursor_timeout_, SIGNAL(timeout()),
199 this, SLOT(toggleCursor()));
201 int const time = QApplication::cursorFlashTime() / 2;
203 cursor_timeout_.setInterval(time);
204 cursor_timeout_.start();
206 // let's initialize this just to be safe
207 cursor_timeout_.setInterval(500);
209 screen_ = QPixmap(viewport()->width(), viewport()->height());
210 cursor_ = new frontend::CursorWidget();
213 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 setAcceptDrops(true);
215 setMouseTracking(true);
216 setMinimumSize(100, 70);
219 viewport()->setAutoFillBackground(false);
220 // We don't need double-buffering nor SystemBackground on
221 // the viewport because we have our own backing pixmap.
222 viewport()->setAttribute(Qt::WA_NoSystemBackground);
224 setFocusPolicy(Qt::WheelFocus);
226 viewport()->setCursor(Qt::IBeamCursor);
228 synthetic_mouse_event_.timeout.timeout.connect(
229 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
232 // Initialize the vertical Scroll Bar
233 QObject::connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
234 this, SLOT(scrollTo(int)));
236 LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
237 << " viewport height: " << viewport()->height());
239 // Enables input methods for asian languages.
240 // Must be set when creating custom text editing widgets.
241 setAttribute(Qt::WA_InputMethodEnabled, true);
246 GuiWorkArea::~GuiWorkArea()
248 buffer_view_->buffer().workAreaManager().remove(this);
254 void GuiWorkArea::close()
256 lyx_view_->removeWorkArea(this);
260 void GuiWorkArea::setFullScreen(bool full_screen)
262 setFrameStyle(QFrame::NoFrame);
263 buffer_view_->setFullScreen(full_screen);
267 BufferView & GuiWorkArea::bufferView()
269 return *buffer_view_;
273 BufferView const & GuiWorkArea::bufferView() const
275 return *buffer_view_;
279 void GuiWorkArea::stopBlinkingCursor()
281 cursor_timeout_.stop();
286 void GuiWorkArea::startBlinkingCursor()
289 //we're not supposed to cache this value.
290 int const time = QApplication::cursorFlashTime() / 2;
293 cursor_timeout_.setInterval(time);
294 cursor_timeout_.start();
298 void GuiWorkArea::redraw()
301 // No need to redraw in this case.
304 // No need to do anything if this is the current view. The BufferView
305 // metrics are already up to date.
306 if (lyx_view_ != guiApp->currentView()) {
307 // FIXME: it would be nice to optimize for the off-screen case.
308 buffer_view_->updateMetrics();
309 buffer_view_->cursor().fixIfBroken();
312 // update cursor position, because otherwise it has to wait until
313 // the blinking interval is over
314 if (cursor_visible_) {
319 LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
321 update(0, 0, viewport()->width(), viewport()->height());
323 /// \warning: scrollbar updating *must* be done after the BufferView is drawn
324 /// because \c BufferView::updateScrollbar() is called in \c BufferView::draw().
326 lyx_view_->updateStatusBar();
328 if (lyxerr.debugging(Debug::WORKAREA))
329 buffer_view_->coordCache().dump();
333 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
335 // In order to avoid bad surprise in the middle of an operation,
336 // we better stop the blinking cursor...
337 // the cursor gets restarted in GuiView::restartCursor()
338 stopBlinkingCursor();
340 theLyXFunc().setLyXView(lyx_view_);
341 theLyXFunc().processKeySym(key, mod);
346 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
349 if (cmd0.action == LFUN_FILE_OPEN) {
350 lyx_view_->dispatch(cmd0);
354 theLyXFunc().setLyXView(lyx_view_);
358 if (cmd0.action == LFUN_MOUSE_PRESS) {
359 if (mod == ShiftModifier)
360 cmd = FuncRequest(cmd0, "region-select");
361 else if (mod == ControlModifier)
362 cmd = FuncRequest(cmd0, "paragraph-select");
369 bool const notJustMovingTheMouse =
370 cmd.action != LFUN_MOUSE_MOTION || cmd.button() != mouse_button::none;
372 // In order to avoid bad surprise in the middle of an operation, we better stop
373 // the blinking cursor.
374 if (notJustMovingTheMouse)
375 stopBlinkingCursor();
377 buffer_view_->mouseEventDispatch(cmd);
379 // Skip these when selecting
380 if (cmd.action != LFUN_MOUSE_MOTION) {
381 lyx_view_->updateLayoutList();
382 lyx_view_->updateToolbars();
385 // GUI tweaks except with mouse motion with no button pressed.
386 if (notJustMovingTheMouse) {
387 // Slight hack: this is only called currently when we
388 // clicked somewhere, so we force through the display
389 // of the new status here.
390 lyx_view_->clearMessage();
392 // Show the cursor immediately after any operation
393 startBlinkingCursor();
398 void GuiWorkArea::resizeBufferView()
400 // WARNING: Please don't put any code that will trigger a repaint here!
401 // We are already inside a paint event.
402 lyx_view_->setBusy(true);
403 buffer_view_->resize(viewport()->width(), viewport()->height());
404 lyx_view_->updateLayoutList();
405 lyx_view_->setBusy(false);
406 need_resize_ = false;
410 void GuiWorkArea::showCursor()
415 CursorShape shape = BAR_SHAPE;
417 Font const & realfont = buffer_view_->cursor().real_current_font;
418 BufferParams const & bp = buffer_view_->buffer().params();
419 bool const samelang = realfont.language() == bp.language;
420 bool const isrtl = realfont.isVisibleRightToLeft();
422 if (!samelang || isrtl != bp.language->rightToLeft()) {
425 shape = REVERSED_L_SHAPE;
428 // The ERT language hack needs fixing up
429 if (realfont.language() == latex_language)
432 Font const font = buffer_view_->cursor().getFont();
433 FontMetrics const & fm = theFontMetrics(font);
434 int const asc = fm.maxAscent();
435 int const des = fm.maxDescent();
439 buffer_view_->cursor().getPos(x, y);
442 // if it doesn't touch the screen, don't try to show it
443 if (y + h < 0 || y >= viewport()->height())
446 cursor_visible_ = true;
447 showCursor(x, y, h, shape);
451 void GuiWorkArea::hideCursor()
453 if (!cursor_visible_)
456 cursor_visible_ = false;
461 void GuiWorkArea::toggleCursor()
470 void GuiWorkArea::updateScrollbar()
472 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
474 verticalScrollBar()->setRange(scroll_.min, scroll_.max);
475 verticalScrollBar()->setPageStep(scroll_.page_step);
476 verticalScrollBar()->setSingleStep(scroll_.single_step);
477 // Block the scrollbar signal to prevent recursive signal/slot calling.
478 verticalScrollBar()->blockSignals(true);
479 verticalScrollBar()->setValue(scroll_.position);
480 verticalScrollBar()->setSliderPosition(scroll_.position);
481 verticalScrollBar()->blockSignals(false);
485 void GuiWorkArea::scrollTo(int value)
487 stopBlinkingCursor();
488 buffer_view_->scrollDocView(value);
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)
503 case QEvent::ToolTip: {
504 QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
505 if (lyxrc.use_tooltip) {
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 // Don't forget to accept the event!
519 case QEvent::ShortcutOverride: {
520 // We catch this event in order to catch the Tab or Shift+Tab key press
521 // which are otherwise reserved to focus switching between controls
523 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
524 if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab)
525 || ke->modifiers() & Qt::ControlModifier)
526 return QAbstractScrollArea::event(e);
532 return QAbstractScrollArea::event(e);
538 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
540 QPoint pos = e->pos();
541 docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
543 QAbstractScrollArea::contextMenuEvent(e);
546 QMenu * menu = guiApp->menus().menu(toqstr(name));
548 QAbstractScrollArea::contextMenuEvent(e);
551 // Position the menu to the right.
552 // FIXME: menu position should be different for RTL text.
553 menu->exec(e->globalPos());
558 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
560 // Repaint the whole screen.
561 // Note: this is different from redraw() as only the backing pixmap
562 // will be redrawn, which is cheap.
563 viewport()->repaint();
565 startBlinkingCursor();
569 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
571 stopBlinkingCursor();
575 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
577 if (dc_event_.active && dc_event_ == *e) {
578 dc_event_.active = false;
579 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
580 q_button_state(e->button()));
585 inputContext()->reset();
587 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
588 q_button_state(e->button()));
589 dispatch(cmd, q_key_state(e->modifiers()));
593 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
595 if (synthetic_mouse_event_.timeout.running())
596 synthetic_mouse_event_.timeout.stop();
598 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
599 q_button_state(e->button()));
604 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
606 // we kill the triple click if we move
607 doubleClickTimeout();
608 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
609 q_motion_state(e->buttons()));
611 // If we're above or below the work area...
612 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
613 // Make sure only a synthetic event can cause a page scroll,
614 // so they come at a steady rate:
616 // _Force_ a scroll up:
619 cmd.y = viewport()->height();
620 // Store the event, to be handled when the timeout expires.
621 synthetic_mouse_event_.cmd = cmd;
623 if (synthetic_mouse_event_.timeout.running())
624 // Discard the event. Note that it _may_ be handled
625 // when the timeout expires if
626 // synthetic_mouse_event_.cmd has not been overwritten.
627 // Ie, when the timeout expires, we handle the
628 // most recent event but discard all others that
629 // occurred after the one used to start the timeout
630 // in the first place.
633 synthetic_mouse_event_.restart_timeout = true;
634 synthetic_mouse_event_.timeout.start();
635 // Fall through to handle this event...
637 } else if (synthetic_mouse_event_.timeout.running()) {
638 // Store the event, to be possibly handled when the timeout
640 // Once the timeout has expired, normal control is returned
641 // to mouseMoveEvent (restart_timeout = false).
642 // This results in a much smoother 'feel' when moving the
643 // mouse back into the work area.
644 synthetic_mouse_event_.cmd = cmd;
645 synthetic_mouse_event_.restart_timeout = false;
649 // Has anything changed on-screen since the last QMouseEvent
651 double const scrollbar_value = verticalScrollBar()->value();
652 if (e->x() == synthetic_mouse_event_.x_old
653 && e->y() == synthetic_mouse_event_.y_old
654 && scrollbar_value == synthetic_mouse_event_.scrollbar_value_old) {
655 // Nothing changed on-screen since the last QMouseEvent.
659 // Yes something has changed. Store the params used to check this.
660 synthetic_mouse_event_.x_old = e->x();
661 synthetic_mouse_event_.y_old = e->y();
662 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
664 // ... and dispatch the event to the LyX core.
669 void GuiWorkArea::wheelEvent(QWheelEvent * e)
671 // Wheel rotation by one notch results in a delta() of 120 (see
672 // documentation of QWheelEvent)
673 double const lines = qApp->wheelScrollLines()
674 * lyxrc.mouse_wheel_speed
675 * e->delta() / 120.0;
676 LYXERR(Debug::SCROLLING, "wheelScrollLines = " << qApp->wheelScrollLines()
677 << " delta = " << e->delta()
678 << " lines = " << lines);
679 verticalScrollBar()->setValue(verticalScrollBar()->value() -
680 int(lines * verticalScrollBar()->singleStep()));
684 void GuiWorkArea::generateSyntheticMouseEvent()
686 // Set things off to generate the _next_ 'pseudo' event.
687 if (synthetic_mouse_event_.restart_timeout)
688 synthetic_mouse_event_.timeout.start();
690 // Has anything changed on-screen since the last timeout signal
692 double const scrollbar_value = verticalScrollBar()->value();
693 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
694 // Yes it has. Store the params used to check this.
695 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
697 // ... and dispatch the event to the LyX core.
698 dispatch(synthetic_mouse_event_.cmd);
703 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
705 // do nothing if there are other events
706 // (the auto repeated events come too fast)
707 // \todo FIXME: remove hard coded Qt keys, process the key binding
709 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
710 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
711 LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
717 LYXERR(Debug::KEY, " count: " << ev->count()
718 << " text: " << fromqstr(ev->text())
719 << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
722 setKeySymbol(&sym, ev);
723 processKeySym(sym, q_key_state(ev->modifiers()));
728 void GuiWorkArea::doubleClickTimeout()
730 dc_event_.active = false;
734 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
736 dc_event_ = DoubleClick(ev);
737 QTimer::singleShot(QApplication::doubleClickInterval(), this,
738 SLOT(doubleClickTimeout()));
739 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
741 q_button_state(ev->button()));
746 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
748 QAbstractScrollArea::resizeEvent(ev);
753 void GuiWorkArea::update(int x, int y, int w, int h)
755 viewport()->repaint(x, y, w, h);
759 void GuiWorkArea::paintEvent(QPaintEvent * ev)
761 QRect const rc = ev->rect();
762 // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
763 // << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
766 screen_ = QPixmap(viewport()->width(), viewport()->height());
769 // Update scrollbars which might have changed due different
770 // BufferView dimension. This is especially important when the
771 // BufferView goes from zero-size to the real-size for the first time,
772 // as the scrollbar paramters are then set for the first time.
774 BOOST_ASSERT(need_resize_ == false);
781 QPainter pain(viewport());
782 pain.drawPixmap(rc, screen_, rc);
787 void GuiWorkArea::updateScreen()
789 GuiPainter pain(&screen_);
790 buffer_view_->draw(pain);
794 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
796 if (schedule_redraw_) {
797 buffer_view_->updateMetrics();
799 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
800 schedule_redraw_ = false;
801 // Show the cursor immediately after the update.
807 cursor_->update(x, y, h, shape);
809 viewport()->update(cursor_->rect());
813 void GuiWorkArea::removeCursor()
816 //if (!qApp->focusWidget())
817 viewport()->update(cursor_->rect());
821 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
823 QString const & commit_string = e->commitString();
824 docstring const & preedit_string
825 = qstring_to_ucs4(e->preeditString());
827 if (!commit_string.isEmpty()) {
829 LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
830 << " commitString: " << fromqstr(e->commitString()));
834 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
835 for (int i = 0; i != commit_string.size(); ++i) {
836 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
841 // Hide the cursor during the kana-kanji transformation.
842 if (preedit_string.empty())
843 startBlinkingCursor();
845 stopBlinkingCursor();
847 // last_width : for checking if last preedit string was/wasn't empty.
848 static bool last_width = false;
849 if (!last_width && preedit_string.empty()) {
850 // if last_width is last length of preedit string.
855 GuiPainter pain(&screen_);
856 buffer_view_->updateMetrics();
857 buffer_view_->draw(pain);
858 FontInfo font = buffer_view_->cursor().getFont().fontInfo();
859 FontMetrics const & fm = theFontMetrics(font);
860 int height = fm.maxHeight();
861 int cur_x = cursor_->rect().left();
862 int cur_y = cursor_->rect().bottom();
864 // redraw area of preedit string.
865 update(0, cur_y - height, viewport()->width(),
866 (height + 1) * preedit_lines_);
868 if (preedit_string.empty()) {
876 // att : stores an IM attribute.
877 QList<QInputMethodEvent::Attribute> const & att = e->attributes();
879 // get attributes of input method cursor.
880 // cursor_pos : cursor position in preedit string.
881 size_t cursor_pos = 0;
882 bool cursor_is_visible = false;
883 for (int i = 0; i != att.size(); ++i) {
884 if (att.at(i).type == QInputMethodEvent::Cursor) {
885 cursor_pos = att.at(i).start;
886 cursor_is_visible = att.at(i).length != 0;
891 size_t preedit_length = preedit_string.length();
893 // get position of selection in input method.
894 // FIXME: isn't there a way to do this simplier?
895 // rStart : cursor position in selected string in IM.
897 // rLength : selected string length in IM.
899 if (cursor_pos < preedit_length) {
900 for (int i = 0; i != att.size(); ++i) {
901 if (att.at(i).type == QInputMethodEvent::TextFormat) {
902 if (att.at(i).start <= int(cursor_pos)
903 && int(cursor_pos) < att.at(i).start + att.at(i).length) {
904 rStart = att.at(i).start;
905 rLength = att.at(i).length;
906 if (!cursor_is_visible)
907 cursor_pos += rLength;
918 int const right_margin = buffer_view_->rightMargin();
919 Painter::preedit_style ps;
920 // Most often there would be only one line:
922 for (size_t pos = 0; pos != preedit_length; ++pos) {
923 char_type const typed_char = preedit_string[pos];
924 // reset preedit string style
925 ps = Painter::preedit_default;
927 // if we reached the right extremity of the screen, go to next line.
928 if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
929 cur_x = right_margin;
933 // preedit strings are displayed with dashed underline
934 // and partial strings are displayed white on black indicating
935 // that we are in selecting mode in the input method.
936 // FIXME: rLength == preedit_length is not a changing condition
937 // FIXME: should be put out of the loop.
939 && pos < rStart + rLength
940 && !(cursor_pos < rLength && rLength == preedit_length))
941 ps = Painter::preedit_selecting;
943 if (pos == cursor_pos
944 && (cursor_pos < rLength && rLength == preedit_length))
945 ps = Painter::preedit_cursor;
947 // draw one character and update cur_x.
948 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
951 // update the preedit string screen area.
952 update(0, cur_y - preedit_lines_*height, viewport()->width(),
953 (height + 1) * preedit_lines_);
955 // Don't forget to accept the event!
960 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
962 QRect cur_r(0,0,0,0);
964 // this is the CJK-specific composition window position.
965 case Qt::ImMicroFocus:
966 cur_r = cursor_->rect();
967 if (preedit_lines_ != 1)
969 cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
970 // return lower right of cursor in LyX.
973 return QWidget::inputMethodQuery(query);
978 void GuiWorkArea::updateWindowTitle()
980 docstring maximize_title;
981 docstring minimize_title;
983 Buffer & buf = buffer_view_->buffer();
984 FileName const fileName = buf.fileName();
985 if (!fileName.empty()) {
986 maximize_title = fileName.displayName(30);
987 minimize_title = from_utf8(fileName.onlyFileName());
988 if (!buf.isClean()) {
989 maximize_title += _(" (changed)");
990 minimize_title += char_type('*');
992 if (buf.isReadonly())
993 maximize_title += _(" (read only)");
996 QString title = windowTitle();
997 QString new_title = toqstr(maximize_title);
998 if (title == new_title)
1001 QWidget::setWindowTitle(new_title);
1002 QWidget::setWindowIconText(toqstr(minimize_title));
1007 void GuiWorkArea::setReadOnly(bool)
1009 updateWindowTitle();
1010 if (this == lyx_view_->currentWorkArea())
1011 lyx_view_->updateBufferDependent(false);
1015 ////////////////////////////////////////////////////////////////////
1019 ////////////////////////////////////////////////////////////////////
1021 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1023 QPalette pal = palette();
1024 pal.setColor(QPalette::Active, QPalette::Button,
1025 pal.color(QPalette::Active, QPalette::Window));
1026 pal.setColor(QPalette::Disabled, QPalette::Button,
1027 pal.color(QPalette::Disabled, QPalette::Window));
1028 pal.setColor(QPalette::Inactive, QPalette::Button,
1029 pal.color(QPalette::Inactive, QPalette::Window));
1031 QToolButton * closeTabButton = new QToolButton(this);
1032 closeTabButton->setPalette(pal);
1033 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1034 closeTabButton->setText("Close");
1035 closeTabButton->setAutoRaise(true);
1036 closeTabButton->setCursor(Qt::ArrowCursor);
1037 closeTabButton->setToolTip(tr("Close tab"));
1038 closeTabButton->setEnabled(true);
1040 QObject::connect(this, SIGNAL(currentChanged(int)),
1041 this, SLOT(on_currentTabChanged(int)));
1042 QObject::connect(closeTabButton, SIGNAL(clicked()),
1043 this, SLOT(closeCurrentTab()));
1045 setCornerWidget(closeTabButton);
1046 setUsesScrollButtons(true);
1050 void TabWorkArea::setFullScreen(bool full_screen)
1052 for (int i = 0; i != count(); ++i) {
1053 if (GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i)))
1054 wa->setFullScreen(full_screen);
1059 void TabWorkArea::showBar(bool show)
1061 tabBar()->setEnabled(show);
1062 tabBar()->setVisible(show);
1066 GuiWorkArea * TabWorkArea::currentWorkArea()
1071 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1077 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1079 for (int i = 0; i != count(); ++i) {
1080 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1082 if (&wa->bufferView().buffer() == &buffer)
1089 void TabWorkArea::closeAll()
1092 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1100 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1102 BOOST_ASSERT(work_area);
1103 int index = indexOf(work_area);
1107 if (index == currentIndex())
1108 // Make sure the work area is up to date.
1109 on_currentTabChanged(index);
1111 // Switch to the work area.
1112 setCurrentIndex(index);
1113 work_area->setFocus();
1119 GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
1121 GuiWorkArea * wa = new GuiWorkArea(buffer, view);
1122 wa->setUpdatesEnabled(false);
1123 // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
1124 // when hiding it again below).
1125 showBar(count() > 0);
1126 addTab(wa, wa->windowTitle());
1127 QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
1128 this, SLOT(updateTabText(GuiWorkArea *)));
1129 // Hide tabbar if there's only one tab.
1130 showBar(count() > 1);
1135 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1137 BOOST_ASSERT(work_area);
1138 int index = indexOf(work_area);
1142 work_area->setUpdatesEnabled(false);
1147 // make sure the next work area is enabled.
1148 currentWidget()->setUpdatesEnabled(true);
1149 // Hide tabbar if there's only one tab.
1150 showBar(count() > 1);
1156 void TabWorkArea::on_currentTabChanged(int i)
1158 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1160 BufferView & bv = wa->bufferView();
1161 bv.cursor().fixIfBroken();
1163 wa->setUpdatesEnabled(true);
1167 currentWorkAreaChanged(wa);
1169 LYXERR(Debug::GUI, "currentTabChanged " << i
1170 << "File" << bv.buffer().absFileName());
1174 void TabWorkArea::closeCurrentTab()
1176 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1180 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1182 int const i = indexOf(wa);
1185 setTabText(i, wa->windowTitle());
1188 } // namespace frontend
1191 #include "GuiWorkArea_moc.cpp"