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