]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XMiniBuffer.C
I fixed the two bugs (Very shallow in fact)
[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/controllers/ControlCommandBuffer.h"
20 #include "frontends/Timeout.h"
21
22 #include "XMiniBuffer.h"
23 #include "gettext.h"
24 #include "debug.h"
25 #include "bufferview_funcs.h"
26
27 #include <boost/bind.hpp>
28
29 #include <vector>
30
31 #ifndef CXX_GLOBAL_CSTD
32 using std::isprint;
33 #endif
34
35 using std::endl;
36 using std::vector;
37
38
39 XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
40         FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
41         : controller_(control), view_(v),
42         info_suffix_shown_(false)
43 {
44         input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
45         suffix_timer_.reset(new Timeout(1500));
46         idle_timer_.reset(new Timeout(6000));
47         suffix_timer_->timeout.connect(boost::bind(&XMiniBuffer::suffix_timeout, this));
48         idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
49         idle_timer_->start(); 
50         messageMode();
51 }
52
53
54 // thanks for nothing, xforms (recursive creation not allowed)
55 void XMiniBuffer::dd_init()
56 {
57         dropdown_.reset(new DropDown(the_buffer_));
58         dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
59         dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
60 }
61
62
63 XMiniBuffer::~XMiniBuffer()
64 {
65 }
66
67
68 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
69                             int key, XEvent * /*xev*/)
70 {
71 #if 0
72         if (dropdown_->isVisible()) {
73                 return dropdown_->peek(xev);
74         }
75 #endif
76
77         switch (event) {
78         case FL_FOCUS:
79                 messageMode(false);
80                 break;
81         case FL_UNFOCUS:
82                 messageMode();
83                 break;
84         case FL_KEYBOARD:
85         {
86                 string input;
87                 if (info_suffix_shown_) {
88                         suffix_timer_->stop();
89                         suffix_timeout();
90                 }
91  
92                 char const * tmp = fl_get_input(ob);
93                 input = tmp ? tmp : "";
94
95                 switch (key) {
96                 case XK_Down:
97 #ifdef XK_KP_Down
98                 case XK_KP_Down:
99 #endif
100                 {
101                         string const h(controller_.historyDown());
102                         if (h.empty()) {
103                                 show_info_suffix(_("[End of history]"), input);
104                         } else {
105                                 set_input(h);
106                         }
107                         return 1;
108                 }
109  
110                 case XK_Up:
111 #ifdef XK_KP_Up
112                 case XK_KP_Up:
113 #endif
114                 {
115                         string const h(controller_.historyUp());
116                         if (h.empty()) {
117                                 show_info_suffix(_("[Beginning of history]"), input);
118                         } else {
119                                 set_input(h);
120                         }
121                         return 1;
122                 }
123  
124                 case 9:
125                 case XK_Tab:
126                 {
127                         string new_input;
128                         vector<string> comp = controller_.completions(input, new_input);
129                          
130                         if (comp.empty() && new_input == input) {
131                                 show_info_suffix(_("[no match]"), input);
132                                 break;
133                         }
134
135                         if (comp.empty()) {
136                                 set_input(new_input);
137                                 show_info_suffix(("[only completion]"), new_input + " ");
138                                 break;
139                         }
140                                  
141                         set_input(new_input);
142
143                         int x,y,w,h;
144                         fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
145                                            &x, &y, &w, &h);
146
147                         // asynchronous completion
148                         int const air = the_buffer_->x;
149                         x += air;
150                         y += h - (the_buffer_->h + air);
151                         w = the_buffer_->w;
152                         dropdown_->select(comp, x, y, w);
153                         return 1;
154                 }
155                 case 27:
156                 case XK_Escape:
157                         messageMode();
158                         return 1;
159                 case 13:
160                 case XK_Return:
161 #ifdef XK_KP_Enter
162                 case XK_KP_Enter:
163 #endif
164                 {
165 #if 0
166                         // This will go in again in a little while
167                         // we need to be able to declare what types
168                         // of argumetns LFUN's should have first. (Lgb)
169                         // First check for match
170                         vector<string>::const_iterator cit =
171                                 find(completion_.begin(),
172                                      completion_.end(),
173                                      input);
174                         if (cit == completion_.end()) {
175                                 // no such func/item
176                                 string const tmp = input + _(" [no match]");
177                                 show_info_suffix(tmp, input);
178                         } else {
179 #endif
180                         messageMode();
181                         redraw();
182                         controller_.dispatch(input);
183 # if 0
184                         }
185 #endif
186                         return 1;
187                 }
188                 default:
189                         return 0;
190                 }
191         }
192         default:
193                 break;
194         }
195
196         return 0;
197 }
198
199
200 extern "C" {
201
202         static
203         int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
204                                      FL_Coord, FL_Coord,
205                                      int key, void * xev)
206         {
207                 XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
208                 return mini->peek_event(ob, event, key,
209                                         static_cast<XEvent *>(xev));
210         }
211 }
212
213
214 FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
215                                           FL_Coord w, FL_Coord h)
216 {
217         FL_OBJECT * obj;
218
219         the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
220         fl_set_object_boxtype(obj, FL_DOWN_BOX);
221         fl_set_object_resize(obj, FL_RESIZE_ALL);
222         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
223         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
224         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
225
226         // To intercept Up, Down, Table for history
227         fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
228         obj->u_vdata = this;
229         obj->wantkey = FL_KEY_TAB;
230
231         return obj;
232 }
233
234
235 void XMiniBuffer::freeze()
236 {
237         // we must prevent peek_event, or we get an unfocus() when the
238         // containing form gets destroyed
239         fl_set_object_prehandler(input_obj_, 0);
240 }
241
242  
243 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
244 {
245         stored_input_ = input;
246         info_suffix_shown_ = true;
247         set_input(input + " " + suffix); 
248         suffix_timer_->start();
249 }
250  
251
252 void XMiniBuffer::idle_timeout()
253 {
254         set_input(currentState(view_->view()));
255 }
256
257  
258 void XMiniBuffer::suffix_timeout()
259 {
260         info_suffix_shown_ = false;
261         set_input(stored_input_);
262 }
263
264  
265 bool XMiniBuffer::isEditingMode() const
266 {
267         return the_buffer_->focus;
268 }
269
270
271 void XMiniBuffer::messageMode(bool on)
272 {
273         set_input("");
274         if (!on) {
275                 fl_activate_object(the_buffer_);
276                 fl_set_focus_object(view_->getForm(), the_buffer_);
277                 redraw();
278                 idle_timer_->stop();
279         } else {
280                 if (isEditingMode()) {
281                         // focus back to the workarea
282                         fl_set_focus_object(view_->getForm(), 0);
283                         idle_timer_->start();
284                 }
285         }
286 }
287
288
289 void XMiniBuffer::redraw()
290 {
291         fl_redraw_object(the_buffer_);
292         XFlush(fl_display);
293 }
294
295
296 void XMiniBuffer::append_char(char c)
297 {
298         if (!c || !isprint(c))
299                 return;
300
301         char const * tmp = fl_get_input(the_buffer_);
302         string str = tmp ? tmp : "";
303
304         str += c;
305
306         fl_set_input(the_buffer_, str.c_str());
307 }
308  
309  
310 void XMiniBuffer::set_complete_input(string const & str)
311 {
312         if (!str.empty()) {
313                 // add a space so the user can type
314                 // an argument immediately
315                 set_input(str + " ");
316         }
317
318  
319
320 void XMiniBuffer::message(string const & str)
321 {
322         if (!isEditingMode())
323                 set_input(str);
324 }
325
326  
327 void XMiniBuffer::set_input(string const & str)
328 {
329         fl_set_input(the_buffer_, str.c_str());
330 }