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
14 #include "support/filetools.h"
15 #include "controllers/ControlCommandBuffer.h"
16 #include "qt_helpers.h"
20 #include "QCommandBuffer.h"
21 #include "QCommandEdit.h"
23 #include <qcombobox.h>
28 #include <qpushbutton.h>
37 class QTempListBox : public QListBox {
41 WType_Modal | WType_Popup | WDestructiveClose) {
42 setHScrollBarMode(AlwaysOff);
45 void mouseReleaseEvent(QMouseEvent * e) {
46 if (e->x() < 0 || e->y() < 0
47 || e->x() > width() || e->y() > height()) {
50 emit selected(currentText());
54 void keyPressEvent(QKeyEvent * e) {
55 if (e->key() == Key_Escape) {
59 QListBox::keyPressEvent(e);
66 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
67 : QWidget(parent), view_(view), controller_(control)
69 QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
70 QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
72 QVBoxLayout * top = new QVBoxLayout(this);
73 QHBoxLayout * layout = new QHBoxLayout(0);
75 QPushButton * up = new QPushButton(qpup, "", this);
76 QToolTip::add(up, qt_("Previous command"));
77 connect(up, SIGNAL(clicked()), this, SLOT(up()));
78 QPushButton * down = new QPushButton(qpdown, "", this);
79 QToolTip::add(down, qt_("Next command"));
80 connect(down, SIGNAL(clicked()), this, SLOT(down()));
82 edit_ = new QCommandEdit(this);
83 edit_->setMinimumSize(edit_->sizeHint());
84 edit_->setFocusPolicy(ClickFocus);
86 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
87 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
88 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
89 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
90 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
92 layout->addWidget(up, 0);
93 layout->addWidget(down, 0);
94 layout->addWidget(edit_, 10);
95 top->addLayout(layout);
100 void QCommandBuffer::focus_command()
106 void QCommandBuffer::cancel()
108 view_->centralWidget()->setFocus();
113 void QCommandBuffer::dispatch()
115 controller_.dispatch(fromqstr(edit_->text()));
116 view_->centralWidget()->setFocus();
122 void QCommandBuffer::complete()
124 string const input = fromqstr(edit_->text());
126 vector<string> comp = controller_.completions(input, new_input);
128 if (comp.empty() && new_input == input) {
129 // show_info_suffix(qt_("[no match]"), input);
134 edit_->setText(toqstr(new_input));
135 // show_info_suffix(("[only completion]"), new_input + ' ');
139 edit_->setText(toqstr(new_input));
141 QTempListBox * list = new QTempListBox;
143 // For some reason the scrollview's contents are larger
144 // than the number of actual items...
145 vector<string>::const_iterator cit = comp.begin();
146 vector<string>::const_iterator end = comp.end();
147 for (; cit != end; ++cit) {
148 list->insertItem(toqstr(*cit));
151 // width() is not big enough by a few pixels. Qt Sucks.
152 list->setMinimumWidth(list->sizeHint().width() + 10);
154 list->resize(list->sizeHint());
155 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
157 int y = std::max(0, pos.y() - list->height());
159 list->move(pos.x(), y);
161 connect(list, SIGNAL(selected(const QString &)),
162 this, SLOT(complete_selected(const QString &)));
169 void QCommandBuffer::complete_selected(QString const & str)
171 QWidget const * widget = static_cast<QWidget const *>(sender());
172 const_cast<QWidget *>(widget)->hide();
173 edit_->setText(str + ' ');
178 void QCommandBuffer::up()
180 string const input(fromqstr(edit_->text()));
181 string const h(controller_.historyUp());
184 // show_info_suffix(qt_("[Beginning of history]"), input);
186 edit_->setText(toqstr(h));
191 void QCommandBuffer::down()
193 string const input(fromqstr(edit_->text()));
194 string const h(controller_.historyDown());
197 // show_info_suffix(qt_("[End of history]"), input);
199 edit_->setText(toqstr(h));
205 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
207 stored_input_ = input;
208 info_suffix_shown_ = true;
209 set_input(input + ' ' + suffix);
210 suffix_timer_->start();
214 void XMiniBuffer::suffix_timeout()
216 info_suffix_shown_ = false;
217 set_input(stored_input_);