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