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 using std::back_inserter;
42 extern LyXAction lyxaction;
49 prefix(string const & s)
51 bool operator()(string const & s) const {
52 return prefixIs(s, p);
56 } // end of anon namespace
59 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
60 FL_Coord h, FL_Coord w)
61 : stored_(false), owner_(o), state_(spaces)
63 add(FL_NORMAL_INPUT, x, y, h, w);
64 timer.setTimeout(6000);
65 timer.timeout.connect(slot(this, &MiniBuffer::init));
66 stored_timer.setTimeout(1500);
67 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
72 // thanks for nothing, xforms (recursive creation not allowed)
73 void MiniBuffer::dd_init()
75 dropdown_ = new DropDown(owner_, the_buffer);
76 dropdown_->result.connect(slot(this, &MiniBuffer::set_complete_input));
77 dropdown_->keypress.connect(slot(this, &MiniBuffer::append_char));
81 MiniBuffer::~MiniBuffer()
87 void MiniBuffer::stored_slot()
91 set_input(stored_input);
96 void MiniBuffer::stored_set(string const & str)
100 stored_timer.start();
104 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
112 char const * tmp = fl_get_input(ob);
113 string input = tmp ? tmp : "";
116 input = stored_input;
123 if (hist_iter != history_->end()) {
126 if (hist_iter == history_->end()) {
127 // no further history
129 set_input(_("[End of history]"));
131 set_input((*hist_iter));
135 if (hist_iter == history_->begin()) {
136 // no further history
138 set_input(_("[Beginning of history]"));
141 set_input((*hist_iter));
147 // Completion handling.
150 lyx::copy_if(completion_.begin(),
152 back_inserter(comp), prefix(input));
156 string const tmp = input + _(" [no match]");
159 } else if (comp.size() == 1) {
162 comp[0] + _(" [sole completion]");
163 stored_set(comp[0] + " ");
166 // More that one match
167 // Find maximal avaliable prefix
168 string const tmp = comp[0];
170 if (tmp.length() > test.length())
171 test += tmp[test.length()];
172 while (test.length() < tmp.length()) {
174 lyx::copy_if(comp.begin(),
178 if (vtmp.size() != comp.size()) {
179 test.erase(test.length() - 1);
182 test += tmp[test.length()];
187 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
190 // asynchronous completion
191 int const air = the_buffer->x;
193 y += h - (the_buffer->h + air);
195 dropdown_->select(comp, x, y, w);
202 owner_->view()->focus(true);
211 // This will go in again in a little while
212 // we need to be able to declare what types
213 // of argumetns LFUN's should have first. (Lgb)
214 // First check for match
215 vector<string>::const_iterator cit =
216 find(completion_.begin(),
219 if (cit == completion_.end()) {
222 string const tmp = input + _(" [no match]");
226 // Return the inputted string
228 owner_->view()->focus(true);
229 if (!input.empty()) {
230 history_->push_back(input);
232 stringReady.emit(input);
240 // Depending on the input state spaces might not
248 string const tmp = input + _(" [no match]");
261 //lyxerr << "Unhandled minibuffer event!" << endl;
272 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
274 int key, void * /*xev*/)
276 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
277 return mini->peek_event(ob, event, key);
283 void MiniBuffer::prepare()
288 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
293 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
294 FL_Coord w, FL_Coord h)
298 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
299 fl_set_object_boxtype(obj, FL_DOWN_BOX);
300 fl_set_object_resize(obj, FL_RESIZE_ALL);
301 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
302 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
303 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
305 // To intercept Up, Down, Table for history
306 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
308 obj->wantkey = FL_KEY_TAB;
316 void MiniBuffer::message(string const & str)
319 string const ntext = strip(str);
320 if (!the_buffer->focus) {
327 void MiniBuffer::messagePush(string const & str)
334 void MiniBuffer::messagePop()
336 if (!text_stored.empty()) {
337 message(text_stored);
343 void MiniBuffer::addSet(string const & s1, string const & s2)
345 string const str = text + ' ' + s1 + ' ' + s2;
350 void MiniBuffer::getString(State spaces,
351 vector<string> const & completion,
352 vector<string> & history)
355 completion_ = completion;
357 hist_iter = history_->end();
362 void MiniBuffer::init()
364 // If we have focus, we don't want to change anything.
365 if (the_buffer->focus)
373 void MiniBuffer::activate()
375 fl_activate_object(the_buffer);
380 void MiniBuffer::deactivate()
383 fl_deactivate_object(the_buffer);
388 void MiniBuffer::redraw()
390 fl_redraw_object(the_buffer);
395 void MiniBuffer::set_complete_input(string const & str)
398 // add a space so the user can type
399 // an argument immediately
400 set_input(str + " ");
405 void MiniBuffer::append_char(char c)
407 if (!c || !isprint(c))
410 char const * tmp = fl_get_input(the_buffer);
411 string str = tmp ? tmp : "";
415 fl_set_input(the_buffer, str.c_str());
419 void MiniBuffer::set_input(string const & str)
421 fl_set_input(the_buffer, str.c_str());