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 #pragma implementation
19 #include "frontends/xforms/DropDown.h"
20 #include "frontends/xforms/XFormsView.h"
21 #include "frontends/controllers/ControlCommandBuffer.h"
22 #include "frontends/Timeout.h"
24 #include "XMiniBuffer.h"
27 #include "bufferview_funcs.h"
29 #include <boost/bind.hpp>
33 #ifndef CXX_GLOBAL_CSTD
41 XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
42 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
43 : controller_(control), view_(v),
46 input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
47 info_timer_.reset(new Timeout(1500));
48 idle_timer_.reset(new Timeout(6000));
49 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
50 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
56 // This is here so that scoped ptr will not require a complete type.
57 XMiniBuffer::~XMiniBuffer()
61 // thanks for nothing, xforms (recursive creation not allowed)
62 void XMiniBuffer::dd_init()
64 dropdown_.reset(new DropDown(the_buffer_));
65 result_con = dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
66 keypress_con = dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
70 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
71 int key, XEvent * /*xev*/)
88 char const * tmp = fl_get_input(ob);
89 input = tmp ? tmp : "";
97 string const h(controller_.historyDown());
99 show_info(_("[End of history]"), input, false);
111 string const h(controller_.historyUp());
113 show_info(_("[Beginning of history]"), input, false);
124 vector<string> comp = controller_.completions(input, new_input);
126 if (comp.empty() && new_input == input) {
127 show_info(_("[no match]"), input);
132 set_input(new_input);
133 show_info(_("[only completion]"), new_input + ' ');
137 set_input(new_input);
140 fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
143 // asynchronous completion
144 int const air = the_buffer_->x;
146 y += h - (the_buffer_->h + air);
148 dropdown_->select(comp, x, y, w);
163 controller_.dispatch(input);
181 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
185 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
186 return mini->peek_event(ob, event, key,
187 static_cast<XEvent *>(xev));
192 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
193 FL_Coord w, FL_Coord h)
197 the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
198 fl_set_object_boxtype(obj, FL_DOWN_BOX);
199 fl_set_object_resize(obj, FL_RESIZE_ALL);
200 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
201 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
202 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
204 // To intercept Up, Down, Table for history
205 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
207 obj->wantkey = FL_KEY_TAB;
213 void XMiniBuffer::freeze()
215 // we must prevent peek_event, or we get an unfocus() when the
216 // containing form gets destroyed
217 fl_set_object_prehandler(input_obj_, 0);
221 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
223 stored_input_ = input;
226 set_input(input + ' ' + info);
229 info_timer_->start();
233 void XMiniBuffer::idle_timeout()
235 set_input(currentState(view_->view().get()));
239 void XMiniBuffer::info_timeout()
242 set_input(stored_input_);
246 bool XMiniBuffer::isEditingMode() const
248 return the_buffer_->focus;
252 void XMiniBuffer::messageMode(bool on)
256 fl_activate_object(the_buffer_);
257 fl_set_focus_object(view_->getForm(), the_buffer_);
261 if (isEditingMode()) {
262 // focus back to the workarea
263 fl_set_focus_object(view_->getForm(), 0);
264 idle_timer_->start();
270 void XMiniBuffer::redraw()
272 fl_redraw_object(the_buffer_);
277 void XMiniBuffer::append_char(char c)
279 if (!c || !isprint(c))
282 char const * tmp = fl_get_input(the_buffer_);
283 string str = tmp ? tmp : "";
287 fl_set_input(the_buffer_, str.c_str());
291 void XMiniBuffer::set_complete_input(string const & str)
294 // add a space so the user can type
295 // an argument immediately
296 set_input(str + ' ');
301 void XMiniBuffer::message(string const & str)
303 if (!isEditingMode())
308 void XMiniBuffer::set_input(string const & str)
310 fl_set_input(the_buffer_, str.c_str());