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