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.
14 #include "GMiniBuffer.h"
16 #include "bufferview_funcs.h"
18 #include "frontends/controllers/ControlCommandBuffer.h"
20 #include <boost/bind.hpp>
28 GMiniBuffer::GMiniBuffer(GView * view, ControlCommandBuffer & control) :
29 controller_(control), view_(view)
31 listCols_.add(listCol_);
32 listStore_ = Gtk::ListStore::create(listCols_);
33 listView_.set_model(listStore_);
34 listView_.append_column("Completions", listCol_);
35 listView_.signal_key_press_event().connect(
36 sigc::mem_fun(*this, &GMiniBuffer::onListKeyPress));
37 listView_.signal_focus_in_event().connect(
38 sigc::mem_fun(*this, &GMiniBuffer::onListFocusIn));
39 listView_.signal_focus_out_event().connect(
40 sigc::mem_fun(*this, &GMiniBuffer::onFocusOut));
41 listSel_ = listView_.get_selection();
42 listSel_->signal_changed().connect(
43 sigc::mem_fun(*this, &GMiniBuffer::onSelected));
46 scrolledWindow_.set_policy(Gtk::POLICY_AUTOMATIC,
47 Gtk::POLICY_AUTOMATIC);
48 scrolledWindow_.set_size_request(300, 150);
49 scrolledWindow_.add(listView_);
51 view_->getBox(GView::Bottom).children().push_back(
52 Gtk::Box_Helpers::Element(scrolledWindow_,Gtk::PACK_SHRINK));
54 entry_.signal_key_press_event().connect(
55 sigc::mem_fun(*this, &GMiniBuffer::onKeyPress));
56 entry_.signal_focus_in_event().connect(
57 sigc::mem_fun(*this, &GMiniBuffer::onFocusIn));
58 entry_.signal_focus_out_event().connect(
59 sigc::mem_fun(*this, &GMiniBuffer::onFocusOut));
60 entry_.signal_activate().connect(
61 sigc::mem_fun(*this, &GMiniBuffer::onCommit));
64 view_->getBox(GView::Bottom).children().push_back(
65 Gtk::Box_Helpers::Element(entry_, Gtk::PACK_SHRINK));
67 infoTimer_.reset(new Timeout(1500));
68 idleTimer_.reset(new Timeout(6000));
69 focusTimer_.reset(new Timeout(50));
70 infoCon_ = infoTimer_->timeout.connect(
71 boost::bind(&GMiniBuffer::infoTimeout, this));
72 idleCon_ = idleTimer_->timeout.connect(
73 boost::bind(&GMiniBuffer::idleTimeout, this));
74 focusTimer_->timeout.connect(
75 boost::bind(&GMiniBuffer::focusTimeout, this));
81 GMiniBuffer::~GMiniBuffer()
86 void GMiniBuffer::message(string const & str)
89 setInput(Glib::locale_to_utf8(str));
93 void GMiniBuffer::showInfo(Glib::ustring const & info, bool append)
95 storedInput_ = entry_.get_text();
96 entry_.set_editable(false);
99 setInput(storedInput_ + ' ' + info);
106 void GMiniBuffer::onSelected()
108 if (!listSel_->count_selected_rows())
110 Gtk::TreeModel::iterator it = listSel_->get_selected();
111 Glib::ustring sel = (*it)[listCol_];
116 void GMiniBuffer::onCommit()
118 controller_.dispatch(Glib::locale_from_utf8(entry_.get_text()));
123 bool GMiniBuffer::onListFocusIn(GdkEventFocus * /*event*/)
125 if (focusTimer_->running())
135 bool GMiniBuffer::onFocusIn(GdkEventFocus * /*event*/)
141 if (focusTimer_->running()) {
151 bool GMiniBuffer::onFocusOut(GdkEventFocus * /*event*/)
153 focusTimer_->start();
158 void GMiniBuffer::focusTimeout()
167 scrolledWindow_.hide();
171 bool GMiniBuffer::onListKeyPress(GdkEventKey * event)
177 switch (event->keyval) {
183 setInput(entry_.get_text() + ' ');
190 bool GMiniBuffer::onKeyPress(GdkEventKey * event)
196 switch (event->keyval) {
199 Glib::ustring const h =
200 Glib::locale_to_utf8(controller_.historyDown());
202 showInfo("[End of history]", false);
209 Glib::ustring const h =
210 Glib::locale_to_utf8(controller_.historyUp());
212 showInfo("[Beginning of history]", false);
222 string new_input_locale;
223 Glib::ustring input = entry_.get_text();
224 std::vector<string> comp =
225 controller_.completions(Glib::locale_from_utf8(input),
227 Glib::ustring new_input =
228 Glib::locale_to_utf8(new_input_locale);
230 if (comp.empty() && new_input == input) {
231 showInfo("[no match]");
236 setInput(new_input + ' ');
237 showInfo("[only completion]");
242 std::vector<string>::iterator it;
243 for (it = comp.begin(); it != comp.end(); ++it)
244 (*listStore_->append())[listCol_] =
245 Glib::locale_to_utf8(*it);
246 scrolledWindow_.show();
254 bool GMiniBuffer::isEditMode() const
256 return entry_.has_focus() || listView_.has_focus();
260 void GMiniBuffer::infoTimeout()
263 setInput(storedInput_);
264 entry_.set_editable(true);
268 void GMiniBuffer::idleTimeout()
270 setInput(Glib::locale_to_utf8(controller_.getCurrentState()));
274 void GMiniBuffer::editMode()
280 void GMiniBuffer::messageMode()
282 view_->focusWorkArea();
286 void GMiniBuffer::setInput(Glib::ustring const & input)
288 entry_.set_text(input);
289 entry_.set_position(-1);
292 } // namespace frontend