1 /* ###########################################################################
4 * read minibuffer.h for more
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2001 The LyX Team.
10 * ###########################################################################
18 #pragma implementation
22 #include "frontends/xforms/DropDown.h"
23 #include "frontends/xforms/XFormsView.h"
25 #include "minibuffer.h"
27 #include "support/lyxalgo.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "frontends/LyXView.h"
32 #include "LyXAction.h"
33 #include "BufferView.h"
34 #include "frontends/Timeout.h"
36 #include <boost/bind.hpp>
40 #ifndef CXX_GLOBAL_CSTD
45 using std::back_inserter;
48 extern LyXAction lyxaction;
55 prefix(string const & s)
57 bool operator()(string const & s) const {
58 return prefixIs(s, p);
62 } // end of anon namespace
65 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
66 FL_Coord h, FL_Coord w)
67 : stored_(false), owner_(o), state_(spaces)
69 add(FL_NORMAL_INPUT, x, y, h, w);
71 timer = new Timeout(6000);
72 timer->timeout.connect(boost::bind(&MiniBuffer::init, this));
74 stored_timer = new Timeout(1500);
75 stored_timer->timeout.connect(boost::bind(&MiniBuffer::stored_slot, this));
80 // thanks for nothing, xforms (recursive creation not allowed)
81 void MiniBuffer::dd_init()
83 dropdown_ = new DropDown(owner_, the_buffer);
84 dropdown_->result.connect(boost::bind(&MiniBuffer::set_complete_input, this, _1));
85 dropdown_->keypress.connect(boost::bind(&MiniBuffer::append_char, this, _1));
89 MiniBuffer::~MiniBuffer()
97 void MiniBuffer::stored_slot()
101 set_input(stored_input);
106 void MiniBuffer::stored_set(string const & str)
110 stored_timer->start();
114 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
122 char const * tmp = fl_get_input(ob);
123 string input = tmp ? tmp : "";
125 stored_timer->stop();
126 input = stored_input;
133 if (hist_iter != history_->end()) {
136 if (hist_iter == history_->end()) {
137 // no further history
139 set_input(_("[End of history]"));
141 set_input((*hist_iter));
145 if (hist_iter == history_->begin()) {
146 // no further history
148 set_input(_("[Beginning of history]"));
151 set_input((*hist_iter));
157 // Completion handling.
160 lyx::copy_if(completion_.begin(),
162 back_inserter(comp), prefix(input));
166 string const tmp = input + _(" [no match]");
169 } else if (comp.size() == 1) {
172 comp[0] + _(" [sole completion]");
173 stored_set(comp[0] + " ");
176 // More that one match
177 // Find maximal avaliable prefix
178 string const tmp = comp[0];
180 if (tmp.length() > test.length())
181 test += tmp[test.length()];
182 while (test.length() < tmp.length()) {
184 lyx::copy_if(comp.begin(),
188 if (vtmp.size() != comp.size()) {
189 test.erase(test.length() - 1);
192 test += tmp[test.length()];
197 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
200 // asynchronous completion
201 int const air = the_buffer->x;
203 y += h - (the_buffer->h + air);
205 dropdown_->select(comp, x, y, w);
212 owner_->view()->focus(true);
221 // This will go in again in a little while
222 // we need to be able to declare what types
223 // of argumetns LFUN's should have first. (Lgb)
224 // First check for match
225 vector<string>::const_iterator cit =
226 find(completion_.begin(),
229 if (cit == completion_.end()) {
232 string const tmp = input + _(" [no match]");
236 // Return the inputted string
238 owner_->view()->focus(true);
239 if (!input.empty()) {
240 history_->push_back(input);
250 // Depending on the input state spaces might not
258 string const tmp = input + _(" [no match]");
271 //lyxerr << "Unhandled minibuffer event!" << endl;
282 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
284 int key, void * /*xev*/)
286 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
287 return mini->peek_event(ob, event, key);
293 void MiniBuffer::prepare()
298 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
303 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
304 FL_Coord w, FL_Coord h)
308 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
309 fl_set_object_boxtype(obj, FL_DOWN_BOX);
310 fl_set_object_resize(obj, FL_RESIZE_ALL);
311 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
312 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
313 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
315 // To intercept Up, Down, Table for history
316 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
318 obj->wantkey = FL_KEY_TAB;
326 void MiniBuffer::message(string const & str)
329 string const ntext = strip(str);
330 if (!the_buffer->focus) {
337 void MiniBuffer::messagePush(string const & str)
344 void MiniBuffer::messagePop()
346 if (!text_stored.empty()) {
347 message(text_stored);
353 void MiniBuffer::addSet(string const & s1, string const & s2)
355 string const str = text + ' ' + s1 + ' ' + s2;
360 void MiniBuffer::getString(State spaces,
361 vector<string> const & completion,
362 vector<string> & history)
365 completion_ = completion;
367 hist_iter = history_->end();
372 void MiniBuffer::init()
374 // If we have focus, we don't want to change anything.
375 if (the_buffer->focus)
383 void MiniBuffer::activate()
385 fl_activate_object(the_buffer);
390 void MiniBuffer::deactivate()
393 fl_deactivate_object(the_buffer);
398 void MiniBuffer::redraw()
400 fl_redraw_object(the_buffer);
405 void MiniBuffer::set_complete_input(string const & str)
408 // add a space so the user can type
409 // an argument immediately
410 set_input(str + " ");
415 void MiniBuffer::append_char(char c)
417 if (!c || !isprint(c))
420 char const * tmp = fl_get_input(the_buffer);
421 string str = tmp ? tmp : "";
425 fl_set_input(the_buffer, str.c_str());
429 void MiniBuffer::set_input(string const & str)
431 fl_set_input(the_buffer, str.c_str());