]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XMiniBuffer.C
Strip out another 180 #includes.
[lyx.git] / src / frontends / xforms / XMiniBuffer.C
1 /**
2  * \file XMiniBuffer.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars
7  * \author Asger and Juergen
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "XMiniBuffer.h"
15
16 #include "freebrowser.h"
17 #include "xforms_helpers.h"
18
19 #include "controllers/ControlCommandBuffer.h"
20
21 #include "frontends/Timeout.h"
22
23 #include "gettext.h"
24
25 #include <boost/bind.hpp>
26
27
28 #ifndef CXX_GLOBAL_CSTD
29 using std::isprint;
30 #endif
31
32 using std::endl;
33 using std::vector;
34
35
36 namespace {
37
38 /// This creates the input widget for the minibuffer
39 FL_OBJECT * create_input_box(void * parent, int type,
40                              FL_Coord, FL_Coord, FL_Coord, FL_Coord);
41
42 FL_FREEBROWSER * create_freebrowser(void * parent);
43
44 } // namespace anon
45
46
47 XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
48                          FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
49         : controller_(control),
50           info_shown_(false)
51 {
52         input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
53         freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
54
55         info_timer_.reset(new Timeout(1500));
56         idle_timer_.reset(new Timeout(6000));
57         info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
58         idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
59         idle_timer_->start();
60         messageMode();
61 }
62
63
64 // This is here so that scoped ptr will not require a complete type.
65 XMiniBuffer::~XMiniBuffer()
66 {}
67
68
69 void XMiniBuffer::freebrowserCB(int action)
70 {
71         if (action < 0 || action > 1)
72                 // unrecognized action
73                 return;
74
75         if (action == 0)
76                 // The freebrowser has been hidden
77                 return;
78
79         if (freebrowser_->last_printable) {
80                 // Append this char to the current input contents
81                 string input = getString(input_);
82                 input += freebrowser_->last_printable;
83                 fl_set_input(input_, input.c_str());
84
85         } else {
86                 // Fill the input widget with the selected
87                 // browser entry.
88                 FL_OBJECT * browser = freebrowser_->browser;
89                 string const str = getString(browser);
90
91                 if (!str.empty()) {
92                         // add a space so the user can type
93                         // an argument immediately
94                         set_input(str + ' ');
95                 }
96         }
97 }
98
99
100 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
101                             int key, XEvent * /*xev*/)
102 {
103         switch (event) {
104         case FL_FOCUS:
105                 messageMode(false);
106                 break;
107         case FL_UNFOCUS:
108                 messageMode();
109                 break;
110         case FL_KEYBOARD:
111         {
112                 string input;
113                 if (info_shown_) {
114                         info_timer_->stop();
115                         info_timeout();
116                 }
117
118                 char const * tmp = fl_get_input(ob);
119                 input = tmp ? tmp : "";
120
121                 switch (key) {
122                 case XK_Down:
123 #ifdef XK_KP_Down
124                 case XK_KP_Down:
125 #endif
126                 {
127                         string const h(controller_.historyDown());
128                         if (h.empty()) {
129                                 show_info(_("[End of history]"), input, false);
130                         } else {
131                                 set_input(h);
132                         }
133                         return 1;
134                 }
135
136                 case XK_Up:
137 #ifdef XK_KP_Up
138                 case XK_KP_Up:
139 #endif
140                 {
141                         string const h(controller_.historyUp());
142                         if (h.empty()) {
143                                 show_info(_("[Beginning of history]"), input, false);
144                         } else {
145                                 set_input(h);
146                         }
147                         return 1;
148                 }
149
150                 case 9:
151                 case XK_Tab:
152                 {
153                         string new_input;
154                         vector<string> comp = controller_.completions(input, new_input);
155
156                         if (comp.empty() && new_input == input) {
157                                 show_info(_("[no match]"), input);
158                                 break;
159                         }
160
161                         if (comp.empty()) {
162                                 set_input(new_input);
163                                 show_info(_("[only completion]"), new_input + ' ');
164                                 break;
165                         }
166
167                         set_input(new_input);
168
169                         // Fill freebrowser_'s browser with the list of
170                         // available completions
171                         FL_OBJECT * browser = freebrowser_->browser;
172                         fl_clear_browser(browser);
173                         vector<string>::const_iterator cit = comp.begin();
174                         vector<string>::const_iterator end = comp.end();
175                         for (; cit != end; ++cit) {
176                                 fl_add_browser_line(browser, cit->c_str());
177                         }
178                         fl_select_browser_line(browser, 1);
179
180                         // Set the position of the freebrowser and display it.
181                         int x,y,w,h;
182                         fl_get_wingeometry(fl_get_real_object_window(input_),
183                                            &x, &y, &w, &h);
184
185                         // asynchronous completion
186                         int const air = input_->x;
187                         x += air;
188                         y += h - (input_->h + air);
189                         w = input_->w;
190                         h = 100;
191
192                         fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
193                         return 1;
194                 }
195                 case 27:
196                 case XK_Escape:
197                         messageMode();
198                         return 1;
199                 case 13:
200                 case XK_Return:
201 #ifdef XK_KP_Enter
202                 case XK_KP_Enter:
203 #endif
204                 {
205                         messageMode();
206                         redraw();
207                         controller_.dispatch(input);
208                         return 1;
209                 }
210                 default:
211                         return 0;
212                 }
213         }
214         default:
215                 break;
216         }
217
218         return 0;
219 }
220
221
222 void XMiniBuffer::freeze()
223 {
224         // we must prevent peek_event, or we get an unfocus() when the
225         // containing form gets destroyed
226         fl_set_object_prehandler(input_, 0);
227 }
228
229
230 void XMiniBuffer::show_info(string const & info, string const & input, bool append)
231 {
232         stored_input_ = input;
233         info_shown_ = true;
234         if (append)
235                 set_input(input + ' ' + info);
236         else
237                 set_input(info);
238         info_timer_->start();
239 }
240
241
242 void XMiniBuffer::idle_timeout()
243 {
244         set_input(controller_.getCurrentState());
245 }
246
247
248 void XMiniBuffer::info_timeout()
249 {
250         info_shown_ = false;
251         set_input(stored_input_);
252 }
253
254
255 bool XMiniBuffer::isEditingMode() const
256 {
257         return input_->focus;
258 }
259
260
261 void XMiniBuffer::messageMode(bool on)
262 {
263         set_input("");
264         if (!on) {
265                 fl_activate_object(input_);
266                 fl_set_focus_object(input_->form, input_);
267                 redraw();
268                 idle_timer_->stop();
269         } else {
270                 if (isEditingMode()) {
271                         // focus back to the workarea
272                         fl_set_focus_object(input_->form, 0);
273                         idle_timer_->start();
274                 }
275         }
276 }
277
278
279 void XMiniBuffer::redraw()
280 {
281         fl_redraw_object(input_);
282         XFlush(fl_display);
283 }
284
285
286 void XMiniBuffer::message(string const & str)
287 {
288         if (!isEditingMode())
289                 set_input(str);
290 }
291
292
293 void XMiniBuffer::set_input(string const & str)
294 {
295         fl_set_input(input_, str.c_str());
296 }
297
298
299 namespace {
300
301 extern "C"
302 int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
303                              FL_Coord, FL_Coord,
304                              int key, void * xev)
305 {
306         XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
307         return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
308 }
309
310
311 extern "C"
312 void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
313 {
314         if (!fb || !fb->parent)
315                 return;
316
317         XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
318         ptr->freebrowserCB(action);
319 }
320
321
322 FL_OBJECT * create_input_box(void * parent, int type,
323                              FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
324 {
325         FL_OBJECT * obj = fl_add_input(type, x, y, w, h, "");
326         fl_set_object_boxtype(obj, FL_DOWN_BOX);
327         fl_set_object_resize(obj, FL_RESIZE_ALL);
328         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
329         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
330         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
331
332         // To intercept Up, Down, Table for history
333         fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
334         obj->u_vdata = parent;
335         obj->wantkey = FL_KEY_TAB;
336
337         return obj;
338 }
339
340
341 FL_FREEBROWSER * create_freebrowser(void * parent)
342 {
343         FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
344         fb->want_printable = 1;
345         fb->callback = C_freebrowserCB;
346         return fb;
347 }
348
349 } // namespace anon