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