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