]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 37c9db7e0d4517d6a5a48efe761dd804a4d0f333..6a15b78b5848f4898ed4483e42178b1babd28877 100644 (file)
@@ -231,7 +231,7 @@ private:
 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
 SyntheticMouseEvent::SyntheticMouseEvent()
        : timeout(200), restart_timeout(true),
-         x_old(-1), y_old(-1), min_scrollbar_old(-1.0), max_scrollbar_old(-1.0)
+         x_old(-1), y_old(-1), min_scrollbar_old(-1), max_scrollbar_old(-1)
 {}
 
 
@@ -239,7 +239,8 @@ 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))
+       preedit_lines_(1), completer_(new GuiCompleter(this)),
+       context_target_pos_()
 {
 }
 
@@ -248,7 +249,8 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
        : buffer_view_(0), lyx_view_(0),
        cursor_visible_(false),
        need_resize_(false), schedule_redraw_(false),
-       preedit_lines_(1), completer_(new GuiCompleter(this))
+       preedit_lines_(1), completer_(new GuiCompleter(this)),
+       context_target_pos_()
 {
        setGuiView(gv);
        setBuffer(buffer);
@@ -363,18 +365,13 @@ void GuiWorkArea::close()
 void GuiWorkArea::setFullScreen(bool full_screen)
 {
        buffer_view_->setFullScreen(full_screen);
+       setFrameStyle(QFrame::NoFrame);
        if (full_screen) {
                setFrameStyle(QFrame::NoFrame);
                if (lyxrc.full_screen_scrollbar)
                        setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-       } else {
-#ifdef Q_WS_MACX
-               setFrameStyle(QFrame::NoFrame);
-#else
-               setFrameStyle(QFrame::Box);
-#endif
+       } else
                setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
-       }
 }
 
 
@@ -447,7 +444,8 @@ void GuiWorkArea::redraw()
 
 void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
 {
-       if (lyx_view_->isFullScreen() && lyx_view_->menuBar()->isVisible()) {
+       if (lyx_view_->isFullScreen() && lyx_view_->menuBar()->isVisible()
+               && lyxrc.full_screen_menubar) {
                // FIXME HACK: we should not have to do this here. See related comment
                // in GuiView::event() (QEvent::ShortcutOverride)
                lyx_view_->menuBar()->hide();
@@ -665,7 +663,25 @@ bool GuiWorkArea::event(QEvent * e)
 
 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
 {
-       QPoint pos = e->pos();
+       QPoint pos;
+       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();
+               }
+       }
        docstring name = buffer_view_->contextMenu(pos.x(), pos.y());
        if (name.empty()) {
                QAbstractScrollArea::contextMenuEvent(e);
@@ -720,6 +736,9 @@ 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(),
@@ -906,8 +925,12 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
 
        KeySymbol sym;
        setKeySymbol(&sym, ev);
-       processKeySym(sym, q_key_state(ev->modifiers()));
-       ev->accept();
+       if (sym.isOK()) {
+               processKeySym(sym, q_key_state(ev->modifiers()));
+               ev->accept();
+       } else {
+               ev->ignore();
+       }
 }
 
 
@@ -1147,12 +1170,14 @@ QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
 {
        QRect cur_r(0, 0, 0, 0);
        switch (query) {
-               // this is the CJK-specific composition window position.
+               // this is the CJK-specific composition window position and
+               // the context menu position when the menu key is pressed.
                case Qt::ImMicroFocus:
                        cur_r = cursor_->rect();
                        if (preedit_lines_ != 1)
                                cur_r.moveLeft(10);
-                       cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
+                       cur_r.moveBottom(cur_r.bottom()
+                               + cur_r.height() * (preedit_lines_ - 1));
                        // return lower right of cursor in LyX.
                        return cur_r;
                default:
@@ -1310,6 +1335,7 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        QObject::connect(this, SIGNAL(currentChanged(int)),
                this, SLOT(on_currentTabChanged(int)));
 
+#if QT_VERSION < 0x040500
        closeBufferButton = new QToolButton(this);
        closeBufferButton->setPalette(pal);
        // FIXME: rename the icon to closebuffer.png
@@ -1322,6 +1348,7 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        QObject::connect(closeBufferButton, SIGNAL(clicked()),
                this, SLOT(closeCurrentBuffer()));
        setCornerWidget(closeBufferButton, Qt::TopRightCorner);
+#endif
 
        // setup drag'n'drop
        QTabBar* tb = new DragTabBar;
@@ -1351,7 +1378,7 @@ void TabWorkArea::setFullScreen(bool full_screen)
        }
 
        if (lyxrc.full_screen_tabbar)
-               showBar(!full_screen && count()>1);
+               showBar(!full_screen && count() > 1);
 }
 
 
@@ -1359,7 +1386,9 @@ void TabWorkArea::showBar(bool show)
 {
        tabBar()->setEnabled(show);
        tabBar()->setVisible(show);
+#if QT_VERSION < 0x040500
        closeBufferButton->setVisible(show);    
+#endif
 }
 
 
@@ -1456,11 +1485,10 @@ bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
                if (currentWorkArea() && currentWorkArea()->isFullScreen())
                        setFullScreen(true);
                else
-                       // Hide tabbar if there's only one tab.
+                       // Show tabbar only if there's more than one tab.
                        showBar(count() > 1);
-       } else {
+       } else
                lastWorkAreaRemoved();
-       }
 
        updateTabTexts();
 
@@ -1491,26 +1519,27 @@ void TabWorkArea::on_currentTabChanged(int i)
 
 void TabWorkArea::closeCurrentBuffer()
 {
-       if (clicked_tab_ != -1)
-               setCurrentIndex(clicked_tab_);
-       else
-               // Before dispatching the LFUN we should be sure this
-               // is the current workarea.
-               currentWorkAreaChanged(currentWorkArea());
-
-       lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
+       GuiWorkArea * wa;
+       if (clicked_tab_ == -1)
+               wa = currentWorkArea();
+       else {
+               wa = dynamic_cast<GuiWorkArea *>(widget(clicked_tab_));
+               LASSERT(wa, /**/);
+       }
+       wa->view().closeWorkArea(wa);
 }
 
 
 void TabWorkArea::closeCurrentTab()
 {
+       GuiWorkArea * wa;
        if (clicked_tab_ == -1)
-               removeWorkArea(currentWorkArea());
+               wa = currentWorkArea();
        else {
-               GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(clicked_tab_));
+               wa = dynamic_cast<GuiWorkArea *>(widget(clicked_tab_));
                LASSERT(wa, /**/);
-               removeWorkArea(wa);
        }
+       wa->view().hideWorkArea(wa);
 }
 
 ///
@@ -1522,7 +1551,9 @@ public:
        DisplayPath(int tab, FileName const & filename)
                : tab_(tab)
        {
-               filename_ = toqstr(filename.onlyFileNameWithoutExt());
+               filename_ = (filename.extension() == "lyx") ?
+                       toqstr(filename.onlyFileNameWithoutExt())
+                       : toqstr(filename.onlyFileName());
                postfix_ = toqstr(filename.absoluteFilePath()).
                        split("/", QString::SkipEmptyParts);
                postfix_.pop_back();