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