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