4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Asger and Juergen
10 * Full author contact details are available in file CREDITS
15 #include "XMiniBuffer.h"
16 #include "ControlCommandBuffer.h"
17 #include "freebrowser.h"
18 #include "xforms_helpers.h"
22 #include "frontends/Timeout.h"
24 #include "support/lstrings.h"
26 #include <boost/bind.hpp>
30 #ifndef CXX_GLOBAL_CSTD
40 /// This creates the input widget for the minibuffer
41 FL_OBJECT * create_input_box(void * parent, int type,
42 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
44 FL_FREEBROWSER * create_freebrowser(void * parent);
49 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
50 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
51 : controller_(control),
54 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
55 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
57 info_timer_.reset(new Timeout(1500));
58 idle_timer_.reset(new Timeout(6000));
59 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
60 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
66 // This is here so that scoped ptr will not require a complete type.
67 XMiniBuffer::~XMiniBuffer()
71 void XMiniBuffer::freebrowserCB(int action)
73 if (action < 0 || action > 1)
74 // unrecognized action
78 // The freebrowser has been hidden
81 if (freebrowser_->last_printable) {
82 // Append this char to the current input contents
83 string input = getString(input_);
84 input += freebrowser_->last_printable;
85 fl_set_input(input_, input.c_str());
88 // Fill the input widget with the selected
90 FL_OBJECT * browser = freebrowser_->browser;
91 string const str = getString(browser);
94 // add a space so the user can type
95 // an argument immediately
102 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
103 int key, XEvent * /*xev*/)
120 char const * tmp = fl_get_input(ob);
121 input = tmp ? tmp : "";
129 string const h(controller_.historyDown());
131 show_info(_("[End of history]"), input, false);
143 string const h(controller_.historyUp());
145 show_info(_("[Beginning of history]"), input, false);
156 vector<string> comp = controller_.completions(input, new_input);
158 if (comp.empty() && new_input == input) {
159 show_info(_("[no match]"), input);
164 set_input(new_input);
165 show_info(_("[only completion]"), new_input + ' ');
169 set_input(new_input);
171 // Fill freebrowser_'s browser with the list of
172 // available completions
173 FL_OBJECT * browser = freebrowser_->browser;
174 fl_clear_browser(browser);
175 vector<string>::const_iterator cit = comp.begin();
176 vector<string>::const_iterator end = comp.end();
177 for (; cit != end; ++cit) {
178 fl_add_browser_line(browser, cit->c_str());
180 fl_select_browser_line(browser, 1);
182 // Set the position of the freebrowser and display it.
184 fl_get_wingeometry(fl_get_real_object_window(input_),
187 // asynchronous completion
188 int const air = input_->x;
190 y += h - (input_->h + air);
194 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
209 controller_.dispatch(input);
224 void XMiniBuffer::freeze()
226 // we must prevent peek_event, or we get an unfocus() when the
227 // containing form gets destroyed
228 fl_set_object_prehandler(input_, 0);
232 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
234 stored_input_ = input;
237 set_input(input + ' ' + info);
240 info_timer_->start();
244 void XMiniBuffer::idle_timeout()
246 set_input(controller_.getCurrentState());
250 void XMiniBuffer::info_timeout()
253 set_input(stored_input_);
257 bool XMiniBuffer::isEditingMode() const
259 return input_->focus;
263 void XMiniBuffer::messageMode(bool on)
267 fl_activate_object(input_);
268 fl_set_focus_object(input_->form, input_);
272 if (isEditingMode()) {
273 // focus back to the workarea
274 fl_set_focus_object(input_->form, 0);
275 idle_timer_->start();
281 void XMiniBuffer::redraw()
283 fl_redraw_object(input_);
288 void XMiniBuffer::message(string const & str)
290 if (!isEditingMode())
295 void XMiniBuffer::set_input(string const & str)
297 fl_set_input(input_, str.c_str());
304 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
308 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
309 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
314 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
316 if (!fb || !fb->parent)
319 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
320 ptr->freebrowserCB(action);
324 FL_OBJECT * create_input_box(void * parent, int type,
325 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
327 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
328 fl_set_object_boxtype(obj, FL_DOWN_BOX);
329 fl_set_object_resize(obj, FL_RESIZE_ALL);
330 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
331 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
332 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
334 // To intercept Up, Down, Table for history
335 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
336 obj->u_vdata = parent;
337 obj->wantkey = FL_KEY_TAB;
343 FL_FREEBROWSER * create_freebrowser(void * parent)
345 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
346 fb->want_printable = 1;
347 fb->callback = C_freebrowserCB;