4 * Read the file COPYING
7 * \author Asger and Juergen
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "frontends/xforms/DropDown.h"
19 #include "frontends/xforms/XFormsView.h"
20 #include "frontends/controllers/ControlCommandBuffer.h"
21 #include "frontends/Timeout.h"
23 #include "XMiniBuffer.h"
26 #include "bufferview_funcs.h"
28 #include <boost/bind.hpp>
32 #ifndef CXX_GLOBAL_CSTD
40 XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
41 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
42 : controller_(control), view_(v),
45 input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
46 info_timer_.reset(new Timeout(1500));
47 idle_timer_.reset(new Timeout(6000));
48 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
49 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
55 // This is here so that scoped ptr will not require a complete type.
56 XMiniBuffer::~XMiniBuffer()
60 // thanks for nothing, xforms (recursive creation not allowed)
61 void XMiniBuffer::dd_init()
63 dropdown_.reset(new DropDown(the_buffer_));
64 result_con = dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
65 keypress_con = dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
69 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
70 int key, XEvent * /*xev*/)
87 char const * tmp = fl_get_input(ob);
88 input = tmp ? tmp : "";
96 string const h(controller_.historyDown());
98 show_info(_("[End of history]"), input, false);
110 string const h(controller_.historyUp());
112 show_info(_("[Beginning of history]"), input, false);
123 vector<string> comp = controller_.completions(input, new_input);
125 if (comp.empty() && new_input == input) {
126 show_info(_("[no match]"), input);
131 set_input(new_input);
132 show_info(("[only completion]"), new_input + " ");
136 set_input(new_input);
139 fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
142 // asynchronous completion
143 int const air = the_buffer_->x;
145 y += h - (the_buffer_->h + air);
147 dropdown_->select(comp, x, y, w);
162 controller_.dispatch(input);
180 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
184 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
185 return mini->peek_event(ob, event, key,
186 static_cast<XEvent *>(xev));
191 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
192 FL_Coord w, FL_Coord h)
196 the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
197 fl_set_object_boxtype(obj, FL_DOWN_BOX);
198 fl_set_object_resize(obj, FL_RESIZE_ALL);
199 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
200 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
201 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
203 // To intercept Up, Down, Table for history
204 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
206 obj->wantkey = FL_KEY_TAB;
212 void XMiniBuffer::freeze()
214 // we must prevent peek_event, or we get an unfocus() when the
215 // containing form gets destroyed
216 fl_set_object_prehandler(input_obj_, 0);
220 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
222 stored_input_ = input;
225 set_input(input + " " + info);
228 info_timer_->start();
232 void XMiniBuffer::idle_timeout()
234 set_input(currentState(view_->view().get()));
238 void XMiniBuffer::info_timeout()
241 set_input(stored_input_);
245 bool XMiniBuffer::isEditingMode() const
247 return the_buffer_->focus;
251 void XMiniBuffer::messageMode(bool on)
255 fl_activate_object(the_buffer_);
256 fl_set_focus_object(view_->getForm(), the_buffer_);
260 if (isEditingMode()) {
261 // focus back to the workarea
262 fl_set_focus_object(view_->getForm(), 0);
263 idle_timer_->start();
269 void XMiniBuffer::redraw()
271 fl_redraw_object(the_buffer_);
276 void XMiniBuffer::append_char(char c)
278 if (!c || !isprint(c))
281 char const * tmp = fl_get_input(the_buffer_);
282 string str = tmp ? tmp : "";
286 fl_set_input(the_buffer_, str.c_str());
290 void XMiniBuffer::set_complete_input(string const & str)
293 // add a space so the user can type
294 // an argument immediately
295 set_input(str + " ");
300 void XMiniBuffer::message(string const & str)
302 if (!isEditingMode())
307 void XMiniBuffer::set_input(string const & str)
309 fl_set_input(the_buffer_, str.c_str());