lyx_mirror/src/frontends/qt2/QCommandBuffer.C
John Levon fabd5d2057 The big Qt toqstr/fromqstr patch.
This kind of wrapping will be needed anyway, and it's an
improvement on what we had. Please give it a run.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5846 a592a061-630c-0410-9148-cb99ea01b6c8
2002-12-17 20:37:13 +00:00

212 lines
4.4 KiB
C

/**
* \file QCommandBuffer.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author John Levon
*
* Full author contact details are available in file CREDITS
*/
#include <config.h>
#ifdef __GNUG__
#pragma implementation
#endif
#include "support/filetools.h"
#include "controllers/ControlCommandBuffer.h"
#include "qt_helpers.h"
#include "debug.h"
#include "QtView.h"
#include "QCommandBuffer.h"
#include "QCommandEdit.h"
#include <qcombobox.h>
#include <qlistbox.h>
#include <qtoolbutton.h>
#include <qpixmap.h>
#include "LString.h"
using std::vector;
namespace {
class QTempListBox : public QListBox {
public:
QTempListBox()
: QListBox(0, 0,
WType_Modal | WType_Popup | WDestructiveClose) {
setHScrollBarMode(AlwaysOff);
}
protected:
void mouseReleaseEvent(QMouseEvent * e) {
if (e->x() < 0 || e->y() < 0
|| e->x() > width() || e->y() > height()) {
hide();
} else {
emit selected(currentText());
}
}
void keyPressEvent(QKeyEvent * e) {
if (e->key() == Key_Escape) {
hide();
return;
}
QListBox::keyPressEvent(e);
}
};
} // end of anon
QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
: QToolBar(view), view_(view), controller_(control)
{
setHorizontalStretchable(true);
QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
(new QToolButton(qpup, qt_("Previous command"), "", this, SLOT(up()), this))->show();
(new QToolButton(qpdown, qt_("Next command"), "", this, SLOT(down()), this))->show();
edit_ = new QCommandEdit(this);
edit_->setMinimumSize(edit_->sizeHint());
edit_->show();
setStretchableWidget(edit_);
show();
connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
}
void QCommandBuffer::focus_command()
{
edit_->setFocus();
}
void QCommandBuffer::cancel()
{
view_->centralWidget()->setFocus();
edit_->setText("");
}
void QCommandBuffer::dispatch()
{
controller_.dispatch(fromqstr(edit_->text()));
view_->centralWidget()->setFocus();
edit_->setText("");
}
void QCommandBuffer::complete()
{
string const input = fromqstr(edit_->text());
string new_input;
vector<string> comp = controller_.completions(input, new_input);
if (comp.empty() && new_input == input) {
// show_info_suffix(qt_("[no match]"), input);
return;
}
if (comp.empty()) {
edit_->setText(toqstr(new_input));
// show_info_suffix(("[only completion]"), new_input + ' ');
return;
}
edit_->setText(toqstr(new_input));
QTempListBox * list = new QTempListBox;
// For some reason the scrollview's contents are larger
// than the number of actual items...
vector<string>::const_iterator cit = comp.begin();
vector<string>::const_iterator end = comp.end();
for (; cit != end; ++cit) {
list->insertItem(toqstr(*cit));
}
// width() is not big enough by a few pixels. Qt Sucks.
list->setMinimumWidth(list->sizeHint().width() + 10);
list->resize(list->sizeHint());
QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
int y = std::max(0, pos.y() - list->height());
list->move(pos.x(), y);
connect(list, SIGNAL(selected(const QString &)),
this, SLOT(complete_selected(const QString &)));
list->show();
list->setFocus();
}
void QCommandBuffer::complete_selected(QString const & str)
{
edit_->setText(str + ' ');
QWidget const * widget = static_cast<QWidget const *>(sender());
const_cast<QWidget *>(widget)->hide();
}
void QCommandBuffer::up()
{
string const input(fromqstr(edit_->text()));
string const h(controller_.historyUp());
if (h.empty()) {
// show_info_suffix(qt_("[Beginning of history]"), input);
} else {
edit_->setText(toqstr(h));
}
}
void QCommandBuffer::down()
{
string const input(fromqstr(edit_->text()));
string const h(controller_.historyDown());
if (h.empty()) {
// show_info_suffix(qt_("[End of history]"), input);
} else {
edit_->setText(toqstr(h));
}
}
#if 0
void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
{
stored_input_ = input;
info_suffix_shown_ = true;
set_input(input + ' ' + suffix);
suffix_timer_->start();
}
void XMiniBuffer::suffix_timeout()
{
info_suffix_shown_ = false;
set_input(stored_input_);
}
#endif