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"
39 extern LyXAction lyxaction;
46 prefix(string const & s)
48 bool operator()(string const & s) const {
49 return prefixIs(s, p);
53 } // end of anon namespace
56 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
57 FL_Coord h, FL_Coord w)
58 : stored_(false), owner_(o), state_(spaces)
60 add(FL_NORMAL_INPUT, x, y, h, w);
61 timer.setTimeout(6000);
62 timer.timeout.connect(slot(this, &MiniBuffer::init));
63 stored_timer.setTimeout(1500);
64 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
69 // thanks for nothing, xforms (recursive creation not allowed)
70 void MiniBuffer::dd_init()
72 dropdown_ = new DropDown(owner_, the_buffer);
73 dropdown_->result.connect(slot(this, &MiniBuffer::set_complete_input));
74 dropdown_->keypress.connect(slot(this, &MiniBuffer::append_char));
78 MiniBuffer::~MiniBuffer()
84 void MiniBuffer::stored_slot()
88 set_input(stored_input);
93 void MiniBuffer::stored_set(string const & str)
101 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
109 char const * tmp = fl_get_input(ob);
110 string input = tmp ? tmp : "";
113 input = stored_input;
120 if (hist_iter != history_->end()) {
123 if (hist_iter == history_->end()) {
124 // no further history
126 set_input(_("[End of history]"));
128 set_input((*hist_iter));
132 if (hist_iter == history_->begin()) {
133 // no further history
135 set_input(_("[Beginning of history]"));
138 set_input((*hist_iter));
144 // Completion handling.
147 lyx::copy_if(completion_.begin(),
149 std::back_inserter(comp), prefix(input));
153 string const tmp = input + _(" [no match]");
156 } else if (comp.size() == 1) {
159 comp[0] + _(" [sole completion]");
160 stored_set(comp[0] + " ");
163 // More that one match
164 // Find maximal avaliable prefix
165 string const tmp = comp[0];
167 if (tmp.length() > test.length())
168 test += tmp[test.length()];
169 while (test.length() < tmp.length()) {
171 lyx::copy_if(comp.begin(),
173 std::back_inserter(vtmp),
175 if (vtmp.size() != comp.size()) {
176 test.erase(test.length() - 1);
179 test += tmp[test.length()];
184 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
187 // asynchronous completion
188 int const air = the_buffer->x;
190 y += h - (the_buffer->h + air);
192 dropdown_->select(comp, x, y, w);
199 owner_->view()->focus(true);
208 // This will go in again in a little while
209 // we need to be able to declare what types
210 // of argumetns LFUN's should have first. (Lgb)
211 // First check for match
212 vector<string>::const_iterator cit =
213 std::find(completion_.begin(),
216 if (cit == completion_.end()) {
219 string const tmp = input + _(" [no match]");
223 // Return the inputted string
225 owner_->view()->focus(true);
226 if (!input.empty()) {
227 history_->push_back(input);
229 stringReady.emit(input);
237 // Depending on the input state spaces might not
245 string const tmp = input + _(" [no match]");
258 //lyxerr << "Unhandled minibuffer event!" << endl;
269 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
271 int key, void * /*xev*/)
273 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
274 return mini->peek_event(ob, event, key);
280 void MiniBuffer::prepare()
285 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
290 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
291 FL_Coord w, FL_Coord h)
295 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
296 fl_set_object_boxtype(obj, FL_DOWN_BOX);
297 fl_set_object_resize(obj, FL_RESIZE_ALL);
298 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
299 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
300 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
302 // To intercept Up, Down, Table for history
303 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
305 obj->wantkey = FL_KEY_TAB;
313 void MiniBuffer::message(string const & str)
316 string const ntext = strip(str);
317 if (!the_buffer->focus) {
324 void MiniBuffer::messagePush(string const & str)
331 void MiniBuffer::messagePop()
333 if (!text_stored.empty()) {
334 message(text_stored);
340 void MiniBuffer::addSet(string const & s1, string const & s2)
342 string const str = text + ' ' + s1 + ' ' + s2;
347 void MiniBuffer::getString(State spaces,
348 vector<string> const & completion,
349 vector<string> & history)
352 completion_ = completion;
354 hist_iter = history_->end();
359 void MiniBuffer::init()
361 // If we have focus, we don't want to change anything.
362 if (the_buffer->focus)
370 void MiniBuffer::activate()
372 fl_activate_object(the_buffer);
377 void MiniBuffer::deactivate()
380 fl_deactivate_object(the_buffer);
385 void MiniBuffer::redraw()
387 fl_redraw_object(the_buffer);
392 void MiniBuffer::set_complete_input(string const & str)
395 // add a space so the user can type
396 // an argument immediately
397 set_input(str + " ");
402 void MiniBuffer::append_char(char c)
404 if (!c || !isprint(c))
407 char const * tmp = fl_get_input(the_buffer);
408 string str = tmp ? tmp : "";
412 fl_set_input(the_buffer, str.c_str());
416 void MiniBuffer::set_input(string const & str)
418 fl_set_input(the_buffer, str.c_str());