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()) {
53 emit selected(currentText());
57 void keyPressEvent(QKeyEvent * e) {
58 if (e->key() == Key_Escape) {
62 QListBox::keyPressEvent(e);
69 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
70 : QWidget(parent), view_(view), controller_(control)
72 QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
73 QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
75 QVBoxLayout * top = new QVBoxLayout(this);
76 QHBoxLayout * layout = new QHBoxLayout(0);
78 QPushButton * up = new QPushButton(qpup, "", this);
79 QToolTip::add(up, qt_("Previous command"));
80 connect(up, SIGNAL(clicked()), this, SLOT(up()));
81 QPushButton * down = new QPushButton(qpdown, "", this);
82 QToolTip::add(down, qt_("Next command"));
83 connect(down, SIGNAL(clicked()), this, SLOT(down()));
85 edit_ = new QCommandEdit(this);
86 edit_->setMinimumSize(edit_->sizeHint());
87 edit_->setFocusPolicy(ClickFocus);
89 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
90 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
91 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
92 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
93 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
95 layout->addWidget(up, 0);
96 layout->addWidget(down, 0);
97 layout->addWidget(edit_, 10);
98 top->addLayout(layout);
103 void QCommandBuffer::focus_command()
109 void QCommandBuffer::cancel()
111 view_->centralWidget()->setFocus();
116 void QCommandBuffer::dispatch()
118 controller_.dispatch(fromqstr(edit_->text()));
119 view_->centralWidget()->setFocus();
125 void QCommandBuffer::complete()
127 string const input = fromqstr(edit_->text());
129 vector<string> comp = controller_.completions(input, new_input);
131 if (comp.empty() && new_input == input) {
132 // show_info_suffix(qt_("[no match]"), input);
137 edit_->setText(toqstr(new_input));
138 // show_info_suffix(("[only completion]"), new_input + ' ');
142 edit_->setText(toqstr(new_input));
144 QTempListBox * list = new QTempListBox;
146 // For some reason the scrollview's contents are larger
147 // than the number of actual items...
148 vector<string>::const_iterator cit = comp.begin();
149 vector<string>::const_iterator end = comp.end();
150 for (; cit != end; ++cit) {
151 list->insertItem(toqstr(*cit));
154 // width() is not big enough by a few pixels. Qt Sucks.
155 list->setMinimumWidth(list->sizeHint().width() + 10);
157 list->resize(list->sizeHint());
158 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
160 int y = std::max(0, pos.y() - list->height());
162 list->move(pos.x(), y);
164 connect(list, SIGNAL(selected(const QString &)),
165 this, SLOT(complete_selected(const QString &)));
172 void QCommandBuffer::complete_selected(QString const & str)
174 QWidget const * widget = static_cast<QWidget const *>(sender());
175 const_cast<QWidget *>(widget)->hide();
176 edit_->setText(str + ' ');
181 void QCommandBuffer::up()
183 string const input(fromqstr(edit_->text()));
184 string const h(controller_.historyUp());
187 // show_info_suffix(qt_("[Beginning of history]"), input);
189 edit_->setText(toqstr(h));
194 void QCommandBuffer::down()
196 string const input(fromqstr(edit_->text()));
197 string const h(controller_.historyDown());
200 // show_info_suffix(qt_("[End of history]"), input);
202 edit_->setText(toqstr(h));
208 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
210 stored_input_ = input;
211 info_suffix_shown_ = true;
212 set_input(input + ' ' + suffix);
213 suffix_timer_->start();
217 void XMiniBuffer::suffix_timeout()
219 info_suffix_shown_ = false;
220 set_input(stored_input_);
225 } // namespace frontend