]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
This commit fixes the crash caused by loading an empty corrupted document.
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "LyXView.h"
15 #include "Gui.h"
16 #include "Dialogs.h"
17 #include "Timeout.h"
18 #include "Toolbars.h"
19 #include "Menubar.h"
20 #include "WorkArea.h"
21
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "bufferview_funcs.h"
26 #include "cursor.h"
27 #include "debug.h"
28 #include "errorlist.h"
29 #include "funcrequest.h"
30 #include "gettext.h"
31 #include "intl.h"
32 #include "lyx_cb.h"
33 #include "lyxfunc.h"
34 #include "lyxrc.h"
35 #include "lyxtext.h"
36 #include "MenuBackend.h"
37 #include "paragraph.h"
38
39 #include "controllers/ControlCommandBuffer.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
43
44 #include <boost/bind.hpp>
45
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52
53 using lyx::frontend::Gui;
54 using lyx::frontend::WorkArea;
55
56 using lyx::support::bformat;
57 using lyx::support::makeDisplayPath;
58 using lyx::support::onlyFilename;
59
60 using std::endl;
61 using std::string;
62
63 using lyx::frontend::ControlCommandBuffer;
64
65 string current_layout;
66
67 Gui & LyXView::gui()
68 {
69         return owner_;
70 }
71
72
73 LyXView::LyXView(Gui & owner)
74         : work_area_(0),
75           owner_(owner),
76           toolbars_(new Toolbars(*this)),
77           intl_(new Intl),
78           autosave_timeout_(new Timeout(5000)),
79           lyxfunc_(new LyXFunc(this)),
80           dialogs_(new Dialogs(*this)),
81           controlcommand_(new ControlCommandBuffer(*this))
82 {
83         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
84 }
85
86 LyXView::~LyXView()
87 {
88 }
89
90
91 void LyXView::setWorkArea(WorkArea * work_area)
92 {
93         work_area_ = work_area;
94 }
95
96
97 void LyXView::redrawWorkArea()
98 {
99         work_area_->redraw();
100         updateStatusBar();
101 }
102
103
104 WorkArea * LyXView::workArea()
105 {
106         return work_area_;
107 }
108
109
110 void LyXView::init()
111 {
112         updateLayoutChoice();
113         updateMenubar();
114
115         // Start autosave timer
116         if (lyxrc.autosave) {
117                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
118                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
119                 autosave_timeout_->start();
120         }
121
122         intl_->initKeyMapper(lyxrc.use_kbmap);
123 }
124
125
126 Buffer * LyXView::buffer() const
127 {
128         return work_area_->bufferView().buffer();
129 }
130
131
132 void LyXView::setBuffer(Buffer * b)
133 {
134         if (work_area_->bufferView().buffer())
135                 disconnectBuffer();
136
137         if (!b)
138                 getDialogs().hideBufferDependent();
139
140         work_area_->bufferView().setBuffer(b);
141
142         if (work_area_->bufferView().buffer()) {
143                 // Buffer-dependent dialogs should be updated or
144                 // hidden. This should go here because some dialogs (eg ToC)
145                 // require bv_->text.
146                 getDialogs().updateBufferDependent(true);
147                 connectBuffer(*work_area_->bufferView().buffer());
148                 setLayout(work_area_->bufferView().firstLayout());
149         }
150
151         updateMenubar();
152         updateToolbars();
153         updateLayoutChoice();
154         updateWindowTitle();
155         redrawWorkArea();
156 }
157
158
159 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
160 {
161         if (work_area_->bufferView().buffer())
162                 disconnectBuffer();
163
164         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
165
166         updateMenubar();
167         updateToolbars();
168         updateLayoutChoice();
169         updateWindowTitle();
170         if (loaded) {
171                 connectBuffer(*work_area_->bufferView().buffer());
172                 setLayout(work_area_->bufferView().firstLayout());
173                 showErrorList("Parse");
174         }
175         redrawWorkArea();
176         return loaded;
177 }
178
179
180 void LyXView::connectBuffer(Buffer & buf)
181 {
182         if (errorsConnection_.connected())
183                 disconnectBuffer();
184
185         errorsConnection_ =
186                 buf.errors.connect(
187                         boost::bind(&LyXView::showErrorList, this, _1));
188
189         messageConnection_ =
190                 buf.message.connect(
191                         boost::bind(&LyXView::message, this, _1));
192
193         busyConnection_ =
194                 buf.busy.connect(
195                         boost::bind(&LyXView::busy, this, _1));
196
197         titleConnection_ =
198                 buf.updateTitles.connect(
199                         boost::bind(&LyXView::updateWindowTitle, this));
200
201         timerConnection_ =
202                 buf.resetAutosaveTimers.connect(
203                         boost::bind(&LyXView::resetAutosaveTimer, this));
204
205         readonlyConnection_ =
206                 buf.readonly.connect(
207                         boost::bind(&LyXView::showReadonly, this, _1));
208
209         closingConnection_ =
210                 buf.closing.connect(
211                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
212 }
213
214
215 void LyXView::disconnectBuffer()
216 {
217         messageConnection_.disconnect();
218         busyConnection_.disconnect();
219         titleConnection_.disconnect();
220         timerConnection_.disconnect();
221         readonlyConnection_.disconnect();
222         closingConnection_.disconnect();
223 }
224
225
226 void LyXView::showErrorList(string const & error_type)
227 {
228         ErrorList & el = buffer()->errorList(error_type);
229         if (!el.empty()) {
230                 getDialogs().show("errorlist", error_type);
231         }
232 }
233
234
235 void LyXView::showReadonly(bool)
236 {
237         updateWindowTitle();
238         getDialogs().updateBufferDependent(false);
239 }
240
241
242 BufferView * LyXView::view() const
243 {
244         return &work_area_->bufferView();
245 }
246
247
248 void LyXView::setLayout(string const & layout)
249 {
250         toolbars_->setLayout(layout);
251 }
252
253
254 void LyXView::updateToolbars()
255 {
256         bool const math = work_area_->bufferView().cursor().inMathed();
257         bool const table =
258                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
259         toolbars_->update(math, table);
260         // update redaonly status of open dialogs. This could also be in
261         // updateMenubar(), but since updateToolbars() and updateMenubar()
262         // are always called together it is only here.
263         getDialogs().checkStatus();
264 }
265
266
267 void LyXView::updateMenubar()
268 {
269         menubar_->update();
270 }
271
272
273 void LyXView::autoSave()
274 {
275         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
276
277         if (view()->available()) {
278                 ::autoSave(view());
279         }
280 }
281
282
283 void LyXView::resetAutosaveTimer()
284 {
285         if (lyxrc.autosave)
286                 autosave_timeout_->restart();
287 }
288
289
290 void LyXView::updateLayoutChoice()
291 {
292         // Don't show any layouts without a buffer
293         if (!view()->buffer()) {
294                 toolbars_->clearLayoutList();
295                 return;
296         }
297
298         // Update the layout display
299         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
300                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
301         }
302
303         if (work_area_->bufferView().cursor().inMathed())
304                 return;
305
306         string const & layout =
307                 work_area_->bufferView().cursor().paragraph().layout()->name();
308
309         if (layout != current_layout) {
310                 toolbars_->setLayout(layout);
311                 current_layout = layout;
312         }
313 }
314
315
316 void LyXView::updateWindowTitle()
317 {
318         static string last_title = "LyX";
319         string maximize_title = "LyX";
320         string minimize_title = "LyX";
321
322         if (view()->available()) {
323                 string const cur_title = buffer()->fileName();
324                 if (!cur_title.empty()) {
325                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
326                         minimize_title = onlyFilename(cur_title);
327                         if (!buffer()->isClean()) {
328                                 maximize_title += _(" (changed)");
329                                 minimize_title += '*';
330                         }
331                         if (buffer()->isReadonly())
332                                 maximize_title += _(" (read only)");
333                 }
334         }
335
336         if (maximize_title != last_title) {
337                 setWindowTitle(maximize_title, minimize_title);
338                 last_title = maximize_title;
339         }
340 }
341
342
343 void LyXView::dispatch(FuncRequest const & cmd)
344 {
345         getLyXFunc().dispatch(cmd);
346 }
347
348
349 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
350 {
351         Buffer const * buffer_ptr = 0;
352         if (inset) {
353                 buffer_ptr = work_area_->bufferView().buffer();
354                 // No FitCursor:
355                 work_area_->bufferView().update(Update::Force);
356         }
357         return buffer_ptr;
358 }