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_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]");
159 // More that one match
160 // Find maximal avaliable prefix
161 string const tmp = comp[0];
163 test += tmp[test.length()];
164 while (test.length() < tmp.length()) {
166 lyx::copy_if(comp.begin(),
168 std::back_inserter(vtmp),
170 if (vtmp.size() != comp.size()) {
171 test.erase(test.length() - 1);
174 test += tmp[test.length()];
179 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
182 // asynchronous completion
183 dropdown_->select(comp, x, y + h, w);
190 owner_->view()->focus(true);
199 // This will go in again in a little while
200 // we need to be able to declare what types
201 // of argumetns LFUN's should have first. (Lgb)
202 // First check for match
203 vector<string>::const_iterator cit =
204 std::find(completion_.begin(),
207 if (cit == completion_.end()) {
210 string const tmp = input + _(" [no match]");
214 // Return the inputted string
216 owner_->view()->focus(true);
217 history_->push_back(input);
218 stringReady.emit(input);
226 // Depending on the input state spaces might not
234 string const tmp = input + _(" [no match]");
247 //lyxerr << "Unhandled minibuffer event!" << endl;
258 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
260 int key, void * /*xev*/)
262 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
263 return mini->peek_event(ob, event, key);
269 void MiniBuffer::prepare()
274 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
279 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
280 FL_Coord w, FL_Coord h)
284 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
285 fl_set_object_boxtype(obj, FL_DOWN_BOX);
286 fl_set_object_resize(obj, FL_RESIZE_ALL);
287 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
288 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
289 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
291 // To intercept Up, Down, Table for history
292 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
294 obj->wantkey = FL_KEY_TAB;
302 void MiniBuffer::message(string const & str)
305 string const ntext = strip(str);
306 if (!the_buffer->focus) {
313 void MiniBuffer::messagePush(string const & str)
320 void MiniBuffer::messagePop()
322 if (!text_stored.empty()) {
323 message(text_stored);
329 void MiniBuffer::addSet(string const & s1, string const & s2)
331 string const str = text + ' ' + s1 + ' ' + s2;
336 void MiniBuffer::getString(State spaces,
337 vector<string> const & completion,
338 vector<string> & history)
341 completion_ = completion;
343 hist_iter = history_->end();
348 void MiniBuffer::init()
350 // If we have focus, we don't want to change anything.
351 if (the_buffer->focus)
359 void MiniBuffer::activate()
361 fl_activate_object(the_buffer);
366 void MiniBuffer::deactivate()
369 fl_deactivate_object(the_buffer);
374 void MiniBuffer::redraw()
376 fl_redraw_object(the_buffer);
381 void MiniBuffer::set_input(string const & str)
383 fl_set_input(the_buffer, str.c_str());