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