2 * \file GuiWorkArea.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "GuiWorkArea.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CoordCache.h"
23 #include "FuncRequest.h"
25 #include "GuiApplication.h"
26 #include "GuiKeySymbol.h"
27 #include "GuiPainter.h"
29 #include "KeySymbol.h"
33 #include "MetricsInfo.h"
34 #include "qt_helpers.h"
37 #include "graphics/GraphicsImage.h"
38 #include "graphics/GraphicsLoader.h"
40 #include "support/FileName.h"
41 #include "support/ForkedcallsController.h"
43 #include "frontends/Application.h"
44 #include "frontends/Dialogs.h" // only used in setReadOnly
45 #include "frontends/FontMetrics.h"
46 #include "frontends/WorkAreaManager.h"
48 #include <QInputContext>
50 #include <QMainWindow>
52 #include <QToolButton>
58 #include <boost/bind.hpp>
59 #include <boost/current_function.hpp>
63 extern "C" int XEventsQueued(Display *display, int mode);
67 int const CursorWidth = 2;
69 int const CursorWidth = 1;
82 using support::FileName;
83 using support::ForkedcallsController;
86 /// return the LyX mouse button state from Qt's
87 static mouse_button::state q_button_state(Qt::MouseButton button)
89 mouse_button::state b = mouse_button::none;
92 b = mouse_button::button1;
95 b = mouse_button::button2;
98 b = mouse_button::button3;
107 /// return the LyX mouse button state from Qt's
108 mouse_button::state q_motion_state(Qt::MouseButtons state)
110 mouse_button::state b = mouse_button::none;
111 if (state & Qt::LeftButton)
112 b |= mouse_button::button1;
113 if (state & Qt::MidButton)
114 b |= mouse_button::button2;
115 if (state & Qt::RightButton)
116 b |= mouse_button::button3;
127 void draw(QPainter & painter)
129 if (show_ && rect_.isValid()) {
132 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
133 painter.setPen(color_);
134 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
135 rect_.bottomRight().x(), rect_.bottomLeft().y());
138 case REVERSED_L_SHAPE:
139 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
140 painter.setPen(color_);
141 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
142 rect_.bottomLeft().x(), rect_.bottomLeft().y());
146 painter.fillRect(rect_, color_);
152 void update(int x, int y, int h, CursorShape shape)
154 color_ = guiApp->colorCache().get(Color_cursor);
158 rect_ = QRect(x, y, CursorWidth + h / 3, h);
160 case REVERSED_L_SHAPE:
161 rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
164 rect_ = QRect(x, y, CursorWidth, h);
169 void show(bool set_show = true) { show_ = set_show; }
170 void hide() { show_ = false; }
172 QRect const & rect() { return rect_; }
186 // This is a 'heartbeat' generating synthetic mouse move events when the
187 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
188 SyntheticMouseEvent::SyntheticMouseEvent()
189 : timeout(200), restart_timeout(true),
190 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
195 // All the below connection objects are needed because of a bug in some
196 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
197 // to these connections we avoid a segfault upon startup, and also at exit.
200 static boost::signals::connection timecon;
203 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
204 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
205 cursor_visible_(false), cursor_timeout_(400),
206 need_resize_(false), schedule_redraw_(false),
209 buffer.workAreaManager().add(this);
211 timecon = cursor_timeout_.timeout
212 .connect(boost::bind(&GuiWorkArea::toggleCursor, this));
214 cursor_timeout_.start();
216 screen_ = QPixmap(viewport()->width(), viewport()->height());
217 cursor_ = new frontend::CursorWidget();
220 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
221 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
222 setAcceptDrops(true);
223 setMouseTracking(true);
224 setMinimumSize(100, 70);
227 viewport()->setAutoFillBackground(false);
228 // We don't need double-buffering nor SystemBackground on
229 // the viewport because we have our own backing pixmap.
230 viewport()->setAttribute(Qt::WA_NoSystemBackground);
232 setFocusPolicy(Qt::WheelFocus);
234 viewport()->setCursor(Qt::IBeamCursor);
236 synthetic_mouse_event_.timeout.timeout.connect(
237 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
240 // Initialize the vertical Scroll Bar
241 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
242 this, SLOT(adjustViewWithScrollBar(int)));
244 // disable context menu for the scrollbar
245 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
247 // PageStep only depends on the viewport height.
248 verticalScrollBar()->setPageStep(viewport()->height());
250 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
251 << "\n Area width\t" << width()
252 << "\n Area height\t" << height()
253 << "\n viewport width\t" << viewport()->width()
254 << "\n viewport height\t" << viewport()->height()
257 // Enables input methods for asian languages.
258 // Must be set when creating custom text editing widgets.
259 setAttribute(Qt::WA_InputMethodEnabled, true);
264 GuiWorkArea::~GuiWorkArea()
266 buffer_view_->buffer().workAreaManager().remove(this);
271 void GuiWorkArea::close()
273 lyx_view_->removeWorkArea(this);
277 BufferView & GuiWorkArea::bufferView()
279 return *buffer_view_;
283 BufferView const & GuiWorkArea::bufferView() const
285 return *buffer_view_;
289 void GuiWorkArea::stopBlinkingCursor()
291 cursor_timeout_.stop();
296 void GuiWorkArea::startBlinkingCursor()
299 cursor_timeout_.restart();
303 void GuiWorkArea::redraw()
306 // No need to redraw in this case.
309 // No need to do anything if this is the current view. The BufferView
310 // metrics are already up to date.
311 if (lyx_view_ != theApp()->currentView()) {
312 // FIXME: it would be nice to optimize for the off-screen case.
313 buffer_view_->updateMetrics();
314 buffer_view_->cursor().fixIfBroken();
319 // update cursor position, because otherwise it has to wait until
320 // the blinking interval is over
321 if (cursor_visible_) {
326 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
328 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
330 int const ymin = std::max(vi.y1, 0);
331 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
333 expose(0, ymin, width(), ymax - ymin);
335 //LYXERR(Debug::WORKAREA)
336 //<< " ymin = " << ymin << " width() = " << width()
337 // << " ymax-ymin = " << ymax-ymin << std::endl;
339 if (lyxerr.debugging(Debug::WORKAREA))
340 buffer_view_->coordCache().dump();
344 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
346 // In order to avoid bad surprise in the middle of an operation,
347 // we better stop the blinking cursor.
348 stopBlinkingCursor();
350 theLyXFunc().setLyXView(lyx_view_);
351 theLyXFunc().processKeySym(key, mod);
355 void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
358 if (cmd0.action == LFUN_FILE_OPEN) {
359 lyx_view_->dispatch(cmd0);
363 theLyXFunc().setLyXView(lyx_view_);
367 if (cmd0.action == LFUN_MOUSE_PRESS) {
368 if (mod == ShiftModifier)
369 cmd = FuncRequest(cmd0, "region-select");
370 else if (mod == ControlModifier)
371 cmd = FuncRequest(cmd0, "paragraph-select");
378 // In order to avoid bad surprise in the middle of an operation, we better stop
379 // the blinking cursor.
380 if (!(cmd.action == LFUN_MOUSE_MOTION
381 && cmd.button() == mouse_button::none))
382 stopBlinkingCursor();
384 buffer_view_->mouseEventDispatch(cmd);
386 // Skip these when selecting
387 if (cmd.action != LFUN_MOUSE_MOTION) {
388 lyx_view_->updateLayoutChoice(false);
389 lyx_view_->updateToolbars();
392 // GUI tweaks except with mouse motion with no button pressed.
393 if (!(cmd.action == LFUN_MOUSE_MOTION
394 && cmd.button() == mouse_button::none)) {
395 // Slight hack: this is only called currently when we
396 // clicked somewhere, so we force through the display
397 // of the new status here.
398 lyx_view_->clearMessage();
400 // Show the cursor immediately after any operation.
401 startBlinkingCursor();
406 void GuiWorkArea::resizeBufferView()
408 // WARNING: Please don't put any code that will trigger a repaint here!
409 // We are already inside a paint event.
410 lyx_view_->setBusy(true);
411 buffer_view_->resize(width(), height());
412 lyx_view_->updateLayoutChoice(false);
413 lyx_view_->setBusy(false);
417 void GuiWorkArea::updateScrollbar()
419 buffer_view_->updateScrollbar();
420 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
421 setScrollbarParams(scroll_.height, scroll_.position,
422 scroll_.lineScrollHeight);
426 void GuiWorkArea::showCursor()
431 CursorShape shape = BAR_SHAPE;
433 Font const & realfont = buffer_view_->cursor().real_current_font;
434 BufferParams const & bp = buffer_view_->buffer().params();
435 bool const samelang = realfont.language() == bp.language;
436 bool const isrtl = realfont.isVisibleRightToLeft();
438 if (!samelang || isrtl != bp.language->rightToLeft()) {
441 shape = REVERSED_L_SHAPE;
444 // The ERT language hack needs fixing up
445 if (realfont.language() == latex_language)
448 Font const font = buffer_view_->cursor().getFont();
449 FontMetrics const & fm = theFontMetrics(font);
450 int const asc = fm.maxAscent();
451 int const des = fm.maxDescent();
455 buffer_view_->cursor().getPos(x, y);
458 // if it doesn't touch the screen, don't try to show it
459 if (y + h < 0 || y >= height())
462 cursor_visible_ = true;
463 showCursor(x, y, h, shape);
467 void GuiWorkArea::hideCursor()
469 if (!cursor_visible_)
472 cursor_visible_ = false;
477 void GuiWorkArea::toggleCursor()
484 // Use this opportunity to deal with any child processes that
485 // have finished but are waiting to communicate this fact
486 // to the rest of LyX.
487 ForkedcallsController & fcc = ForkedcallsController::get();
488 fcc.handleCompletedProcesses();
490 cursor_timeout_.restart();
494 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
496 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
497 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
499 verticalScrollBar()->setTracking(false);
501 // do what cursor movement does (some grey)
503 int scroll_max_ = std::max(0, h - height());
505 verticalScrollBar()->setRange(0, scroll_max_);
506 verticalScrollBar()->setSliderPosition(scroll_pos);
507 verticalScrollBar()->setSingleStep(scroll_line_step);
508 verticalScrollBar()->setValue(scroll_pos);
510 verticalScrollBar()->setTracking(true);
514 void GuiWorkArea::adjustViewWithScrollBar(int action)
516 stopBlinkingCursor();
517 if (action == QAbstractSlider::SliderPageStepAdd)
518 buffer_view_->scrollDown(viewport()->height());
519 else if (action == QAbstractSlider::SliderPageStepSub)
520 buffer_view_->scrollUp(viewport()->height());
522 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
524 if (lyxrc.cursor_follows_scrollbar) {
525 buffer_view_->setCursorFromScrollbar();
526 lyx_view_->updateLayoutChoice(false);
528 // Show the cursor immediately after any operation.
529 startBlinkingCursor();
530 QApplication::syncX();
534 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
536 // Repaint the whole screen.
537 // Note: this is different from redraw() as only the backing pixmap
538 // will be redrawn, which is cheap.
539 viewport()->repaint();
541 startBlinkingCursor();
545 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
547 stopBlinkingCursor();
551 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
553 if (dc_event_.active && dc_event_ == *e) {
554 dc_event_.active = false;
555 FuncRequest cmd(LFUN_MOUSE_TRIPLE, e->x(), e->y(),
556 q_button_state(e->button()));
561 inputContext()->reset();
563 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
564 q_button_state(e->button()));
565 dispatch(cmd, q_key_state(e->modifiers()));
569 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
571 if (synthetic_mouse_event_.timeout.running())
572 synthetic_mouse_event_.timeout.stop();
574 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
575 q_button_state(e->button()));
580 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
582 // we kill the triple click if we move
583 doubleClickTimeout();
584 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
585 q_motion_state(e->buttons()));
587 // If we're above or below the work area...
588 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
589 // Make sure only a synthetic event can cause a page scroll,
590 // so they come at a steady rate:
592 // _Force_ a scroll up:
595 cmd.y = viewport()->height();
596 // Store the event, to be handled when the timeout expires.
597 synthetic_mouse_event_.cmd = cmd;
599 if (synthetic_mouse_event_.timeout.running())
600 // Discard the event. Note that it _may_ be handled
601 // when the timeout expires if
602 // synthetic_mouse_event_.cmd has not been overwritten.
603 // Ie, when the timeout expires, we handle the
604 // most recent event but discard all others that
605 // occurred after the one used to start the timeout
606 // in the first place.
609 synthetic_mouse_event_.restart_timeout = true;
610 synthetic_mouse_event_.timeout.start();
611 // Fall through to handle this event...
614 } else if (synthetic_mouse_event_.timeout.running()) {
615 // Store the event, to be possibly handled when the timeout
617 // Once the timeout has expired, normal control is returned
618 // to mouseMoveEvent (restart_timeout = false).
619 // This results in a much smoother 'feel' when moving the
620 // mouse back into the work area.
621 synthetic_mouse_event_.cmd = cmd;
622 synthetic_mouse_event_.restart_timeout = false;
626 // Has anything changed on-screen since the last QMouseEvent
628 double const scrollbar_value = verticalScrollBar()->value();
629 if (e->x() != synthetic_mouse_event_.x_old ||
630 e->y() != synthetic_mouse_event_.y_old ||
631 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
632 // Yes it has. Store the params used to check this.
633 synthetic_mouse_event_.x_old = e->x();
634 synthetic_mouse_event_.y_old = e->y();
635 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
637 // ... and dispatch the event to the LyX core.
643 void GuiWorkArea::wheelEvent(QWheelEvent * e)
645 // Wheel rotation by one notch results in a delta() of 120 (see
646 // documentation of QWheelEvent)
647 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
648 verticalScrollBar()->setValue(verticalScrollBar()->value() -
649 lines * verticalScrollBar()->singleStep());
650 adjustViewWithScrollBar();
654 void GuiWorkArea::generateSyntheticMouseEvent()
656 // Set things off to generate the _next_ 'pseudo' event.
657 if (synthetic_mouse_event_.restart_timeout)
658 synthetic_mouse_event_.timeout.start();
660 // Has anything changed on-screen since the last timeout signal
662 double const scrollbar_value = verticalScrollBar()->value();
663 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
664 // Yes it has. Store the params used to check this.
665 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
667 // ... and dispatch the event to the LyX core.
668 dispatch(synthetic_mouse_event_.cmd);
673 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
675 // do nothing if there are other events
676 // (the auto repeated events come too fast)
677 // \todo FIXME: remove hard coded Qt keys, process the key binding
679 if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat()
680 && (Qt::Key_PageDown || Qt::Key_PageUp)) {
682 << BOOST_CURRENT_FUNCTION << endl
683 << "system is busy: scroll key event ignored" << endl;
689 LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
690 << " count=" << ev->count()
691 << " text=" << fromqstr(ev->text())
692 << " isAutoRepeat=" << ev->isAutoRepeat()
693 << " key=" << ev->key()
697 setKeySymbol(&sym, ev);
698 processKeySym(sym, q_key_state(ev->modifiers()));
702 void GuiWorkArea::doubleClickTimeout()
704 dc_event_.active = false;
708 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
710 dc_event_ = DoubleClick(ev);
711 QTimer::singleShot(QApplication::doubleClickInterval(), this,
712 SLOT(doubleClickTimeout()));
713 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
715 q_button_state(ev->button()));
720 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
722 QAbstractScrollArea::resizeEvent(ev);
727 void GuiWorkArea::update(int x, int y, int w, int h)
729 viewport()->repaint(x, y, w, h);
733 void GuiWorkArea::paintEvent(QPaintEvent * ev)
735 QRect const rc = ev->rect();
737 LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
739 << " w: " << rc.width()
740 << " h: " << rc.height() << endl;
744 verticalScrollBar()->setPageStep(viewport()->height());
745 screen_ = QPixmap(viewport()->width(), viewport()->height());
750 need_resize_ = false;
753 QPainter pain(viewport());
754 pain.drawPixmap(rc, screen_, rc);
759 void GuiWorkArea::expose(int x, int y, int w, int h)
766 void GuiWorkArea::setWindowTitle(docstring const & t, docstring const & it)
768 QString title = windowTitle();
769 QString new_title = toqstr(t);
770 if (title == new_title)
773 QWidget::setWindowTitle(new_title);
774 QWidget::setWindowIconText(toqstr(it));
779 void GuiWorkArea::updateScreen()
781 GuiPainter pain(&screen_);
782 verticalScrollBar()->show();
783 buffer_view_->draw(pain);
787 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
789 if (schedule_redraw_) {
790 buffer_view_->updateMetrics();
792 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
793 schedule_redraw_ = false;
794 // Show the cursor immediately after the update.
800 cursor_->update(x, y, h, shape);
802 viewport()->update(cursor_->rect());
806 void GuiWorkArea::removeCursor()
809 //if (!qApp->focusWidget())
810 viewport()->update(cursor_->rect());
814 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
816 QString const & commit_string = e->commitString();
817 docstring const & preedit_string
818 = qstring_to_ucs4(e->preeditString());
820 if (!commit_string.isEmpty()) {
822 LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
823 << " preeditString =" << fromqstr(e->preeditString())
824 << " 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, GuiWorkArea::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) > GuiWorkArea::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, GuiWorkArea::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 setWindowTitle(maximize_title, minimize_title);
995 void GuiWorkArea::setReadOnly(bool)
998 if (this == lyx_view_->currentWorkArea())
999 lyx_view_->getDialogs().updateBufferDependent(false);
1003 ////////////////////////////////////////////////////////////////////
1007 ////////////////////////////////////////////////////////////////////
1009 TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
1011 QPalette pal = palette();
1012 pal.setColor(QPalette::Active, QPalette::Button,
1013 pal.color(QPalette::Active, QPalette::Window));
1014 pal.setColor(QPalette::Disabled, QPalette::Button,
1015 pal.color(QPalette::Disabled, QPalette::Window));
1016 pal.setColor(QPalette::Inactive, QPalette::Button,
1017 pal.color(QPalette::Inactive, QPalette::Window));
1019 QToolButton * closeTabButton = new QToolButton(this);
1020 closeTabButton->setPalette(pal);
1021 closeTabButton->setIcon(QIcon(":/images/closetab.png"));
1022 closeTabButton->setText("Close");
1023 closeTabButton->setAutoRaise(true);
1024 closeTabButton->setCursor(Qt::ArrowCursor);
1025 closeTabButton->setToolTip(tr("Close tab"));
1026 closeTabButton->setEnabled(true);
1028 QObject::connect(this, SIGNAL(currentChanged(int)),
1029 this, SLOT(on_currentTabChanged(int)));
1030 QObject::connect(closeTabButton, SIGNAL(clicked()),
1031 this, SLOT(closeCurrentTab()));
1033 setCornerWidget(closeTabButton);
1034 #if QT_VERSION >= 0x040200
1035 setUsesScrollButtons(true);
1040 void TabWorkArea::showBar(bool show)
1042 tabBar()->setVisible(show);
1046 GuiWorkArea * TabWorkArea::currentWorkArea()
1051 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
1057 GuiWorkArea * TabWorkArea::workArea(Buffer & buffer)
1059 for (int i = 0; i != count(); ++i) {
1060 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1062 if (&wa->bufferView().buffer() == &buffer)
1069 void TabWorkArea::closeAll()
1072 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
1080 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
1082 BOOST_ASSERT(work_area);
1083 int index = indexOf(work_area);
1087 if (index == currentIndex())
1088 // Make sure the work area is up to date.
1089 on_currentTabChanged(index);
1091 // Switch to the work area.
1092 setCurrentIndex(index);
1093 work_area->setFocus();
1099 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
1101 BOOST_ASSERT(work_area);
1102 int index = indexOf(work_area);
1106 work_area->setUpdatesEnabled(false);
1111 // make sure the next work area is enabled.
1112 currentWidget()->setUpdatesEnabled(true);
1113 // Hide tabbar if there's only one tab.
1114 showBar(count() > 1);
1120 void TabWorkArea::on_currentTabChanged(int i)
1122 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
1124 BufferView & bv = wa->bufferView();
1126 bv.cursor().fixIfBroken();
1127 wa->setUpdatesEnabled(true);
1131 currentWorkAreaChanged(wa);
1133 LYXERR(Debug::GUI) << "currentTabChanged " << i
1134 << "File" << bv.buffer().absFileName() << endl;
1138 void TabWorkArea::closeCurrentTab()
1140 lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
1144 void TabWorkArea::updateTabText(GuiWorkArea * wa)
1146 int const i = indexOf(wa);
1149 setTabText(i, wa->windowTitle());
1152 } // namespace frontend
1155 #include "GuiWorkArea_moc.cpp"