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>
29 GMiniBuffer::GMiniBuffer(GView * view, ControlCommandBuffer & control) :
30 controller_(control), view_(view)
32 listCols_.add(listCol_);
33 listStore_ = Gtk::ListStore::create(listCols_);
34 listView_.set_model(listStore_);
35 listView_.append_column("Completions", listCol_);
36 listView_.signal_key_press_event().connect(
37 SigC::slot(*this, &GMiniBuffer::onListKeyPress));
38 listView_.signal_focus_in_event().connect(
39 SigC::slot(*this, &GMiniBuffer::onListFocusIn));
40 listView_.signal_focus_out_event().connect(
41 SigC::slot(*this, &GMiniBuffer::onFocusOut));
42 listSel_ = listView_.get_selection();
43 listSel_->signal_changed().connect(
44 SigC::slot(*this, &GMiniBuffer::onSelected));
47 scrolledWindow_.set_policy(Gtk::POLICY_AUTOMATIC,
48 Gtk::POLICY_AUTOMATIC);
49 scrolledWindow_.set_size_request(300, 150);
50 scrolledWindow_.add(listView_);
52 view_->getBox(GView::Bottom).children().push_back(
53 Gtk::Box_Helpers::Element(scrolledWindow_,Gtk::PACK_SHRINK));
55 entry_.signal_key_press_event().connect(
56 SigC::slot(*this, &GMiniBuffer::onKeyPress));
57 entry_.signal_focus_in_event().connect(
58 SigC::slot(*this, &GMiniBuffer::onFocusIn));
59 entry_.signal_focus_out_event().connect(
60 SigC::slot(*this, &GMiniBuffer::onFocusOut));
61 entry_.signal_activate().connect(
62 SigC::slot(*this, &GMiniBuffer::onCommit));
65 view_->getBox(GView::Bottom).children().push_back(
66 Gtk::Box_Helpers::Element(entry_, Gtk::PACK_SHRINK));
68 infoTimer_.reset(new Timeout(1500));
69 idleTimer_.reset(new Timeout(6000));
70 focusTimer_.reset(new Timeout(50));
71 infoCon_ = infoTimer_->timeout.connect(
72 boost::bind(&GMiniBuffer::infoTimeout, this));
73 idleCon_ = idleTimer_->timeout.connect(
74 boost::bind(&GMiniBuffer::idleTimeout, this));
75 focusTimer_->timeout.connect(
76 boost::bind(&GMiniBuffer::focusTimeout, this));
82 GMiniBuffer::~GMiniBuffer()
87 void GMiniBuffer::message(string const & str)
90 setInput(Glib::locale_to_utf8(str));
94 void GMiniBuffer::showInfo(Glib::ustring const & info, bool append)
96 storedInput_ = entry_.get_text();
97 entry_.set_editable(false);
100 setInput(storedInput_ + ' ' + info);
107 void GMiniBuffer::onSelected()
109 if (!listSel_->count_selected_rows())
111 Gtk::TreeModel::iterator it = listSel_->get_selected();
112 Glib::ustring sel = (*it)[listCol_];
117 void GMiniBuffer::onCommit()
119 controller_.dispatch(Glib::locale_from_utf8(entry_.get_text()));
124 bool GMiniBuffer::onListFocusIn(GdkEventFocus * /*event*/)
126 if (focusTimer_->running())
136 bool GMiniBuffer::onFocusIn(GdkEventFocus * /*event*/)
142 if (focusTimer_->running()) {
152 bool GMiniBuffer::onFocusOut(GdkEventFocus * /*event*/)
154 focusTimer_->start();
159 void GMiniBuffer::focusTimeout()
168 scrolledWindow_.hide();
172 bool GMiniBuffer::onListKeyPress(GdkEventKey * event)
178 switch (event->keyval) {
184 setInput(entry_.get_text() + ' ');
191 bool GMiniBuffer::onKeyPress(GdkEventKey * event)
197 switch (event->keyval) {
200 Glib::ustring const h =
201 Glib::locale_to_utf8(controller_.historyDown());
203 showInfo("[End of history]", false);
210 Glib::ustring const h =
211 Glib::locale_to_utf8(controller_.historyUp());
213 showInfo("[Beginning of history]", false);
223 string new_input_locale;
224 Glib::ustring input = entry_.get_text();
225 std::vector<string> comp =
226 controller_.completions(Glib::locale_from_utf8(input),
228 Glib::ustring new_input =
229 Glib::locale_to_utf8(new_input_locale);
231 if (comp.empty() && new_input == input) {
232 showInfo("[no match]");
237 setInput(new_input + ' ');
238 showInfo("[only completion]");
243 std::vector<string>::iterator it;
244 for (it = comp.begin(); it != comp.end(); ++it)
245 (*listStore_->append())[listCol_] =
246 Glib::locale_to_utf8(*it);
247 scrolledWindow_.show();
255 bool GMiniBuffer::isEditMode() const
257 return entry_.has_focus() || listView_.has_focus();
261 void GMiniBuffer::infoTimeout()
264 setInput(storedInput_);
265 entry_.set_editable(true);
269 void GMiniBuffer::idleTimeout()
271 setInput(Glib::locale_to_utf8(controller_.getCurrentState()));
275 void GMiniBuffer::editMode()
281 void GMiniBuffer::messageMode()
283 view_->focusWorkArea();
287 void GMiniBuffer::setInput(Glib::ustring const & input)
289 entry_.set_text(input);
290 entry_.set_position(-1);
293 } // namespace frontend