]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
some math dialog non-improvements
[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 keyPressEvent(QKeyEvent * e) {
39                 if (e->key() == Key_Escape) {
40                         hide();
41                         return;
42                 }
43                 QListBox::keyPressEvent(e);
44         }
45 };
46
47 }
48  
49 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
50         : QToolBar(view), view_(view), controller_(control)
51 {
52         setHorizontalStretchable(true);
53  
54         QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
55
56         QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
57         upb->show();
58  
59         QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
60         downb->show();
61  
62         edit_ = new QCommandEdit(this);
63         edit_->setMinimumSize(edit_->sizeHint());
64         edit_->show();
65         setStretchableWidget(edit_);
66  
67         show();
68  
69         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
70         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
71         connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
72         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
73         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
74 }
75
76
77
78 void QCommandBuffer::focus_command()
79 {
80         edit_->setFocus();
81 }
82
83  
84 void QCommandBuffer::cancel()
85 {
86         view_->centralWidget()->setFocus();
87         edit_->setText("");
88 }
89
90  
91 void QCommandBuffer::dispatch()
92 {
93         controller_.dispatch(edit_->text().latin1());
94         view_->centralWidget()->setFocus();
95         edit_->setText("");
96 }
97
98
99 void QCommandBuffer::complete()
100 {
101         string const input = edit_->text().latin1();
102         string new_input;
103         vector<string> comp = controller_.completions(input, new_input);
104                          
105         if (comp.empty() && new_input == input) {
106         //      show_info_suffix(_("[no match]"), input);
107                 return;
108         }
109
110         if (comp.empty()) {
111                 edit_->setText(new_input.c_str());
112         //      show_info_suffix(("[only completion]"), new_input + " ");
113                 return;
114         }
115                                  
116         edit_->setText(new_input.c_str());
117
118         QTempListBox * list = new QTempListBox();
119         vector<string>::const_iterator cit = comp.begin();
120         vector<string>::const_iterator end = comp.end();
121         for (; cit != end; ++cit) {
122                 list->insertItem(cit->c_str());
123         }
124
125         // For some reason we get lots of empty entries and the 
126         // scrollbar is wrong as a result. No fix. Qt Sucks.
127  
128         // width() is not big enough by a few pixels. Qt Sucks.
129         list->setMinimumWidth(list->sizeHint().width() + 10);
130  
131         list->resize(list->sizeHint());
132         QPoint pos(edit_->mapToGlobal(QPoint(0, 0))); 
133         int y = pos.y() - list->height();
134         if (y < 0)
135                 y = 0;
136         list->move(pos.x(), y);
137  
138         connect(list, SIGNAL(selected(const QString &)),
139                 this, SLOT(complete_selected(const QString &))); 
140  
141         // Note we *cannot* make a single click popup, because
142         // events get generated for outside the popup on Qt 2.3.1
143         // and even gives valid QListBoxItem *'s. We have no way
144         // to work past this. Qt Sucks. 
145         //connect(list, SIGNAL(clicked(QListBoxItem *)),
146         //      this, SLOT(complete_selected2(QListBoxItem *)));
147
148         list->show();
149         list->setFocus();
150 }
151
152
153 void QCommandBuffer::complete_selected(const QString & str)
154 {
155         edit_->setText(str + " ");
156         QWidget const * widget = static_cast<QWidget const *>(sender());
157         const_cast<QWidget *>(widget)->hide();
158 }
159
160  
161 void QCommandBuffer::up()
162 {
163         string const input = edit_->text().latin1();
164         string const h(controller_.historyUp());
165  
166         if (h.empty()) {
167         //      show_info_suffix(_("[Beginning of history]"), input);
168         } else {
169                 edit_->setText(h.c_str());
170         }
171 }
172
173
174 void QCommandBuffer::down()
175 {
176         string const input = edit_->text().latin1();
177         string const h(controller_.historyDown());
178  
179         if (h.empty()) {
180         //      show_info_suffix(_("[End of history]"), input);
181         } else {
182                 edit_->setText(h.c_str());
183         }
184 }
185
186
187 #if 0 
188 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
189 {
190         stored_input_ = input;
191         info_suffix_shown_ = true;
192         set_input(input + " " + suffix); 
193         suffix_timer_->start();
194 }
195  
196
197 void XMiniBuffer::suffix_timeout()
198 {
199         info_suffix_shown_ = false;
200         set_input(stored_input_);
201 }
202
203 #endif