]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 89d18bcbd2ad91cbb0ea0203b731ec354e54891b..026c7699db10dbf98d0ece4c1d1c698a71bc4f2d 100644 (file)
@@ -47,6 +47,7 @@
 #include "support/debug.h"
 #include "support/gettext.h"
 #include "support/FileName.h"
+#include "support/lassert.h"
 
 #include "frontends/Application.h"
 #include "frontends/FontMetrics.h"
 
 #include <cmath>
 
-#ifdef Q_WS_WIN
-int const CursorWidth = 2;
-#else
-int const CursorWidth = 1;
-#endif
 int const TabIndicatorWidth = 3;
 
 #undef KeyPress
@@ -128,7 +124,9 @@ namespace frontend {
 
 class CursorWidget {
 public:
-       CursorWidget() {}
+       CursorWidget() {
+               recomputeWidth();
+       }
 
        void draw(QPainter & painter)
        {
@@ -141,7 +139,7 @@ public:
                int bot = rect_.bottom();
 
                // draw vertical line
-               painter.fillRect(x_, y, CursorWidth, rect_.height(), color_);
+               painter.fillRect(x_, y, cursor_width_, rect_.height(), color_);
 
                // draw RTL/LTR indication
                painter.setPen(color_);
@@ -149,7 +147,7 @@ public:
                        if (rtl_)
                                painter.drawLine(x_, bot, x_ - l, bot);
                        else
-                               painter.drawLine(x_, bot, x_ + CursorWidth + r, bot);
+                               painter.drawLine(x_, bot, x_ + cursor_width_ + r, bot);
                }
 
                // draw completion triangle
@@ -160,8 +158,8 @@ public:
                                painter.drawLine(x_ - 1, m - d, x_ - 1 - d, m);
                                painter.drawLine(x_ - 1, m + d, x_ - 1 - d, m);
                        } else {
-                               painter.drawLine(x_ + CursorWidth, m - d, x_ + CursorWidth + d, m);
-                               painter.drawLine(x_ + CursorWidth, m + d, x_ + CursorWidth + d, m);
+                               painter.drawLine(x_ + cursor_width_, m - d, x_ + cursor_width_ + d, m);
+                               painter.drawLine(x_ + cursor_width_, m + d, x_ + cursor_width_ + d, m);
                        }
                }
        }
@@ -196,11 +194,17 @@ public:
                }
 
                // compute overall rectangle
-               rect_ = QRect(x - l, y, CursorWidth + r + l, h);
+               rect_ = QRect(x - l, y, cursor_width_ + r + l, h);
        }
 
        void show(bool set_show = true) { show_ = set_show; }
        void hide() { show_ = false; }
+       int cursorWidth() const { return cursor_width_; }
+       void recomputeWidth() {
+               cursor_width_ = lyxrc.cursor_width
+                       ? lyxrc.cursor_width 
+                       : 1 + int((lyxrc.zoom + 50) / 200.0);
+       }
 
        QRect const & rect() { return rect_; }
 
@@ -219,6 +223,8 @@ private:
        QRect rect_;
        /// x position (were the vertical line is drawn)
        int x_;
+       
+       int cursor_width_;
 };
 
 
@@ -233,8 +239,7 @@ GuiWorkArea::GuiWorkArea(QWidget *)
        : buffer_view_(0), lyx_view_(0),
        cursor_visible_(false),
        need_resize_(false), schedule_redraw_(false),
-       preedit_lines_(1), completer_(new GuiCompleter(this, this)),
-       context_target_pos_()
+       preedit_lines_(1), completer_(new GuiCompleter(this, this))
 {
 }
 
@@ -243,8 +248,7 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
        : buffer_view_(0), read_only_(buffer.isReadonly()), lyx_view_(0),
        cursor_visible_(false),
        need_resize_(false), schedule_redraw_(false),
-       preedit_lines_(1), completer_(new GuiCompleter(this, this)),
-       context_target_pos_()
+       preedit_lines_(1), completer_(new GuiCompleter(this, this))
 {
        setGuiView(gv);
        setBuffer(buffer);
@@ -602,6 +606,7 @@ void GuiWorkArea::showCursor()
                && !completer_->popupVisible()
                && !completer_->inlineVisible();
        cursor_visible_ = true;
+       cursor_->recomputeWidth();
        showCursor(p.x_, p.y_, h, l_shape, isrtl, completable);
 }
 
@@ -697,31 +702,36 @@ bool GuiWorkArea::event(QEvent * e)
 
 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
 {
-       QPoint pos;
+       docstring name;
        if (e->reason() == QContextMenuEvent::Mouse)
-               // the position is set on mouse press
-               pos = context_target_pos_;
-       else
-               pos = e->pos();
-       Cursor const & cur = buffer_view_->cursor();
-       if (e->reason() == QContextMenuEvent::Keyboard && cur.inTexted()) {
-               // Do not access the context menu of math right in front of before
-               // the cursor. This does not work when the cursor is in text.
-               Inset * inset = cur.paragraph().getInset(cur.pos());
-               if (inset && inset->asInsetMath())
-                       --pos.rx();
-               else if (cur.pos() > 0) {
-                       Inset * inset = cur.paragraph().getInset(cur.pos() - 1);
-                       if (inset)
-                               ++pos.rx();
+               // the menu name is set on mouse press
+               name = context_menu_name_;
+       else {
+               QPoint pos = e->pos();
+               Cursor const & cur = buffer_view_->cursor();
+               if (e->reason() == QContextMenuEvent::Keyboard && cur.inTexted()) {
+                       // Do not access the context menu of math right in front of before
+                       // the cursor. This does not work when the cursor is in text.
+                       Inset * inset = cur.paragraph().getInset(cur.pos());
+                       if (inset && inset->asInsetMath())
+                               --pos.rx();
+                       else if (cur.pos() > 0) {
+                               Inset * inset = cur.paragraph().getInset(cur.pos() - 1);
+                               if (inset)
+                                       ++pos.rx();
+                       }
                }
+               name = buffer_view_->contextMenu(pos.x(), pos.y());
        }
-       docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
+       
        if (name.empty()) {
                QAbstractScrollArea::contextMenuEvent(e);
                return;
        }
-       QMenu * menu = guiApp->menus().menu(toqstr(name), *lyx_view_);
+       // always show mnemonics when the keyboard is used to show the context menu
+       // FIXME: This should be fixed in Qt itself
+       bool const keyboard = (e->reason() == QContextMenuEvent::Keyboard);
+       QMenu * menu = guiApp->menus().menu(toqstr(name), *lyx_view_, keyboard);
        if (!menu) {
                QAbstractScrollArea::contextMenuEvent(e);
                return;
@@ -763,14 +773,20 @@ void GuiWorkArea::mousePressEvent(QMouseEvent * e)
                return;
        }
 
-       if (e->button() == Qt::RightButton)
-               context_target_pos_ = e->pos();
-
        inputContext()->reset();
 
        FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
                q_button_state(e->button()));
        dispatch(cmd, q_key_state(e->modifiers()));
+
+       // Save the context menu on mouse press, because also the mouse
+       // cursor is set on mouse press. Afterwards, we can either release
+       // the mousebutton somewhere else, or the cursor might have moved
+       // due to the DEPM. We need to do this after the mouse has been
+       // set in dispatch(), because the selection state might change.
+       if (e->button() == Qt::RightButton)
+               context_menu_name_ = buffer_view_->contextMenu(e->x(), e->y());
+
        e->accept();
 }
 
@@ -930,7 +946,11 @@ void GuiWorkArea::generateSyntheticMouseEvent()
 
        // In which paragraph do we have to set the cursor ?
        Cursor & cur = buffer_view_->cursor();
-       TextMetrics const & tm = buffer_view_->textMetrics(cur.text());
+       // FIXME: we don't know howto handle math.
+       Text * text = cur.text();
+       if (!text)
+               return;
+       TextMetrics const & tm = buffer_view_->textMetrics(text);
 
        pair<pit_type, const ParagraphMetrics *> p = up ? tm.first() : tm.last();
        ParagraphMetrics const & pm = *p.second;
@@ -996,14 +1016,35 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
        // it looks like this is only needed on X11
 #ifdef Q_WS_X11
        if (qApp->hasPendingEvents() && ev->isAutoRepeat()) {
-               LYXERR(Debug::KEY, "system is busy: keyPress event ignored");
-               ev->ignore();
-               return;
+               switch (ev->key()) {
+               case Qt::Key_PageDown:
+               case Qt::Key_PageUp:
+               case Qt::Key_Left:
+               case Qt::Key_Right:
+               case Qt::Key_Up:
+               case Qt::Key_Down:
+                       LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
+                       ev->ignore();
+                       return;
+               }
        }
 #endif
 
+       KeyModifier m = q_key_state(ev->modifiers());
+
+       std::string str;
+       if (m & ShiftModifier)
+               str += "Shift-";
+       if (m & ControlModifier)
+               str += "Control-";
+       if (m & AltModifier)
+               str += "Alt-";
+       if (m & MetaModifier)
+               str += "Meta-";
+       
        LYXERR(Debug::KEY, " count: " << ev->count() << " text: " << ev->text()
-               << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
+               << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key()
+               << " keyState: " << str);
 
        KeySymbol sym;
        setKeySymbol(&sym, ev);
@@ -1045,7 +1086,7 @@ void GuiWorkArea::resizeEvent(QResizeEvent * ev)
 
 void GuiWorkArea::update(int x, int y, int w, int h)
 {
-       viewport()->repaint(x, y, w, h);
+       viewport()->update(x, y, w, h);
 }
 
 
@@ -1275,11 +1316,11 @@ void GuiWorkArea::updateWindowTitle()
        docstring maximize_title;
        docstring minimize_title;
 
-       Buffer & buf = buffer_view_->buffer();
-       FileName const fileName = buf.fileName();
-       if (!fileName.empty()) {
-               maximize_title = fileName.displayName(30);
-               minimize_title = from_utf8(fileName.onlyFileName());
+       Buffer const & buf = buffer_view_->buffer();
+       FileName const file_name = buf.fileName();
+       if (!file_name.empty()) {
+               maximize_title = file_name.displayName(130);
+               minimize_title = from_utf8(file_name.onlyFileName());
                if (buf.lyxvc().inUse()) {
                        if (buf.lyxvc().locking())
                                maximize_title +=  _(" (version control, locking)");
@@ -1294,14 +1335,12 @@ void GuiWorkArea::updateWindowTitle()
                        maximize_title += _(" (read only)");
        }
 
-       QString title = windowTitle();
-       QString new_title = toqstr(maximize_title);
-       if (title == new_title)
-               return;
-
-       QWidget::setWindowTitle(new_title);
-       QWidget::setWindowIconText(toqstr(minimize_title));
-       titleChanged(this);
+       QString const new_title = toqstr(maximize_title);
+       if (new_title != windowTitle()) {
+               QWidget::setWindowTitle(new_title);
+               QWidget::setWindowIconText(toqstr(minimize_title));
+               titleChanged(this);
+       }
 }
 
 
@@ -1867,12 +1906,13 @@ void TabWorkArea::updateTabTexts()
 
        // set new tab titles
        for (It it = paths.begin(); it != paths.end(); ++it) {
-               GuiWorkArea * i_wa = workArea(it->tab());
-               Buffer & buf = i_wa->bufferView().buffer();
+               int const tab_index = it->tab();
+               Buffer const & buf = workArea(tab_index)->bufferView().buffer();
+               QString tab_text = it->displayString();
                if (!buf.fileName().empty() && !buf.isClean())
-                       setTabText(it->tab(), it->displayString() + "*");
-               else
-                       setTabText(it->tab(), it->displayString());
+                       tab_text += "*";
+               setTabText(tab_index, tab_text);
+               setTabToolTip(tab_index, it->abs());
        }
 }