]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
last try ... sigh
[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  
15 #include "QtView.h"
16 #include "QCommandBuffer.h"
17 #include "QCommandEdit.h"
18  
19 #include <qcombobox.h>
20 #include <qtoolbutton.h>
21 #include <qpixmap.h>
22  
23 namespace {
24  
25 class QTempComboBox : public QComboBox {
26 public:
27         QTempComboBox(QWidget * parent) : QComboBox(parent) {
28                 setWFlags(WDestructiveClose);
29         }
30
31         void popup() { QComboBox::popup(); }
32 };
33
34 };
35  
36 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
37         : QToolBar(view), view_(view), controller_(control)
38 {
39         setHorizontalStretchable(true);
40  
41         QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
42
43         QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
44         upb->show();
45  
46         QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
47         downb->show();
48  
49         edit_ = new QCommandEdit(this);
50         edit_->setMinimumSize(edit_->sizeHint());
51         edit_->show();
52         setStretchableWidget(edit_);
53  
54         show();
55  
56         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
57         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
58         connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
59         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
60         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
61 }
62
63
64
65 void QCommandBuffer::focus_command()
66 {
67         edit_->setFocus();
68 }
69
70  
71 void QCommandBuffer::cancel()
72 {
73         view_->centralWidget()->setFocus();
74         edit_->setText("");
75 }
76
77  
78 void QCommandBuffer::dispatch()
79 {
80         controller_.dispatch(edit_->text().latin1());
81         view_->centralWidget()->setFocus();
82         edit_->setText("");
83 }
84
85
86 void QCommandBuffer::complete()
87 {
88         string const input = edit_->text().latin1();
89         string new_input;
90         vector<string> comp = controller_.completions(input, new_input);
91                          
92         if (comp.empty() && new_input == input) {
93         //      show_info_suffix(_("[no match]"), input);
94                 return;
95         }
96
97         if (comp.empty()) {
98                 edit_->setText(new_input.c_str());
99         //      show_info_suffix(("[only completion]"), new_input + " ");
100                 return;
101         }
102                                  
103         edit_->setText(new_input.c_str());
104
105         QTempComboBox * combo = new QTempComboBox(view_);
106         combo->move(edit_->x() + x(), edit_->y() + y());
107
108         vector<string>::const_iterator cit = comp.begin();
109         vector<string>::const_iterator end = comp.end();
110         for (; cit != end; ++cit) {
111                 combo->insertItem(cit->c_str());
112         }
113
114         combo->setMinimumWidth(combo->sizeHint().width());
115         combo->resize(combo->width(), edit_->height());
116  
117         connect(combo, SIGNAL(activated(const QString &)),
118                 this, SLOT(complete_selected(const QString &))); 
119  
120         combo->show();
121         combo->setFocus();
122         combo->popup();
123 }
124
125
126 void QCommandBuffer::complete_selected(const QString & str)
127 {
128         edit_->setText(str + " ");
129         // FIXME
130         QWidget const * widget = static_cast<QWidget const *>(sender());
131         edit_->setFocus();
132         const_cast<QWidget *>(widget)->hide();
133 }
134
135  
136 void QCommandBuffer::up()
137 {
138         string const input = edit_->text().latin1();
139         string const h(controller_.historyUp());
140  
141         if (h.empty()) {
142         //      show_info_suffix(_("[Beginning of history]"), input);
143         } else {
144                 edit_->setText(h.c_str());
145         }
146 }
147
148
149 void QCommandBuffer::down()
150 {
151         string const input = edit_->text().latin1();
152         string const h(controller_.historyDown());
153  
154         if (h.empty()) {
155         //      show_info_suffix(_("[End of history]"), input);
156         } else {
157                 edit_->setText(h.c_str());
158         }
159 }
160
161
162 #if 0 
163 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
164 {
165         stored_input_ = input;
166         info_suffix_shown_ = true;
167         set_input(input + " " + suffix); 
168         suffix_timer_->start();
169 }
170  
171
172 void XMiniBuffer::suffix_timeout()
173 {
174         info_suffix_shown_ = false;
175         set_input(stored_input_);
176 }
177
178 #endif