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.
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
19 #include "GMiniBuffer.h"
21 #include "bufferview_funcs.h"
23 #include "frontends/controllers/ControlCommandBuffer.h"
25 #include <boost/bind.hpp>
33 GMiniBuffer::GMiniBuffer(GView * view, ControlCommandBuffer & control) :
34 controller_(control), view_(view)
36 listCols_.add(listCol_);
37 listStore_ = Gtk::ListStore::create(listCols_);
38 listView_.set_model(listStore_);
39 listView_.append_column("Completions", listCol_);
40 listView_.signal_key_press_event().connect(
41 sigc::mem_fun(*this, &GMiniBuffer::onListKeyPress));
42 listView_.signal_focus_in_event().connect(
43 sigc::mem_fun(*this, &GMiniBuffer::onListFocusIn));
44 listView_.signal_focus_out_event().connect(
45 sigc::mem_fun(*this, &GMiniBuffer::onFocusOut));
46 listSel_ = listView_.get_selection();
47 listSel_->signal_changed().connect(
48 sigc::mem_fun(*this, &GMiniBuffer::onSelected));
51 scrolledWindow_.set_policy(Gtk::POLICY_AUTOMATIC,
52 Gtk::POLICY_AUTOMATIC);
53 scrolledWindow_.set_size_request(300, 150);
54 scrolledWindow_.add(listView_);
56 view_->getBox(GView::Bottom).children().push_back(
57 Gtk::Box_Helpers::Element(scrolledWindow_,Gtk::PACK_SHRINK));
59 entry_.signal_key_press_event().connect(
60 sigc::mem_fun(*this, &GMiniBuffer::onKeyPress));
61 entry_.signal_focus_in_event().connect(
62 sigc::mem_fun(*this, &GMiniBuffer::onFocusIn));
63 entry_.signal_focus_out_event().connect(
64 sigc::mem_fun(*this, &GMiniBuffer::onFocusOut));
65 entry_.signal_activate().connect(
66 sigc::mem_fun(*this, &GMiniBuffer::onCommit));
69 view_->getBox(GView::Bottom).children().push_back(
70 Gtk::Box_Helpers::Element(entry_, Gtk::PACK_SHRINK));
72 infoTimer_.reset(new Timeout(1500));
73 idleTimer_.reset(new Timeout(6000));
74 focusTimer_.reset(new Timeout(50));
75 infoCon_ = infoTimer_->timeout.connect(
76 boost::bind(&GMiniBuffer::infoTimeout, this));
77 idleCon_ = idleTimer_->timeout.connect(
78 boost::bind(&GMiniBuffer::idleTimeout, this));
79 focusTimer_->timeout.connect(
80 boost::bind(&GMiniBuffer::focusTimeout, this));
86 GMiniBuffer::~GMiniBuffer()
91 void GMiniBuffer::message(string const & str)
94 setInput(Glib::locale_to_utf8(str));
98 void GMiniBuffer::showInfo(Glib::ustring const & info, bool append)
100 storedInput_ = entry_.get_text();
101 entry_.set_editable(false);
104 setInput(storedInput_ + ' ' + info);
111 void GMiniBuffer::onSelected()
113 if (!listSel_->count_selected_rows())
115 Gtk::TreeModel::iterator it = listSel_->get_selected();
116 Glib::ustring sel = (*it)[listCol_];
121 void GMiniBuffer::onCommit()
123 controller_.dispatch(Glib::locale_from_utf8(entry_.get_text()));
128 bool GMiniBuffer::onListFocusIn(GdkEventFocus * /*event*/)
130 if (focusTimer_->running())
140 bool GMiniBuffer::onFocusIn(GdkEventFocus * /*event*/)
146 if (focusTimer_->running()) {
156 bool GMiniBuffer::onFocusOut(GdkEventFocus * /*event*/)
158 focusTimer_->start();
163 void GMiniBuffer::focusTimeout()
172 scrolledWindow_.hide();
176 bool GMiniBuffer::onListKeyPress(GdkEventKey * event)
182 switch (event->keyval) {
188 setInput(entry_.get_text() + ' ');
195 bool GMiniBuffer::onKeyPress(GdkEventKey * event)
201 switch (event->keyval) {
204 Glib::ustring const h =
205 Glib::locale_to_utf8(controller_.historyDown());
207 showInfo("[End of history]", false);
214 Glib::ustring const h =
215 Glib::locale_to_utf8(controller_.historyUp());
217 showInfo("[Beginning of history]", false);
227 string new_input_locale;
228 Glib::ustring input = entry_.get_text();
229 std::vector<string> comp =
230 controller_.completions(Glib::locale_from_utf8(input),
232 Glib::ustring new_input =
233 Glib::locale_to_utf8(new_input_locale);
235 if (comp.empty() && new_input == input) {
236 showInfo("[no match]");
241 setInput(new_input + ' ');
242 showInfo("[only completion]");
247 std::vector<string>::iterator it;
248 for (it = comp.begin(); it != comp.end(); ++it)
249 (*listStore_->append())[listCol_] =
250 Glib::locale_to_utf8(*it);
251 scrolledWindow_.show();
259 bool GMiniBuffer::isEditMode() const
261 return entry_.has_focus() || listView_.has_focus();
265 void GMiniBuffer::infoTimeout()
268 setInput(storedInput_);
269 entry_.set_editable(true);
273 void GMiniBuffer::idleTimeout()
275 setInput(Glib::locale_to_utf8(controller_.getCurrentState()));
279 void GMiniBuffer::editMode()
285 void GMiniBuffer::messageMode()
287 view_->focusWorkArea();
291 void GMiniBuffer::setInput(Glib::ustring const & input)
293 entry_.set_text(input);
294 entry_.set_position(-1);
297 } // namespace frontend