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