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