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