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