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 #pragma implementation
17 #include "support/filetools.h"
18 #include "controllers/ControlCommandBuffer.h"
23 #include "QCommandBuffer.h"
24 #include "QCommandEdit.h"
26 #include <qcombobox.h>
28 #include <qtoolbutton.h>
37 class QTempListBox : public QListBox {
41 WType_Modal | WType_Popup | WDestructiveClose) {
42 setHScrollBarMode(AlwaysOff);
46 void mouseReleaseEvent(QMouseEvent * e) {
47 if (e->x() < 0 || e->y() < 0
48 || e->x() > width() || e->y() > height()) {
51 emit selected(currentText());
55 void keyPressEvent(QKeyEvent * e) {
56 if (e->key() == Key_Escape) {
60 QListBox::keyPressEvent(e);
66 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
67 : QToolBar(view), view_(view), controller_(control)
69 setHorizontalStretchable(true);
71 QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
73 QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
76 QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
79 edit_ = new QCommandEdit(this);
80 edit_->setMinimumSize(edit_->sizeHint());
82 setStretchableWidget(edit_);
86 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
87 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
88 connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
89 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
90 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
95 void QCommandBuffer::focus_command()
101 void QCommandBuffer::cancel()
103 view_->centralWidget()->setFocus();
108 void QCommandBuffer::dispatch()
110 controller_.dispatch(edit_->text().latin1());
111 view_->centralWidget()->setFocus();
116 void QCommandBuffer::complete()
118 string const input = edit_->text().latin1();
120 vector<string> comp = controller_.completions(input, new_input);
122 if (comp.empty() && new_input == input) {
123 // show_info_suffix(_("[no match]"), input);
128 edit_->setText(new_input.c_str());
129 // show_info_suffix(("[only completion]"), new_input + " ");
133 edit_->setText(new_input.c_str());
135 QTempListBox * list = new QTempListBox();
137 // For some reason the scrollview's contents are larger
138 // than the number of actual items...
139 vector<string>::const_iterator cit = comp.begin();
140 vector<string>::const_iterator end = comp.end();
141 for (; cit != end; ++cit) {
142 list->insertItem(cit->c_str());
145 // width() is not big enough by a few pixels. Qt Sucks.
146 list->setMinimumWidth(list->sizeHint().width() + 10);
148 list->resize(list->sizeHint());
149 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
150 int y = pos.y() - list->height();
153 list->move(pos.x(), y);
155 connect(list, SIGNAL(selected(const QString &)),
156 this, SLOT(complete_selected(const QString &)));
163 void QCommandBuffer::complete_selected(const QString & str)
165 edit_->setText(str + " ");
166 QWidget const * widget = static_cast<QWidget const *>(sender());
167 const_cast<QWidget *>(widget)->hide();
171 void QCommandBuffer::up()
173 string const input = edit_->text().latin1();
174 string const h(controller_.historyUp());
177 // show_info_suffix(_("[Beginning of history]"), input);
179 edit_->setText(h.c_str());
184 void QCommandBuffer::down()
186 string const input = edit_->text().latin1();
187 string const h(controller_.historyDown());
190 // show_info_suffix(_("[End of history]"), input);
192 edit_->setText(h.c_str());
198 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
200 stored_input_ = input;
201 info_suffix_shown_ = true;
202 set_input(input + " " + suffix);
203 suffix_timer_->start();
207 void XMiniBuffer::suffix_timeout()
209 info_suffix_shown_ = false;
210 set_input(stored_input_);