]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
LyX Drinkers Union: patch 1
[lyx.git] / src / LyXView.C
1
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include "LyXView.h"
22 #include "lyx_main.h"
23 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
24 #include "lyxlookup.h"
25 #endif
26 #include "minibuffer.h"
27 #include "debug.h"
28 #include "intl.h"
29 #include "lyxrc.h"
30 #include "support/filetools.h"        // OnlyFilename()
31 #include "layout.h"
32 #include "lyxtext.h"
33 #include "buffer.h"
34 #include "frontends/Dialogs.h"
35 #include "frontends/Toolbar.h"
36 #include "frontends/Menubar.h"
37 #include "MenuBackend.h"
38 #include "ToolbarDefaults.h"
39 #include "lyx_gui_misc.h"       // [update,Close,Redraw]AllBufferRelatedDialogs
40 #include "bufferview_funcs.h" // CurrentState()
41 #include "gettext.h"
42 #include "lyxfunc.h"
43 #include "BufferView.h"
44
45 using std::endl;
46
47 extern void AutoSave(BufferView *);
48 extern void QuitLyX();
49 LyXTextClass::size_type current_layout = 0;
50
51 // This is very temporary
52 BufferView * current_view;
53
54 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
55
56
57 LyXView::LyXView(int width, int height)
58 {
59         create_form_form_main(width, height);
60         fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
61         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
62         lyxfunc = new LyXFunc(this);
63
64         // Connect the minibuffer signals
65         minibuffer->stringReady.connect(SigC::slot(lyxfunc,
66                                                    &LyXFunc::miniDispatch));
67         minibuffer->timeout.connect(SigC::slot(lyxfunc,
68                                                &LyXFunc::initMiniBuffer));
69         
70         intl = new Intl;
71
72         // Make sure the buttons are disabled if needed.
73         toolbar->update();
74         menubar->update();
75
76         dialogs_ = new Dialogs(this);
77         // temporary until all dialogs moved into Dialogs.
78         dialogs_->updateBufferDependent
79                 .connect(SigC::slot(&updateAllVisibleBufferRelatedDialogs));
80         dialogs_->hideBufferDependent
81                 .connect(SigC::slot(&CloseAllBufferRelatedDialogs));
82         Dialogs::redrawGUI.connect(SigC::slot(this, &LyXView::redraw));
83         Dialogs::redrawGUI.connect(SigC::slot(&RedrawAllBufferRelatedDialogs));
84 }
85
86
87 LyXView::~LyXView()
88 {
89         delete menubar;
90         delete toolbar;
91         delete bufferview;
92         delete minibuffer;
93         delete lyxfunc;
94         delete intl;
95         delete dialogs_;
96 }
97
98
99 /// Redraw the main form.
100 void LyXView::redraw() {
101         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
102         fl_redraw_form(form_);
103         minibuffer->redraw();
104 }
105
106
107 void LyXView::resize() 
108 {
109         view()->resize();
110 }
111
112
113 /// returns the buffer currently shown in the main form.
114 Buffer * LyXView::buffer() const
115 {
116         return bufferview->buffer();
117 }
118
119
120 BufferView * LyXView::view() const
121 {
122         return bufferview;
123 }
124
125
126 FL_FORM * LyXView::getForm() const
127 {
128         return form_;
129 }
130
131
132 Toolbar * LyXView::getToolbar() const
133 {
134         return toolbar;
135 }
136
137
138 void LyXView::setLayout(LyXTextClass::size_type layout)
139 {
140         toolbar->setLayout(layout);
141 }
142
143
144 void LyXView::updateToolbar()
145 {
146         toolbar->update();
147         menubar->update();
148 }
149
150
151 LyXFunc * LyXView::getLyXFunc() const
152 {
153         return lyxfunc;
154 }
155
156
157 MiniBuffer * LyXView::getMiniBuffer() const
158 {
159         return minibuffer;
160 }
161
162
163 void LyXView::message(string const & str)
164 {
165         minibuffer->message(str);
166 }
167
168
169 void LyXView::messagePush(string const & str)
170 {
171         minibuffer->messagePush(str);
172 }
173
174
175 void LyXView::messagePop()
176 {
177         minibuffer->messagePop();
178 }
179
180
181 Menubar * LyXView::getMenubar() const
182 {
183         return menubar;
184 }
185
186
187 void LyXView::updateMenubar() 
188 {
189         if ((!view() || !view()->buffer())
190             && menubackend.hasMenu("main_nobuffer"))
191                 menubar->set("main_nobuffer");
192         else
193                 menubar->set("main");
194 }
195
196
197 Intl * LyXView::getIntl() const
198 {
199         return intl;
200 }
201
202
203 // Callback for autosave timer
204 void LyXView::AutoSave()
205 {
206         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
207         if (view()->available())
208                 ::AutoSave(view());
209 }
210
211
212 /// Reset autosave timer
213 void LyXView::resetAutosaveTimer()
214 {
215         if (lyxrc.autosave)
216                 autosave_timeout.restart();
217 }
218
219
220 // Callback for close main form from window manager
221 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
222 {
223         QuitLyX();
224         return FL_IGNORE;
225 }
226
227
228 // Wrapper for the above
229 extern "C"
230 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
231 {
232         return LyXView::atCloseMainFormCB(form, p);
233 }
234
235
236 void LyXView::setPosition(int x, int y)
237 {
238         fl_set_form_position(form_, x, y);
239 }
240
241
242 void LyXView::show(int place, int border, string const & title)
243 {
244         fl_show_form(form_, place, border, title.c_str());
245         lyxfunc->initMiniBuffer();
246 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
247         InitLyXLookup(fl_get_display(), form_->window);
248 #endif
249 }
250
251
252 void LyXView::create_form_form_main(int width, int height)
253         /* to make this work as it should, .lyxrc should have been
254          * read first; OR maybe this one should be made dynamic.
255          * Hmmmm. Lgb. 
256          * We will probably not have lyxrc before the main form is
257          * initialized, because error messages from lyxrc parsing 
258          * are presented (and rightly so) in GUI popups. Asger. 
259          */
260 {
261         // the main form
262         form_ = fl_bgn_form(FL_NO_BOX, width, height);
263         form_->u_vdata = this;
264         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
265         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
266
267         // Parameters for the appearance of the main form
268         int const air = 2;
269         int const bw = abs(fl_get_border_width());
270         
271         //
272         // THE MENUBAR
273         //
274         menubar = new Menubar(this, menubackend);
275
276         //
277         // TOOLBAR
278         //
279
280         toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
281
282         // Setup the toolbar
283         toolbar->set(true);
284
285         //
286         // WORKAREA
287         //
288
289         int const ywork = 60 + 2 * air + bw;
290         int const workheight = height - ywork - (25 + 2 * air);
291
292         ::current_view = bufferview = new BufferView(this, air, ywork,
293                                                      width - 3 * air,
294                                                      workheight);
295
296         //
297         // MINIBUFFER
298         //
299
300         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
301                                     width - (2 * air), 25);
302
303         //
304         // TIMERS
305         //
306
307         autosave_timeout.timeout.connect(SigC::slot(this, &LyXView::AutoSave));
308         
309         //
310         // Misc
311         //
312
313         //  assign an icon to main form
314         string iconname = LibFileSearch("images", "lyx", "xpm");
315         if (!iconname.empty()) {
316                 unsigned int w, h;
317                 Pixmap lyx_p, lyx_mask;
318                 lyx_p = fl_read_pixmapfile(fl_root,
319                                            iconname.c_str(),
320                                            &w,
321                                            &h,
322                                            &lyx_mask,
323                                            0,
324                                            0,
325                                            0); // this leaks
326                 fl_set_form_icon(form_, lyx_p, lyx_mask);
327         }
328
329         // set min size
330         fl_set_form_minsize(form_, 50, 50);
331         
332         fl_end_form();
333 }
334
335
336 void LyXView::init()
337 {
338         // Set the textclass choice
339         invalidateLayoutChoice();
340         updateLayoutChoice();
341         updateMenubar();
342         
343         // Start autosave timer
344         if (lyxrc.autosave) {
345                 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
346                 autosave_timeout.start();
347         }
348
349         intl->InitKeyMapper(lyxrc.use_kbmap);
350 }
351
352
353 void LyXView::invalidateLayoutChoice()
354 {
355         last_textclass = -1;
356 }
357
358
359 void LyXView::updateLayoutChoice()
360 {
361         // This has a side-effect that the layouts are not showed when no
362         // document is loaded.
363         if (!view() || !view()->buffer()) {
364                 toolbar->clearLayoutList();
365                 return; 
366         }
367
368         // Update the layout display
369         if (last_textclass != int(buffer()->params.textclass)) {
370                 toolbar->updateLayoutList(true);
371                 last_textclass = int(buffer()->params.textclass);
372                 current_layout = 0;
373         } else
374                 toolbar->updateLayoutList(false);
375
376         
377
378         LyXTextClass::size_type layout =
379                 bufferview->text->cursor.par()->getLayout();
380
381         if (layout != current_layout){
382                 toolbar->setLayout(layout);
383                 current_layout = layout;
384         }
385 }
386
387
388 // Updates the title of the window with the filename of the current document
389 void LyXView::updateWindowTitle()
390 {
391         static string last_title = "LyX";
392         string title = "LyX";
393
394         if (view()->available()) {
395                 string const cur_title = buffer()->fileName();
396                 if (!cur_title.empty()){
397                         title += ": " + MakeDisplayPath(cur_title, 30);
398                         if (!buffer()->isLyxClean())
399                                 title += _(" (Changed)");
400                         if (buffer()->isReadonly())
401                                 title += _(" (read only)");
402                 }
403         }
404         // Don't update title if it's the same as last time
405         if (title != last_title) {
406                 fl_set_form_title(form_, title.c_str());
407                 last_title = title;
408         }
409 }
410
411
412 void LyXView::showState()
413 {
414         message(CurrentState(view()));
415         getToolbar()->update();
416         menubar->update();
417 }