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