3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Jürgen
9 * Full author contact details are available in file CREDITS.
14 #include "XMiniBuffer.h"
16 #include "freebrowser.h"
17 #include "xforms_helpers.h"
19 #include "controllers/ControlCommandBuffer.h"
21 #include "frontends/Timeout.h"
25 #include <boost/bind.hpp>
28 #ifndef CXX_GLOBAL_CSTD
38 /// This creates the input widget for the minibuffer
39 FL_OBJECT * create_input_box(void * parent, int type,
40 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
42 FL_FREEBROWSER * create_freebrowser(void * parent);
47 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
48 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
49 : controller_(control),
52 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
53 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
55 info_timer_.reset(new Timeout(1500));
56 idle_timer_.reset(new Timeout(6000));
57 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
58 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
64 // This is here so that scoped ptr will not require a complete type.
65 XMiniBuffer::~XMiniBuffer()
69 void XMiniBuffer::freebrowserCB(int action)
71 if (action < 0 || action > 1)
72 // unrecognized action
76 // The freebrowser has been hidden
79 if (freebrowser_->last_printable) {
80 // Append this char to the current input contents
81 string input = getString(input_);
82 input += freebrowser_->last_printable;
83 fl_set_input(input_, input.c_str());
86 // Fill the input widget with the selected
88 FL_OBJECT * browser = freebrowser_->browser;
89 string const str = getString(browser);
92 // add a space so the user can type
93 // an argument immediately
100 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
101 int key, XEvent * /*xev*/)
118 char const * tmp = fl_get_input(ob);
119 input = tmp ? tmp : "";
127 string const h(controller_.historyDown());
129 show_info(_("[End of history]"), input, false);
141 string const h(controller_.historyUp());
143 show_info(_("[Beginning of history]"), input, false);
154 vector<string> comp = controller_.completions(input, new_input);
156 if (comp.empty() && new_input == input) {
157 show_info(_("[no match]"), input);
162 set_input(new_input);
163 show_info(_("[only completion]"), new_input + ' ');
167 set_input(new_input);
169 // Fill freebrowser_'s browser with the list of
170 // available completions
171 FL_OBJECT * browser = freebrowser_->browser;
172 fl_clear_browser(browser);
173 vector<string>::const_iterator cit = comp.begin();
174 vector<string>::const_iterator end = comp.end();
175 for (; cit != end; ++cit) {
176 fl_add_browser_line(browser, cit->c_str());
178 fl_select_browser_line(browser, 1);
180 // Set the position of the freebrowser and display it.
182 fl_get_wingeometry(fl_get_real_object_window(input_),
185 // asynchronous completion
186 int const air = input_->x;
188 y += h - (input_->h + air);
192 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
207 controller_.dispatch(input);
222 void XMiniBuffer::freeze()
224 // we must prevent peek_event, or we get an unfocus() when the
225 // containing form gets destroyed
226 fl_set_object_prehandler(input_, 0);
230 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
232 stored_input_ = input;
235 set_input(input + ' ' + info);
238 info_timer_->start();
242 void XMiniBuffer::idle_timeout()
244 set_input(controller_.getCurrentState());
248 void XMiniBuffer::info_timeout()
251 set_input(stored_input_);
255 bool XMiniBuffer::isEditingMode() const
257 return input_->focus;
261 void XMiniBuffer::messageMode(bool on)
265 fl_activate_object(input_);
266 fl_set_focus_object(input_->form, input_);
270 if (isEditingMode()) {
271 // focus back to the workarea
272 fl_set_focus_object(input_->form, 0);
273 idle_timer_->start();
279 void XMiniBuffer::redraw()
281 fl_redraw_object(input_);
286 void XMiniBuffer::message(string const & str)
288 if (!isEditingMode())
293 void XMiniBuffer::set_input(string const & str)
295 fl_set_input(input_, str.c_str());
302 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
306 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
307 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
312 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
314 if (!fb || !fb->parent)
317 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
318 ptr->freebrowserCB(action);
322 FL_OBJECT * create_input_box(void * parent, int type,
323 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
325 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
326 fl_set_object_boxtype(obj, FL_DOWN_BOX);
327 fl_set_object_resize(obj, FL_RESIZE_ALL);
328 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
329 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
330 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
332 // To intercept Up, Down, Table for history
333 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
334 obj->u_vdata = parent;
335 obj->wantkey = FL_KEY_TAB;
341 FL_FREEBROWSER * create_freebrowser(void * parent)
343 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
344 fb->want_printable = 1;
345 fb->callback = C_freebrowserCB;