3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Jürgen
9 * Full author contact details are available in file CREDITS.
14 #include "XMiniBuffer.h"
16 #include "freebrowser.h"
17 #include "xforms_helpers.h"
19 #include "controllers/ControlCommandBuffer.h"
21 #include "frontends/Timeout.h"
25 #include <boost/bind.hpp>
34 /// This creates the input widget for the minibuffer
35 FL_OBJECT * create_input_box(void * parent, int type,
36 FL_Coord, FL_Coord, FL_Coord, FL_Coord);
38 FL_FREEBROWSER * create_freebrowser(void * parent);
43 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
44 FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
45 : controller_(control),
48 input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
49 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
51 info_timer_.reset(new Timeout(1500));
52 idle_timer_.reset(new Timeout(6000));
53 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
54 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
60 // This is here so that scoped ptr will not require a complete type.
61 XMiniBuffer::~XMiniBuffer()
65 void XMiniBuffer::freebrowserCB(int action)
67 if (action < 0 || action > 1)
68 // unrecognized action
72 // The freebrowser has been hidden
75 if (freebrowser_->last_printable) {
76 // Append this char to the current input contents
77 string input = getString(input_);
78 input += freebrowser_->last_printable;
79 fl_set_input(input_, input.c_str());
82 // Fill the input widget with the selected
84 FL_OBJECT * browser = freebrowser_->browser;
85 string const str = getString(browser);
88 // add a space so the user can type
89 // an argument immediately
96 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
97 int key, XEvent * /*xev*/)
114 char const * tmp = fl_get_input(ob);
115 input = tmp ? tmp : "";
123 string const h(controller_.historyDown());
125 show_info(_("[End of history]"), input, false);
137 string const h(controller_.historyUp());
139 show_info(_("[Beginning of history]"), input, false);
150 vector<string> comp = controller_.completions(input, new_input);
152 if (comp.empty() && new_input == input) {
153 show_info(_("[no match]"), input);
158 set_input(new_input);
159 show_info(_("[only completion]"), new_input + ' ');
163 set_input(new_input);
165 // Fill freebrowser_'s browser with the list of
166 // available completions
167 FL_OBJECT * browser = freebrowser_->browser;
168 fl_clear_browser(browser);
169 vector<string>::const_iterator cit = comp.begin();
170 vector<string>::const_iterator end = comp.end();
171 for (; cit != end; ++cit) {
172 fl_add_browser_line(browser, cit->c_str());
174 fl_select_browser_line(browser, 1);
176 // Set the position of the freebrowser and display it.
178 fl_get_wingeometry(fl_get_real_object_window(input_),
181 // asynchronous completion
182 int const air = input_->x;
184 y += h - (input_->h + air);
188 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
203 controller_.dispatch(input);
218 void XMiniBuffer::freeze()
220 // we must prevent peek_event, or we get an unfocus() when the
221 // containing form gets destroyed
222 fl_set_object_prehandler(input_, 0);
226 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
228 stored_input_ = input;
231 set_input(input + ' ' + info);
234 info_timer_->start();
238 void XMiniBuffer::idle_timeout()
240 set_input(controller_.getCurrentState());
244 void XMiniBuffer::info_timeout()
247 set_input(stored_input_);
251 bool XMiniBuffer::isEditingMode() const
253 return input_->focus;
257 void XMiniBuffer::messageMode(bool on)
261 fl_activate_object(input_);
262 fl_set_focus_object(input_->form, input_);
266 if (isEditingMode()) {
267 // focus back to the workarea
268 fl_set_focus_object(input_->form, 0);
269 idle_timer_->start();
275 void XMiniBuffer::redraw()
277 fl_redraw_object(input_);
282 void XMiniBuffer::message(string const & str)
284 if (!isEditingMode())
289 void XMiniBuffer::set_input(string const & str)
291 fl_set_input(input_, str.c_str());
298 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
302 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
303 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
308 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
310 if (!fb || !fb->parent)
313 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
314 ptr->freebrowserCB(action);
318 FL_OBJECT * create_input_box(void * parent, int type,
319 FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
321 FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
322 fl_set_object_boxtype(obj, FL_DOWN_BOX);
323 fl_set_object_resize(obj, FL_RESIZE_ALL);
324 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
325 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
326 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
328 // To intercept Up, Down, Table for history
329 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
330 obj->u_vdata = parent;
331 obj->wantkey = FL_KEY_TAB;
337 FL_FREEBROWSER * create_freebrowser(void * parent)
339 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
340 fb->want_printable = 1;
341 fb->callback = C_freebrowserCB;