2 * \file QCommandBuffer.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
17 #include "QCommandBuffer.h"
18 #include "QCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "controllers/ControlCommandBuffer.h"
23 #include "support/filetools.h"
28 #include <qpushbutton.h>
30 using lyx::support::libFileSearch;
40 class QTempListBox : public QListBox {
44 WType_Modal | WType_Popup | WDestructiveClose) {
45 setHScrollBarMode(AlwaysOff);
48 void mouseReleaseEvent(QMouseEvent * e) {
49 if (e->x() < 0 || e->y() < 0
50 || e->x() > width() || e->y() > height()) {
54 selected(currentText());
58 void keyPressEvent(QKeyEvent * e) {
59 if (e->key() == Key_Escape) {
63 QListBox::keyPressEvent(e);
70 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
71 : QWidget(parent), view_(view), controller_(control)
73 QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm")));
74 QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm")));
76 QVBoxLayout * top = new QVBoxLayout(this);
77 QHBoxLayout * layout = new QHBoxLayout(0);
79 QPushButton * up = new QPushButton(qpup, "", this);
80 QToolTip::add(up, qt_("Previous command"));
81 connect(up, SIGNAL(clicked()), this, SLOT(up()));
82 QPushButton * down = new QPushButton(qpdown, "", this);
83 QToolTip::add(down, qt_("Next command"));
84 connect(down, SIGNAL(clicked()), this, SLOT(down()));
86 edit_ = new QCommandEdit(this);
87 edit_->setMinimumSize(edit_->sizeHint());
88 edit_->setFocusPolicy(ClickFocus);
90 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
91 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
92 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
93 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
94 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
96 layout->addWidget(up, 0);
97 layout->addWidget(down, 0);
98 layout->addWidget(edit_, 10);
99 top->addLayout(layout);
104 void QCommandBuffer::focus_command()
110 void QCommandBuffer::cancel()
112 view_->centralWidget()->setFocus();
117 void QCommandBuffer::dispatch()
119 controller_.dispatch(fromqstr(edit_->text()));
120 view_->centralWidget()->setFocus();
126 void QCommandBuffer::complete()
128 string const input = fromqstr(edit_->text());
130 vector<string> comp = controller_.completions(input, new_input);
132 if (comp.empty() && new_input == input) {
133 // show_info_suffix(qt_("[no match]"), input);
138 edit_->setText(toqstr(new_input));
139 // show_info_suffix(("[only completion]"), new_input + ' ');
143 edit_->setText(toqstr(new_input));
145 QTempListBox * list = new QTempListBox;
147 // For some reason the scrollview's contents are larger
148 // than the number of actual items...
149 vector<string>::const_iterator cit = comp.begin();
150 vector<string>::const_iterator end = comp.end();
151 for (; cit != end; ++cit) {
152 list->insertItem(toqstr(*cit));
155 // width() is not big enough by a few pixels. Qt Sucks.
156 list->setMinimumWidth(list->sizeHint().width() + 10);
158 list->resize(list->sizeHint());
159 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
161 int y = std::max(0, pos.y() - list->height());
163 list->move(pos.x(), y);
165 connect(list, SIGNAL(selected(const QString &)),
166 this, SLOT(complete_selected(const QString &)));
173 void QCommandBuffer::complete_selected(QString const & str)
175 QWidget const * widget = static_cast<QWidget const *>(sender());
176 const_cast<QWidget *>(widget)->hide();
177 edit_->setText(str + ' ');
182 void QCommandBuffer::up()
184 string const input(fromqstr(edit_->text()));
185 string const h(controller_.historyUp());
188 // show_info_suffix(qt_("[Beginning of history]"), input);
190 edit_->setText(toqstr(h));
195 void QCommandBuffer::down()
197 string const input(fromqstr(edit_->text()));
198 string const h(controller_.historyDown());
201 // show_info_suffix(qt_("[End of history]"), input);
203 edit_->setText(toqstr(h));
209 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
211 stored_input_ = input;
212 info_suffix_shown_ = true;
213 set_input(input + ' ' + suffix);
214 suffix_timer_->start();
218 void XMiniBuffer::suffix_timeout()
220 info_suffix_shown_ = false;
221 set_input(stored_input_);
226 } // namespace frontend