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