]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCommandBuffer.cpp
Compil fix.
[lyx.git] / src / frontends / qt4 / GuiCommandBuffer.cpp
index 2817ab553556c6347f11821a458bf8d141d5b1b4..6958e45469e3f161d971edcd626d27398965555b 100644 (file)
@@ -3,6 +3,8 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
+ * \author Lars
+ * \author Asger and Jürgen
  * \author John Levon
  *
  * Full author contact details are available in file CREDITS.
 
 #include <config.h>
 
-// Qt defines a macro 'signals' that clashes with a boost namespace.
-// All is well if the namespace is visible first.
-#include "GuiView.h"
-
 #include "GuiCommandBuffer.h"
+
 #include "GuiCommandEdit.h"
+#include "GuiView.h"
 #include "qt_helpers.h"
 
-#include "support/filetools.h"
+#include "BufferView.h"
+#include "Cursor.h"
+#include "LyXFunc.h"
+#include "LyXAction.h"
+#include "FuncRequest.h"
+
+#include "support/lyxalgo.h"
+#include "support/lstrings.h"
 
 #include <QHBoxLayout>
 #include <QKeyEvent>
-#include <QLayout>
 #include <QListWidget>
 #include <QMouseEvent>
 #include <QPixmap>
 #include <QToolTip>
 #include <QVBoxLayout>
 
-using lyx::support::libFileSearch;
-
-using std::vector;
-using std::string;
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
@@ -75,19 +79,20 @@ protected:
 } // end of anon
 
 
-GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
-       : view_(view), controller_(*view)
+GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
+       : view_(view), history_pos_(history_.end())
 {
-       QPixmap qpup(toqstr(libFileSearch("images", "up", "png").absFilename()));
-       QPixmap qpdown(toqstr(libFileSearch("images", "down", "png").absFilename()));
+       transform(lyxaction.func_begin(), lyxaction.func_end(),
+               back_inserter(commands_), firster());
+
+       QPixmap qpup(":/images/up.png");
+       QPixmap qpdown(":/images/down.png");
 
        QVBoxLayout * top = new QVBoxLayout(this);
        QHBoxLayout * layout = new QHBoxLayout(0);
 
        QPushButton * up = new QPushButton(qpup, "", this);
        up->setMaximumSize(24, 24);
-       up->setToolTip(qt_("Previous command"));
-       connect(up, SIGNAL(clicked()), this, SLOT(up()));
        QPushButton * down = new QPushButton(qpdown, "", this);
        down->setToolTip(qt_("Next command"));
        down->setMaximumSize(24, 24);
@@ -123,10 +128,11 @@ void GuiCommandBuffer::cancel()
 
 void GuiCommandBuffer::dispatch()
 {
-       controller_.dispatch(fromqstr(edit_->text()));
+       QString cmd = edit_->text();
        view_->setFocus();
        edit_->setText(QString());
        edit_->clearFocus();
+       dispatch(fromqstr(cmd));
 }
 
 
@@ -134,7 +140,7 @@ void GuiCommandBuffer::complete()
 {
        string const input = fromqstr(edit_->text());
        string new_input;
-       vector<string> comp = controller_.completions(input, new_input);
+       vector<string> comp = completions(input, new_input);
 
        if (comp.empty() && new_input == input) {
                // show_info_suffix(qt_("[no match]"), input);
@@ -161,7 +167,7 @@ void GuiCommandBuffer::complete()
        list->resize(list->sizeHint());
        QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
 
-       int const y = std::max(0, pos.y() - list->height());
+       int const y = max(0, pos.y() - list->height());
 
        list->move(pos.x(), y);
 
@@ -188,7 +194,7 @@ void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
 void GuiCommandBuffer::up()
 {
        string const input = fromqstr(edit_->text());
-       string const h = controller_.historyUp();
+       string const h = historyUp();
 
        if (h.empty()) {
        //      show_info_suffix(qt_("[Beginning of history]"), input);
@@ -201,7 +207,7 @@ void GuiCommandBuffer::up()
 void GuiCommandBuffer::down()
 {
        string const input = fromqstr(edit_->text());
-       string const h = controller_.historyDown();
+       string const h = historyDown();
 
        if (h.empty()) {
        //      show_info_suffix(qt_("[End of history]"), input);
@@ -216,27 +222,107 @@ void GuiCommandBuffer::hideParent()
        view_->setFocus();
        edit_->setText(QString());
        edit_->clearFocus();
-       controller_.hide();
+       hide();
+}
+
+
+namespace {
+
+class prefix_p {
+public:
+       string p;
+       prefix_p(string const & s) : p(s) {}
+       bool operator()(string const & s) const { return prefixIs(s, p); }
+};
+
+} // end of anon namespace
+
+
+string const GuiCommandBuffer::historyUp()
+{
+       if (history_pos_ == history_.begin())
+               return string();
+
+       return *(--history_pos_);
 }
 
 
-#if 0
-void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
+string const GuiCommandBuffer::historyDown()
 {
-       stored_input_ = input;
-       info_suffix_shown_ = true;
-       set_input(input + ' ' + suffix);
-       suffix_timer_->start();
+       if (history_pos_ == history_.end())
+               return string();
+       if (history_pos_ + 1 == history_.end())
+               return string();
+
+       return *(++history_pos_);
 }
 
 
-void XMiniBuffer::suffix_timeout()
+docstring const GuiCommandBuffer::getCurrentState() const
 {
-       info_suffix_shown_ = false;
-       set_input(stored_input_);
+       return view_->view()->cursor().currentState();
 }
 
-#endif
+
+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(),
+               back_inserter(comp), prefix_p(prefix));
+
+       if (comp.empty()) {
+               new_prefix = prefix;
+               return comp;
+       }
+
+       if (comp.size() == 1) {
+               new_prefix = comp[0];
+               return vector<string>();
+       }
+
+       // find maximal available prefix
+       string const tmp = comp[0];
+       string test = prefix;
+       if (tmp.length() > test.length())
+               test += tmp[test.length()];
+       while (test.length() < tmp.length()) {
+               vector<string> vtmp;
+               copy_if(comp.begin(), comp.end(),
+                       back_inserter(vtmp), prefix_p(test));
+               if (vtmp.size() != comp.size()) {
+                       test.erase(test.length() - 1);
+                       break;
+               }
+               test += tmp[test.length()];
+       }
+
+       new_prefix = test;
+       return comp;
+}
+
+
+void GuiCommandBuffer::dispatch(string const & str)
+{
+       if (str.empty())
+               return;
+
+       history_.push_back(str);
+       history_pos_ = history_.end();
+       FuncRequest func = lyxaction.lookupFunc(str);
+       func.origin = FuncRequest::COMMANDBUFFER;
+       theLyXFunc().setLyXView(view_);
+       lyx::dispatch(func);
+}
 
 } // namespace frontend
 } // namespace lyx