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));
77 MiniBuffer::~MiniBuffer()
83 void MiniBuffer::stored_slot()
87 set_input(stored_input);
92 void MiniBuffer::stored_set(string const & str)
100 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
108 char const * tmp = fl_get_input(ob);
109 string input = tmp ? tmp : "";
112 input = stored_input;
119 if (hist_iter != history_->end()) {
122 if (hist_iter == history_->end()) {
123 // no further history
125 set_input(_("[End of history]"));
127 set_input((*hist_iter));
131 if (hist_iter == history_->begin()) {
132 // no further history
134 set_input(_("[Beginning of history]"));
137 set_input((*hist_iter));
143 // Completion handling.
146 lyx::copy_if(completion_.begin(),
148 std::back_inserter(comp), prefix(input));
152 string const tmp = input + _(" [no match]");
155 } else if (comp.size() == 1) {
158 comp[0] + _(" [sole completion]");
159 stored_set(comp[0] + " ");
162 // More that one match
163 // Find maximal avaliable prefix
164 string const tmp = comp[0];
166 if (tmp.length() > test.length())
167 test += tmp[test.length()];
168 while (test.length() < tmp.length()) {
170 lyx::copy_if(comp.begin(),
172 std::back_inserter(vtmp),
174 if (vtmp.size() != comp.size()) {
175 test.erase(test.length() - 1);
178 test += tmp[test.length()];
183 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
186 // asynchronous completion
187 dropdown_->select(comp, x, y + h, w);
194 owner_->view()->focus(true);
203 // This will go in again in a little while
204 // we need to be able to declare what types
205 // of argumetns LFUN's should have first. (Lgb)
206 // First check for match
207 vector<string>::const_iterator cit =
208 std::find(completion_.begin(),
211 if (cit == completion_.end()) {
214 string const tmp = input + _(" [no match]");
218 // Return the inputted string
220 owner_->view()->focus(true);
221 if (!input.empty()) {
222 history_->push_back(input);
224 stringReady.emit(input);
232 // Depending on the input state spaces might not
240 string const tmp = input + _(" [no match]");
253 //lyxerr << "Unhandled minibuffer event!" << endl;
264 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
266 int key, void * /*xev*/)
268 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
269 return mini->peek_event(ob, event, key);
275 void MiniBuffer::prepare()
280 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
285 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
286 FL_Coord w, FL_Coord h)
290 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
291 fl_set_object_boxtype(obj, FL_DOWN_BOX);
292 fl_set_object_resize(obj, FL_RESIZE_ALL);
293 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
294 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
295 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
297 // To intercept Up, Down, Table for history
298 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
300 obj->wantkey = FL_KEY_TAB;
308 void MiniBuffer::message(string const & str)
311 string const ntext = strip(str);
312 if (!the_buffer->focus) {
319 void MiniBuffer::messagePush(string const & str)
326 void MiniBuffer::messagePop()
328 if (!text_stored.empty()) {
329 message(text_stored);
335 void MiniBuffer::addSet(string const & s1, string const & s2)
337 string const str = text + ' ' + s1 + ' ' + s2;
342 void MiniBuffer::getString(State spaces,
343 vector<string> const & completion,
344 vector<string> & history)
347 completion_ = completion;
349 hist_iter = history_->end();
354 void MiniBuffer::init()
356 // If we have focus, we don't want to change anything.
357 if (the_buffer->focus)
365 void MiniBuffer::activate()
367 fl_activate_object(the_buffer);
372 void MiniBuffer::deactivate()
375 fl_deactivate_object(the_buffer);
380 void MiniBuffer::redraw()
382 fl_redraw_object(the_buffer);
387 void MiniBuffer::set_complete_input(string const & str)
390 // add a space so the user can type
391 // an argument immediately
392 set_input(str + " ");
397 void MiniBuffer::set_input(string const & str)
399 fl_set_input(the_buffer, str.c_str());