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>
25 #include <qtoolbutton.h>
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, ControlCommandBuffer & control)
64 : QToolBar(view), view_(view), controller_(control)
66 setHorizontalStretchable(true);
68 QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
69 QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
71 (new QToolButton(qpup, qt_("Previous command"), "", this, SLOT(up()), this))->show();
72 (new QToolButton(qpdown, qt_("Next command"), "", this, SLOT(down()), this))->show();
74 edit_ = new QCommandEdit(this);
75 edit_->setMinimumSize(edit_->sizeHint());
77 setStretchableWidget(edit_);
81 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
82 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
83 connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
84 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
85 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
90 void QCommandBuffer::focus_command()
96 void QCommandBuffer::cancel()
98 view_->centralWidget()->setFocus();
103 void QCommandBuffer::dispatch()
105 controller_.dispatch(fromqstr(edit_->text()));
106 view_->centralWidget()->setFocus();
111 void QCommandBuffer::complete()
113 string const input = fromqstr(edit_->text());
115 vector<string> comp = controller_.completions(input, new_input);
117 if (comp.empty() && new_input == input) {
118 // show_info_suffix(qt_("[no match]"), input);
123 edit_->setText(toqstr(new_input));
124 // show_info_suffix(("[only completion]"), new_input + ' ');
128 edit_->setText(toqstr(new_input));
130 QTempListBox * list = new QTempListBox;
132 // For some reason the scrollview's contents are larger
133 // than the number of actual items...
134 vector<string>::const_iterator cit = comp.begin();
135 vector<string>::const_iterator end = comp.end();
136 for (; cit != end; ++cit) {
137 list->insertItem(toqstr(*cit));
140 // width() is not big enough by a few pixels. Qt Sucks.
141 list->setMinimumWidth(list->sizeHint().width() + 10);
143 list->resize(list->sizeHint());
144 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
146 int y = std::max(0, pos.y() - list->height());
148 list->move(pos.x(), y);
150 connect(list, SIGNAL(selected(const QString &)),
151 this, SLOT(complete_selected(const QString &)));
158 void QCommandBuffer::complete_selected(QString const & str)
160 edit_->setText(str + ' ');
161 QWidget const * widget = static_cast<QWidget const *>(sender());
162 const_cast<QWidget *>(widget)->hide();
166 void QCommandBuffer::up()
168 string const input(fromqstr(edit_->text()));
169 string const h(controller_.historyUp());
172 // show_info_suffix(qt_("[Beginning of history]"), input);
174 edit_->setText(toqstr(h));
179 void QCommandBuffer::down()
181 string const input(fromqstr(edit_->text()));
182 string const h(controller_.historyDown());
185 // show_info_suffix(qt_("[End of history]"), input);
187 edit_->setText(toqstr(h));
193 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
195 stored_input_ = input;
196 info_suffix_shown_ = true;
197 set_input(input + ' ' + suffix);
198 suffix_timer_->start();
202 void XMiniBuffer::suffix_timeout()
204 info_suffix_shown_ = false;
205 set_input(stored_input_);