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"
15 #include "XFormsView.h"
17 #include "freebrowser.h"
18 #include "xforms_helpers.h"
20 #include "controllers/ControlCommandBuffer.h"
22 #include "frontends/Timeout.h"
26 #include <boost/bind.hpp>
28 using boost::shared_ptr;
38 /// This creates the input widget for the minibuffer
39 FL_OBJECT * create_input_box(void * parent, int type);
41 FL_FREEBROWSER * create_freebrowser(void * parent);
46 XMiniBuffer::XMiniBuffer(XFormsView & owner,
47 ControlCommandBuffer & control)
48 : controller_(control),
51 input_ = create_input_box(this, FL_NORMAL_INPUT);
52 freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
54 // The minibuffer is 25 pixels high and is embedded inside a
55 // 2 pixel deep frame.
58 BoxList & boxlist = owner.getBox(XFormsView::Bottom)->children();
59 minibuffer_ = boxlist.push_back(Box(0,0));
60 shared_ptr<Box> center = embed(input_, minibuffer_->children(), widgets_, air);
61 center->set(Box::Expand);
62 center->setMinimumDimensions(0, 25);
64 owner.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
67 info_timer_.reset(new Timeout(1500));
68 idle_timer_.reset(new Timeout(6000));
69 info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
70 idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
76 // This is here so that scoped ptr will not require a complete type.
77 XMiniBuffer::~XMiniBuffer()
81 void XMiniBuffer::freebrowserCB(int action)
83 if (action < 0 || action > 1)
84 // unrecognized action
88 // The freebrowser has been hidden
91 if (freebrowser_->last_printable) {
92 // Append this char to the current input contents
93 string input = getString(input_);
94 input += freebrowser_->last_printable;
95 fl_set_input(input_, input.c_str());
98 // Fill the input widget with the selected
100 FL_OBJECT * browser = freebrowser_->browser;
101 string const str = getString(browser);
104 // add a space so the user can type
105 // an argument immediately
106 set_input(str + ' ');
112 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
113 int key, XEvent * /*xev*/)
130 char const * tmp = fl_get_input(ob);
131 input = tmp ? tmp : "";
139 string const h(controller_.historyDown());
141 show_info(_("[End of history]"), input, false);
153 string const h(controller_.historyUp());
155 show_info(_("[Beginning of history]"), input, false);
166 vector<string> comp = controller_.completions(input, new_input);
168 if (comp.empty() && new_input == input) {
169 show_info(_("[no match]"), input);
174 set_input(new_input);
175 show_info(_("[only completion]"), new_input + ' ');
179 set_input(new_input);
181 // Fill freebrowser_'s browser with the list of
182 // available completions
183 FL_OBJECT * browser = freebrowser_->browser;
184 fl_clear_browser(browser);
185 vector<string>::const_iterator cit = comp.begin();
186 vector<string>::const_iterator end = comp.end();
187 for (; cit != end; ++cit) {
188 fl_add_browser_line(browser, cit->c_str());
190 fl_select_browser_line(browser, 1);
192 // Set the position of the freebrowser and display it.
194 fl_get_wingeometry(fl_get_real_object_window(input_),
197 // asynchronous completion
198 int const air = input_->x;
200 y += h - (input_->h + air);
204 fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
219 controller_.dispatch(input);
234 void XMiniBuffer::freeze()
236 // we must prevent peek_event, or we get an unfocus() when the
237 // containing form gets destroyed
238 fl_set_object_prehandler(input_, 0);
242 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
244 stored_input_ = input;
247 set_input(input + ' ' + info);
250 info_timer_->start();
254 void XMiniBuffer::idle_timeout()
256 set_input(controller_.getCurrentState());
260 void XMiniBuffer::info_timeout()
263 set_input(stored_input_);
267 bool XMiniBuffer::isEditingMode() const
269 return input_->focus;
273 void XMiniBuffer::messageMode(bool on)
277 fl_activate_object(input_);
278 fl_set_focus_object(input_->form, input_);
282 if (isEditingMode()) {
283 // focus back to the workarea
284 fl_set_focus_object(input_->form, 0);
285 idle_timer_->start();
291 void XMiniBuffer::redraw()
293 fl_redraw_object(input_);
298 void XMiniBuffer::message(string const & str)
300 if (!isEditingMode())
305 void XMiniBuffer::set_input(string const & str)
307 fl_set_input(input_, str.c_str());
314 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
318 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
319 return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
324 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
326 if (!fb || !fb->parent)
329 XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
330 ptr->freebrowserCB(action);
334 FL_OBJECT * create_input_box(void * parent, int type)
336 FL_OBJECT * obj = fl_add_input(type, 0, 0, 0, 0, "");
337 fl_set_object_boxtype(obj, FL_DOWN_BOX);
338 fl_set_object_resize(obj, FL_RESIZE_ALL);
339 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
340 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
341 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
343 // To intercept Up, Down, Table for history
344 fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
345 obj->u_vdata = parent;
346 obj->wantkey = FL_KEY_TAB;
352 FL_FREEBROWSER * create_freebrowser(void * parent)
354 FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
355 fb->want_printable = 1;
356 fb->callback = C_freebrowserCB;
362 } // namespace frontend