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"
23 #include "frontends/xforms/XFormsView.h"
25 #include "minibuffer.h"
27 #include "support/lyxalgo.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "frontends/LyXView.h"
32 #include "LyXAction.h"
33 #include "BufferView.h"
34 #include "frontends/Timeout.h"
36 #include <boost/bind.hpp>
41 using std::back_inserter;
44 extern LyXAction lyxaction;
51 prefix(string const & s)
53 bool operator()(string const & s) const {
54 return prefixIs(s, p);
58 } // end of anon namespace
61 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
62 FL_Coord h, FL_Coord w)
63 : stored_(false), owner_(o), state_(spaces)
65 add(FL_NORMAL_INPUT, x, y, h, w);
67 timer = new Timeout(6000);
68 timer->timeout.connect(boost::bind(&MiniBuffer::init, this));
70 stored_timer = new Timeout(1500);
71 stored_timer->timeout.connect(boost::bind(&MiniBuffer::stored_slot, this));
76 // thanks for nothing, xforms (recursive creation not allowed)
77 void MiniBuffer::dd_init()
79 dropdown_ = new DropDown(owner_, the_buffer);
80 dropdown_->result.connect(boost::bind(&MiniBuffer::set_complete_input, this, _1));
81 dropdown_->keypress.connect(boost::bind(&MiniBuffer::append_char, this, _1));
85 MiniBuffer::~MiniBuffer()
93 void MiniBuffer::stored_slot()
97 set_input(stored_input);
102 void MiniBuffer::stored_set(string const & str)
106 stored_timer->start();
110 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
118 char const * tmp = fl_get_input(ob);
119 string input = tmp ? tmp : "";
121 stored_timer->stop();
122 input = stored_input;
129 if (hist_iter != history_->end()) {
132 if (hist_iter == history_->end()) {
133 // no further history
135 set_input(_("[End of history]"));
137 set_input((*hist_iter));
141 if (hist_iter == history_->begin()) {
142 // no further history
144 set_input(_("[Beginning of history]"));
147 set_input((*hist_iter));
153 // Completion handling.
156 lyx::copy_if(completion_.begin(),
158 back_inserter(comp), prefix(input));
162 string const tmp = input + _(" [no match]");
165 } else if (comp.size() == 1) {
168 comp[0] + _(" [sole completion]");
169 stored_set(comp[0] + " ");
172 // More that one match
173 // Find maximal avaliable prefix
174 string const tmp = comp[0];
176 if (tmp.length() > test.length())
177 test += tmp[test.length()];
178 while (test.length() < tmp.length()) {
180 lyx::copy_if(comp.begin(),
184 if (vtmp.size() != comp.size()) {
185 test.erase(test.length() - 1);
188 test += tmp[test.length()];
193 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
196 // asynchronous completion
197 int const air = the_buffer->x;
199 y += h - (the_buffer->h + air);
201 dropdown_->select(comp, x, y, w);
208 owner_->view()->focus(true);
217 // This will go in again in a little while
218 // we need to be able to declare what types
219 // of argumetns LFUN's should have first. (Lgb)
220 // First check for match
221 vector<string>::const_iterator cit =
222 find(completion_.begin(),
225 if (cit == completion_.end()) {
228 string const tmp = input + _(" [no match]");
232 // Return the inputted string
234 owner_->view()->focus(true);
235 if (!input.empty()) {
236 history_->push_back(input);
246 // Depending on the input state spaces might not
254 string const tmp = input + _(" [no match]");
267 //lyxerr << "Unhandled minibuffer event!" << endl;
278 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
280 int key, void * /*xev*/)
282 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
283 return mini->peek_event(ob, event, key);
289 void MiniBuffer::prepare()
294 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
299 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
300 FL_Coord w, FL_Coord h)
304 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
305 fl_set_object_boxtype(obj, FL_DOWN_BOX);
306 fl_set_object_resize(obj, FL_RESIZE_ALL);
307 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
308 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
309 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
311 // To intercept Up, Down, Table for history
312 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
314 obj->wantkey = FL_KEY_TAB;
322 void MiniBuffer::message(string const & str)
325 string const ntext = strip(str);
326 if (!the_buffer->focus) {
333 void MiniBuffer::messagePush(string const & str)
340 void MiniBuffer::messagePop()
342 if (!text_stored.empty()) {
343 message(text_stored);
349 void MiniBuffer::addSet(string const & s1, string const & s2)
351 string const str = text + ' ' + s1 + ' ' + s2;
356 void MiniBuffer::getString(State spaces,
357 vector<string> const & completion,
358 vector<string> & history)
361 completion_ = completion;
363 hist_iter = history_->end();
368 void MiniBuffer::init()
370 // If we have focus, we don't want to change anything.
371 if (the_buffer->focus)
379 void MiniBuffer::activate()
381 fl_activate_object(the_buffer);
386 void MiniBuffer::deactivate()
389 fl_deactivate_object(the_buffer);
394 void MiniBuffer::redraw()
396 fl_redraw_object(the_buffer);
401 void MiniBuffer::set_complete_input(string const & str)
404 // add a space so the user can type
405 // an argument immediately
406 set_input(str + " ");
411 void MiniBuffer::append_char(char c)
413 if (!c || !isprint(c))
416 char const * tmp = fl_get_input(the_buffer);
417 string str = tmp ? tmp : "";
421 fl_set_input(the_buffer, str.c_str());
425 void MiniBuffer::set_input(string const & str)
427 fl_set_input(the_buffer, str.c_str());