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