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