]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XMiniBuffer.C
Changes to make RH-7.3 be able to run lyx.
[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,
79                             int key, XEvent * /*xev*/)
80 {
81 #if 0
82         if (dropdown_->isVisible()) {
83                 return dropdown_->peek(xev);
84         }
85 #endif
86
87         switch (event) {
88         case FL_UNFOCUS:
89                 messageMode();
90                 break;
91         case FL_KEYBOARD:
92         {
93                 string input;
94                 if (information_displayed_) {
95                         information_timer_->stop();
96                         input = stored_input;
97                         restore_input();
98                 } else {
99                         char const * tmp = fl_get_input(ob);
100                         input = tmp ? tmp : "";
101                 }
102
103
104                 switch (key) {
105                 case XK_Down:
106                         if (hist_iter != history_->end()) {
107                                 ++hist_iter;
108                         }
109                         if (hist_iter == history_->end()) {
110                                 // no further history
111                                 show_information(_("[End of history]"), input);
112                         } else {
113                                 set_input((*hist_iter));
114                         }
115                         return 1;
116                 case XK_Up:
117                         if (hist_iter == history_->begin()) {
118                                 // no further history
119                                 show_information(_("[Beginning of history]"),
120                                                  input);
121                         } else {
122                                 --hist_iter;
123                                 set_input((*hist_iter));
124                         }
125                         return 1;
126                 case 9:
127                 case XK_Tab:
128                 {
129                         // Completion handling.
130
131                         vector<string> comp;
132                         lyx::copy_if(completion_.begin(),
133                                      completion_.end(),
134                                      back_inserter(comp), prefix(input));
135
136                         if (comp.empty()) {
137                                 // No matches
138                                 string const tmp = input + _(" [no match]");
139                                 show_information(tmp, input);
140                         } else if (comp.size() == 1) {
141                                 // Perfect match
142                                 string const tmp =
143                                         comp[0] + _(" [sole completion]");
144                                 show_information(tmp, comp[0] + " ");
145                         } else {
146                                 // More that one match
147                                 // Find maximal avaliable prefix
148                                 string const tmp = comp[0];
149                                 string test(input);
150                                 if (tmp.length() > test.length())
151                                         test += tmp[test.length()];
152                                 while (test.length() < tmp.length()) {
153                                         vector<string> vtmp;
154                                         lyx::copy_if(comp.begin(),
155                                                      comp.end(),
156                                                      back_inserter(vtmp),
157                                                      prefix(test));
158                                         if (vtmp.size() != comp.size()) {
159                                                 test.erase(test.length() - 1);
160                                                 break;
161                                         }
162                                         test += tmp[test.length()];
163                                 }
164                                 set_input(test);
165
166                                 int x,y,w,h;
167                                 fl_get_wingeometry(fl_get_real_object_window(the_buffer),
168                                                    &x, &y, &w, &h);
169
170                                 // asynchronous completion
171                                 int const air = the_buffer->x;
172                                 x += air;
173                                 y += h - (the_buffer->h + air);
174                                 w = the_buffer->w;
175                                 dropdown_->select(comp, x, y, w);
176                         }
177                         return 1;
178                 }
179                 case 27:
180                 case XK_Escape:
181                         // Abort
182 #if 0
183                         owner_->view()->focus(true);
184 #endif
185                         message_timeout();
186                         messageMode();
187                         //escape.emit();
188                         return 1;
189                 case 13:
190                 case XK_Return:
191                 {
192 #if 0
193                         // This will go in again in a little while
194                         // we need to be able to declare what types
195                         // of argumetns LFUN's should have first. (Lgb)
196                         // First check for match
197                         vector<string>::const_iterator cit =
198                                 find(completion_.begin(),
199                                      completion_.end(),
200                                      input);
201                         if (cit == completion_.end()) {
202                                 // no such func/item
203                                 string const tmp = input + _(" [no match]");
204                                 show_information(tmp, input);
205                         } else {
206 #endif
207                                 // Return the inputted string
208                                 messageMode();
209 #if 0
210                                 owner_->view()->focus(true);
211 #endif
212                                 if (!input.empty()) {
213                                         history_->push_back(input);
214                                 }
215                                 inputReady(input);
216 # if 0
217                         }
218 #endif
219                         return 1;
220                 }
221                 default:
222                         return 0;
223                 }
224         }
225         default:
226                 //lyxerr << "Unhandled minibuffer event!" << endl;
227                 break;
228         }
229
230         return 0;
231 }
232
233
234 extern "C" {
235
236         static
237         int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
238                                      FL_Coord, FL_Coord,
239                                      int key, void * xev)
240         {
241                 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
242                 return mini->peek_event(ob, event, key,
243                                         static_cast<XEvent *>(xev));
244         }
245
246 }
247
248
249 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
250                                           FL_Coord w, FL_Coord h)
251 {
252         FL_OBJECT * obj;
253
254         the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
255         fl_set_object_boxtype(obj, FL_DOWN_BOX);
256         fl_set_object_resize(obj, FL_RESIZE_ALL);
257         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
258         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
259         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
260
261         // To intercept Up, Down, Table for history
262         fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
263         obj->u_vdata = this;
264         obj->wantkey = FL_KEY_TAB;
265
266         set_input(text);
267
268         return obj;
269 }
270
271
272 bool XMiniBuffer::isEditingMode() const
273 {
274         return the_buffer->focus;
275 }
276
277
278 void XMiniBuffer::editingMode()
279 {
280         fl_activate_object(the_buffer);
281         fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
282                             the_buffer);
283         redraw();
284 }
285
286
287 void XMiniBuffer::messageMode()
288 {
289         fl_deactivate_object(the_buffer);
290         redraw();
291 }
292
293
294 void XMiniBuffer::redraw()
295 {
296         fl_redraw_object(the_buffer);
297         XFlush(fl_display);
298 }
299
300
301 void XMiniBuffer::append_char(char c)
302 {
303         if (!c || !isprint(c))
304                 return;
305
306         char const * tmp = fl_get_input(the_buffer);
307         string str = tmp ? tmp : "";
308
309         str += c;
310
311         fl_set_input(the_buffer, str.c_str());
312 }
313
314
315 void XMiniBuffer::set_input(string const & str)
316 {
317         fl_set_input(the_buffer, str.c_str());
318 }