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),
44 input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
45 info_timer_.reset(new Timeout(1500));
46 idle_timer_.reset(new Timeout(6000));
47 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
48 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
54 // This is here so that scoped ptr will not require a complete type.
55 XMiniBuffer::~XMiniBuffer()
59 // thanks for nothing, xforms (recursive creation not allowed)
60 void XMiniBuffer::dd_init()
62 dropdown_.reset(new DropDown(the_buffer_));
63 result_con = dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
64 keypress_con = dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
68 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
69 int key, XEvent * /*xev*/)
86 char const * tmp = fl_get_input(ob);
87 input = tmp ? tmp : "";
95 string const h(controller_.historyDown());
97 show_info(_("[End of history]"), input, false);
109 string const h(controller_.historyUp());
111 show_info(_("[Beginning of history]"), input, false);
122 vector<string> comp = controller_.completions(input, new_input);
124 if (comp.empty() && new_input == input) {
125 show_info(_("[no match]"), input);
130 set_input(new_input);
131 show_info(("[only completion]"), new_input + " ");
135 set_input(new_input);
138 fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
141 // asynchronous completion
142 int const air = the_buffer_->x;
144 y += h - (the_buffer_->h + air);
146 dropdown_->select(comp, x, y, w);
161 controller_.dispatch(input);
179 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
183 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
184 return mini->peek_event(ob, event, key,
185 static_cast<XEvent *>(xev));
190 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
191 FL_Coord w, FL_Coord h)
195 the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
196 fl_set_object_boxtype(obj, FL_DOWN_BOX);
197 fl_set_object_resize(obj, FL_RESIZE_ALL);
198 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
199 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
200 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
202 // To intercept Up, Down, Table for history
203 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
205 obj->wantkey = FL_KEY_TAB;
211 void XMiniBuffer::freeze()
213 // we must prevent peek_event, or we get an unfocus() when the
214 // containing form gets destroyed
215 fl_set_object_prehandler(input_obj_, 0);
219 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
221 stored_input_ = input;
224 set_input(input + " " + info);
227 info_timer_->start();
231 void XMiniBuffer::idle_timeout()
233 set_input(currentState(view_->view()));
237 void XMiniBuffer::info_timeout()
240 set_input(stored_input_);
244 bool XMiniBuffer::isEditingMode() const
246 return the_buffer_->focus;
250 void XMiniBuffer::messageMode(bool on)
254 fl_activate_object(the_buffer_);
255 fl_set_focus_object(view_->getForm(), the_buffer_);
259 if (isEditingMode()) {
260 // focus back to the workarea
261 fl_set_focus_object(view_->getForm(), 0);
262 idle_timer_->start();
268 void XMiniBuffer::redraw()
270 fl_redraw_object(the_buffer_);
275 void XMiniBuffer::append_char(char c)
277 if (!c || !isprint(c))
280 char const * tmp = fl_get_input(the_buffer_);
281 string str = tmp ? tmp : "";
285 fl_set_input(the_buffer_, str.c_str());
289 void XMiniBuffer::set_complete_input(string const & str)
292 // add a space so the user can type
293 // an argument immediately
294 set_input(str + " ");
299 void XMiniBuffer::message(string const & str)
301 if (!isEditingMode())
306 void XMiniBuffer::set_input(string const & str)
308 fl_set_input(the_buffer_, str.c_str());