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"
17 #include "ControlCommandBuffer.h"
21 #include "frontends/Timeout.h"
23 #include <boost/bind.hpp>
27 #ifndef CXX_GLOBAL_CSTD
37 /// This creates the input widget for the minibuffer
38 FL_OBJECT * create_input_box(void * parent, int type,
39 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
44 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
45 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
46 : controller_(control),
49 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
50 info_timer_.reset(new Timeout(1500));
51 idle_timer_.reset(new Timeout(6000));
52 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
53 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
59 // This is here so that scoped ptr will not require a complete type.
60 XMiniBuffer::~XMiniBuffer()
64 // thanks for nothing, xforms (recursive creation not allowed)
65 void XMiniBuffer::dd_init()
67 dropdown_.reset(new DropDown(input_));
68 result_con = dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
69 keypress_con = dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
73 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
74 int key, XEvent * /*xev*/)
91 char const * tmp = fl_get_input(ob);
92 input = tmp ? tmp : "";
100 string const h(controller_.historyDown());
102 show_info(_("[End of history]"), input, false);
114 string const h(controller_.historyUp());
116 show_info(_("[Beginning of history]"), input, false);
127 vector<string> comp = controller_.completions(input, new_input);
129 if (comp.empty() && new_input == input) {
130 show_info(_("[no match]"), input);
135 set_input(new_input);
136 show_info(_("[only completion]"), new_input + ' ');
140 set_input(new_input);
143 fl_get_wingeometry(fl_get_real_object_window(input_),
146 // asynchronous completion
147 int const air = input_->x;
149 y += h - (input_->h + air);
151 dropdown_->select(comp, x, y, w);
166 controller_.dispatch(input);
181 void XMiniBuffer::freeze()
183 // we must prevent peek_event, or we get an unfocus() when the
184 // containing form gets destroyed
185 fl_set_object_prehandler(input_, 0);
189 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
191 stored_input_ = input;
194 set_input(input + ' ' + info);
197 info_timer_->start();
201 void XMiniBuffer::idle_timeout()
203 set_input(controller_.getCurrentState());
207 void XMiniBuffer::info_timeout()
210 set_input(stored_input_);
214 bool XMiniBuffer::isEditingMode() const
216 return input_->focus;
220 void XMiniBuffer::messageMode(bool on)
224 fl_activate_object(input_);
225 fl_set_focus_object(input_->form, input_);
229 if (isEditingMode()) {
230 // focus back to the workarea
231 fl_set_focus_object(input_->form, 0);
232 idle_timer_->start();
238 void XMiniBuffer::redraw()
240 fl_redraw_object(input_);
245 void XMiniBuffer::append_char(char c)
247 if (!c || !isprint(c))
250 char const * tmp = fl_get_input(input_);
251 string str = tmp ? tmp : "";
255 fl_set_input(input_, str.c_str());
259 void XMiniBuffer::set_complete_input(string const & str)
262 // add a space so the user can type
263 // an argument immediately
264 set_input(str + ' ');
269 void XMiniBuffer::message(string const & str)
271 if (!isEditingMode())
276 void XMiniBuffer::set_input(string const & str)
278 fl_set_input(input_, str.c_str());
285 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
289 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
290 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
294 FL_OBJECT * create_input_box(void * parent, int type,
295 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
297 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
298 fl_set_object_boxtype(obj, FL_DOWN_BOX);
299 fl_set_object_resize(obj, FL_RESIZE_ALL);
300 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
301 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
302 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
304 // To intercept Up, Down, Table for history
305 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
306 obj->u_vdata = parent;
307 obj->wantkey = FL_KEY_TAB;