]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XMiniBuffer.C
portoaddons
[lyx.git] / src / frontends / xforms / XMiniBuffer.C
1 // -*- C++ -*-
2 /**
3  * \file XMiniBuffer.C
4  * Copyright 1995-2002 the LyX Team
5  * Read the file COPYING
6  *
7  * \author Lars
8  * \author Asger and Juergen
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "frontends/xforms/DropDown.h"
18 #include "frontends/xforms/XFormsView.h"
19 #include "frontends/Timeout.h"
20
21 #include "XMiniBuffer.h"
22 #include "BufferView.h"
23 #include "LyXAction.h"
24 #include "gettext.h"
25 #include "support/lyxalgo.h"
26 #include "support/lstrings.h"
27
28 #include <boost/bind.hpp>
29
30 #include <vector>
31 #include <cctype>
32
33 #ifndef CXX_GLOBAL_CSTD
34 using std::isprint;
35 #endif
36
37 using std::vector;
38
39
40 namespace {
41
42 struct prefix {
43         string p;
44         prefix(string const & s)
45                 : p(s) {}
46         bool operator()(string const & s) const {
47                 return prefixIs(s, p);
48         }
49 };
50
51 } // end of anon namespace
52
53
54 XMiniBuffer::XMiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
55                          FL_Coord h, FL_Coord w)
56         : MiniBuffer(o)
57 {
58         create_input_box(FL_NORMAL_INPUT, x, y, h, w);
59         messageMode();
60 }
61
62
63 // thanks for nothing, xforms (recursive creation not allowed)
64 void XMiniBuffer::dd_init()
65 {
66         dropdown_ = new DropDown(owner_, the_buffer);
67         dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
68         dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
69 }
70
71
72 XMiniBuffer::~XMiniBuffer()
73 {
74         delete dropdown_;
75 }
76
77
78 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event, int key, XEvent * xev)
79 {
80 #if 0
81         if (dropdown_->isVisible()) {
82                 return dropdown_->peek(xev);
83         }
84 #endif
85         
86         switch (event) {
87         case FL_UNFOCUS:
88                 messageMode();
89                 break;
90         case FL_KEYBOARD:
91         {
92                 string input;
93                 if (information_displayed_) {
94                         information_timer_->stop();
95                         input = stored_input;
96                         restore_input();
97                 } else {
98                         char const * tmp = fl_get_input(ob);
99                         input = tmp ? tmp : "";
100                 }
101                   
102
103                 switch (key) {
104                 case XK_Down:
105                         if (hist_iter != history_->end()) {
106                                 ++hist_iter;
107                         }
108                         if (hist_iter == history_->end()) {
109                                 // no further history
110                                 show_information(_("[End of history]"), input);
111                         } else {
112                                 set_input((*hist_iter));
113                         }
114                         return 1;
115                 case XK_Up:
116                         if (hist_iter == history_->begin()) {
117                                 // no further history
118                                 show_information(_("[Beginning of history]"),
119                                                  input);
120                         } else {
121                                 --hist_iter;
122                                 set_input((*hist_iter));
123                         }
124                         return 1;
125                 case 9:
126                 case XK_Tab:
127                 {
128                         // Completion handling.
129
130                         vector<string> comp;
131                         lyx::copy_if(completion_.begin(),
132                                      completion_.end(),
133                                      back_inserter(comp), prefix(input));
134
135                         if (comp.empty()) {
136                                 // No matches
137                                 string const tmp = input + _(" [no match]");
138                                 show_information(tmp, input);
139                         } else if (comp.size() == 1) {
140                                 // Perfect match
141                                 string const tmp =
142                                         comp[0] + _(" [sole completion]");
143                                 show_information(tmp, comp[0] + " ");
144                         } else {
145                                 // More that one match
146                                 // Find maximal avaliable prefix
147                                 string const tmp = comp[0];
148                                 string test(input);
149                                 if (tmp.length() > test.length())
150                                         test += tmp[test.length()];
151                                 while (test.length() < tmp.length()) {
152                                         vector<string> vtmp;
153                                         lyx::copy_if(comp.begin(),
154                                                      comp.end(),
155                                                      back_inserter(vtmp),
156                                                      prefix(test));
157                                         if (vtmp.size() != comp.size()) {
158                                                 test.erase(test.length() - 1);
159                                                 break;
160                                         }
161                                         test += tmp[test.length()];
162                                 }
163                                 set_input(test);
164
165                                 int x,y,w,h;
166                                 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
167                                                    &x, &y, &w, &h);
168
169                                 // asynchronous completion
170                                 int const air = the_buffer->x;
171                                 x += air;
172                                 y += h - (the_buffer->h + air);
173                                 w = the_buffer->w;
174                                 dropdown_->select(comp, x, y, w);
175                         }
176                         return 1;
177                 }
178                 case 27:
179                 case XK_Escape:
180                         // Abort
181 #if 0
182                         owner_->view()->focus(true);
183 #endif
184                         message_timeout();
185                         messageMode();
186                         //escape.emit();
187                         return 1;
188                 case 13:
189                 case XK_Return:
190                 {
191 #if 0
192                         // This will go in again in a little while
193                         // we need to be able to declare what types
194                         // of argumetns LFUN's should have first. (Lgb)
195                         // First check for match
196                         vector<string>::const_iterator cit =
197                                 find(completion_.begin(),
198                                      completion_.end(),
199                                      input);
200                         if (cit == completion_.end()) {
201                                 // no such func/item
202                                 string const tmp = input + _(" [no match]");
203                                 show_information(tmp, input);
204                         } else {
205 #endif
206                                 // Return the inputted string
207                                 messageMode();
208 #if 0
209                                 owner_->view()->focus(true);
210 #endif
211                                 if (!input.empty()) {
212                                         history_->push_back(input);
213                                 }
214                                 inputReady(input);
215 # if 0
216                         }
217 #endif
218                         return 1;
219                 }
220                 default:
221                         return 0;
222                 }
223         }
224         default:
225                 //lyxerr << "Unhandled minibuffer event!" << endl;
226                 break;
227         }
228
229         return 0;
230 }
231
232
233 extern "C" {
234
235         static
236         int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
237                                      FL_Coord, FL_Coord,
238                                      int key, void * xev)
239         {
240                 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
241                 return mini->peek_event(ob, event, key,
242                                         static_cast<XEvent *>(xev));
243         }
244
245 }
246
247
248 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
249                                           FL_Coord w, FL_Coord h)
250 {
251         FL_OBJECT * obj;
252
253         the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
254         fl_set_object_boxtype(obj, FL_DOWN_BOX);
255         fl_set_object_resize(obj, FL_RESIZE_ALL);
256         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
257         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
258         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
259
260         // To intercept Up, Down, Table for history
261         fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
262         obj->u_vdata = this;
263         obj->wantkey = FL_KEY_TAB;
264
265         set_input(text);
266
267         return obj;
268 }
269
270
271 bool XMiniBuffer::isEditingMode() const
272 {
273         return the_buffer->focus;
274 }
275
276
277 void XMiniBuffer::editingMode()
278 {
279         fl_activate_object(the_buffer);
280         fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
281                             the_buffer);
282         redraw();
283 }
284
285
286 void XMiniBuffer::messageMode()
287 {
288         fl_deactivate_object(the_buffer);
289         redraw();
290 }
291
292
293 void XMiniBuffer::redraw()
294 {
295         fl_redraw_object(the_buffer);
296         XFlush(fl_display);
297 }
298
299
300 void XMiniBuffer::append_char(char c)
301 {
302         if (!c || !isprint(c))
303                 return;
304
305         char const * tmp = fl_get_input(the_buffer);
306         string str = tmp ? tmp : "";
307
308         str += c;
309
310         fl_set_input(the_buffer, str.c_str());
311 }
312
313
314 void XMiniBuffer::set_input(string const & str)
315 {
316         fl_set_input(the_buffer, str.c_str());
317 }