2 * \file GuiCommandBuffer.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Jürgen
10 * Full author contact details are available in file CREDITS.
15 #include "GuiCommandBuffer.h"
17 #include "GuiCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "BufferView.h"
24 #include "LyXAction.h"
25 #include "FuncRequest.h"
27 #include "support/lyxalgo.h"
28 #include "support/lstrings.h"
30 #include <QHBoxLayout>
32 #include <QListWidget>
33 #include <QMouseEvent>
35 #include <QPushButton>
37 #include <QVBoxLayout>
40 using namespace lyx::support;
47 class QTempListBox : public QListWidget {
50 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
51 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52 setWindowModality(Qt::WindowModal);
53 setWindowFlags(Qt::Popup);
54 setAttribute(Qt::WA_DeleteOnClose);
57 void mouseReleaseEvent(QMouseEvent * ev) {
58 if (ev->x() < 0 || ev->y() < 0
59 || ev->x() > width() || ev->y() > height()) {
63 itemPressed(currentItem());
67 void keyPressEvent(QKeyEvent * ev) {
68 if (ev->key() == Qt::Key_Escape) {
71 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
73 itemPressed(currentItem());
75 QListWidget::keyPressEvent(ev);
82 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
83 : view_(view), history_pos_(history_.end())
85 transform(lyxaction.func_begin(), lyxaction.func_end(),
86 back_inserter(commands_), firster());
88 QPixmap qpup(":/images/up.png");
89 QPixmap qpdown(":/images/down.png");
91 QVBoxLayout * top = new QVBoxLayout(this);
92 QHBoxLayout * layout = new QHBoxLayout(0);
94 QPushButton * up = new QPushButton(qpup, "", this);
95 up->setMaximumSize(24, 24);
96 QPushButton * down = new QPushButton(qpdown, "", this);
97 down->setToolTip(qt_("Next command"));
98 down->setMaximumSize(24, 24);
99 connect(down, SIGNAL(clicked()), this, SLOT(down()));
100 connect(up, SIGNAL(clicked()), this, SLOT(up()));
102 edit_ = new GuiCommandEdit(this);
103 edit_->setMinimumSize(edit_->sizeHint());
104 edit_->setFocusPolicy(Qt::ClickFocus);
106 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
107 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
108 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
109 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
110 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
111 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
113 layout->addWidget(up, 0);
114 layout->addWidget(down, 0);
115 layout->addWidget(edit_, 10);
116 layout->setMargin(0);
117 top->addLayout(layout);
119 setFocusProxy(edit_);
123 void GuiCommandBuffer::cancel()
126 edit_->setText(QString());
130 void GuiCommandBuffer::dispatch()
132 QString cmd = edit_->text();
134 edit_->setText(QString());
136 dispatch(fromqstr(cmd));
140 void GuiCommandBuffer::complete()
142 string const input = fromqstr(edit_->text());
144 vector<string> comp = completions(input, new_input);
146 if (comp.empty() && new_input == input) {
147 // show_info_suffix(qt_("[no match]"), input);
152 edit_->setText(toqstr(new_input));
153 // show_info_suffix(("[only completion]"), new_input + ' ');
157 edit_->setText(toqstr(new_input));
159 QTempListBox * list = new QTempListBox;
161 // For some reason the scrollview's contents are larger
162 // than the number of actual items...
163 vector<string>::const_iterator cit = comp.begin();
164 vector<string>::const_iterator end = comp.end();
165 for (; cit != end; ++cit)
166 list->addItem(toqstr(*cit));
168 list->resize(list->sizeHint());
169 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
171 int const y = max(0, pos.y() - list->height());
173 list->move(pos.x(), y);
175 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
176 this, SLOT(complete_selected(QListWidgetItem *)));
177 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
178 this, SLOT(complete_selected(QListWidgetItem *)));
185 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
187 QWidget const * widget = static_cast<QWidget const *>(sender());
188 const_cast<QWidget *>(widget)->hide();
189 edit_->setText(item->text() + ' ');
190 edit_->activateWindow();
195 void GuiCommandBuffer::up()
197 string const input = fromqstr(edit_->text());
198 string const h = historyUp();
201 // show_info_suffix(qt_("[Beginning of history]"), input);
203 edit_->setText(toqstr(h));
208 void GuiCommandBuffer::down()
210 string const input = fromqstr(edit_->text());
211 string const h = historyDown();
214 // show_info_suffix(qt_("[End of history]"), input);
216 edit_->setText(toqstr(h));
221 void GuiCommandBuffer::hideParent()
224 edit_->setText(QString());
235 prefix_p(string const & s) : p(s) {}
236 bool operator()(string const & s) const { return prefixIs(s, p); }
239 } // end of anon namespace
242 string const GuiCommandBuffer::historyUp()
244 if (history_pos_ == history_.begin())
247 return *(--history_pos_);
251 string const GuiCommandBuffer::historyDown()
253 if (history_pos_ == history_.end())
255 if (history_pos_ + 1 == history_.end())
258 return *(++history_pos_);
262 docstring const GuiCommandBuffer::getCurrentState() const
264 return view_->view()->cursor().currentState();
268 void GuiCommandBuffer::hide() const
270 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
271 theLyXFunc().setLyXView(view_);
277 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
281 copy_if(commands_.begin(), commands_.end(),
282 back_inserter(comp), prefix_p(prefix));
289 if (comp.size() == 1) {
290 new_prefix = comp[0];
291 return vector<string>();
294 // find maximal available prefix
295 string const tmp = comp[0];
296 string test = prefix;
297 if (tmp.length() > test.length())
298 test += tmp[test.length()];
299 while (test.length() < tmp.length()) {
301 copy_if(comp.begin(), comp.end(),
302 back_inserter(vtmp), prefix_p(test));
303 if (vtmp.size() != comp.size()) {
304 test.erase(test.length() - 1);
307 test += tmp[test.length()];
315 void GuiCommandBuffer::dispatch(string const & str)
320 history_.push_back(str);
321 history_pos_ = history_.end();
322 FuncRequest func = lyxaction.lookupFunc(str);
323 func.origin = FuncRequest::COMMANDBUFFER;
324 theLyXFunc().setLyXView(view_);
328 } // namespace frontend
331 #include "moc_GuiCommandBuffer.cpp"