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