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"
19 #include "frontends/controllers/ControlCommandBuffer.h"
21 #include <boost/bind.hpp>
27 GMiniBuffer::GMiniBuffer(GView * view, ControlCommandBuffer & control) :
28 controller_(control), view_(view)
30 listCols_.add(listCol_);
31 listStore_ = Gtk::ListStore::create(listCols_);
32 listView_.set_model(listStore_);
33 listView_.append_column("Completions", listCol_);
34 listView_.signal_key_press_event().connect(
35 SigC::slot(*this, &GMiniBuffer::onListKeyPress));
36 listView_.signal_focus_in_event().connect(
37 SigC::slot(*this, &GMiniBuffer::onListFocusIn));
38 listView_.signal_focus_out_event().connect(
39 SigC::slot(*this, &GMiniBuffer::onFocusOut));
40 listSel_ = listView_.get_selection();
41 listSel_->signal_changed().connect(
42 SigC::slot(*this, &GMiniBuffer::onSelected));
44 scrolledWindow_.set_policy(Gtk::POLICY_AUTOMATIC,
45 Gtk::POLICY_AUTOMATIC);
46 scrolledWindow_.set_size_request(300, 150);
47 scrolledWindow_.add(listView_);
48 view_->getVBox().children().push_back(
49 Gtk::Box_Helpers::Element(scrolledWindow_,
51 entry_.signal_key_press_event().connect(
52 SigC::slot(*this, &GMiniBuffer::onKeyPress));
53 entry_.signal_focus_in_event().connect(
54 SigC::slot(*this, &GMiniBuffer::onFocusIn));
55 entry_.signal_focus_out_event().connect(
56 SigC::slot(*this, &GMiniBuffer::onFocusOut));
57 entry_.signal_activate().connect(
58 SigC::slot(*this, &GMiniBuffer::onCommit));
60 view_->getVBox().children().push_back(
61 Gtk::Box_Helpers::Element(entry_,
63 infoTimer_.reset(new Timeout(1500));
64 idleTimer_.reset(new Timeout(6000));
65 focusTimer_.reset(new Timeout(50));
66 infoCon_ = infoTimer_->timeout.connect(
67 boost::bind(&GMiniBuffer::infoTimeout, this));
68 idleCon_ = idleTimer_->timeout.connect(
69 boost::bind(&GMiniBuffer::idleTimeout, this));
70 focusTimer_->timeout.connect(
71 boost::bind(&GMiniBuffer::focusTimeout, this));
77 GMiniBuffer::~GMiniBuffer()
82 void GMiniBuffer::message(string const & str)
85 setInput(Glib::locale_to_utf8(str));
89 void GMiniBuffer::showInfo(Glib::ustring const & info, bool append)
91 storedInput_ = entry_.get_text();
92 entry_.set_editable(false);
95 setInput(storedInput_ + ' ' + info);
102 void GMiniBuffer::onSelected()
104 if (!listSel_->count_selected_rows())
106 Gtk::TreeModel::iterator it = listSel_->get_selected();
107 Glib::ustring sel = (*it)[listCol_];
112 void GMiniBuffer::onCommit()
114 controller_.dispatch(Glib::locale_from_utf8(entry_.get_text()));
119 bool GMiniBuffer::onListFocusIn(GdkEventFocus * /*event*/)
121 if (focusTimer_->running())
131 bool GMiniBuffer::onFocusIn(GdkEventFocus * /*event*/)
137 if (focusTimer_->running()) {
147 bool GMiniBuffer::onFocusOut(GdkEventFocus * /*event*/)
149 focusTimer_->start();
154 void GMiniBuffer::focusTimeout()
163 scrolledWindow_.hide();
167 bool GMiniBuffer::onListKeyPress(GdkEventKey * event)
173 switch (event->keyval) {
179 setInput(entry_.get_text() + ' ');
186 bool GMiniBuffer::onKeyPress(GdkEventKey * event)
192 switch (event->keyval) {
195 Glib::ustring const h =
196 Glib::locale_to_utf8(controller_.historyDown());
198 showInfo("[End of history]", false);
205 Glib::ustring const h =
206 Glib::locale_to_utf8(controller_.historyUp());
208 showInfo("[Beginning of history]", false);
218 Glib::ustring new_input, input;
219 string new_input_locale;
220 input = entry_.get_text();
221 std::vector<string> comp =
222 controller_.completions(Glib::locale_from_utf8(input),
224 new_input = Glib::locale_to_utf8(new_input_locale);
225 if (comp.empty() && new_input == input) {
226 showInfo("[no match]");
231 setInput(new_input + ' ');
232 showInfo("[only completion]");
237 std::vector<string>::iterator it;
238 for (it = comp.begin(); it != comp.end(); ++it)
239 (*listStore_->append())[listCol_] =
240 Glib::locale_to_utf8(*it);
241 scrolledWindow_.show();
249 bool GMiniBuffer::isEditMode() const
251 return entry_.has_focus() || listView_.has_focus();
255 void GMiniBuffer::infoTimeout()
258 setInput(storedInput_);
259 entry_.set_editable(true);
263 void GMiniBuffer::idleTimeout()
265 setInput(Glib::locale_to_utf8(controller_.getCurrentState()));
269 void GMiniBuffer::editMode()
275 void GMiniBuffer::messageMode()
277 view_->focusWorkArea();
281 void GMiniBuffer::setInput(Glib::ustring const & input)
283 entry_.set_text(input);
284 entry_.set_position(-1);