]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
Some string(widget->text()) fixes. Weirdness
[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 protected:
45         void mouseReleaseEvent(QMouseEvent * e) {
46                 if (e->x() < 0 || e->y() < 0
47                     || e->x() > width() || e->y() > height()) {
48                         hide();
49                 } else {
50                         emit selected(currentText());
51                 }
52         }
53
54         void keyPressEvent(QKeyEvent * e) {
55                 if (e->key() == Key_Escape) {
56                         hide();
57                         return;
58                 }
59                 QListBox::keyPressEvent(e);
60         }
61 };
62
63 } // end of anon
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         (new QToolButton(qp, _("Up"), "", this, SLOT(up()), this))->show();
74         (new QToolButton(qp, _("Down"), "", this, SLOT(down()), this))->show();
75
76         edit_ = new QCommandEdit(this);
77         edit_->setMinimumSize(edit_->sizeHint());
78         edit_->show();
79         setStretchableWidget(edit_);
80
81         show();
82
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()));
88 }
89
90
91
92 void QCommandBuffer::focus_command()
93 {
94         edit_->setFocus();
95 }
96
97
98 void QCommandBuffer::cancel()
99 {
100         view_->centralWidget()->setFocus();
101         edit_->setText("");
102 }
103
104
105 void QCommandBuffer::dispatch()
106 {
107         controller_.dispatch(edit_->text().latin1());
108         view_->centralWidget()->setFocus();
109         edit_->setText("");
110 }
111
112
113 void QCommandBuffer::complete()
114 {
115         string const input = edit_->text().latin1();
116         string new_input;
117         vector<string> comp = controller_.completions(input, new_input);
118
119         if (comp.empty() && new_input == input) {
120         //      show_info_suffix(_("[no match]"), input);
121                 return;
122         }
123
124         if (comp.empty()) {
125                 edit_->setText(new_input.c_str());
126         //      show_info_suffix(("[only completion]"), new_input + ' ');
127                 return;
128         }
129
130         edit_->setText(new_input.c_str());
131
132         QTempListBox * list = new QTempListBox;
133
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());
140         }
141
142         // width() is not big enough by a few pixels. Qt Sucks.
143         list->setMinimumWidth(list->sizeHint().width() + 10);
144
145         list->resize(list->sizeHint());
146         QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
147
148         int y = std::max(0, pos.y() - list->height());
149
150         list->move(pos.x(), y);
151
152         connect(list, SIGNAL(selected(const QString &)),
153                 this, SLOT(complete_selected(const QString &)));
154
155         list->show();
156         list->setFocus();
157 }
158
159
160 void QCommandBuffer::complete_selected(QString const & str)
161 {
162         edit_->setText(str + ' ');
163         QWidget const * widget = static_cast<QWidget const *>(sender());
164         const_cast<QWidget *>(widget)->hide();
165 }
166
167
168 void QCommandBuffer::up()
169 {
170         string const input(edit_->text().latin1());
171         string const h(controller_.historyUp());
172
173         if (h.empty()) {
174         //      show_info_suffix(_("[Beginning of history]"), input);
175         } else {
176                 edit_->setText(h.c_str());
177         }
178 }
179
180
181 void QCommandBuffer::down()
182 {
183         string const input(edit_->text().latin1());
184         string const h(controller_.historyDown());
185
186         if (h.empty()) {
187         //      show_info_suffix(_("[End of history]"), input);
188         } else {
189                 edit_->setText(h.c_str());
190         }
191 }
192
193
194 #if 0
195 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
196 {
197         stored_input_ = input;
198         info_suffix_shown_ = true;
199         set_input(input + ' ' + suffix);
200         suffix_timer_->start();
201 }
202
203
204 void XMiniBuffer::suffix_timeout()
205 {
206         info_suffix_shown_ = false;
207         set_input(stored_input_);
208 }
209
210 #endif