]> git.lyx.org Git - lyx.git/blob - src/minibuffer.C
f365325f3141da1a1ae92256223e435858313b42
[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
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         obj->owner->view()->focus(true);
50
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                         mini->owner->view()->focus(true);
104                         mini->Init();
105                         return 1; 
106                 case 13:
107                 case XK_Return:
108                         // Execute a command. 
109                         mini->cur_cmd = string(fl_get_input(ob));
110                         ExecutingCB(ob, 0);
111                         return 1;
112                 default:
113                         return 0;
114                 }
115         }
116         return 0;
117 }
118
119 extern "C" int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event, 
120                                        FL_Coord, FL_Coord,
121                                        int key, void * xev)
122 {
123         return MiniBuffer::peek_event(ob, event, 0, 0, key, xev);
124 }
125
126
127 void MiniBuffer::ExecCommand()
128 {
129         text.clear();
130         fl_set_input(the_buffer, "");
131         fl_set_focus_object(owner->getForm(), the_buffer);
132 }
133
134
135 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
136                            FL_Coord w, FL_Coord h)
137 {
138         FL_OBJECT * obj;
139         
140         the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
141         fl_set_object_boxtype(obj, FL_DOWN_BOX);
142         fl_set_object_resize(obj, FL_RESIZE_ALL);
143         fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
144         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
145         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
146         fl_set_object_callback(obj, C_MiniBuffer_ExecutingCB, 0);
147
148         // To intercept Up, Down, Table for history
149         fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
150         obj->u_vdata = this;
151         obj->wantkey = FL_KEY_TAB;
152         
153         // timer
154         timer = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "Timer");
155         fl_set_object_callback(timer, C_MiniBuffer_TimerCB, 0);
156         timer->u_vdata = this;
157         fl_set_input(the_buffer, text.c_str());
158
159         return obj;
160 }
161
162
163 // Added optional arg `delay_secs', defaults to 4.
164 //When 0, no timeout is done. RVDK_PATCH_5 
165 void MiniBuffer::Set(string const& s1, string const& s2,
166                      string const& s3, int delay_secs)
167 {
168         setTimer(delay_secs);
169
170         string ntext = strip(s1 + ' ' + s2 + ' ' + s3);
171
172         if (!the_buffer->focus) {
173                 fl_set_input(the_buffer, ntext.c_str());
174                 XFlush(fl_display);
175                 text = ntext;
176         }
177 }
178
179
180 void MiniBuffer::Init()
181 {
182         // If we have focus, we don't want to change anything.
183         if (the_buffer->focus)
184                 return;
185
186         // When meta-fake key is pressed, show the key sequence so far + "M-".
187         if (owner->getLyXFunc()->wasMetaKey()) {
188                 text = owner->getLyXFunc()->keyseqStr();
189                 text += " M-";
190         }
191
192         // Else, when a non-complete key sequence is pressed,
193         // show the available options.
194         else if (owner->getLyXFunc()->keyseqUncomplete()) 
195                 text = owner->getLyXFunc()->keyseqOptions();
196    
197         // Else, show the buffer state.
198         else if (owner->view()->available()) {
199                         string nicename = 
200                                 MakeDisplayPath(owner->buffer()->
201                                                 fileName());
202                         // Should we do this instead? (kindo like emacs)
203                         // leaves more room for other information
204                         text = "LyX: ";
205                         text += nicename;
206                         if (owner->buffer()->lyxvc.inUse()) {
207                                 text += " [";
208                                 text += owner->buffer()->lyxvc.version();
209                                 text += ' ';
210                                 text += owner->buffer()->lyxvc.locker();
211                                 if (owner->buffer()->isReadonly())
212                                         text += " (RO)";
213                                 text += ']';
214                         } else if (owner->buffer()->isReadonly())
215                                 text += " [RO]";
216                         if (!owner->buffer()->isLyxClean())
217                                 text += _(" (Changed)");
218         } else {
219                 if (text != _("Welcome to LyX!")) // this is a hack
220                         text = _("* No document open *");
221         }
222         
223
224         fl_set_input(the_buffer, text.c_str());
225         setTimer(0);
226         XFlush(fl_display);
227 }
228
229
230 // allows to store and reset the contents one time. Usefull for
231 // status messages like "load font" (Matthias)
232 void MiniBuffer::Store()
233 {
234         text_stored = fl_get_input(the_buffer);
235 }
236
237
238 void MiniBuffer::Reset()
239 {
240         if (!text_stored.empty()){
241                 Set(text_stored);
242                 text_stored.clear();
243         }
244 }
245
246 void MiniBuffer::Activate()
247 {
248         fl_activate_object(the_buffer);
249         fl_redraw_object(the_buffer);
250 }
251
252 void MiniBuffer::Deactivate()
253 {
254         fl_deactivate_object(the_buffer);
255 }
256
257