2 * \file QCommandBuffer.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
11 #include "support/filetools.h"
12 #include "controllers/ControlCommandBuffer.h"
17 #include "QCommandBuffer.h"
18 #include "QCommandEdit.h"
20 #include <qcombobox.h>
22 #include <qtoolbutton.h>
29 class QTempListBox : public QListBox {
33 WType_Modal | WType_Popup | WDestructiveClose) {
34 setHScrollBarMode(AlwaysOff);
38 void mouseReleaseEvent(QMouseEvent * e) {
39 if (e->x() < 0 || e->y() < 0
40 || e->x() > width() || e->y() > height()) {
43 emit selected(currentText());
47 void keyPressEvent(QKeyEvent * e) {
48 if (e->key() == Key_Escape) {
52 QListBox::keyPressEvent(e);
58 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
59 : QToolBar(view), view_(view), controller_(control)
61 setHorizontalStretchable(true);
63 QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
65 QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
68 QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
71 edit_ = new QCommandEdit(this);
72 edit_->setMinimumSize(edit_->sizeHint());
74 setStretchableWidget(edit_);
78 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
79 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
80 connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
81 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
82 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
87 void QCommandBuffer::focus_command()
93 void QCommandBuffer::cancel()
95 view_->centralWidget()->setFocus();
100 void QCommandBuffer::dispatch()
102 controller_.dispatch(edit_->text().latin1());
103 view_->centralWidget()->setFocus();
108 void QCommandBuffer::complete()
110 string const input = edit_->text().latin1();
112 vector<string> comp = controller_.completions(input, new_input);
114 if (comp.empty() && new_input == input) {
115 // show_info_suffix(_("[no match]"), input);
120 edit_->setText(new_input.c_str());
121 // show_info_suffix(("[only completion]"), new_input + " ");
125 edit_->setText(new_input.c_str());
127 QTempListBox * list = new QTempListBox();
129 // For some reason the scrollview's contents are larger
130 // than the number of actual items...
131 vector<string>::const_iterator cit = comp.begin();
132 vector<string>::const_iterator end = comp.end();
133 for (; cit != end; ++cit) {
134 list->insertItem(cit->c_str());
137 // width() is not big enough by a few pixels. Qt Sucks.
138 list->setMinimumWidth(list->sizeHint().width() + 10);
140 list->resize(list->sizeHint());
141 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
142 int y = pos.y() - list->height();
145 list->move(pos.x(), y);
147 connect(list, SIGNAL(selected(const QString &)),
148 this, SLOT(complete_selected(const QString &)));
155 void QCommandBuffer::complete_selected(const QString & str)
157 edit_->setText(str + " ");
158 QWidget const * widget = static_cast<QWidget const *>(sender());
159 const_cast<QWidget *>(widget)->hide();
163 void QCommandBuffer::up()
165 string const input = edit_->text().latin1();
166 string const h(controller_.historyUp());
169 // show_info_suffix(_("[Beginning of history]"), input);
171 edit_->setText(h.c_str());
176 void QCommandBuffer::down()
178 string const input = edit_->text().latin1();
179 string const h(controller_.historyDown());
182 // show_info_suffix(_("[End of history]"), input);
184 edit_->setText(h.c_str());
190 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
192 stored_input_ = input;
193 info_suffix_shown_ = true;
194 set_input(input + " " + suffix);
195 suffix_timer_->start();
199 void XMiniBuffer::suffix_timeout()
201 info_suffix_shown_ = false;
202 set_input(stored_input_);