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"
40 extern LyXAction lyxaction;
47 prefix(string const & s)
49 bool operator()(string const & s) const {
50 return prefixIs(s, p);
54 } // end of anon namespace
57 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
58 FL_Coord h, FL_Coord w)
59 : stored_(false), owner_(o), state_(spaces)
61 add(FL_NORMAL_INPUT, x, y, h, w);
62 timer.setTimeout(6000);
63 timer.timeout.connect(slot(this, &MiniBuffer::init));
64 stored_timer.setTimeout(1500);
65 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
70 // thanks for nothing, xforms (recursive creation not allowed)
71 void MiniBuffer::dd_init()
73 dropdown_ = new DropDown(owner_, the_buffer);
74 dropdown_->result.connect(slot(this, &MiniBuffer::set_complete_input));
75 dropdown_->keypress.connect(slot(this, &MiniBuffer::append_char));
79 MiniBuffer::~MiniBuffer()
85 void MiniBuffer::stored_slot()
89 set_input(stored_input);
94 void MiniBuffer::stored_set(string const & str)
102 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
110 char const * tmp = fl_get_input(ob);
111 string input = tmp ? tmp : "";
114 input = stored_input;
121 if (hist_iter != history_->end()) {
124 if (hist_iter == history_->end()) {
125 // no further history
127 set_input(_("[End of history]"));
129 set_input((*hist_iter));
133 if (hist_iter == history_->begin()) {
134 // no further history
136 set_input(_("[Beginning of history]"));
139 set_input((*hist_iter));
145 // Completion handling.
148 lyx::copy_if(completion_.begin(),
150 std::back_inserter(comp), prefix(input));
154 string const tmp = input + _(" [no match]");
157 } else if (comp.size() == 1) {
160 comp[0] + _(" [sole completion]");
161 stored_set(comp[0] + " ");
164 // More that one match
165 // Find maximal avaliable prefix
166 string const tmp = comp[0];
168 if (tmp.length() > test.length())
169 test += tmp[test.length()];
170 while (test.length() < tmp.length()) {
172 lyx::copy_if(comp.begin(),
174 std::back_inserter(vtmp),
176 if (vtmp.size() != comp.size()) {
177 test.erase(test.length() - 1);
180 test += tmp[test.length()];
185 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
188 // asynchronous completion
189 int const air = the_buffer->x;
191 y += h - (the_buffer->h + air);
193 dropdown_->select(comp, x, y, w);
200 owner_->view()->focus(true);
209 // This will go in again in a little while
210 // we need to be able to declare what types
211 // of argumetns LFUN's should have first. (Lgb)
212 // First check for match
213 vector<string>::const_iterator cit =
214 std::find(completion_.begin(),
217 if (cit == completion_.end()) {
220 string const tmp = input + _(" [no match]");
224 // Return the inputted string
226 owner_->view()->focus(true);
227 if (!input.empty()) {
228 history_->push_back(input);
230 stringReady.emit(input);
238 // Depending on the input state spaces might not
246 string const tmp = input + _(" [no match]");
259 //lyxerr << "Unhandled minibuffer event!" << endl;
270 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
272 int key, void * /*xev*/)
274 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
275 return mini->peek_event(ob, event, key);
281 void MiniBuffer::prepare()
286 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
291 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
292 FL_Coord w, FL_Coord h)
296 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
297 fl_set_object_boxtype(obj, FL_DOWN_BOX);
298 fl_set_object_resize(obj, FL_RESIZE_ALL);
299 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
300 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
301 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
303 // To intercept Up, Down, Table for history
304 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
306 obj->wantkey = FL_KEY_TAB;
314 void MiniBuffer::message(string const & str)
317 string const ntext = strip(str);
318 if (!the_buffer->focus) {
325 void MiniBuffer::messagePush(string const & str)
332 void MiniBuffer::messagePop()
334 if (!text_stored.empty()) {
335 message(text_stored);
341 void MiniBuffer::addSet(string const & s1, string const & s2)
343 string const str = text + ' ' + s1 + ' ' + s2;
348 void MiniBuffer::getString(State spaces,
349 vector<string> const & completion,
350 vector<string> & history)
353 completion_ = completion;
355 hist_iter = history_->end();
360 void MiniBuffer::init()
362 // If we have focus, we don't want to change anything.
363 if (the_buffer->focus)
371 void MiniBuffer::activate()
373 fl_activate_object(the_buffer);
378 void MiniBuffer::deactivate()
381 fl_deactivate_object(the_buffer);
386 void MiniBuffer::redraw()
388 fl_redraw_object(the_buffer);
393 void MiniBuffer::set_complete_input(string const & str)
396 // add a space so the user can type
397 // an argument immediately
398 set_input(str + " ");
403 void MiniBuffer::append_char(char c)
405 if (!c || !isprint(c))
408 char const * tmp = fl_get_input(the_buffer);
409 string str = tmp ? tmp : "";
413 fl_set_input(the_buffer, str.c_str());
417 void MiniBuffer::set_input(string const & str)
419 fl_set_input(the_buffer, str.c_str());