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