]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
Lots and lots of little trivial bits.
[lyx.git] / src / frontends / qt2 / QCommandBuffer.C
1 /**
2  * \file QCommandBuffer.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "support/filetools.h"
18 #include "controllers/ControlCommandBuffer.h"
19 #include "gettext.h"
20 #include "debug.h"
21
22 #include "QtView.h"
23 #include "QCommandBuffer.h"
24 #include "QCommandEdit.h"
25
26 #include <qcombobox.h>
27 #include <qlistbox.h>
28 #include <qtoolbutton.h>
29 #include <qpixmap.h>
30
31 #include "LString.h"
32
33 using std::vector;
34
35 namespace {
36
37 class QTempListBox : public QListBox {
38 public:
39         QTempListBox()
40                 : QListBox(0, 0,
41                 WType_Modal | WType_Popup | WDestructiveClose) {
42                 setHScrollBarMode(AlwaysOff);
43         }
44
45 protected:
46         void mouseReleaseEvent(QMouseEvent * e) {
47                 if (e->x() < 0 || e->y() < 0
48                         || e->x() > width() || e->y() > height()) {
49                         hide();
50                 } else {
51                         emit selected(currentText());
52                 }
53         }
54
55         void keyPressEvent(QKeyEvent * e) {
56                 if (e->key() == Key_Escape) {
57                         hide();
58                         return;
59                 }
60                 QListBox::keyPressEvent(e);
61         }
62 };
63
64 }
65
66 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
67         : QToolBar(view), view_(view), controller_(control)
68 {
69         setHorizontalStretchable(true);
70
71         QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
72
73         QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
74         upb->show();
75
76         QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
77         downb->show();
78
79         edit_ = new QCommandEdit(this);
80         edit_->setMinimumSize(edit_->sizeHint());
81         edit_->show();
82         setStretchableWidget(edit_);
83
84         show();
85
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()));
91 }
92
93
94
95 void QCommandBuffer::focus_command()
96 {
97         edit_->setFocus();
98 }
99
100
101 void QCommandBuffer::cancel()
102 {
103         view_->centralWidget()->setFocus();
104         edit_->setText("");
105 }
106
107
108 void QCommandBuffer::dispatch()
109 {
110         controller_.dispatch(edit_->text().latin1());
111         view_->centralWidget()->setFocus();
112         edit_->setText("");
113 }
114
115
116 void QCommandBuffer::complete()
117 {
118         string const input = edit_->text().latin1();
119         string new_input;
120         vector<string> comp = controller_.completions(input, new_input);
121
122         if (comp.empty() && new_input == input) {
123         //      show_info_suffix(_("[no match]"), input);
124                 return;
125         }
126
127         if (comp.empty()) {
128                 edit_->setText(new_input.c_str());
129         //      show_info_suffix(("[only completion]"), new_input + " ");
130                 return;
131         }
132
133         edit_->setText(new_input.c_str());
134
135         QTempListBox * list = new QTempListBox();
136
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());
143         }
144
145         // width() is not big enough by a few pixels. Qt Sucks.
146         list->setMinimumWidth(list->sizeHint().width() + 10);
147
148         list->resize(list->sizeHint());
149         QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
150         int y = pos.y() - list->height();
151         if (y < 0)
152                 y = 0;
153         list->move(pos.x(), y);
154
155         connect(list, SIGNAL(selected(const QString &)),
156                 this, SLOT(complete_selected(const QString &)));
157
158         list->show();
159         list->setFocus();
160 }
161
162
163 void QCommandBuffer::complete_selected(const QString & str)
164 {
165         edit_->setText(str + " ");
166         QWidget const * widget = static_cast<QWidget const *>(sender());
167         const_cast<QWidget *>(widget)->hide();
168 }
169
170
171 void QCommandBuffer::up()
172 {
173         string const input = edit_->text().latin1();
174         string const h(controller_.historyUp());
175
176         if (h.empty()) {
177         //      show_info_suffix(_("[Beginning of history]"), input);
178         } else {
179                 edit_->setText(h.c_str());
180         }
181 }
182
183
184 void QCommandBuffer::down()
185 {
186         string const input = edit_->text().latin1();
187         string const h(controller_.historyDown());
188
189         if (h.empty()) {
190         //      show_info_suffix(_("[End of history]"), input);
191         } else {
192                 edit_->setText(h.c_str());
193         }
194 }
195
196
197 #if 0
198 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
199 {
200         stored_input_ = input;
201         info_suffix_shown_ = true;
202         set_input(input + " " + suffix);
203         suffix_timer_->start();
204 }
205
206
207 void XMiniBuffer::suffix_timeout()
208 {
209         info_suffix_shown_ = false;
210         set_input(stored_input_);
211 }
212
213 #endif