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>
33 #include <QListWidget>
34 #include <QMouseEvent>
36 #include <QPushButton>
38 #include <QVBoxLayout>
41 using namespace lyx::support;
48 class QTempListBox : public QListWidget {
51 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
52 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53 setWindowModality(Qt::WindowModal);
54 setWindowFlags(Qt::Popup);
55 setAttribute(Qt::WA_DeleteOnClose);
58 void mouseReleaseEvent(QMouseEvent * ev) {
59 if (ev->x() < 0 || ev->y() < 0
60 || ev->x() > width() || ev->y() > height()) {
64 itemPressed(currentItem());
68 void keyPressEvent(QKeyEvent * ev) {
69 if (ev->key() == Qt::Key_Escape) {
72 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
74 itemPressed(currentItem());
76 QListWidget::keyPressEvent(ev);
83 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
84 : view_(view), history_pos_(history_.end())
86 transform(lyxaction.func_begin(), lyxaction.func_end(),
87 back_inserter(commands_), firster());
89 QPixmap qpup(":/images/up.png");
90 QPixmap qpdown(":/images/down.png");
92 QVBoxLayout * top = new QVBoxLayout(this);
93 QHBoxLayout * layout = new QHBoxLayout(0);
95 QPushButton * up = new QPushButton(qpup, "", this);
96 up->setMaximumSize(24, 24);
97 QPushButton * down = new QPushButton(qpdown, "", this);
98 down->setToolTip(qt_("Next command"));
99 down->setMaximumSize(24, 24);
100 connect(down, SIGNAL(clicked()), this, SLOT(down()));
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 dispatch(fromqstr(edit_->text()));
134 edit_->setText(QString());
139 void GuiCommandBuffer::complete()
141 string const input = fromqstr(edit_->text());
143 vector<string> comp = completions(input, new_input);
145 if (comp.empty() && new_input == input) {
146 // show_info_suffix(qt_("[no match]"), input);
151 edit_->setText(toqstr(new_input));
152 // show_info_suffix(("[only completion]"), new_input + ' ');
156 edit_->setText(toqstr(new_input));
158 QTempListBox * list = new QTempListBox;
160 // For some reason the scrollview's contents are larger
161 // than the number of actual items...
162 vector<string>::const_iterator cit = comp.begin();
163 vector<string>::const_iterator end = comp.end();
164 for (; cit != end; ++cit)
165 list->addItem(toqstr(*cit));
167 list->resize(list->sizeHint());
168 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
170 int const y = max(0, pos.y() - list->height());
172 list->move(pos.x(), y);
174 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
175 this, SLOT(complete_selected(QListWidgetItem *)));
176 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
177 this, SLOT(complete_selected(QListWidgetItem *)));
184 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
186 QWidget const * widget = static_cast<QWidget const *>(sender());
187 const_cast<QWidget *>(widget)->hide();
188 edit_->setText(item->text() + ' ');
189 edit_->activateWindow();
194 void GuiCommandBuffer::up()
196 string const input = fromqstr(edit_->text());
197 string const h = historyUp();
200 // show_info_suffix(qt_("[Beginning of history]"), input);
202 edit_->setText(toqstr(h));
207 void GuiCommandBuffer::down()
209 string const input = fromqstr(edit_->text());
210 string const h = historyDown();
213 // show_info_suffix(qt_("[End of history]"), input);
215 edit_->setText(toqstr(h));
220 void GuiCommandBuffer::hideParent()
223 edit_->setText(QString());
234 prefix_p(string const & s) : p(s) {}
235 bool operator()(string const & s) const { return prefixIs(s, p); }
238 } // end of anon namespace
241 string const GuiCommandBuffer::historyUp()
243 if (history_pos_ == history_.begin())
246 return *(--history_pos_);
250 string const GuiCommandBuffer::historyDown()
252 if (history_pos_ == history_.end())
254 if (history_pos_ + 1 == history_.end())
257 return *(++history_pos_);
261 docstring const GuiCommandBuffer::getCurrentState() const
263 return view_->view()->cursor().currentState();
267 void GuiCommandBuffer::hide() const
269 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
270 theLyXFunc().setLyXView(view_);
276 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
280 copy_if(commands_.begin(), commands_.end(),
281 back_inserter(comp), prefix_p(prefix));
288 if (comp.size() == 1) {
289 new_prefix = comp[0];
290 return vector<string>();
293 // find maximal available prefix
294 string const tmp = comp[0];
295 string test = prefix;
296 if (tmp.length() > test.length())
297 test += tmp[test.length()];
298 while (test.length() < tmp.length()) {
300 copy_if(comp.begin(), comp.end(),
301 back_inserter(vtmp), prefix_p(test));
302 if (vtmp.size() != comp.size()) {
303 test.erase(test.length() - 1);
306 test += tmp[test.length()];
314 void GuiCommandBuffer::dispatch(string const & str)
319 history_.push_back(str);
320 history_pos_ = history_.end();
321 FuncRequest func = lyxaction.lookupFunc(str);
322 func.origin = FuncRequest::COMMANDBUFFER;
323 theLyXFunc().setLyXView(view_);
327 } // namespace frontend
330 #include "GuiCommandBuffer_moc.cpp"