]> git.lyx.org Git - lyx.git/blob - src/minibuffer.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / minibuffer.C
1 /* ###########################################################################
2  *
3  *                 The MiniBuffer Class
4  *                 read minibuffer.h for more
5  *                 information.
6  * 
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.  
9  * 
10  * ###########################################################################
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation "minibuffer.h"
17 #endif
18
19 #include "support/filetools.h"
20 #include "lyx_main.h" 
21 #include "lyxfunc.h"
22 #include FORMS_H_LOCATION
23 #include "minibuffer.h"  
24 #include "LyXView.h"
25 #include "debug.h"
26 #include "gettext.h"
27 #include "LyXAction.h"
28 #include "BufferView.h"
29 #include "buffer.h"
30
31 using std::endl;
32
33 #ifdef SIGC_CXX_NAMESPACES
34 using SigC::slot;
35 #endif
36
37 extern bool keyseqUncomplete();
38 extern string keyseqOptions(int l = 190);
39 extern string keyseqStr(int l = 190);
40 extern LyXAction lyxaction;
41
42 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
43                        FL_Coord h, FL_Coord w)
44         : owner(o)
45 {
46         text = _("Welcome to LyX!");
47         shows_no_match = true;
48         history_idx = history_cnt = 0;
49         add(FL_NORMAL_INPUT, x, y, h, w);
50         timer.timeout.connect(slot(this, &MiniBuffer::Init));
51 }
52
53
54 void MiniBuffer::ExecutingCB(FL_OBJECT * ob, long)
55 {
56         MiniBuffer * obj = static_cast<MiniBuffer*>(ob->u_vdata);
57         lyxerr.debug() << "Getting ready to execute: " << obj->cur_cmd << endl;
58         obj->owner->view()->focus(true);
59
60         if (obj->cur_cmd.empty()) { 
61                 obj->Init();
62                 return ; 
63         }
64         obj->Set(_("Executing:"), obj->cur_cmd);
65         obj->addHistory(obj->cur_cmd);
66         
67         // Dispatch only returns requested data for a few commands (ale)
68         string res = obj->owner->getLyXFunc()->Dispatch(obj->cur_cmd);
69         lyxerr.debug() << "Minibuffer Res: " << res << endl;
70         obj->shows_no_match = false;
71
72         return ;
73 }
74
75
76 extern "C" void C_MiniBuffer_ExecutingCB(FL_OBJECT * ob, long)
77 {
78         MiniBuffer * obj = static_cast<MiniBuffer*>(ob->u_vdata);
79         obj->Init();
80 }
81
82
83 // This is not as dirty as it seems, the hidden buttons removed by this
84 // function were just kludges for an uncomplete keyboard callback (ale)
85 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
86                            int key, void */*xev*/)
87 {
88         MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
89
90         switch (event) {
91         case FL_KEYBOARD:
92                 switch (key) {
93                 case XK_Down:
94                         mini->history_idx++;
95                         if (!mini->getHistory().empty()) {
96                                 fl_set_input(ob, mini->getHistory().c_str());
97                         } else
98                                 mini->history_idx--;
99                         return 1; 
100                 case XK_Up:
101                         if (mini->history_idx > 0) mini->history_idx--;
102                         fl_set_input(ob, mini->getHistory().c_str());
103                         return 1; 
104                 case 9:
105                 case XK_Tab:
106                 {
107                         // complete or increment the command
108                         string s(lyxaction.getApproxFuncName(fl_get_input(ob)));
109                         if (!s.empty())
110                                 fl_set_input(ob, s.c_str());
111                         return 1; 
112                 }
113                 case 27:
114                 case XK_Escape:
115                         // Abort
116                         mini->owner->view()->focus(true);
117                         mini->Init();
118                         return 1; 
119                 case 13:
120                 case XK_Return:
121                         // Execute a command. 
122                         mini->cur_cmd = string(fl_get_input(ob));
123                         ExecutingCB(ob, 0);
124                         return 1;
125                 default:
126                         return 0;
127                 }
128         case FL_PUSH:
129                 // This actually clears the buffer.
130                 mini->PrepareForCommand();
131                 return 1;
132         case FL_DRAW:
133                 //lyxerr << "Minibuffer event: DRAW" << endl;
134                 break;
135         default:
136                 //lyxerr << "Unhandled minibuffer event!" << endl;
137                 break;
138         }
139         
140
141         return 0;
142 }
143
144
145 extern "C" int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event, 
146                                        FL_Coord, FL_Coord,
147                                        int key, void * xev)
148 {
149         return MiniBuffer::peek_event(ob, event, 0, 0, key, xev);
150 }
151
152
153 void MiniBuffer::PrepareForCommand()
154 {
155         text.erase();
156         fl_set_input(the_buffer, "");
157         fl_set_focus_object(owner->getForm(), the_buffer);
158 }
159
160
161 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
162                            FL_Coord w, FL_Coord h)
163 {
164         FL_OBJECT * obj;
165         
166         the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
167         fl_set_object_boxtype(obj, FL_DOWN_BOX);
168         fl_set_object_resize(obj, FL_RESIZE_ALL);
169         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
170         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
171         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
172         fl_set_object_callback(obj, C_MiniBuffer_ExecutingCB, 0);
173
174         // To intercept Up, Down, Table for history
175         fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
176         obj->u_vdata = this;
177         obj->wantkey = FL_KEY_TAB;
178
179         fl_set_input(the_buffer, text.c_str());
180         
181         return obj;
182 }
183
184
185 // Added optional arg `delay_secs', defaults to 4.
186 //When 0, no timeout is done. RVDK_PATCH_5 
187 void MiniBuffer::Set(string const& s1, string const& s2,
188                      string const& s3, unsigned int delay_secs)
189 {
190         if (delay_secs)
191                 timer.setTimeout(delay_secs * 1000).restart();
192         else
193                 timer.stop();
194         
195         string ntext = strip(s1 + ' ' + s2 + ' ' + s3);
196
197         if (!the_buffer->focus) {
198                 fl_set_input(the_buffer, ntext.c_str());
199                 XFlush(fl_get_display());
200                 text = ntext;
201         }
202 }
203
204
205 void MiniBuffer::Init()
206 {
207         // If we have focus, we don't want to change anything.
208         if (the_buffer->focus)
209                 return;
210
211         // When meta-fake key is pressed, show the key sequence so far + "M-".
212         if (owner->getLyXFunc()->wasMetaKey()) {
213                 text = owner->getLyXFunc()->keyseqStr();
214                 text += " M-";
215         }
216
217         // Else, when a non-complete key sequence is pressed,
218         // show the available options.
219         else if (owner->getLyXFunc()->keyseqUncomplete()) 
220                 text = owner->getLyXFunc()->keyseqOptions();
221    
222         // Else, show the buffer state.
223         else if (owner->view()->available()) {
224                         string nicename = 
225                                 MakeDisplayPath(owner->buffer()->
226                                                 fileName());
227                         // Should we do this instead? (kindo like emacs)
228                         // leaves more room for other information
229                         text = "LyX: ";
230                         text += nicename;
231                         if (owner->buffer()->lyxvc.inUse()) {
232                                 text += " [";
233                                 text += owner->buffer()->lyxvc.version();
234                                 text += ' ';
235                                 text += owner->buffer()->lyxvc.locker();
236                                 if (owner->buffer()->isReadonly())
237                                         text += " (RO)";
238                                 text += ']';
239                         } else if (owner->buffer()->isReadonly())
240                                 text += " [RO]";
241                         if (!owner->buffer()->isLyxClean())
242                                 text += _(" (Changed)");
243         } else {
244                 if (text != _("Welcome to LyX!")) // this is a hack
245                         text = _("* No document open *");
246         }
247         
248
249         fl_set_input(the_buffer, text.c_str());
250
251         timer.stop();
252
253         XFlush(fl_get_display());
254 }
255
256
257 // allows to store and reset the contents one time. Usefull for
258 // status messages like "load font" (Matthias)
259 void MiniBuffer::Store()
260 {
261         text_stored = fl_get_input(the_buffer);
262 }
263
264
265 void MiniBuffer::Reset()
266 {
267         if (!text_stored.empty()){
268                 Set(text_stored);
269                 text_stored.erase();
270         }
271 }
272
273
274 void MiniBuffer::Activate()
275 {
276         fl_activate_object(the_buffer);
277         fl_redraw_object(the_buffer);
278 }
279
280
281 void MiniBuffer::Deactivate()
282 {
283         fl_deactivate_object(the_buffer);
284 }