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