]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Rename .C ==> .cpp for files in src/frontends, part two
[lyx.git] / src / frontends / LyXView.cpp
1 /**
2  * \file LyXView.cpp
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         // Make sure the TOC is updated before anything else.
137         updateToc();
138
139         if (work_area_->bufferView().buffer()) {
140                 // Buffer-dependent dialogs should be updated or
141                 // hidden. This should go here because some dialogs (eg ToC)
142                 // require bv_->text.
143                 getDialogs().updateBufferDependent(true);
144                 connectBuffer(*work_area_->bufferView().buffer());
145         }
146
147         if (quitting)
148                 return;
149
150         updateMenubar();
151         updateToolbars();
152         updateLayoutChoice();
153         updateWindowTitle();
154         updateStatusBar();
155         updateTab();
156         busy(false);
157         work_area_->redraw();
158 }
159
160
161 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
162 {
163         busy(true);
164
165         BOOST_ASSERT(work_area_);
166         if (work_area_->bufferView().buffer())
167                 disconnectBuffer();
168
169         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
170
171         updateToc();
172         updateMenubar();
173         updateToolbars();
174         updateLayoutChoice();
175         updateWindowTitle();
176         updateTab();
177         if (loaded) {
178                 connectBuffer(*work_area_->bufferView().buffer());
179                 showErrorList("Parse");
180         }
181         updateStatusBar();
182         busy(false);
183         work_area_->redraw();
184         return loaded;
185 }
186
187
188 void LyXView::connectBuffer(Buffer & buf)
189 {
190         if (errorsConnection_.connected())
191                 disconnectBuffer();
192
193         BOOST_ASSERT(work_area_);
194         bufferChangedConnection_ =
195                 buf.changed.connect(
196                         boost::bind(&WorkArea::redraw, work_area_));
197
198         bufferStructureChangedConnection_ =
199                 buf.structureChanged.connect(
200                         boost::bind(&LyXView::updateToc, this));
201
202         errorsConnection_ =
203                 buf.errors.connect(
204                         boost::bind(&LyXView::showErrorList, this, _1));
205
206         messageConnection_ =
207                 buf.message.connect(
208                         boost::bind(&LyXView::message, this, _1));
209
210         busyConnection_ =
211                 buf.busy.connect(
212                         boost::bind(&LyXView::busy, this, _1));
213
214         titleConnection_ =
215                 buf.updateTitles.connect(
216                         boost::bind(&LyXView::updateWindowTitle, this));
217
218         timerConnection_ =
219                 buf.resetAutosaveTimers.connect(
220                         boost::bind(&LyXView::resetAutosaveTimer, this));
221
222         readonlyConnection_ =
223                 buf.readonly.connect(
224                         boost::bind(&LyXView::showReadonly, this, _1));
225
226         closingConnection_ =
227                 buf.closing.connect(
228                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
229 }
230
231
232 void LyXView::disconnectBuffer()
233 {
234         errorsConnection_.disconnect();
235         bufferChangedConnection_.disconnect();
236         bufferStructureChangedConnection_.disconnect();
237         messageConnection_.disconnect();
238         busyConnection_.disconnect();
239         titleConnection_.disconnect();
240         timerConnection_.disconnect();
241         readonlyConnection_.disconnect();
242         closingConnection_.disconnect();
243         layout_changed_connection_.disconnect();
244 }
245
246
247 void LyXView::connectBufferView(BufferView & bv)
248 {
249         show_dialog_connection_ = bv.showDialog.connect(
250                         boost::bind(&LyXView::showDialog, this, _1));
251         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
252                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
253         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
254                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
255         update_dialog_connection_ = bv.updateDialog.connect(
256                         boost::bind(&LyXView::updateDialog, this, _1, _2));
257         layout_changed_connection_ = bv.layoutChanged.connect(
258                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
259 }
260
261
262 void LyXView::disconnectBufferView()
263 {
264         show_dialog_connection_.disconnect();
265         show_dialog_with_data_connection_.disconnect();
266         show_inset_dialog_connection_.disconnect();
267         update_dialog_connection_.disconnect();
268 }
269
270
271 void LyXView::showErrorList(string const & error_type)
272 {
273         ErrorList & el = buffer()->errorList(error_type);
274         if (!el.empty()) {
275                 getDialogs().show("errorlist", error_type);
276         }
277 }
278
279
280 void LyXView::showDialog(string const & name)
281 {
282         getDialogs().show(name);
283 }
284
285
286 void LyXView::showDialogWithData(string const & name, string const & data)
287 {
288         getDialogs().show(name, data);
289 }
290
291
292 void LyXView::showInsetDialog(string const & name, string const & data,
293                 InsetBase * inset)
294 {
295         getDialogs().show(name, data, inset);
296 }
297
298
299 void LyXView::updateDialog(string const & name, string const & data)
300 {
301         if (getDialogs().visible(name))
302                 getDialogs().update(name, data);
303 }
304
305
306 void LyXView::showReadonly(bool)
307 {
308         updateWindowTitle();
309         getDialogs().updateBufferDependent(false);
310 }
311
312
313 BufferView * LyXView::view() const
314 {
315         BOOST_ASSERT(work_area_);
316         return &work_area_->bufferView();
317 }
318
319
320 void LyXView::updateToc()
321 {
322         updateDialog("toc", "");
323 }
324
325
326 void LyXView::updateToolbars()
327 {
328         BOOST_ASSERT(work_area_);
329         bool const math =
330                 work_area_->bufferView().cursor().inMathed();
331         bool const table =
332                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
333         bool const review =
334                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
335                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
336                 
337         toolbars_->update(math, table, review);
338         // update redaonly status of open dialogs. This could also be in
339         // updateMenubar(), but since updateToolbars() and updateMenubar()
340         // are always called together it is only here.
341         getDialogs().checkStatus();
342 }
343
344
345 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
346 {
347         return toolbars_->getToolbarState(name);
348 }
349
350
351 void LyXView::toggleToolbarState(string const & name)
352 {
353         // it is possible to get current toolbar status like this,...
354         // but I decide to obey the order of ToolbarBackend::flags
355         // and disregard real toolbar status.
356         // toolbars_->saveToolbarInfo();
357         //
358         // toggle state on/off/auto
359         toolbars_->toggleToolbarState(name);
360         // update toolbar
361         updateToolbars();
362 }
363
364
365 void LyXView::updateMenubar()
366 {
367         menubar_->update();
368 }
369
370
371 void LyXView::autoSave()
372 {
373         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
374
375         if (view()->buffer())
376                 lyx::autoSave(view());
377 }
378
379
380 void LyXView::resetAutosaveTimer()
381 {
382         if (lyxrc.autosave)
383                 autosave_timeout_->restart();
384 }
385
386
387 void LyXView::updateLayoutChoice()
388 {
389         // Don't show any layouts without a buffer
390         if (!view()->buffer()) {
391                 toolbars_->clearLayoutList();
392                 return;
393         }
394
395         // Update the layout display
396         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
397                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
398         }
399
400         BOOST_ASSERT(work_area_);
401         if (work_area_->bufferView().cursor().inMathed())
402                 return;
403
404         string const & layout =
405                 work_area_->bufferView().cursor().paragraph().layout()->name();
406
407         if (layout != current_layout) {
408                 toolbars_->setLayout(layout);
409                 current_layout = layout;
410         }
411 }
412
413
414 void LyXView::updateWindowTitle()
415 {
416         docstring maximize_title = lyx::from_ascii("LyX");
417         docstring minimize_title = lyx::from_ascii("LyX");
418
419         if (view()->buffer()) {
420                 string const cur_title = buffer()->fileName();
421                 if (!cur_title.empty()) {
422                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
423                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
424                         if (!buffer()->isClean()) {
425                                 maximize_title += _(" (changed)");
426                                 minimize_title += lyx::char_type('*');
427                         }
428                         if (buffer()->isReadonly())
429                                 maximize_title += _(" (read only)");
430                 }
431         }
432
433         setWindowTitle(maximize_title, minimize_title);
434         updateTab();
435 }
436
437
438 void LyXView::dispatch(FuncRequest const & cmd)
439 {
440         theLyXFunc().setLyXView(this);
441         lyx::dispatch(cmd);
442 }
443
444
445 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
446 {
447         Buffer const * buffer_ptr = 0;
448         if (inset) {
449                 BOOST_ASSERT(work_area_);
450                 work_area_->scheduleRedraw();
451
452                 buffer_ptr = work_area_->bufferView().buffer();
453         }
454         return buffer_ptr;
455 }
456
457
458 } // namespace lyx