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 #include "support/filetools.h"
14 #include "controllers/ControlCommandBuffer.h"
15 #include "qt_helpers.h"
18 #include "QCommandBuffer.h"
19 #include "QCommandEdit.h"
24 #include <qpushbutton.h>
26 using lyx::support::LibFileSearch;
34 class QTempListBox : public QListBox {
38 WType_Modal | WType_Popup | WDestructiveClose) {
39 setHScrollBarMode(AlwaysOff);
42 void mouseReleaseEvent(QMouseEvent * e) {
43 if (e->x() < 0 || e->y() < 0
44 || e->x() > width() || e->y() > height()) {
47 emit selected(currentText());
51 void keyPressEvent(QKeyEvent * e) {
52 if (e->key() == Key_Escape) {
56 QListBox::keyPressEvent(e);
63 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
64 : QWidget(parent), view_(view), controller_(control)
66 QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
67 QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
69 QVBoxLayout * top = new QVBoxLayout(this);
70 QHBoxLayout * layout = new QHBoxLayout(0);
72 QPushButton * up = new QPushButton(qpup, "", this);
73 QToolTip::add(up, qt_("Previous command"));
74 connect(up, SIGNAL(clicked()), this, SLOT(up()));
75 QPushButton * down = new QPushButton(qpdown, "", this);
76 QToolTip::add(down, qt_("Next command"));
77 connect(down, SIGNAL(clicked()), this, SLOT(down()));
79 edit_ = new QCommandEdit(this);
80 edit_->setMinimumSize(edit_->sizeHint());
81 edit_->setFocusPolicy(ClickFocus);
83 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
84 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
85 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
86 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
87 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
89 layout->addWidget(up, 0);
90 layout->addWidget(down, 0);
91 layout->addWidget(edit_, 10);
92 top->addLayout(layout);
97 void QCommandBuffer::focus_command()
103 void QCommandBuffer::cancel()
105 view_->centralWidget()->setFocus();
110 void QCommandBuffer::dispatch()
112 controller_.dispatch(fromqstr(edit_->text()));
113 view_->centralWidget()->setFocus();
119 void QCommandBuffer::complete()
121 string const input = fromqstr(edit_->text());
123 vector<string> comp = controller_.completions(input, new_input);
125 if (comp.empty() && new_input == input) {
126 // show_info_suffix(qt_("[no match]"), input);
131 edit_->setText(toqstr(new_input));
132 // show_info_suffix(("[only completion]"), new_input + ' ');
136 edit_->setText(toqstr(new_input));
138 QTempListBox * list = new QTempListBox;
140 // For some reason the scrollview's contents are larger
141 // than the number of actual items...
142 vector<string>::const_iterator cit = comp.begin();
143 vector<string>::const_iterator end = comp.end();
144 for (; cit != end; ++cit) {
145 list->insertItem(toqstr(*cit));
148 // width() is not big enough by a few pixels. Qt Sucks.
149 list->setMinimumWidth(list->sizeHint().width() + 10);
151 list->resize(list->sizeHint());
152 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
154 int y = std::max(0, pos.y() - list->height());
156 list->move(pos.x(), y);
158 connect(list, SIGNAL(selected(const QString &)),
159 this, SLOT(complete_selected(const QString &)));
166 void QCommandBuffer::complete_selected(QString const & str)
168 QWidget const * widget = static_cast<QWidget const *>(sender());
169 const_cast<QWidget *>(widget)->hide();
170 edit_->setText(str + ' ');
175 void QCommandBuffer::up()
177 string const input(fromqstr(edit_->text()));
178 string const h(controller_.historyUp());
181 // show_info_suffix(qt_("[Beginning of history]"), input);
183 edit_->setText(toqstr(h));
188 void QCommandBuffer::down()
190 string const input(fromqstr(edit_->text()));
191 string const h(controller_.historyDown());
194 // show_info_suffix(qt_("[End of history]"), input);
196 edit_->setText(toqstr(h));
202 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
204 stored_input_ = input;
205 info_suffix_shown_ = true;
206 set_input(input + ' ' + suffix);
207 suffix_timer_->start();
211 void XMiniBuffer::suffix_timeout()
213 info_suffix_shown_ = false;
214 set_input(stored_input_);