]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCommandBuffer.cpp
Don't allow newline characters in the command buffer.
[lyx.git] / src / frontends / qt4 / GuiCommandBuffer.cpp
1 /**
2  * \file GuiCommandBuffer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars
7  * \author Asger and Jürgen
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiCommandBuffer.h"
16
17 #include "GuiApplication.h"
18 #include "GuiCommandEdit.h"
19 #include "GuiView.h"
20 #include "qt_helpers.h"
21 #include "Validator.h"
22
23 #include "BufferView.h"
24 #include "Cursor.h"
25 #include "LyX.h"
26 #include "LyXAction.h"
27 #include "FuncRequest.h"
28 #include "Session.h"
29
30 #include "support/lyxalgo.h"
31 #include "support/lstrings.h"
32
33 #include <QHBoxLayout>
34 #include <QKeyEvent>
35 #include <QListWidget>
36 #include <QMouseEvent>
37 #include <QPixmap>
38 #include <QPushButton>
39 #include <QToolTip>
40 #include <QVBoxLayout>
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46 namespace frontend {
47
48 namespace {
49
50 class QTempListBox : public QListWidget {
51 public:
52         QTempListBox() {
53                 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
54                 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55                 setWindowModality(Qt::WindowModal);
56                 setWindowFlags(Qt::Popup);
57                 setAttribute(Qt::WA_DeleteOnClose);
58         }
59 protected:
60         bool event(QEvent * ev) {
61                 if (ev->type() == QEvent::MouseButtonPress) {
62                         QMouseEvent * me = static_cast<QMouseEvent *>(ev);
63                         if (me->x() < 0 || me->y() < 0
64                             || me->x() > width() || me->y() > height())
65                                 hide();
66                         return true;
67                 }
68                 return QListWidget::event(ev);
69         }
70
71         void keyPressEvent(QKeyEvent * ev) {
72                 if (ev->key() == Qt::Key_Escape) {
73                         hide();
74                         return;
75                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
76                         // emit signal
77                         itemClicked(currentItem());
78                 } else
79                         QListWidget::keyPressEvent(ev);
80         }
81 };
82
83 } // end of anon
84
85
86 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
87         : view_(view)
88 {
89         transform(lyxaction.func_begin(), lyxaction.func_end(),
90                 back_inserter(commands_), firster());
91
92         QPixmap qpup = getPixmap("images/", "up", "png");
93         QPixmap qpdown = getPixmap("images/", "down", "png");
94
95         QVBoxLayout * top = new QVBoxLayout(this);
96         QHBoxLayout * layout = new QHBoxLayout(0);
97
98         upPB = new QPushButton(qpup, "", this);
99         upPB->setToolTip(qt_("List of previous commands"));
100         upPB->setMaximumSize(24, 24);
101         downPB = new QPushButton(qpdown, "", this);
102         downPB->setToolTip(qt_("Next command"));
103         downPB->setMaximumSize(24, 24);
104         downPB->setEnabled(false);
105         connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
106         connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
107
108         edit_ = new GuiCommandEdit(this);
109         edit_->setMinimumSize(edit_->sizeHint());
110         edit_->setFocusPolicy(Qt::ClickFocus);
111         edit_->setValidator(new NoNewLineValidator(edit_));
112
113         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
114         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
115         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
116         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
117         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
118         connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
119
120         layout->addWidget(upPB, 0);
121         layout->addWidget(downPB, 0);
122         layout->addWidget(edit_, 10);
123         layout->setMargin(0);
124         top->addLayout(layout);
125         top->setMargin(0);
126         setFocusProxy(edit_);
127
128         LastCommandsSection::LastCommands last_commands 
129                 = theSession().lastCommands().getcommands();
130         LastCommandsSection::LastCommands::const_iterator it 
131                 = last_commands.begin();
132         LastCommandsSection::LastCommands::const_iterator end 
133                 = last_commands.end();
134
135         upPB->setEnabled(it != end);
136
137         for(; it != end; ++it)
138                 history_.push_back(*it);
139         history_pos_ = history_.end();
140 }
141
142
143 void GuiCommandBuffer::cancel()
144 {
145         view_->setFocus();
146         edit_->setText(QString());
147 }
148
149
150 void GuiCommandBuffer::dispatch()
151 {
152         QString cmd = edit_->text();
153         view_->setFocus();
154         edit_->setText(QString());
155         edit_->clearFocus();
156         std::string const cmd_ = fromqstr(cmd);
157         theSession().lastCommands().add(cmd_);
158         dispatch(cmd_);
159 }
160
161
162 void GuiCommandBuffer::listHistoryUp()
163 {
164         if (history_.size()==1) {
165                 edit_->setText(toqstr(history_.back()));
166                 upPB->setEnabled(false);
167                 return;
168         }
169         QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
170         showList(history_, pos, true);
171 }
172
173
174 void GuiCommandBuffer::complete()
175 {
176         string const input = fromqstr(edit_->text());
177         string new_input;
178         vector<string> const & comp = completions(input, new_input);
179
180         if (comp.empty()) {
181                 if (new_input != input)
182                         edit_->setText(toqstr(new_input));
183                 return;
184         }
185
186         edit_->setText(toqstr(new_input));
187         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
188         showList(comp, pos);
189 }
190
191 void GuiCommandBuffer::showList(vector<string> const & list,
192         QPoint const & pos, bool reversed) const
193 {
194         QTempListBox * listBox = new QTempListBox;
195
196         // For some reason the scrollview's contents are larger
197         // than the number of actual items...
198         vector<string>::const_iterator cit = list.begin();
199         vector<string>::const_iterator end = list.end();
200         for (; cit != end; ++cit) {
201                 if (reversed)
202                         listBox->insertItem(0, toqstr(*cit));
203                 else
204                         listBox->addItem(toqstr(*cit));
205         }
206
207         listBox->resize(listBox->sizeHint());
208
209         int const y = max(0, pos.y() - listBox->height());
210         listBox->move(pos.x(), y);
211
212         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
213                 this, SLOT(itemSelected(QListWidgetItem *)));
214         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
215                 this, SLOT(itemSelected(QListWidgetItem *)));
216
217         listBox->show();
218         listBox->setFocus();
219 }
220
221
222 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
223 {
224         QWidget const * widget = static_cast<QWidget const *>(sender());
225         const_cast<QWidget *>(widget)->hide();
226         edit_->setText(item->text()+ ' ');
227         edit_->activateWindow();
228         edit_->setFocus();
229 }
230
231
232 void GuiCommandBuffer::up()
233 {
234         string const input = fromqstr(edit_->text());
235         string const h = historyUp();
236
237         if (!h.empty())
238                 edit_->setText(toqstr(h));
239
240         upPB->setEnabled(history_pos_ != history_.begin());
241         downPB->setEnabled(history_pos_ != history_.end());
242 }
243
244
245 void GuiCommandBuffer::down()
246 {
247         string const input = fromqstr(edit_->text());
248         string const h = historyDown();
249
250         if (!h.empty())
251                 edit_->setText(toqstr(h));
252
253         downPB->setEnabled(!history_.empty()
254                            && history_pos_ != history_.end() - 1);
255         upPB->setEnabled(history_pos_ != history_.begin());
256 }
257         
258
259 void GuiCommandBuffer::hideParent()
260 {
261         view_->setFocus();
262         edit_->setText(QString());
263         edit_->clearFocus();
264         hide();
265 }
266
267
268 namespace {
269
270 class prefix_p {
271 public:
272         string p;
273         prefix_p(string const & s) : p(s) {}
274         bool operator()(string const & s) const { return prefixIs(s, p); }
275 };
276
277 } // end of anon namespace
278
279
280 string const GuiCommandBuffer::historyUp()
281 {
282         if (history_pos_ == history_.begin())
283                 return string();
284
285         return *(--history_pos_);
286 }
287
288
289 string const GuiCommandBuffer::historyDown()
290 {
291         if (history_pos_ == history_.end())
292                 return string();
293         if (history_pos_ + 1 == history_.end())
294                 return string();
295
296         return *(++history_pos_);
297 }
298
299
300 docstring const GuiCommandBuffer::getCurrentState() const
301 {
302         return view_->currentBufferView()->cursor().currentState();
303 }
304
305
306 void GuiCommandBuffer::hide() const
307 {
308         FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
309         lyx::dispatch(cmd);
310 }
311
312
313 vector<string> const
314 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
315 {
316         vector<string> comp;
317
318         lyx::copy_if(commands_.begin(), commands_.end(),
319                 back_inserter(comp), prefix_p(prefix));
320
321         if (comp.empty()) {
322                 new_prefix = prefix;
323                 return comp;
324         }
325
326         if (comp.size() == 1) {
327                 new_prefix = comp[0];
328                 return vector<string>();
329         }
330
331         // find maximal available prefix
332         string const tmp = comp[0];
333         string test = prefix;
334         if (tmp.length() > test.length())
335                 test += tmp[test.length()];
336         while (test.length() < tmp.length()) {
337                 vector<string> vtmp;
338                 lyx::copy_if(comp.begin(), comp.end(),
339                         back_inserter(vtmp), prefix_p(test));
340                 if (vtmp.size() != comp.size()) {
341                         test.erase(test.length() - 1);
342                         break;
343                 }
344                 test += tmp[test.length()];
345         }
346
347         new_prefix = test;
348         return comp;
349 }
350
351
352 void GuiCommandBuffer::dispatch(string const & str)
353 {
354         if (str.empty())
355                 return;
356
357         history_.push_back(trim(str));
358         history_pos_ = history_.end();
359         upPB->setEnabled(history_pos_ != history_.begin());
360         downPB->setEnabled(history_pos_ != history_.end());
361         FuncRequest func = lyxaction.lookupFunc(str);
362         func.setOrigin(FuncRequest::COMMANDBUFFER);
363         lyx::dispatch(func);
364 }
365
366 } // namespace frontend
367 } // namespace lyx
368
369 #include "moc_GuiCommandBuffer.cpp"