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