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);
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, ControlCommandBuffer & control)
67 : QToolBar(view), view_(view), controller_(control)
69 setHorizontalStretchable(true);
71 QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
73 (new QToolButton(qp, _("Up"), "", this, SLOT(up()), this))->show();
74 (new QToolButton(qp, _("Down"), "", this, SLOT(down()), this))->show();
76 edit_ = new QCommandEdit(this);
77 edit_->setMinimumSize(edit_->sizeHint());
79 setStretchableWidget(edit_);
83 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
84 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
85 connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
86 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
87 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
92 void QCommandBuffer::focus_command()
98 void QCommandBuffer::cancel()
100 view_->centralWidget()->setFocus();
105 void QCommandBuffer::dispatch()
107 controller_.dispatch(edit_->text().latin1());
108 view_->centralWidget()->setFocus();
113 void QCommandBuffer::complete()
115 string const input = edit_->text().latin1();
117 vector<string> comp = controller_.completions(input, new_input);
119 if (comp.empty() && new_input == input) {
120 // show_info_suffix(_("[no match]"), input);
125 edit_->setText(new_input.c_str());
126 // show_info_suffix(("[only completion]"), new_input + " ");
130 edit_->setText(new_input.c_str());
132 QTempListBox * list = new QTempListBox;
134 // For some reason the scrollview's contents are larger
135 // than the number of actual items...
136 vector<string>::const_iterator cit = comp.begin();
137 vector<string>::const_iterator end = comp.end();
138 for (; cit != end; ++cit) {
139 list->insertItem(cit->c_str());
142 // width() is not big enough by a few pixels. Qt Sucks.
143 list->setMinimumWidth(list->sizeHint().width() + 10);
145 list->resize(list->sizeHint());
146 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
148 int y = std::max(0, pos.y() - list->height());
150 list->move(pos.x(), y);
152 connect(list, SIGNAL(selected(const QString &)),
153 this, SLOT(complete_selected(const QString &)));
160 void QCommandBuffer::complete_selected(QString const & str)
162 edit_->setText(str + " ");
163 QWidget const * widget = static_cast<QWidget const *>(sender());
164 const_cast<QWidget *>(widget)->hide();
168 void QCommandBuffer::up()
170 string const input(edit_->text().latin1());
171 string const h(controller_.historyUp());
174 // show_info_suffix(_("[Beginning of history]"), input);
176 edit_->setText(h.c_str());
181 void QCommandBuffer::down()
183 string const input(edit_->text().latin1());
184 string const h(controller_.historyDown());
187 // show_info_suffix(_("[End of history]"), input);
189 edit_->setText(h.c_str());
195 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
197 stored_input_ = input;
198 info_suffix_shown_ = true;
199 set_input(input + " " + suffix);
200 suffix_timer_->start();
204 void XMiniBuffer::suffix_timeout()
206 info_suffix_shown_ = false;
207 set_input(stored_input_);