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>
45 using support::prefixIs;
50 class QTempListBox : public QListWidget {
53 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55 setWindowModality(Qt::WindowModal);
56 setWindowFlags(Qt::Popup);
57 setAttribute(Qt::WA_DeleteOnClose);
60 void mouseReleaseEvent(QMouseEvent * ev) {
61 if (ev->x() < 0 || ev->y() < 0
62 || ev->x() > width() || ev->y() > height()) {
66 itemPressed(currentItem());
70 void keyPressEvent(QKeyEvent * ev) {
71 if (ev->key() == Qt::Key_Escape) {
74 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
76 itemPressed(currentItem());
78 QListWidget::keyPressEvent(ev);
85 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
86 : view_(view), history_pos_(history_.end())
88 transform(lyxaction.func_begin(), lyxaction.func_end(),
89 back_inserter(commands_), firster());
91 QPixmap qpup(":/images/up.png");
92 QPixmap qpdown(":/images/down.png");
94 QVBoxLayout * top = new QVBoxLayout(this);
95 QHBoxLayout * layout = new QHBoxLayout(0);
97 QPushButton * up = new QPushButton(qpup, "", this);
98 up->setMaximumSize(24, 24);
99 QPushButton * down = new QPushButton(qpdown, "", this);
100 down->setToolTip(qt_("Next command"));
101 down->setMaximumSize(24, 24);
102 connect(down, SIGNAL(clicked()), this, SLOT(down()));
104 edit_ = new GuiCommandEdit(this);
105 edit_->setMinimumSize(edit_->sizeHint());
106 edit_->setFocusPolicy(Qt::ClickFocus);
108 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
109 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
110 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
111 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
112 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
113 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
115 layout->addWidget(up, 0);
116 layout->addWidget(down, 0);
117 layout->addWidget(edit_, 10);
118 layout->setMargin(0);
119 top->addLayout(layout);
121 setFocusProxy(edit_);
125 void GuiCommandBuffer::cancel()
128 edit_->setText(QString());
132 void GuiCommandBuffer::dispatch()
134 dispatch(fromqstr(edit_->text()));
136 edit_->setText(QString());
141 void GuiCommandBuffer::complete()
143 string const input = fromqstr(edit_->text());
145 vector<string> comp = completions(input, new_input);
147 if (comp.empty() && new_input == input) {
148 // show_info_suffix(qt_("[no match]"), input);
153 edit_->setText(toqstr(new_input));
154 // show_info_suffix(("[only completion]"), new_input + ' ');
158 edit_->setText(toqstr(new_input));
160 QTempListBox * list = new QTempListBox;
162 // For some reason the scrollview's contents are larger
163 // than the number of actual items...
164 vector<string>::const_iterator cit = comp.begin();
165 vector<string>::const_iterator end = comp.end();
166 for (; cit != end; ++cit)
167 list->addItem(toqstr(*cit));
169 list->resize(list->sizeHint());
170 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
172 int const y = std::max(0, pos.y() - list->height());
174 list->move(pos.x(), y);
176 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
177 this, SLOT(complete_selected(QListWidgetItem *)));
178 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
179 this, SLOT(complete_selected(QListWidgetItem *)));
186 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
188 QWidget const * widget = static_cast<QWidget const *>(sender());
189 const_cast<QWidget *>(widget)->hide();
190 edit_->setText(item->text() + ' ');
191 edit_->activateWindow();
196 void GuiCommandBuffer::up()
198 string const input = fromqstr(edit_->text());
199 string const h = historyUp();
202 // show_info_suffix(qt_("[Beginning of history]"), input);
204 edit_->setText(toqstr(h));
209 void GuiCommandBuffer::down()
211 string const input = fromqstr(edit_->text());
212 string const h = historyDown();
215 // show_info_suffix(qt_("[End of history]"), input);
217 edit_->setText(toqstr(h));
222 void GuiCommandBuffer::hideParent()
225 edit_->setText(QString());
236 prefix_p(string const & s) : p(s) {}
237 bool operator()(string const & s) const { return prefixIs(s, p); }
240 } // end of anon namespace
243 string const GuiCommandBuffer::historyUp()
245 if (history_pos_ == history_.begin())
248 return *(--history_pos_);
252 string const GuiCommandBuffer::historyDown()
254 if (history_pos_ == history_.end())
256 if (history_pos_ + 1 == history_.end())
259 return *(++history_pos_);
263 docstring const GuiCommandBuffer::getCurrentState() const
265 return view_->view()->cursor().currentState();
269 void GuiCommandBuffer::hide() const
271 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
272 theLyXFunc().setLyXView(view_);
278 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
282 copy_if(commands_.begin(), commands_.end(),
283 back_inserter(comp), prefix_p(prefix));
290 if (comp.size() == 1) {
291 new_prefix = comp[0];
292 return vector<string>();
295 // find maximal available prefix
296 string const tmp = comp[0];
297 string test = prefix;
298 if (tmp.length() > test.length())
299 test += tmp[test.length()];
300 while (test.length() < tmp.length()) {
302 copy_if(comp.begin(), comp.end(),
303 back_inserter(vtmp), prefix_p(test));
304 if (vtmp.size() != comp.size()) {
305 test.erase(test.length() - 1);
308 test += tmp[test.length()];
316 void GuiCommandBuffer::dispatch(string const & str)
321 history_.push_back(str);
322 history_pos_ = history_.end();
323 FuncRequest func = lyxaction.lookupFunc(str);
324 func.origin = FuncRequest::COMMANDBUFFER;
325 theLyXFunc().setLyXView(view_);
329 } // namespace frontend
332 #include "GuiCommandBuffer_moc.cpp"