]> git.lyx.org Git - lyx.git/blob - src/minibuffer.C
fix starting up which binary is really a symlink; make sure insetinclude file browser...
[lyx.git] / src / minibuffer.C
1 /* ###########################################################################
2  *
3  *                 The MiniBuffer Class
4  *                 read minibuffer.h for more
5  *                 information.
6  * 
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.  
9  * 
10  * ###########################################################################
11  */
12
13 #include <config.h>
14
15 #include <iostream>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 // FIXME: temporary 
22 #include "frontends/xforms/DropDown.h"
23  
24 #include "minibuffer.h"
25
26 #include "support/lyxalgo.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29 #include "LyXView.h"
30 #include "XFormsView.h"
31 #include "gettext.h"
32 #include "LyXAction.h"
33 #include "BufferView.h"
34
35 #include <cctype>
36
37 using SigC::slot;
38 using std::vector;
39
40 extern LyXAction lyxaction;
41
42
43 namespace {
44
45 struct prefix {
46         string p;
47         prefix(string const & s) 
48                 : p(s) {}
49         bool operator()(string const & s) const {
50                 return prefixIs(s, p);
51         }
52 };
53
54 } // end of anon namespace
55
56
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)
60 {
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));
66         deactivate();
67 }
68
69  
70 // thanks for nothing, xforms (recursive creation not allowed) 
71 void MiniBuffer::dd_init()
72 {
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));
76 }
77
78
79 MiniBuffer::~MiniBuffer()
80 {
81         delete dropdown_;
82 }
83
84  
85 void MiniBuffer::stored_slot() 
86 {
87         if (stored_) {
88                 stored_ = false;
89                 set_input(stored_input);
90         }
91 }
92
93
94 void MiniBuffer::stored_set(string const & str) 
95 {
96         stored_input = str;
97         stored_ = true;
98         stored_timer.start();
99 }
100
101
102 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
103 {
104         switch (event) {
105         case FL_UNFOCUS:
106                 deactivate();
107                 break;
108         case FL_KEYBOARD:
109         {
110                 char const * tmp = fl_get_input(ob);
111                 string input = tmp ? tmp : "";
112                 if (stored_) {
113                         stored_timer.stop();
114                         input = stored_input;
115                         set_input(input);
116                         stored_ = false;
117                 }
118                 
119                 switch (key) {
120                 case XK_Down:
121                         if (hist_iter != history_->end()) {
122                                 ++hist_iter;
123                         }
124                         if (hist_iter == history_->end()) {
125                                 // no further history
126                                 stored_set(input);
127                                 set_input(_("[End of history]"));
128                         } else {
129                                 set_input((*hist_iter));
130                         }
131                         return 1; 
132                 case XK_Up:
133                         if (hist_iter == history_->begin()) {
134                                 // no further history
135                                 stored_set(input);
136                                 set_input(_("[Beginning of history]"));
137                         } else {
138                                 --hist_iter;
139                                 set_input((*hist_iter));
140                         }
141                         return 1; 
142                 case 9:
143                 case XK_Tab:
144                 {
145                         // Completion handling.
146                         
147                         vector<string> comp;
148                         lyx::copy_if(completion_.begin(),
149                                      completion_.end(),
150                                      std::back_inserter(comp), prefix(input));
151
152                         if (comp.empty()) {
153                                 // No matches
154                                 string const tmp = input + _(" [no match]");
155                                 stored_set(input);
156                                 set_input(tmp);
157                         } else if (comp.size() == 1) {
158                                 // Perfect match
159                                 string const tmp =
160                                         comp[0] + _(" [sole completion]");
161                                 stored_set(comp[0] + " ");
162                                 set_input(tmp);
163                         } else {
164                                 // More that one match
165                                 // Find maximal avaliable prefix
166                                 string const tmp = comp[0];
167                                 string test(input);
168                                 if (tmp.length() > test.length())
169                                         test += tmp[test.length()];
170                                 while (test.length() < tmp.length()) {
171                                         vector<string> vtmp;
172                                         lyx::copy_if(comp.begin(),
173                                                      comp.end(),
174                                                      std::back_inserter(vtmp),
175                                                      prefix(test));
176                                         if (vtmp.size() != comp.size()) {
177                                                 test.erase(test.length() - 1);
178                                                 break;
179                                         }
180                                         test += tmp[test.length()];
181                                 }
182                                 set_input(test);
183                                 
184                                 int x,y,w,h;
185                                 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
186                                         &x, &y, &w, &h);
187  
188                                 // asynchronous completion
189                                 int const air = the_buffer->x;
190                                 x += air;
191                                 y += h - (the_buffer->h + air);
192                                 w = the_buffer->w;
193                                 dropdown_->select(comp, x, y, w);
194                         }
195                         return 1; 
196                 }
197                 case 27:
198                 case XK_Escape:
199                         // Abort
200                         owner_->view()->focus(true);
201                         init();
202                         deactivate();
203                         //escape.emit();
204                         return 1; 
205                 case 13:
206                 case XK_Return:
207                 {
208 #if 0
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(),
215                                           completion_.end(),
216                                           input);
217                         if (cit == completion_.end()) {
218                                 // no such func/item
219                                 stored_set(input);
220                                 string const tmp = input + _(" [no match]");
221                                 set_input(tmp);
222                         } else {
223 #endif
224                                 // Return the inputted string
225                                 deactivate();
226                                 owner_->view()->focus(true);
227                                 if (!input.empty()) {
228                                         history_->push_back(input);
229                                 }
230                                 stringReady.emit(input);
231 # if 0
232                         }
233 #endif
234                         return 1;
235                 }
236                 case XK_space:
237                 {
238                         // Depending on the input state spaces might not
239                         // be allowed.
240                         switch (state_) {
241                         case spaces:
242                                 return 0;
243                         case nospaces:
244                         {
245                                 stored_set(input);
246                                 string const tmp = input + _(" [no match]");
247                                 set_input(tmp);
248                                 return 1;
249                         }
250                         }
251                         
252                 }
253                 
254                 default:
255                         return 0;
256                 }
257         }
258         default:
259                 //lyxerr << "Unhandled minibuffer event!" << endl;
260                 break;
261         }
262         
263         return 0;
264 }
265
266
267 extern "C" {
268         
269         static
270         int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event, 
271                                     FL_Coord, FL_Coord,
272                                     int key, void * /*xev*/)
273         {
274                 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
275                 return mini->peek_event(ob, event, key);
276         }
277         
278 }
279
280
281 void MiniBuffer::prepare()
282 {
283         text.erase();
284         set_input("");
285         activate();
286         fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
287                             the_buffer);
288 }
289
290
291 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
292                            FL_Coord w, FL_Coord h)
293 {
294         FL_OBJECT * obj;
295         
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);
302         
303         // To intercept Up, Down, Table for history
304         fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
305         obj->u_vdata = this;
306         obj->wantkey = FL_KEY_TAB;
307
308         set_input(text);
309         
310         return obj;
311 }
312
313
314 void MiniBuffer::message(string const & str) 
315 {
316         timer.restart();
317         string const ntext = strip(str);
318         if (!the_buffer->focus) {
319                 set_input(ntext);
320                 text = ntext;
321         }
322 }
323
324
325 void MiniBuffer::messagePush(string const & str) 
326 {
327         text_stored = text;
328         message(str);
329 }
330
331
332 void MiniBuffer::messagePop()
333 {
334         if (!text_stored.empty()) {
335                 message(text_stored);
336                 text_stored.erase();
337         }
338 }
339
340
341 void MiniBuffer::addSet(string const & s1, string const & s2)
342 {
343         string const str = text + ' ' +  s1 + ' ' + s2;
344         message(str);
345 }
346
347
348 void MiniBuffer::getString(State spaces,
349                            vector<string> const & completion,
350                            vector<string> & history)
351 {
352         state_ = spaces;
353         completion_ = completion;
354         history_ = &history;
355         hist_iter = history_->end();
356         prepare();
357 }
358
359
360 void MiniBuffer::init()
361 {
362         // If we have focus, we don't want to change anything.
363         if (the_buffer->focus)
364                 return;
365
366         timeout.emit();
367         timer.stop();
368 }
369
370
371 void MiniBuffer::activate()
372 {
373         fl_activate_object(the_buffer);
374         redraw();
375 }
376
377
378 void MiniBuffer::deactivate()
379 {
380         redraw();
381         fl_deactivate_object(the_buffer);
382         XFlush(fl_display);
383 }
384
385
386 void MiniBuffer::redraw() 
387 {
388         fl_redraw_object(the_buffer);
389         XFlush(fl_display);
390 }
391
392
393 void MiniBuffer::set_complete_input(string const & str)
394 {
395         if (!str.empty()) {
396                 // add a space so the user can type
397                 // an argument immediately
398                 set_input(str + " ");
399         }
400 }
401
402  
403 void MiniBuffer::append_char(char c)
404 {
405         if (!c || !isprint(c))
406                 return;
407
408         char const * tmp = fl_get_input(the_buffer);
409         string str = tmp ? tmp : "";
410
411         str += c;
412
413         fl_set_input(the_buffer, str.c_str());
414 }
415
416  
417 void MiniBuffer::set_input(string const & str)
418 {
419         fl_set_input(the_buffer, str.c_str());
420         XFlush(fl_display);
421 }