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