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>
32 using namespace lyx::support;
39 class QTempListBox : public QListBox {
43 WType_Modal | WType_Popup | WDestructiveClose) {
44 setHScrollBarMode(AlwaysOff);
47 void mouseReleaseEvent(QMouseEvent * e) {
48 if (e->x() < 0 || e->y() < 0
49 || e->x() > width() || e->y() > height()) {
52 emit selected(currentText());
56 void keyPressEvent(QKeyEvent * e) {
57 if (e->key() == Key_Escape) {
61 QListBox::keyPressEvent(e);
68 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
69 : QWidget(parent), view_(view), controller_(control)
71 QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
72 QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
74 QVBoxLayout * top = new QVBoxLayout(this);
75 QHBoxLayout * layout = new QHBoxLayout(0);
77 QPushButton * up = new QPushButton(qpup, "", this);
78 QToolTip::add(up, qt_("Previous command"));
79 connect(up, SIGNAL(clicked()), this, SLOT(up()));
80 QPushButton * down = new QPushButton(qpdown, "", this);
81 QToolTip::add(down, qt_("Next command"));
82 connect(down, SIGNAL(clicked()), this, SLOT(down()));
84 edit_ = new QCommandEdit(this);
85 edit_->setMinimumSize(edit_->sizeHint());
86 edit_->setFocusPolicy(ClickFocus);
88 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
89 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
90 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
91 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
92 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
94 layout->addWidget(up, 0);
95 layout->addWidget(down, 0);
96 layout->addWidget(edit_, 10);
97 top->addLayout(layout);
102 void QCommandBuffer::focus_command()
108 void QCommandBuffer::cancel()
110 view_->centralWidget()->setFocus();
115 void QCommandBuffer::dispatch()
117 controller_.dispatch(fromqstr(edit_->text()));
118 view_->centralWidget()->setFocus();
124 void QCommandBuffer::complete()
126 string const input = fromqstr(edit_->text());
128 vector<string> comp = controller_.completions(input, new_input);
130 if (comp.empty() && new_input == input) {
131 // show_info_suffix(qt_("[no match]"), input);
136 edit_->setText(toqstr(new_input));
137 // show_info_suffix(("[only completion]"), new_input + ' ');
141 edit_->setText(toqstr(new_input));
143 QTempListBox * list = new QTempListBox;
145 // For some reason the scrollview's contents are larger
146 // than the number of actual items...
147 vector<string>::const_iterator cit = comp.begin();
148 vector<string>::const_iterator end = comp.end();
149 for (; cit != end; ++cit) {
150 list->insertItem(toqstr(*cit));
153 // width() is not big enough by a few pixels. Qt Sucks.
154 list->setMinimumWidth(list->sizeHint().width() + 10);
156 list->resize(list->sizeHint());
157 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
159 int y = std::max(0, pos.y() - list->height());
161 list->move(pos.x(), y);
163 connect(list, SIGNAL(selected(const QString &)),
164 this, SLOT(complete_selected(const QString &)));
171 void QCommandBuffer::complete_selected(QString const & str)
173 QWidget const * widget = static_cast<QWidget const *>(sender());
174 const_cast<QWidget *>(widget)->hide();
175 edit_->setText(str + ' ');
180 void QCommandBuffer::up()
182 string const input(fromqstr(edit_->text()));
183 string const h(controller_.historyUp());
186 // show_info_suffix(qt_("[Beginning of history]"), input);
188 edit_->setText(toqstr(h));
193 void QCommandBuffer::down()
195 string const input(fromqstr(edit_->text()));
196 string const h(controller_.historyDown());
199 // show_info_suffix(qt_("[End of history]"), input);
201 edit_->setText(toqstr(h));
207 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
209 stored_input_ = input;
210 info_suffix_shown_ = true;
211 set_input(input + ' ' + suffix);
212 suffix_timer_->start();
216 void XMiniBuffer::suffix_timeout()
218 info_suffix_shown_ = false;
219 set_input(stored_input_);