]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCommandBuffer.cpp
Revert "Mark some intentional fall-throughs (in a way understandable to gcc)"
[lyx.git] / src / frontends / qt4 / GuiCommandBuffer.cpp
index 2e38950f7085615bdec169224b7abe7df1dcd752..4d94d97738af0af4d8aac90d9487bdd6abf1686e 100644 (file)
 
 #include "GuiCommandBuffer.h"
 
+#include "GuiApplication.h"
 #include "GuiCommandEdit.h"
 #include "GuiView.h"
 #include "qt_helpers.h"
 
 #include "BufferView.h"
 #include "Cursor.h"
-#include "LyXFunc.h"
+#include "LyX.h"
 #include "LyXAction.h"
 #include "FuncRequest.h"
+#include "Session.h"
 
 #include "support/lyxalgo.h"
 #include "support/lstrings.h"
 
 #include <QHBoxLayout>
 #include <QKeyEvent>
-#include <QLayout>
 #include <QListWidget>
 #include <QMouseEvent>
 #include <QPixmap>
@@ -55,14 +56,15 @@ public:
                setAttribute(Qt::WA_DeleteOnClose);
        }
 protected:
-       void mouseReleaseEvent(QMouseEvent * ev) {
-               if (ev->x() < 0 || ev->y() < 0
-                   || ev->x() > width() || ev->y() > height()) {
-                       hide();
-               } else {
-                       // emit signal
-                       itemPressed(currentItem());
+       bool event(QEvent * ev) {
+               if (ev->type() == QEvent::MouseButtonPress) {
+                       QMouseEvent * me = static_cast<QMouseEvent *>(ev);
+                       if (me->x() < 0 || me->y() < 0
+                           || me->x() > width() || me->y() > height())
+                               hide();
+                       return true;
                }
+               return QListWidget::event(ev);
        }
 
        void keyPressEvent(QKeyEvent * ev) {
@@ -71,68 +73,101 @@ protected:
                        return;
                } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
                        // emit signal
-                       itemPressed(currentItem());
+                       itemClicked(currentItem());
                } else
                        QListWidget::keyPressEvent(ev);
        }
 };
 
-} // end of anon
+} // namespace
 
 
 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
-       : view_(view), history_pos_(history_.end())
+       : view_(view)
 {
        transform(lyxaction.func_begin(), lyxaction.func_end(),
                back_inserter(commands_), firster());
 
-       QPixmap qpup(":/images/up.png");
-       QPixmap qpdown(":/images/down.png");
+       QPixmap qpup = getPixmap("images/", "up", "svgz,png");
+       QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
 
        QVBoxLayout * top = new QVBoxLayout(this);
        QHBoxLayout * layout = new QHBoxLayout(0);
 
-       QPushButton * up = new QPushButton(qpup, "", this);
-       up->setMaximumSize(24, 24);
-       QPushButton * down = new QPushButton(qpdown, "", this);
-       down->setToolTip(qt_("Next command"));
-       down->setMaximumSize(24, 24);
-       connect(down, SIGNAL(clicked()), this, SLOT(down()));
-
        edit_ = new GuiCommandEdit(this);
        edit_->setMinimumSize(edit_->sizeHint());
        edit_->setFocusPolicy(Qt::ClickFocus);
 
-       connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
+       int height = max(24, 2 * (edit_->sizeHint().height() / 2));
+       QSize button_size = QSize(height, height);
+       QSize icon_size = button_size - QSize(4, 4);
+
+       upPB = new QPushButton(qpup, "", this);
+       upPB->setToolTip(qt_("List of previous commands"));
+       upPB->setMaximumSize(button_size);
+       upPB->setIconSize(icon_size);
+       downPB = new QPushButton(qpdown, "", this);
+       downPB->setToolTip(qt_("Next command"));
+       downPB->setMaximumSize(button_size);
+       downPB->setIconSize(icon_size);
+       downPB->setEnabled(false);
+       connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
+       connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
+
        connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
        connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
        connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
        connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
-       connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
+       connect(edit_, SIGNAL(escapePressed()), this, SLOT(hideParent()));
 
-       layout->addWidget(up, 0);
-       layout->addWidget(down, 0);
+       layout->addWidget(upPB, 0);
+       layout->addWidget(downPB, 0);
        layout->addWidget(edit_, 10);
        layout->setMargin(0);
        top->addLayout(layout);
        top->setMargin(0);
        setFocusProxy(edit_);
+
+       LastCommandsSection::LastCommands last_commands
+               = theSession().lastCommands().getcommands();
+       LastCommandsSection::LastCommands::const_iterator it
+               = last_commands.begin();
+       LastCommandsSection::LastCommands::const_iterator end
+               = last_commands.end();
+
+       upPB->setEnabled(it != end);
+
+       for(; it != end; ++it)
+               history_.push_back(*it);
+       history_pos_ = history_.end();
 }
 
 
-void GuiCommandBuffer::cancel()
+void GuiCommandBuffer::dispatch()
 {
-       view_->setFocus();
-       edit_->setText(QString());
+       std::string const cmd = fromqstr(edit_->text());
+       if (!cmd.empty())
+               theSession().lastCommands().add(cmd);
+       DispatchResult const & dr = dispatch(cmd);
+       if (!dr.error()) {
+               view_->setFocus();
+               edit_->setText(QString());
+               edit_->clearFocus();
+               // If the toolbar was "auto", it is not needed anymore
+               view_->resetCommandExecute();
+       }
 }
 
 
-void GuiCommandBuffer::dispatch()
+void GuiCommandBuffer::listHistoryUp()
 {
-       dispatch(fromqstr(edit_->text()));
-       view_->setFocus();
-       edit_->setText(QString());
-       edit_->clearFocus();
+       if (history_.size()==1) {
+               edit_->setText(toqstr(history_.back()));
+               upPB->setEnabled(false);
+               return;
+       }
+       QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
+       showList(history_, pos, true);
 }
 
 
@@ -140,52 +175,55 @@ void GuiCommandBuffer::complete()
 {
        string const input = fromqstr(edit_->text());
        string new_input;
-       vector<string> comp = completions(input, new_input);
-
-       if (comp.empty() && new_input == input) {
-               // show_info_suffix(qt_("[no match]"), input);
-               return;
-       }
+       vector<string> const & comp = completions(input, new_input);
 
        if (comp.empty()) {
-               edit_->setText(toqstr(new_input));
-       //      show_info_suffix(("[only completion]"), new_input + ' ');
+               if (new_input != input)
+                       edit_->setText(toqstr(new_input));
                return;
        }
 
        edit_->setText(toqstr(new_input));
+       QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
+       showList(comp, pos);
+}
 
-       QTempListBox * list = new QTempListBox;
+void GuiCommandBuffer::showList(vector<string> const & list,
+       QPoint const & pos, bool reversed) const
+{
+       QTempListBox * listBox = new QTempListBox;
 
        // For some reason the scrollview's contents are larger
        // than the number of actual items...
-       vector<string>::const_iterator cit = comp.begin();
-       vector<string>::const_iterator end = comp.end();
-       for (; cit != end; ++cit)
-               list->addItem(toqstr(*cit));
-
-       list->resize(list->sizeHint());
-       QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
+       vector<string>::const_iterator cit = list.begin();
+       vector<string>::const_iterator end = list.end();
+       for (; cit != end; ++cit) {
+               if (reversed)
+                       listBox->insertItem(0, toqstr(*cit));
+               else
+                       listBox->addItem(toqstr(*cit));
+       }
 
-       int const y = max(0, pos.y() - list->height());
+       listBox->resize(listBox->sizeHint());
 
-       list->move(pos.x(), y);
+       int const y = max(0, pos.y() - listBox->height());
+       listBox->move(pos.x(), y);
 
-       connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
-               this, SLOT(complete_selected(QListWidgetItem *)));
-       connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
-               this, SLOT(complete_selected(QListWidgetItem *)));
+       connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
+               this, SLOT(itemSelected(QListWidgetItem *)));
+       connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
+               this, SLOT(itemSelected(QListWidgetItem *)));
 
-       list->show();
-       list->setFocus();
+       listBox->show();
+       listBox->setFocus();
 }
 
 
-void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
+void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
 {
        QWidget const * widget = static_cast<QWidget const *>(sender());
        const_cast<QWidget *>(widget)->hide();
-       edit_->setText(item->text() + ' ');
+       edit_->setText(item->text()+ ' ');
        edit_->activateWindow();
        edit_->setFocus();
 }
@@ -193,36 +231,35 @@ void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
 
 void GuiCommandBuffer::up()
 {
-       string const input = fromqstr(edit_->text());
        string const h = historyUp();
 
-       if (h.empty()) {
-       //      show_info_suffix(qt_("[Beginning of history]"), input);
-       } else {
+       if (!h.empty())
                edit_->setText(toqstr(h));
-       }
+
+       upPB->setEnabled(history_pos_ != history_.begin());
+       downPB->setEnabled(history_pos_ != history_.end());
 }
 
 
 void GuiCommandBuffer::down()
 {
-       string const input = fromqstr(edit_->text());
        string const h = historyDown();
 
-       if (h.empty()) {
-       //      show_info_suffix(qt_("[End of history]"), input);
-       } else {
+       if (!h.empty())
                edit_->setText(toqstr(h));
-       }
+
+       downPB->setEnabled(!history_.empty()
+                          && history_pos_ != history_.end() - 1);
+       upPB->setEnabled(history_pos_ != history_.begin());
 }
 
 
 void GuiCommandBuffer::hideParent()
 {
        view_->setFocus();
+       view_->resetCommandExecute();
        edit_->setText(QString());
        edit_->clearFocus();
-       hide();
 }
 
 
@@ -235,7 +272,7 @@ public:
        bool operator()(string const & s) const { return prefixIs(s, p); }
 };
 
-} // end of anon namespace
+} // namespace
 
 
 string const GuiCommandBuffer::historyUp()
@@ -258,26 +295,12 @@ string const GuiCommandBuffer::historyDown()
 }
 
 
-docstring const GuiCommandBuffer::getCurrentState() const
-{
-       return view_->view()->cursor().currentState();
-}
-
-
-void GuiCommandBuffer::hide() const
-{
-       FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
-       theLyXFunc().setLyXView(view_);
-       lyx::dispatch(cmd);
-}
-
-
 vector<string> const
 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
 {
        vector<string> comp;
 
-       copy_if(commands_.begin(), commands_.end(),
+       lyx::copy_if(commands_.begin(), commands_.end(),
                back_inserter(comp), prefix_p(prefix));
 
        if (comp.empty()) {
@@ -297,7 +320,7 @@ GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
                test += tmp[test.length()];
        while (test.length() < tmp.length()) {
                vector<string> vtmp;
-               copy_if(comp.begin(), comp.end(),
+               lyx::copy_if(comp.begin(), comp.end(),
                        back_inserter(vtmp), prefix_p(test));
                if (vtmp.size() != comp.size()) {
                        test.erase(test.length() - 1);
@@ -311,20 +334,23 @@ GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
 }
 
 
-void GuiCommandBuffer::dispatch(string const & str)
+DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
 {
-       if (str.empty())
-               return;
+       if (str.empty()) {
+               static DispatchResult empty_dr;
+               return empty_dr;
+       }
 
-       history_.push_back(str);
+       history_.push_back(trim(str));
        history_pos_ = history_.end();
+       upPB->setEnabled(history_pos_ != history_.begin());
+       downPB->setEnabled(history_pos_ != history_.end());
        FuncRequest func = lyxaction.lookupFunc(str);
-       func.origin = FuncRequest::COMMANDBUFFER;
-       theLyXFunc().setLyXView(view_);
-       lyx::dispatch(func);
+       func.setOrigin(FuncRequest::COMMANDBUFFER);
+       return lyx::dispatch(func);
 }
 
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiCommandBuffer_moc.cpp"
+#include "moc_GuiCommandBuffer.cpp"