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"
24 #include "minibuffer.h"
26 #include "support/lyxalgo.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
30 #include "XFormsView.h"
32 #include "LyXAction.h"
33 #include "BufferView.h"
34 #include "frontends/Timeout.h"
40 using std::back_inserter;
43 extern LyXAction lyxaction;
50 prefix(string const & s)
52 bool operator()(string const & s) const {
53 return prefixIs(s, p);
57 } // end of anon namespace
60 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
61 FL_Coord h, FL_Coord w)
62 : stored_(false), owner_(o), state_(spaces)
64 add(FL_NORMAL_INPUT, x, y, h, w);
66 timer = new Timeout(6000);
67 timer->timeout.connect(slot(this, &MiniBuffer::init));
69 stored_timer = new Timeout(1500);
70 stored_timer->timeout.connect(slot(this, &MiniBuffer::stored_slot));
75 // thanks for nothing, xforms (recursive creation not allowed)
76 void MiniBuffer::dd_init()
78 dropdown_ = new DropDown(owner_, the_buffer);
79 dropdown_->result.connect(slot(this, &MiniBuffer::set_complete_input));
80 dropdown_->keypress.connect(slot(this, &MiniBuffer::append_char));
84 MiniBuffer::~MiniBuffer()
92 void MiniBuffer::stored_slot()
96 set_input(stored_input);
101 void MiniBuffer::stored_set(string const & str)
105 stored_timer->start();
109 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
117 char const * tmp = fl_get_input(ob);
118 string input = tmp ? tmp : "";
120 stored_timer->stop();
121 input = stored_input;
128 if (hist_iter != history_->end()) {
131 if (hist_iter == history_->end()) {
132 // no further history
134 set_input(_("[End of history]"));
136 set_input((*hist_iter));
140 if (hist_iter == history_->begin()) {
141 // no further history
143 set_input(_("[Beginning of history]"));
146 set_input((*hist_iter));
152 // Completion handling.
155 lyx::copy_if(completion_.begin(),
157 back_inserter(comp), prefix(input));
161 string const tmp = input + _(" [no match]");
164 } else if (comp.size() == 1) {
167 comp[0] + _(" [sole completion]");
168 stored_set(comp[0] + " ");
171 // More that one match
172 // Find maximal avaliable prefix
173 string const tmp = comp[0];
175 if (tmp.length() > test.length())
176 test += tmp[test.length()];
177 while (test.length() < tmp.length()) {
179 lyx::copy_if(comp.begin(),
183 if (vtmp.size() != comp.size()) {
184 test.erase(test.length() - 1);
187 test += tmp[test.length()];
192 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
195 // asynchronous completion
196 int const air = the_buffer->x;
198 y += h - (the_buffer->h + air);
200 dropdown_->select(comp, x, y, w);
207 owner_->view()->focus(true);
216 // This will go in again in a little while
217 // we need to be able to declare what types
218 // of argumetns LFUN's should have first. (Lgb)
219 // First check for match
220 vector<string>::const_iterator cit =
221 find(completion_.begin(),
224 if (cit == completion_.end()) {
227 string const tmp = input + _(" [no match]");
231 // Return the inputted string
233 owner_->view()->focus(true);
234 if (!input.empty()) {
235 history_->push_back(input);
237 stringReady.emit(input);
245 // Depending on the input state spaces might not
253 string const tmp = input + _(" [no match]");
266 //lyxerr << "Unhandled minibuffer event!" << endl;
277 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
279 int key, void * /*xev*/)
281 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
282 return mini->peek_event(ob, event, key);
288 void MiniBuffer::prepare()
293 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
298 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
299 FL_Coord w, FL_Coord h)
303 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
304 fl_set_object_boxtype(obj, FL_DOWN_BOX);
305 fl_set_object_resize(obj, FL_RESIZE_ALL);
306 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
307 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
308 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
310 // To intercept Up, Down, Table for history
311 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
313 obj->wantkey = FL_KEY_TAB;
321 void MiniBuffer::message(string const & str)
324 string const ntext = strip(str);
325 if (!the_buffer->focus) {
332 void MiniBuffer::messagePush(string const & str)
339 void MiniBuffer::messagePop()
341 if (!text_stored.empty()) {
342 message(text_stored);
348 void MiniBuffer::addSet(string const & s1, string const & s2)
350 string const str = text + ' ' + s1 + ' ' + s2;
355 void MiniBuffer::getString(State spaces,
356 vector<string> const & completion,
357 vector<string> & history)
360 completion_ = completion;
362 hist_iter = history_->end();
367 void MiniBuffer::init()
369 // If we have focus, we don't want to change anything.
370 if (the_buffer->focus)
378 void MiniBuffer::activate()
380 fl_activate_object(the_buffer);
385 void MiniBuffer::deactivate()
388 fl_deactivate_object(the_buffer);
393 void MiniBuffer::redraw()
395 fl_redraw_object(the_buffer);
400 void MiniBuffer::set_complete_input(string const & str)
403 // add a space so the user can type
404 // an argument immediately
405 set_input(str + " ");
410 void MiniBuffer::append_char(char c)
412 if (!c || !isprint(c))
415 char const * tmp = fl_get_input(the_buffer);
416 string str = tmp ? tmp : "";
420 fl_set_input(the_buffer, str.c_str());
424 void MiniBuffer::set_input(string const & str)
426 fl_set_input(the_buffer, str.c_str());