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 "GuiApplication.h"
18 #include "GuiCommandEdit.h"
20 #include "qt_helpers.h"
22 #include "BufferView.h"
25 #include "LyXAction.h"
26 #include "FuncRequest.h"
29 #include "support/lyxalgo.h"
30 #include "support/lstrings.h"
32 #include <QHBoxLayout>
34 #include <QListWidget>
35 #include <QMouseEvent>
37 #include <QPushButton>
39 #include <QVBoxLayout>
42 using namespace lyx::support;
49 class QTempListBox : public QListWidget {
52 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
53 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 setWindowModality(Qt::WindowModal);
55 setWindowFlags(Qt::Popup);
56 setAttribute(Qt::WA_DeleteOnClose);
59 bool event(QEvent * ev) {
60 if (ev->type() == QEvent::MouseButtonPress) {
61 QMouseEvent * me = static_cast<QMouseEvent *>(ev);
62 if (me->x() < 0 || me->y() < 0
63 || me->x() > width() || me->y() > height())
67 return QListWidget::event(ev);
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 itemClicked(currentItem());
78 QListWidget::keyPressEvent(ev);
85 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
88 transform(lyxaction.func_begin(), lyxaction.func_end(),
89 back_inserter(commands_), firster());
91 QPixmap qpup = getPixmap("images/", "up", "svgz,png");
92 QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
94 QVBoxLayout * top = new QVBoxLayout(this);
95 QHBoxLayout * layout = new QHBoxLayout(0);
97 upPB = new QPushButton(qpup, "", this);
98 upPB->setToolTip(qt_("List of previous commands"));
99 upPB->setMaximumSize(24, 24);
100 downPB = new QPushButton(qpdown, "", this);
101 downPB->setToolTip(qt_("Next command"));
102 downPB->setMaximumSize(24, 24);
103 downPB->setEnabled(false);
104 connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
105 connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
107 edit_ = new GuiCommandEdit(this);
108 edit_->setMinimumSize(edit_->sizeHint());
109 edit_->setFocusPolicy(Qt::ClickFocus);
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(escapePressed()), 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::dispatch()
142 std::string const cmd = fromqstr(edit_->text());
144 theSession().lastCommands().add(cmd);
145 DispatchResult const & dr = dispatch(cmd);
148 edit_->setText(QString());
150 // If the toolbar was "auto", it is not needed anymore
151 view_->resetCommandExecute();
156 void GuiCommandBuffer::listHistoryUp()
158 if (history_.size()==1) {
159 edit_->setText(toqstr(history_.back()));
160 upPB->setEnabled(false);
163 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
164 showList(history_, pos, true);
168 void GuiCommandBuffer::complete()
170 string const input = fromqstr(edit_->text());
172 vector<string> const & comp = completions(input, new_input);
175 if (new_input != input)
176 edit_->setText(toqstr(new_input));
180 edit_->setText(toqstr(new_input));
181 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
185 void GuiCommandBuffer::showList(vector<string> const & list,
186 QPoint const & pos, bool reversed) const
188 QTempListBox * listBox = new QTempListBox;
190 // For some reason the scrollview's contents are larger
191 // than the number of actual items...
192 vector<string>::const_iterator cit = list.begin();
193 vector<string>::const_iterator end = list.end();
194 for (; cit != end; ++cit) {
196 listBox->insertItem(0, toqstr(*cit));
198 listBox->addItem(toqstr(*cit));
201 listBox->resize(listBox->sizeHint());
203 int const y = max(0, pos.y() - listBox->height());
204 listBox->move(pos.x(), y);
206 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
207 this, SLOT(itemSelected(QListWidgetItem *)));
208 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
209 this, SLOT(itemSelected(QListWidgetItem *)));
216 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
218 QWidget const * widget = static_cast<QWidget const *>(sender());
219 const_cast<QWidget *>(widget)->hide();
220 edit_->setText(item->text()+ ' ');
221 edit_->activateWindow();
226 void GuiCommandBuffer::up()
228 string const h = historyUp();
231 edit_->setText(toqstr(h));
233 upPB->setEnabled(history_pos_ != history_.begin());
234 downPB->setEnabled(history_pos_ != history_.end());
238 void GuiCommandBuffer::down()
240 string const h = historyDown();
243 edit_->setText(toqstr(h));
245 downPB->setEnabled(!history_.empty()
246 && history_pos_ != history_.end() - 1);
247 upPB->setEnabled(history_pos_ != history_.begin());
251 void GuiCommandBuffer::hideParent()
254 view_->resetCommandExecute();
255 edit_->setText(QString());
265 prefix_p(string const & s) : p(s) {}
266 bool operator()(string const & s) const { return prefixIs(s, p); }
269 } // end of anon namespace
272 string const GuiCommandBuffer::historyUp()
274 if (history_pos_ == history_.begin())
277 return *(--history_pos_);
281 string const GuiCommandBuffer::historyDown()
283 if (history_pos_ == history_.end())
285 if (history_pos_ + 1 == history_.end())
288 return *(++history_pos_);
292 docstring const GuiCommandBuffer::getCurrentState() const
294 return view_->currentBufferView()->cursor().currentState();
299 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
303 lyx::copy_if(commands_.begin(), commands_.end(),
304 back_inserter(comp), prefix_p(prefix));
311 if (comp.size() == 1) {
312 new_prefix = comp[0];
313 return vector<string>();
316 // find maximal available prefix
317 string const tmp = comp[0];
318 string test = prefix;
319 if (tmp.length() > test.length())
320 test += tmp[test.length()];
321 while (test.length() < tmp.length()) {
323 lyx::copy_if(comp.begin(), comp.end(),
324 back_inserter(vtmp), prefix_p(test));
325 if (vtmp.size() != comp.size()) {
326 test.erase(test.length() - 1);
329 test += tmp[test.length()];
337 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
340 static DispatchResult empty_dr;
344 history_.push_back(trim(str));
345 history_pos_ = history_.end();
346 upPB->setEnabled(history_pos_ != history_.begin());
347 downPB->setEnabled(history_pos_ != history_.end());
348 FuncRequest func = lyxaction.lookupFunc(str);
349 func.setOrigin(FuncRequest::COMMANDBUFFER);
350 return lyx::dispatch(func);
353 } // namespace frontend
356 #include "moc_GuiCommandBuffer.cpp"