4 * Copyright 1995-2002 the LyX Team
5 * Read the file COPYING
8 * \author Asger and Juergen
14 #pragma implementation
17 #include "frontends/xforms/DropDown.h"
18 #include "frontends/xforms/XFormsView.h"
19 #include "frontends/controllers/ControlCommandBuffer.h"
20 #include "frontends/Timeout.h"
22 #include "XMiniBuffer.h"
25 #include "bufferview_funcs.h"
27 #include <boost/bind.hpp>
31 #ifndef CXX_GLOBAL_CSTD
39 XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
40 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
41 : controller_(control), view_(v),
42 info_suffix_shown_(false)
44 input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
45 suffix_timer_.reset(new Timeout(1500));
46 idle_timer_.reset(new Timeout(6000));
47 suffix_timer_->timeout.connect(boost::bind(&XMiniBuffer::suffix_timeout, this));
48 idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
54 // thanks for nothing, xforms (recursive creation not allowed)
55 void XMiniBuffer::dd_init()
57 dropdown_.reset(new DropDown(the_buffer_));
58 dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
59 dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
63 XMiniBuffer::~XMiniBuffer()
68 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
69 int key, XEvent * /*xev*/)
72 if (dropdown_->isVisible()) {
73 return dropdown_->peek(xev);
87 if (info_suffix_shown_) {
88 suffix_timer_->stop();
92 char const * tmp = fl_get_input(ob);
93 input = tmp ? tmp : "";
101 string const h(controller_.historyDown());
103 show_info_suffix(_("[End of history]"), input);
115 string const h(controller_.historyUp());
117 show_info_suffix(_("[Beginning of history]"), input);
128 vector<string> comp = controller_.completions(input, new_input);
130 if (comp.empty() && new_input == input) {
131 show_info_suffix(_("[no match]"), input);
136 set_input(new_input);
137 show_info_suffix(("[only completion]"), new_input + " ");
141 set_input(new_input);
144 fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
147 // asynchronous completion
148 int const air = the_buffer_->x;
150 y += h - (the_buffer_->h + air);
152 dropdown_->select(comp, x, y, w);
166 // This will go in again in a little while
167 // we need to be able to declare what types
168 // of argumetns LFUN's should have first. (Lgb)
169 // First check for match
170 vector<string>::const_iterator cit =
171 find(completion_.begin(),
174 if (cit == completion_.end()) {
176 string const tmp = input + _(" [no match]");
177 show_info_suffix(tmp, input);
182 controller_.dispatch(input);
203 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
207 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
208 return mini->peek_event(ob, event, key,
209 static_cast<XEvent *>(xev));
214 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
215 FL_Coord w, FL_Coord h)
219 the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
220 fl_set_object_boxtype(obj, FL_DOWN_BOX);
221 fl_set_object_resize(obj, FL_RESIZE_ALL);
222 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
223 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
224 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
226 // To intercept Up, Down, Table for history
227 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
229 obj->wantkey = FL_KEY_TAB;
235 void XMiniBuffer::freeze()
237 // we must prevent peek_event, or we get an unfocus() when the
238 // containing form gets destroyed
239 fl_set_object_prehandler(input_obj_, 0);
243 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
245 stored_input_ = input;
246 info_suffix_shown_ = true;
247 set_input(input + " " + suffix);
248 suffix_timer_->start();
252 void XMiniBuffer::idle_timeout()
254 set_input(currentState(view_->view()));
258 void XMiniBuffer::suffix_timeout()
260 info_suffix_shown_ = false;
261 set_input(stored_input_);
265 bool XMiniBuffer::isEditingMode() const
267 return the_buffer_->focus;
271 void XMiniBuffer::messageMode(bool on)
275 fl_activate_object(the_buffer_);
276 fl_set_focus_object(view_->getForm(), the_buffer_);
280 if (isEditingMode()) {
281 // focus back to the workarea
282 fl_set_focus_object(view_->getForm(), 0);
283 idle_timer_->start();
289 void XMiniBuffer::redraw()
291 fl_redraw_object(the_buffer_);
296 void XMiniBuffer::append_char(char c)
298 if (!c || !isprint(c))
301 char const * tmp = fl_get_input(the_buffer_);
302 string str = tmp ? tmp : "";
306 fl_set_input(the_buffer_, str.c_str());
310 void XMiniBuffer::set_complete_input(string const & str)
313 // add a space so the user can type
314 // an argument immediately
315 set_input(str + " ");
320 void XMiniBuffer::message(string const & str)
322 if (!isEditingMode())
327 void XMiniBuffer::set_input(string const & str)
329 fl_set_input(the_buffer_, str.c_str());