3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Juergen
9 * Full author contact details are available in file CREDITS
14 #include "XMiniBuffer.h"
15 #include "ControlCommandBuffer.h"
16 #include "freebrowser.h"
17 #include "xforms_helpers.h"
21 #include "frontends/Timeout.h"
23 #include "support/lstrings.h"
25 #include <boost/bind.hpp>
29 #ifndef CXX_GLOBAL_CSTD
39 /// This creates the input widget for the minibuffer
40 FL_OBJECT * create_input_box(void * parent, int type,
41 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
43 FL_FREEBROWSER * create_freebrowser(void * parent);
48 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
49 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
50 : controller_(control),
53 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
54 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
56 info_timer_.reset(new Timeout(1500));
57 idle_timer_.reset(new Timeout(6000));
58 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
59 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
65 // This is here so that scoped ptr will not require a complete type.
66 XMiniBuffer::~XMiniBuffer()
70 void XMiniBuffer::freebrowserCB(int action)
72 if (action < 0 || action > 1)
73 // unrecognized action
77 // The freebrowser has been hidden
80 if (freebrowser_->last_printable) {
81 // Append this char to the current input contents
82 string input = getString(input_);
83 input += freebrowser_->last_printable;
84 fl_set_input(input_, input.c_str());
87 // Fill the input widget with the selected
89 FL_OBJECT * browser = freebrowser_->browser;
90 string const str = getString(browser);
93 // add a space so the user can type
94 // an argument immediately
101 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
102 int key, XEvent * /*xev*/)
119 char const * tmp = fl_get_input(ob);
120 input = tmp ? tmp : "";
128 string const h(controller_.historyDown());
130 show_info(_("[End of history]"), input, false);
142 string const h(controller_.historyUp());
144 show_info(_("[Beginning of history]"), input, false);
155 vector<string> comp = controller_.completions(input, new_input);
157 if (comp.empty() && new_input == input) {
158 show_info(_("[no match]"), input);
163 set_input(new_input);
164 show_info(_("[only completion]"), new_input + ' ');
168 set_input(new_input);
170 // Fill freebrowser_'s browser with the list of
171 // available completions
172 FL_OBJECT * browser = freebrowser_->browser;
173 fl_clear_browser(browser);
174 vector<string>::const_iterator cit = comp.begin();
175 vector<string>::const_iterator end = comp.end();
176 for (; cit != end; ++cit) {
177 fl_add_browser_line(browser, cit->c_str());
179 fl_select_browser_line(browser, 1);
181 // Set the position of the freebrowser and display it.
183 fl_get_wingeometry(fl_get_real_object_window(input_),
186 // asynchronous completion
187 int const air = input_->x;
189 y += h - (input_->h + air);
193 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
208 controller_.dispatch(input);
223 void XMiniBuffer::freeze()
225 // we must prevent peek_event, or we get an unfocus() when the
226 // containing form gets destroyed
227 fl_set_object_prehandler(input_, 0);
231 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
233 stored_input_ = input;
236 set_input(input + ' ' + info);
239 info_timer_->start();
243 void XMiniBuffer::idle_timeout()
245 set_input(controller_.getCurrentState());
249 void XMiniBuffer::info_timeout()
252 set_input(stored_input_);
256 bool XMiniBuffer::isEditingMode() const
258 return input_->focus;
262 void XMiniBuffer::messageMode(bool on)
266 fl_activate_object(input_);
267 fl_set_focus_object(input_->form, input_);
271 if (isEditingMode()) {
272 // focus back to the workarea
273 fl_set_focus_object(input_->form, 0);
274 idle_timer_->start();
280 void XMiniBuffer::redraw()
282 fl_redraw_object(input_);
287 void XMiniBuffer::message(string const & str)
289 if (!isEditingMode())
294 void XMiniBuffer::set_input(string const & str)
296 fl_set_input(input_, str.c_str());
303 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
307 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
308 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
313 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
315 if (!fb || !fb->parent)
318 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
319 ptr->freebrowserCB(action);
323 FL_OBJECT * create_input_box(void * parent, int type,
324 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
326 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
327 fl_set_object_boxtype(obj, FL_DOWN_BOX);
328 fl_set_object_resize(obj, FL_RESIZE_ALL);
329 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
330 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
331 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
333 // To intercept Up, Down, Table for history
334 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
335 obj->u_vdata = parent;
336 obj->wantkey = FL_KEY_TAB;
342 FL_FREEBROWSER * create_freebrowser(void * parent)
344 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
345 fb->want_printable = 1;
346 fb->callback = C_freebrowserCB;