]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
fix crash on exit.
[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 "Dialogs.h"
16 #include "Timeout.h"
17 #include "Toolbars.h"
18 #include "Menubar.h"
19 #include "WorkArea.h"
20
21 #include "buffer.h"
22 #include "bufferparams.h"
23 #include "BufferView.h"
24 #include "bufferview_funcs.h"
25 #include "cursor.h"
26 #include "debug.h"
27 #include "errorlist.h"
28 #include "funcrequest.h"
29 #include "gettext.h"
30 #include "intl.h"
31 #include "lyx_cb.h"
32 #include "lyxfunc.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "MenuBackend.h"
36 #include "paragraph.h"
37
38 #include "controllers/ControlCommandBuffer.h"
39
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
42
43 #include <boost/bind.hpp>
44
45
46 namespace lyx {
47
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 # include <unistd.h>
53 #endif
54
55 using lyx::frontend::WorkArea;
56
57 using lyx::docstring;
58 using lyx::support::bformat;
59 using lyx::support::makeDisplayPath;
60 using lyx::support::onlyFilename;
61
62 using std::endl;
63 using std::string;
64
65 using lyx::frontend::ControlCommandBuffer;
66
67 string current_layout;
68
69
70 LyXView::LyXView(int id)
71         : id_(id), work_area_(0),
72           toolbars_(new Toolbars(*this)),
73           autosave_timeout_(new Timeout(5000)),
74           dialogs_(new Dialogs(*this)),
75           controlcommand_(new ControlCommandBuffer(*this))
76 {
77         // Start autosave timer
78         if (lyxrc.autosave) {
79                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
80                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
81                 autosave_timeout_->start();
82         }
83 }
84
85
86 LyXView::~LyXView()
87 {
88 }
89
90
91 // FIXME, there's only one WorkArea per LyXView possible for now.
92 void LyXView::setWorkArea(WorkArea * work_area)
93 {
94         work_area_ = work_area;
95         work_area_ids_.clear();
96         work_area_ids_.push_back(work_area_->id());
97 }
98
99
100 Buffer * LyXView::buffer() const
101 {
102         return work_area_->bufferView().buffer();
103 }
104
105
106 void LyXView::setBuffer(Buffer * b)
107 {
108         if (work_area_->bufferView().buffer())
109                 disconnectBuffer();
110
111         if (!b)
112                 getDialogs().hideBufferDependent();
113
114         work_area_->bufferView().setBuffer(b);
115
116         if (work_area_->bufferView().buffer()) {
117                 // Buffer-dependent dialogs should be updated or
118                 // hidden. This should go here because some dialogs (eg ToC)
119                 // require bv_->text.
120                 getDialogs().updateBufferDependent(true);
121                 connectBuffer(*work_area_->bufferView().buffer());
122         }
123
124         if (quitting)
125                 return;
126
127         updateMenubar();
128         updateToolbars();
129         updateLayoutChoice();
130         updateWindowTitle();
131         updateStatusBar();
132         work_area_->redraw();
133 }
134
135
136 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
137 {
138         if (work_area_->bufferView().buffer())
139                 disconnectBuffer();
140
141         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
142
143         updateMenubar();
144         updateToolbars();
145         updateLayoutChoice();
146         updateWindowTitle();
147         if (loaded) {
148                 connectBuffer(*work_area_->bufferView().buffer());
149                 showErrorList("Parse");
150         }
151         updateStatusBar();
152         work_area_->redraw();
153         return loaded;
154 }
155
156
157 void LyXView::connectBuffer(Buffer & buf)
158 {
159         if (errorsConnection_.connected())
160                 disconnectBuffer();
161
162         bufferChangedConnection_ =
163                 buf.changed.connect(
164                         boost::bind(&WorkArea::redraw, work_area_));
165
166         errorsConnection_ =
167                 buf.errors.connect(
168                         boost::bind(&LyXView::showErrorList, this, _1));
169
170         messageConnection_ =
171                 buf.message.connect(
172                         boost::bind(&LyXView::message, this, _1));
173
174         busyConnection_ =
175                 buf.busy.connect(
176                         boost::bind(&LyXView::busy, this, _1));
177
178         titleConnection_ =
179                 buf.updateTitles.connect(
180                         boost::bind(&LyXView::updateWindowTitle, this));
181
182         timerConnection_ =
183                 buf.resetAutosaveTimers.connect(
184                         boost::bind(&LyXView::resetAutosaveTimer, this));
185
186         readonlyConnection_ =
187                 buf.readonly.connect(
188                         boost::bind(&LyXView::showReadonly, this, _1));
189
190         closingConnection_ =
191                 buf.closing.connect(
192                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
193 }
194
195
196 void LyXView::disconnectBuffer()
197 {
198         errorsConnection_.disconnect();
199         bufferChangedConnection_.disconnect();
200         messageConnection_.disconnect();
201         busyConnection_.disconnect();
202         titleConnection_.disconnect();
203         timerConnection_.disconnect();
204         readonlyConnection_.disconnect();
205         closingConnection_.disconnect();
206         layout_changed_connection_.disconnect();
207 }
208
209
210 void LyXView::connectBufferView(BufferView & bv)
211 {
212         show_dialog_connection_ = bv.showDialog.connect(
213                         boost::bind(&LyXView::showDialog, this, _1));
214         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
215                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
216         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
217                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
218         update_dialog_connection_ = bv.updateDialog.connect(
219                         boost::bind(&LyXView::updateDialog, this, _1, _2));
220         layout_changed_connection_ = bv.layoutChanged.connect(
221                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
222 }
223
224
225 void LyXView::disconnectBufferView()
226 {
227         show_dialog_connection_.disconnect();
228         show_dialog_with_data_connection_.disconnect();
229         show_inset_dialog_connection_.disconnect();
230         update_dialog_connection_.disconnect();
231 }
232
233
234 void LyXView::showErrorList(string const & error_type)
235 {
236         ErrorList & el = buffer()->errorList(error_type);
237         if (!el.empty()) {
238                 getDialogs().show("errorlist", error_type);
239         }
240 }
241
242
243 void LyXView::showDialog(string const & name)
244 {
245         getDialogs().show(name);
246 }
247
248
249 void LyXView::showDialogWithData(string const & name, string const & data)
250 {
251         getDialogs().show(name, data);
252 }
253
254
255 void LyXView::showInsetDialog(string const & name, string const & data,
256                 InsetBase * inset)
257 {
258         getDialogs().show(name, data, inset);
259 }
260
261
262 void LyXView::updateDialog(string const & name, string const & data)
263 {
264         if (getDialogs().visible(name))
265                 getDialogs().update(name, data);
266 }
267
268
269 void LyXView::showReadonly(bool)
270 {
271         updateWindowTitle();
272         getDialogs().updateBufferDependent(false);
273 }
274
275
276 BufferView * LyXView::view() const
277 {
278         return &work_area_->bufferView();
279 }
280
281
282 void LyXView::updateToolbars()
283 {
284         bool const math = work_area_->bufferView().cursor().inMathed();
285         bool const table =
286                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
287         toolbars_->update(math, table);
288         // update redaonly status of open dialogs. This could also be in
289         // updateMenubar(), but since updateToolbars() and updateMenubar()
290         // are always called together it is only here.
291         getDialogs().checkStatus();
292 }
293
294
295 void LyXView::updateMenubar()
296 {
297         menubar_->update();
298 }
299
300
301 void LyXView::autoSave()
302 {
303         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
304
305         if (view()->buffer())
306                 lyx::autoSave(view());
307 }
308
309
310 void LyXView::resetAutosaveTimer()
311 {
312         if (lyxrc.autosave)
313                 autosave_timeout_->restart();
314 }
315
316
317 void LyXView::updateLayoutChoice()
318 {
319         // Don't show any layouts without a buffer
320         if (!view()->buffer()) {
321                 toolbars_->clearLayoutList();
322                 return;
323         }
324
325         // Update the layout display
326         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
327                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
328         }
329
330         if (work_area_->bufferView().cursor().inMathed())
331                 return;
332
333         string const & layout =
334                 work_area_->bufferView().cursor().paragraph().layout()->name();
335
336         if (layout != current_layout) {
337                 toolbars_->setLayout(layout);
338                 current_layout = layout;
339         }
340 }
341
342
343 void LyXView::updateWindowTitle()
344 {
345         static docstring last_title = lyx::from_ascii("LyX");
346         docstring maximize_title = lyx::from_ascii("LyX");
347         docstring minimize_title = lyx::from_ascii("LyX");
348
349         if (view()->buffer()) {
350                 string const cur_title = buffer()->fileName();
351                 if (!cur_title.empty()) {
352                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
353                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
354                         if (!buffer()->isClean()) {
355                                 maximize_title += _(" (changed)");
356                                 minimize_title += lyx::char_type('*');
357                         }
358                         if (buffer()->isReadonly())
359                                 maximize_title += _(" (read only)");
360                 }
361         }
362
363         if (maximize_title != last_title) {
364                 setWindowTitle(maximize_title, minimize_title);
365                 last_title = maximize_title;
366         }
367 }
368
369
370 void LyXView::dispatch(FuncRequest const & cmd)
371 {
372         if (cmd.action == LFUN_WINDOW_CLOSE) {
373                 close();
374                 closed(id_);
375                 return;
376         }
377
378         theLyXFunc().setLyXView(this);
379         lyx::dispatch(cmd);
380 }
381
382
383 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
384 {
385         Buffer const * buffer_ptr = 0;
386         if (inset) {
387                 buffer_ptr = work_area_->bufferView().buffer();
388                 // No FitCursor:
389                 work_area_->bufferView().update(Update::Force);
390         }
391         return buffer_ptr;
392 }
393
394
395 } // namespace lyx