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