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