]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
dont use pragma impementation and interface anymore
[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
14 #include "support/filetools.h"
15 #include "controllers/ControlCommandBuffer.h"
16 #include "qt_helpers.h"
17 #include "debug.h"
18
19 #include "QtView.h"
20 #include "QCommandBuffer.h"
21 #include "QCommandEdit.h"
22
23 #include <qcombobox.h>
24 #include <qlistbox.h>
25 #include <qtoolbutton.h>
26 #include <qpixmap.h>
27
28 #include "LString.h"
29
30 using std::vector;
31
32 namespace {
33
34 class QTempListBox : public QListBox {
35 public:
36         QTempListBox()
37                 : QListBox(0, 0,
38                            WType_Modal | WType_Popup | WDestructiveClose) {
39                 setHScrollBarMode(AlwaysOff);
40         }
41 protected:
42         void mouseReleaseEvent(QMouseEvent * e) {
43                 if (e->x() < 0 || e->y() < 0
44                     || e->x() > width() || e->y() > height()) {
45                         hide();
46                 } else {
47                         emit selected(currentText());
48                 }
49         }
50
51         void keyPressEvent(QKeyEvent * e) {
52                 if (e->key() == Key_Escape) {
53                         hide();
54                         return;
55                 }
56                 QListBox::keyPressEvent(e);
57         }
58 };
59
60 } // end of anon
61
62
63 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
64         : QToolBar(view), view_(view), controller_(control)
65 {
66         setHorizontalStretchable(true);
67
68         QPixmap qpup(toqstr(LibFileSearch("images", "up", "xpm")));
69         QPixmap qpdown(toqstr(LibFileSearch("images", "down", "xpm")));
70
71         (new QToolButton(qpup, qt_("Previous command"), "", this, SLOT(up()), this))->show();
72         (new QToolButton(qpdown, qt_("Next command"), "", this, SLOT(down()), this))->show();
73
74         edit_ = new QCommandEdit(this);
75         edit_->setMinimumSize(edit_->sizeHint());
76         edit_->show();
77         setStretchableWidget(edit_);
78
79         show();
80
81         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
82         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
83         connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
84         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
85         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
86 }
87
88
89
90 void QCommandBuffer::focus_command()
91 {
92         edit_->setFocus();
93 }
94
95
96 void QCommandBuffer::cancel()
97 {
98         view_->centralWidget()->setFocus();
99         edit_->setText("");
100 }
101
102
103 void QCommandBuffer::dispatch()
104 {
105         controller_.dispatch(fromqstr(edit_->text()));
106         view_->centralWidget()->setFocus();
107         edit_->setText("");
108 }
109
110
111 void QCommandBuffer::complete()
112 {
113         string const input = fromqstr(edit_->text());
114         string new_input;
115         vector<string> comp = controller_.completions(input, new_input);
116
117         if (comp.empty() && new_input == input) {
118         //      show_info_suffix(qt_("[no match]"), input);
119                 return;
120         }
121
122         if (comp.empty()) {
123                 edit_->setText(toqstr(new_input));
124         //      show_info_suffix(("[only completion]"), new_input + ' ');
125                 return;
126         }
127
128         edit_->setText(toqstr(new_input));
129
130         QTempListBox * list = new QTempListBox;
131
132         // For some reason the scrollview's contents are larger
133         // than the number of actual items...
134         vector<string>::const_iterator cit = comp.begin();
135         vector<string>::const_iterator end = comp.end();
136         for (; cit != end; ++cit) {
137                 list->insertItem(toqstr(*cit));
138         }
139
140         // width() is not big enough by a few pixels. Qt Sucks.
141         list->setMinimumWidth(list->sizeHint().width() + 10);
142
143         list->resize(list->sizeHint());
144         QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
145
146         int y = std::max(0, pos.y() - list->height());
147
148         list->move(pos.x(), y);
149
150         connect(list, SIGNAL(selected(const QString &)),
151                 this, SLOT(complete_selected(const QString &)));
152
153         list->show();
154         list->setFocus();
155 }
156
157
158 void QCommandBuffer::complete_selected(QString const & str)
159 {
160         edit_->setText(str + ' ');
161         QWidget const * widget = static_cast<QWidget const *>(sender());
162         const_cast<QWidget *>(widget)->hide();
163 }
164
165
166 void QCommandBuffer::up()
167 {
168         string const input(fromqstr(edit_->text()));
169         string const h(controller_.historyUp());
170
171         if (h.empty()) {
172         //      show_info_suffix(qt_("[Beginning of history]"), input);
173         } else {
174                 edit_->setText(toqstr(h));
175         }
176 }
177
178
179 void QCommandBuffer::down()
180 {
181         string const input(fromqstr(edit_->text()));
182         string const h(controller_.historyDown());
183
184         if (h.empty()) {
185         //      show_info_suffix(qt_("[End of history]"), input);
186         } else {
187                 edit_->setText(toqstr(h));
188         }
189 }
190
191
192 #if 0
193 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
194 {
195         stored_input_ = input;
196         info_suffix_shown_ = true;
197         set_input(input + ' ' + suffix);
198         suffix_timer_->start();
199 }
200
201
202 void XMiniBuffer::suffix_timeout()
203 {
204         info_suffix_shown_ = false;
205         set_input(stored_input_);
206 }
207
208 #endif