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)
105 char const * tmp = fl_get_input(ob);
106 string input = tmp ? tmp : "";
109 input = stored_input;
116 if (hist_iter != history_->end()) {
119 if (hist_iter == history_->end()) {
120 // no further history
122 set_input(_("[End of history]"));
124 set_input((*hist_iter));
128 if (hist_iter == history_->begin()) {
129 // no further history
131 set_input(_("[Beginning of history]"));
134 set_input((*hist_iter));
140 // Completion handling.
143 lyx::copy_if(completion_.begin(),
145 std::back_inserter(comp), prefix(input));
149 string const tmp = input + _(" [no match]");
152 } else if (comp.size() == 1) {
155 comp[0] + _(" [sole completion]");
156 stored_set(comp[0] + " ");
159 // More that one match
160 // Find maximal avaliable prefix
161 string const tmp = comp[0];
163 if (tmp.length() > test.length())
164 test += tmp[test.length()];
165 while (test.length() < tmp.length()) {
167 lyx::copy_if(comp.begin(),
169 std::back_inserter(vtmp),
171 if (vtmp.size() != comp.size()) {
172 test.erase(test.length() - 1);
175 test += tmp[test.length()];
180 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
183 // asynchronous completion
184 dropdown_->select(comp, x, y + h, w);
191 owner_->view()->focus(true);
200 // This will go in again in a little while
201 // we need to be able to declare what types
202 // of argumetns LFUN's should have first. (Lgb)
203 // First check for match
204 vector<string>::const_iterator cit =
205 std::find(completion_.begin(),
208 if (cit == completion_.end()) {
211 string const tmp = input + _(" [no match]");
215 // Return the inputted string
217 owner_->view()->focus(true);
218 history_->push_back(input);
219 stringReady.emit(input);
227 // Depending on the input state spaces might not
235 string const tmp = input + _(" [no match]");
248 //lyxerr << "Unhandled minibuffer event!" << endl;
259 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
261 int key, void * /*xev*/)
263 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
264 return mini->peek_event(ob, event, key);
270 void MiniBuffer::prepare()
275 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
280 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
281 FL_Coord w, FL_Coord h)
285 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
286 fl_set_object_boxtype(obj, FL_DOWN_BOX);
287 fl_set_object_resize(obj, FL_RESIZE_ALL);
288 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
289 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
290 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
292 // To intercept Up, Down, Table for history
293 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
295 obj->wantkey = FL_KEY_TAB;
303 void MiniBuffer::message(string const & str)
306 string const ntext = strip(str);
307 if (!the_buffer->focus) {
314 void MiniBuffer::messagePush(string const & str)
321 void MiniBuffer::messagePop()
323 if (!text_stored.empty()) {
324 message(text_stored);
330 void MiniBuffer::addSet(string const & s1, string const & s2)
332 string const str = text + ' ' + s1 + ' ' + s2;
337 void MiniBuffer::getString(State spaces,
338 vector<string> const & completion,
339 vector<string> & history)
342 completion_ = completion;
344 hist_iter = history_->end();
349 void MiniBuffer::init()
351 // If we have focus, we don't want to change anything.
352 if (the_buffer->focus)
360 void MiniBuffer::activate()
362 fl_activate_object(the_buffer);
367 void MiniBuffer::deactivate()
370 fl_deactivate_object(the_buffer);
375 void MiniBuffer::redraw()
377 fl_redraw_object(the_buffer);
382 void MiniBuffer::set_complete_input(string const & str)
389 void MiniBuffer::set_input(string const & str)
391 fl_set_input(the_buffer, str.c_str());