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_Popup | WDestructiveClose) {
34 setHScrollBarMode(AlwaysOff);
38 void keyPressEvent(QKeyEvent * e) {
39 if (e->key() == Key_Escape) {
43 QListBox::keyPressEvent(e);
49 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
50 : QToolBar(view), view_(view), controller_(control)
52 setHorizontalStretchable(true);
54 QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
56 QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
59 QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
62 edit_ = new QCommandEdit(this);
63 edit_->setMinimumSize(edit_->sizeHint());
65 setStretchableWidget(edit_);
69 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
70 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
71 connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
72 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
73 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
78 void QCommandBuffer::focus_command()
84 void QCommandBuffer::cancel()
86 view_->centralWidget()->setFocus();
91 void QCommandBuffer::dispatch()
93 controller_.dispatch(edit_->text().latin1());
94 view_->centralWidget()->setFocus();
99 void QCommandBuffer::complete()
101 string const input = edit_->text().latin1();
103 vector<string> comp = controller_.completions(input, new_input);
105 if (comp.empty() && new_input == input) {
106 // show_info_suffix(_("[no match]"), input);
111 edit_->setText(new_input.c_str());
112 // show_info_suffix(("[only completion]"), new_input + " ");
116 edit_->setText(new_input.c_str());
118 QTempListBox * list = new QTempListBox();
119 vector<string>::const_iterator cit = comp.begin();
120 vector<string>::const_iterator end = comp.end();
121 for (; cit != end; ++cit) {
122 list->insertItem(cit->c_str());
125 // For some reason we get lots of empty entries and the
126 // scrollbar is wrong as a result. No fix. Qt Sucks.
128 // width() is not big enough by a few pixels. Qt Sucks.
129 list->setMinimumWidth(list->sizeHint().width() + 10);
131 list->resize(list->sizeHint());
132 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
133 int y = pos.y() - list->height();
136 list->move(pos.x(), y);
138 connect(list, SIGNAL(selected(const QString &)),
139 this, SLOT(complete_selected(const QString &)));
141 // Note we *cannot* make a single click popup, because
142 // events get generated for outside the popup on Qt 2.3.1
143 // and even gives valid QListBoxItem *'s. We have no way
144 // to work past this. Qt Sucks.
145 //connect(list, SIGNAL(clicked(QListBoxItem *)),
146 // this, SLOT(complete_selected2(QListBoxItem *)));
153 void QCommandBuffer::complete_selected(const QString & str)
155 edit_->setText(str + " ");
156 QWidget const * widget = static_cast<QWidget const *>(sender());
157 const_cast<QWidget *>(widget)->hide();
161 void QCommandBuffer::up()
163 string const input = edit_->text().latin1();
164 string const h(controller_.historyUp());
167 // show_info_suffix(_("[Beginning of history]"), input);
169 edit_->setText(h.c_str());
174 void QCommandBuffer::down()
176 string const input = edit_->text().latin1();
177 string const h(controller_.historyDown());
180 // show_info_suffix(_("[End of history]"), input);
182 edit_->setText(h.c_str());
188 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
190 stored_input_ = input;
191 info_suffix_shown_ = true;
192 set_input(input + " " + suffix);
193 suffix_timer_->start();
197 void XMiniBuffer::suffix_timeout()
199 info_suffix_shown_ = false;
200 set_input(stored_input_);