3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
15 #include "GMiniBuffer.h"
17 #include "bufferview_funcs.h"
18 #include <boost/bind.hpp>
20 #include "frontends/controllers/ControlCommandBuffer.h"
23 GMiniBuffer::GMiniBuffer(GView * view, ControlCommandBuffer & control) :
24 controller_(control), view_(view)
26 listCols_.add(listCol_);
27 listStore_ = Gtk::ListStore::create(listCols_);
28 listView_.set_model(listStore_);
29 listView_.append_column("Completions", listCol_);
30 listView_.signal_key_press_event().connect(
31 SigC::slot(*this, &GMiniBuffer::onListKeyPress));
32 listView_.signal_focus_in_event().connect(
33 SigC::slot(*this, &GMiniBuffer::onListFocusIn));
34 listView_.signal_focus_out_event().connect(
35 SigC::slot(*this, &GMiniBuffer::onFocusOut));
36 listSel_ = listView_.get_selection();
37 listSel_->signal_changed().connect(
38 SigC::slot(*this, &GMiniBuffer::onSelected));
40 scrolledWindow_.set_policy(Gtk::POLICY_AUTOMATIC,
41 Gtk::POLICY_AUTOMATIC);
42 scrolledWindow_.set_size_request(300, 150);
43 scrolledWindow_.add(listView_);
44 view_->getVBox().children().push_back(
45 Gtk::Box_Helpers::Element(scrolledWindow_,
47 entry_.signal_key_press_event().connect(
48 SigC::slot(*this, &GMiniBuffer::onKeyPress));
49 entry_.signal_focus_in_event().connect(
50 SigC::slot(*this, &GMiniBuffer::onFocusIn));
51 entry_.signal_focus_out_event().connect(
52 SigC::slot(*this, &GMiniBuffer::onFocusOut));
53 entry_.signal_activate().connect(
54 SigC::slot(*this, &GMiniBuffer::onCommit));
56 view_->getVBox().children().push_back(
57 Gtk::Box_Helpers::Element(entry_,
59 infoTimer_.reset(new Timeout(1500));
60 idleTimer_.reset(new Timeout(6000));
61 focusTimer_.reset(new Timeout(50));
62 infoCon_ = infoTimer_->timeout.connect(
63 boost::bind(&GMiniBuffer::infoTimeout, this));
64 idleCon_ = idleTimer_->timeout.connect(
65 boost::bind(&GMiniBuffer::idleTimeout, this));
66 focusTimer_->timeout.connect(
67 boost::bind(&GMiniBuffer::focusTimeout, this));
73 GMiniBuffer::~GMiniBuffer()
78 void GMiniBuffer::message(string const & str)
81 setInput(Glib::locale_to_utf8(str));
85 void GMiniBuffer::showInfo(Glib::ustring const & info, bool append)
87 storedInput_ = entry_.get_text();
88 entry_.set_editable(false);
91 setInput(storedInput_ + ' ' + info);
98 void GMiniBuffer::onSelected()
100 if (!listSel_->count_selected_rows())
102 Gtk::TreeModel::iterator it = listSel_->get_selected();
103 Glib::ustring sel = (*it)[listCol_];
108 void GMiniBuffer::onCommit()
110 controller_.dispatch(Glib::locale_from_utf8(entry_.get_text()));
115 bool GMiniBuffer::onListFocusIn(GdkEventFocus * /*event*/)
117 if (focusTimer_->running())
127 bool GMiniBuffer::onFocusIn(GdkEventFocus * /*event*/)
133 if (focusTimer_->running()) {
143 bool GMiniBuffer::onFocusOut(GdkEventFocus * /*event*/)
145 focusTimer_->start();
150 void GMiniBuffer::focusTimeout()
159 scrolledWindow_.hide();
163 bool GMiniBuffer::onListKeyPress(GdkEventKey * event)
169 switch (event->keyval) {
175 setInput(entry_.get_text() + ' ');
182 bool GMiniBuffer::onKeyPress(GdkEventKey * event)
188 switch (event->keyval) {
191 Glib::ustring const h =
192 Glib::locale_to_utf8(controller_.historyDown());
194 showInfo("[End of history]", false);
201 Glib::ustring const h =
202 Glib::locale_to_utf8(controller_.historyUp());
204 showInfo("[Beginning of history]", false);
214 Glib::ustring new_input, input;
215 string new_input_locale;
216 input = entry_.get_text();
217 std::vector<string> comp =
218 controller_.completions(Glib::locale_from_utf8(input),
220 new_input = Glib::locale_to_utf8(new_input_locale);
221 if (comp.empty() && new_input == input) {
222 showInfo("[no match]");
227 setInput(new_input + ' ');
228 showInfo("[only completion]");
233 std::vector<string>::iterator it;
234 for (it = comp.begin(); it != comp.end(); ++it)
235 (*listStore_->append())[listCol_] =
236 Glib::locale_to_utf8(*it);
237 scrolledWindow_.show();
245 bool GMiniBuffer::isEditMode() const
247 return entry_.has_focus() || listView_.has_focus();
251 void GMiniBuffer::infoTimeout()
254 setInput(storedInput_);
255 entry_.set_editable(true);
259 void GMiniBuffer::idleTimeout()
261 setInput(Glib::locale_to_utf8(controller_.getCurrentState()));
265 void GMiniBuffer::editMode()
271 void GMiniBuffer::messageMode()
273 view_->focusWorkArea();
277 void GMiniBuffer::setInput(Glib::ustring const & input)
279 entry_.set_text(input);
280 entry_.set_position(-1);