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
15 #include "XMiniBuffer.h"
16 #include "ControlCommandBuffer.h"
17 #include "freebrowser.h"
18 #include "xforms_helpers.h"
22 #include "frontends/Timeout.h"
24 #include "support/lstrings.h"
26 #include <boost/bind.hpp>
30 #ifndef CXX_GLOBAL_CSTD
40 /// This creates the input widget for the minibuffer
41 FL_OBJECT * create_input_box(void * parent, int type,
42 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
44 FL_FREEBROWSER * create_freebrowser(void * parent);
46 FL_OBJECT * get_freebrowser_browser(boost::shared_ptr<FL_FREEBROWSER> &);
51 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
52 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
53 : controller_(control),
56 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
57 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
59 info_timer_.reset(new Timeout(1500));
60 idle_timer_.reset(new Timeout(6000));
61 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
62 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
68 // This is here so that scoped ptr will not require a complete type.
69 XMiniBuffer::~XMiniBuffer()
73 void XMiniBuffer::freebrowserCB(int action)
75 if (action < 0 || action > 1)
76 // unrecognized action
80 // The freebrowser has been hidden
83 if (freebrowser_->last_printable) {
84 // Append this char to the current input contents
85 string input = getString(input_);
86 input += freebrowser_->last_printable;
87 fl_set_input(input_, input.c_str());
90 // Fill the input widget with the selected
92 FL_OBJECT * browser = get_freebrowser_browser(freebrowser_);
93 string const str = getString(browser);
96 // add a space so the user can type
97 // an argument immediately
104 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
105 int key, XEvent * /*xev*/)
122 char const * tmp = fl_get_input(ob);
123 input = tmp ? tmp : "";
131 string const h(controller_.historyDown());
133 show_info(_("[End of history]"), input, false);
145 string const h(controller_.historyUp());
147 show_info(_("[Beginning of history]"), input, false);
158 vector<string> comp = controller_.completions(input, new_input);
160 if (comp.empty() && new_input == input) {
161 show_info(_("[no match]"), input);
166 set_input(new_input);
167 show_info(_("[only completion]"), new_input + ' ');
171 set_input(new_input);
173 // Fill freebrowser_'s browser with the list of
174 // available completions
175 FL_OBJECT * browser =
176 get_freebrowser_browser(freebrowser_);
177 fl_clear_browser(browser);
178 vector<string>::const_iterator cit = comp.begin();
179 vector<string>::const_iterator end = comp.end();
180 for (; cit != end; ++cit) {
181 fl_add_browser_line(browser, cit->c_str());
183 fl_select_browser_line(browser, 1);
185 // Set the position of the freebrowser and display it.
187 fl_get_wingeometry(fl_get_real_object_window(input_),
190 // asynchronous completion
191 int const air = input_->x;
193 y += h - (input_->h + air);
197 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
212 controller_.dispatch(input);
227 void XMiniBuffer::freeze()
229 // we must prevent peek_event, or we get an unfocus() when the
230 // containing form gets destroyed
231 fl_set_object_prehandler(input_, 0);
235 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
237 stored_input_ = input;
240 set_input(input + ' ' + info);
243 info_timer_->start();
247 void XMiniBuffer::idle_timeout()
249 set_input(controller_.getCurrentState());
253 void XMiniBuffer::info_timeout()
256 set_input(stored_input_);
260 bool XMiniBuffer::isEditingMode() const
262 return input_->focus;
266 void XMiniBuffer::messageMode(bool on)
270 fl_activate_object(input_);
271 fl_set_focus_object(input_->form, input_);
275 if (isEditingMode()) {
276 // focus back to the workarea
277 fl_set_focus_object(input_->form, 0);
278 idle_timer_->start();
284 void XMiniBuffer::redraw()
286 fl_redraw_object(input_);
291 void XMiniBuffer::message(string const & str)
293 if (!isEditingMode())
298 void XMiniBuffer::set_input(string const & str)
300 fl_set_input(input_, str.c_str());
307 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
311 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
312 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
317 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
319 if (!fb || !fb->parent)
322 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
323 ptr->freebrowserCB(action);
327 FL_OBJECT * create_input_box(void * parent, int type,
328 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
330 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
331 fl_set_object_boxtype(obj, FL_DOWN_BOX);
332 fl_set_object_resize(obj, FL_RESIZE_ALL);
333 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
334 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
335 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
337 // To intercept Up, Down, Table for history
338 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
339 obj->u_vdata = parent;
340 obj->wantkey = FL_KEY_TAB;
346 FL_FREEBROWSER * create_freebrowser(void * parent)
348 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
349 fb->want_printable = 1;
350 fb->callback = C_freebrowserCB;
354 FL_OBJECT * get_freebrowser_browser(boost::shared_ptr<FL_FREEBROWSER> & fb)
356 FL_FREEBROWSER * ptr = fb.get();
357 return ptr ? fl_get_freebrowser_browser(ptr) : 0;