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>
32 /// This creates the input widget for the minibuffer
33 FL_OBJECT * create_input_box(void * parent, int type,
34 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
36 FL_FREEBROWSER * create_freebrowser(void * parent);
41 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
42 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
43 : controller_(control),
46 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
47 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
49 info_timer_.reset(new Timeout(1500));
50 idle_timer_.reset(new Timeout(6000));
51 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
52 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
58 // This is here so that scoped ptr will not require a complete type.
59 XMiniBuffer::~XMiniBuffer()
63 void XMiniBuffer::freebrowserCB(int action)
65 if (action < 0 || action > 1)
66 // unrecognized action
70 // The freebrowser has been hidden
73 if (freebrowser_->last_printable) {
74 // Append this char to the current input contents
75 string input = getString(input_);
76 input += freebrowser_->last_printable;
77 fl_set_input(input_, input.c_str());
80 // Fill the input widget with the selected
82 FL_OBJECT * browser = freebrowser_->browser;
83 string const str = getString(browser);
86 // add a space so the user can type
87 // an argument immediately
94 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
95 int key, XEvent * /*xev*/)
112 char const * tmp = fl_get_input(ob);
113 input = tmp ? tmp : "";
121 string const h(controller_.historyDown());
123 show_info(_("[End of history]"), input, false);
135 string const h(controller_.historyUp());
137 show_info(_("[Beginning of history]"), input, false);
148 vector<string> comp = controller_.completions(input, new_input);
150 if (comp.empty() && new_input == input) {
151 show_info(_("[no match]"), input);
156 set_input(new_input);
157 show_info(_("[only completion]"), new_input + ' ');
161 set_input(new_input);
163 // Fill freebrowser_'s browser with the list of
164 // available completions
165 FL_OBJECT * browser = freebrowser_->browser;
166 fl_clear_browser(browser);
167 vector<string>::const_iterator cit = comp.begin();
168 vector<string>::const_iterator end = comp.end();
169 for (; cit != end; ++cit) {
170 fl_add_browser_line(browser, cit->c_str());
172 fl_select_browser_line(browser, 1);
174 // Set the position of the freebrowser and display it.
176 fl_get_wingeometry(fl_get_real_object_window(input_),
179 // asynchronous completion
180 int const air = input_->x;
182 y += h - (input_->h + air);
186 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
201 controller_.dispatch(input);
216 void XMiniBuffer::freeze()
218 // we must prevent peek_event, or we get an unfocus() when the
219 // containing form gets destroyed
220 fl_set_object_prehandler(input_, 0);
224 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
226 stored_input_ = input;
229 set_input(input + ' ' + info);
232 info_timer_->start();
236 void XMiniBuffer::idle_timeout()
238 set_input(controller_.getCurrentState());
242 void XMiniBuffer::info_timeout()
245 set_input(stored_input_);
249 bool XMiniBuffer::isEditingMode() const
251 return input_->focus;
255 void XMiniBuffer::messageMode(bool on)
259 fl_activate_object(input_);
260 fl_set_focus_object(input_->form, input_);
264 if (isEditingMode()) {
265 // focus back to the workarea
266 fl_set_focus_object(input_->form, 0);
267 idle_timer_->start();
273 void XMiniBuffer::redraw()
275 fl_redraw_object(input_);
280 void XMiniBuffer::message(string const & str)
282 if (!isEditingMode())
287 void XMiniBuffer::set_input(string const & str)
289 fl_set_input(input_, str.c_str());
296 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
300 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
301 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
306 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
308 if (!fb || !fb->parent)
311 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
312 ptr->freebrowserCB(action);
316 FL_OBJECT * create_input_box(void * parent, int type,
317 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
319 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
320 fl_set_object_boxtype(obj, FL_DOWN_BOX);
321 fl_set_object_resize(obj, FL_RESIZE_ALL);
322 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
323 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
324 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
326 // To intercept Up, Down, Table for history
327 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
328 obj->u_vdata = parent;
329 obj->wantkey = FL_KEY_TAB;
335 FL_FREEBROWSER * create_freebrowser(void * parent)
337 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
338 fb->want_printable = 1;
339 fb->callback = C_freebrowserCB;