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