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
16 #include "frontends/xforms/DropDown.h"
17 #include "frontends/xforms/XFormsView.h"
18 #include "frontends/controllers/ControlCommandBuffer.h"
19 #include "frontends/Timeout.h"
21 #include "XMiniBuffer.h"
24 #include "bufferview_funcs.h"
26 #include <boost/bind.hpp>
30 #ifndef CXX_GLOBAL_CSTD
38 XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
39 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
40 : controller_(control), view_(v),
43 input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
44 info_timer_.reset(new Timeout(1500));
45 idle_timer_.reset(new Timeout(6000));
46 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
47 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
53 // This is here so that scoped ptr will not require a complete type.
54 XMiniBuffer::~XMiniBuffer()
58 // thanks for nothing, xforms (recursive creation not allowed)
59 void XMiniBuffer::dd_init()
61 dropdown_.reset(new DropDown(the_buffer_));
62 result_con = dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
63 keypress_con = dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
67 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
68 int key, XEvent * /*xev*/)
85 char const * tmp = fl_get_input(ob);
86 input = tmp ? tmp : "";
94 string const h(controller_.historyDown());
96 show_info(_("[End of history]"), input, false);
108 string const h(controller_.historyUp());
110 show_info(_("[Beginning of history]"), input, false);
121 vector<string> comp = controller_.completions(input, new_input);
123 if (comp.empty() && new_input == input) {
124 show_info(_("[no match]"), input);
129 set_input(new_input);
130 show_info(_("[only completion]"), new_input + ' ');
134 set_input(new_input);
137 fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
140 // asynchronous completion
141 int const air = the_buffer_->x;
143 y += h - (the_buffer_->h + air);
145 dropdown_->select(comp, x, y, w);
160 controller_.dispatch(input);
178 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
182 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
183 return mini->peek_event(ob, event, key,
184 static_cast<XEvent *>(xev));
189 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
190 FL_Coord w, FL_Coord h)
194 the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
195 fl_set_object_boxtype(obj, FL_DOWN_BOX);
196 fl_set_object_resize(obj, FL_RESIZE_ALL);
197 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
198 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
199 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
201 // To intercept Up, Down, Table for history
202 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
204 obj->wantkey = FL_KEY_TAB;
210 void XMiniBuffer::freeze()
212 // we must prevent peek_event, or we get an unfocus() when the
213 // containing form gets destroyed
214 fl_set_object_prehandler(input_obj_, 0);
218 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
220 stored_input_ = input;
223 set_input(input + ' ' + info);
226 info_timer_->start();
230 void XMiniBuffer::idle_timeout()
232 set_input(currentState(view_->view().get()));
236 void XMiniBuffer::info_timeout()
239 set_input(stored_input_);
243 bool XMiniBuffer::isEditingMode() const
245 return the_buffer_->focus;
249 void XMiniBuffer::messageMode(bool on)
253 fl_activate_object(the_buffer_);
254 fl_set_focus_object(view_->getForm(), the_buffer_);
258 if (isEditingMode()) {
259 // focus back to the workarea
260 fl_set_focus_object(view_->getForm(), 0);
261 idle_timer_->start();
267 void XMiniBuffer::redraw()
269 fl_redraw_object(the_buffer_);
274 void XMiniBuffer::append_char(char c)
276 if (!c || !isprint(c))
279 char const * tmp = fl_get_input(the_buffer_);
280 string str = tmp ? tmp : "";
284 fl_set_input(the_buffer_, str.c_str());
288 void XMiniBuffer::set_complete_input(string const & str)
291 // add a space so the user can type
292 // an argument immediately
293 set_input(str + ' ');
298 void XMiniBuffer::message(string const & str)
300 if (!isEditingMode())
305 void XMiniBuffer::set_input(string const & str)
307 fl_set_input(the_buffer_, str.c_str());