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"
28 #include "support/lyxalgo.h"
29 #include "support/lstrings.h"
31 #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 bool event(QEvent * ev) {
59 if (ev->type() == QEvent::MouseButtonPress) {
60 QMouseEvent * me = static_cast<QMouseEvent *>(ev);
61 if (me->x() < 0 || me->y() < 0
62 || me->x() > width() || me->y() > height())
66 return QListWidget::event(ev);
69 void keyPressEvent(QKeyEvent * ev) {
70 if (ev->key() == Qt::Key_Escape) {
73 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
75 itemClicked(currentItem());
77 QListWidget::keyPressEvent(ev);
84 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
87 transform(lyxaction.func_begin(), lyxaction.func_end(),
88 back_inserter(commands_), firster());
90 QPixmap qpup(":/images/up.png");
91 QPixmap qpdown(":/images/down.png");
93 QVBoxLayout * top = new QVBoxLayout(this);
94 QHBoxLayout * layout = new QHBoxLayout(0);
96 upPB = new QPushButton(qpup, "", this);
97 upPB->setToolTip(qt_("List of previous commands"));
98 upPB->setMaximumSize(24, 24);
99 downPB = new QPushButton(qpdown, "", this);
100 downPB->setToolTip(qt_("Next command"));
101 downPB->setMaximumSize(24, 24);
102 downPB->setEnabled(false);
103 connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
104 connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
106 edit_ = new GuiCommandEdit(this);
107 edit_->setMinimumSize(edit_->sizeHint());
108 edit_->setFocusPolicy(Qt::ClickFocus);
110 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
111 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
112 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
113 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
114 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
115 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
117 layout->addWidget(upPB, 0);
118 layout->addWidget(downPB, 0);
119 layout->addWidget(edit_, 10);
120 layout->setMargin(0);
121 top->addLayout(layout);
123 setFocusProxy(edit_);
125 LastCommandsSection::LastCommands last_commands
126 = theSession().lastCommands().getcommands();
127 LastCommandsSection::LastCommands::const_iterator it
128 = last_commands.begin();
129 LastCommandsSection::LastCommands::const_iterator end
130 = last_commands.end();
132 upPB->setEnabled(it != end);
134 for(; it != end; ++it)
135 history_.push_back(*it);
136 history_pos_ = history_.end();
140 void GuiCommandBuffer::cancel()
143 edit_->setText(QString());
147 void GuiCommandBuffer::dispatch()
149 QString cmd = edit_->text();
151 edit_->setText(QString());
153 std::string const cmd_ = fromqstr(cmd);
154 theSession().lastCommands().add(cmd_);
159 void GuiCommandBuffer::listHistoryUp()
161 if (history_.size()==1) {
162 edit_->setText(toqstr(history_.back()));
163 upPB->setEnabled(false);
166 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
167 showList(history_, pos, true);
171 void GuiCommandBuffer::complete()
173 string const input = fromqstr(edit_->text());
175 vector<string> const & comp = completions(input, new_input);
178 if (new_input != input)
179 edit_->setText(toqstr(new_input));
183 edit_->setText(toqstr(new_input));
184 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
188 void GuiCommandBuffer::showList(vector<string> const & list,
189 QPoint const & pos, bool reversed) const
191 QTempListBox * listBox = new QTempListBox;
193 // For some reason the scrollview's contents are larger
194 // than the number of actual items...
195 vector<string>::const_iterator cit = list.begin();
196 vector<string>::const_iterator end = list.end();
197 for (; cit != end; ++cit) {
199 listBox->insertItem(0, toqstr(*cit));
201 listBox->addItem(toqstr(*cit));
204 listBox->resize(listBox->sizeHint());
206 int const y = max(0, pos.y() - listBox->height());
207 listBox->move(pos.x(), y);
209 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
210 this, SLOT(item_selected(QListWidgetItem *)));
211 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
212 this, SLOT(item_selected(QListWidgetItem *)));
219 void GuiCommandBuffer::item_selected(QListWidgetItem * item)
221 QWidget const * widget = static_cast<QWidget const *>(sender());
222 const_cast<QWidget *>(widget)->hide();
223 edit_->setText(item->text()+ ' ');
224 edit_->activateWindow();
229 void GuiCommandBuffer::up()
231 string const input = fromqstr(edit_->text());
232 string const h = historyUp();
235 edit_->setText(toqstr(h));
237 upPB->setEnabled(history_pos_ != history_.begin());
238 downPB->setEnabled(history_pos_ != history_.end());
242 void GuiCommandBuffer::down()
244 string const input = fromqstr(edit_->text());
245 string const h = historyDown();
248 edit_->setText(toqstr(h));
250 downPB->setEnabled(history_pos_ != history_.end()-1);
251 upPB->setEnabled(history_pos_ != history_.begin());
255 void GuiCommandBuffer::hideParent()
258 edit_->setText(QString());
269 prefix_p(string const & s) : p(s) {}
270 bool operator()(string const & s) const { return prefixIs(s, p); }
273 } // end of anon namespace
276 string const GuiCommandBuffer::historyUp()
278 if (history_pos_ == history_.begin())
281 return *(--history_pos_);
285 string const GuiCommandBuffer::historyDown()
287 if (history_pos_ == history_.end())
289 if (history_pos_ + 1 == history_.end())
292 return *(++history_pos_);
296 docstring const GuiCommandBuffer::getCurrentState() const
298 return view_->view()->cursor().currentState();
302 void GuiCommandBuffer::hide() const
304 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
305 theLyXFunc().setLyXView(view_);
311 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
315 copy_if(commands_.begin(), commands_.end(),
316 back_inserter(comp), prefix_p(prefix));
323 if (comp.size() == 1) {
324 new_prefix = comp[0];
325 return vector<string>();
328 // find maximal available prefix
329 string const tmp = comp[0];
330 string test = prefix;
331 if (tmp.length() > test.length())
332 test += tmp[test.length()];
333 while (test.length() < tmp.length()) {
335 copy_if(comp.begin(), comp.end(),
336 back_inserter(vtmp), prefix_p(test));
337 if (vtmp.size() != comp.size()) {
338 test.erase(test.length() - 1);
341 test += tmp[test.length()];
349 void GuiCommandBuffer::dispatch(string const & str)
354 history_.push_back(trim(str));
355 history_pos_ = history_.end();
356 upPB->setEnabled(history_pos_ != history_.begin());
357 downPB->setEnabled(history_pos_ != history_.end());
358 FuncRequest func = lyxaction.lookupFunc(str);
359 func.origin = FuncRequest::COMMANDBUFFER;
360 theLyXFunc().setLyXView(view_);
364 } // namespace frontend
367 #include "moc_GuiCommandBuffer.cpp"