3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
23 #include "buffer_funcs.h"
24 #include "BufferList.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
30 #include "ErrorList.h"
31 #include "FuncRequest.h"
39 #include "MenuBackend.h"
40 #include "Paragraph.h"
42 #include "controllers/ControlCommandBuffer.h"
44 #include "support/lstrings.h"
45 #include "support/filetools.h" // OnlyFilename()
47 #include <boost/bind.hpp>
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
59 using frontend::WorkArea;
61 using support::bformat;
62 using support::FileName;
63 using support::makeDisplayPath;
64 using support::onlyFilename;
69 using lyx::frontend::ControlCommandBuffer;
71 docstring current_layout;
74 LyXView::LyXView(int id)
76 toolbars_(new Toolbars(*this)),
77 autosave_timeout_(new Timeout(5000)),
78 dialogs_(new Dialogs(*this)),
79 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
81 // Start autosave timer
83 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
84 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
85 autosave_timeout_->start();
96 // FIXME, there's only one WorkArea per LyXView possible for now.
97 void LyXView::setWorkArea(WorkArea * work_area)
99 BOOST_ASSERT(work_area);
100 work_area_ = work_area;
101 work_area_ids_.clear();
102 work_area_ids_.push_back(work_area_->id());
106 // FIXME, there's only one WorkArea per LyXView possible for now.
107 WorkArea const * LyXView::currentWorkArea() const
113 // FIXME, there's only one WorkArea per LyXView possible for now.
114 WorkArea * LyXView::currentWorkArea()
120 Buffer * LyXView::buffer() const
122 BOOST_ASSERT(work_area_);
123 return work_area_->bufferView().buffer();
127 void LyXView::setBuffer(Buffer * b, bool child_document)
131 BOOST_ASSERT(work_area_);
132 Buffer * oldBuffer = work_area_->bufferView().buffer();
133 // parentfilename will be used in case when we switch to a child
134 // document (hence when child_document is true)
135 string parentfilename;
137 parentfilename = oldBuffer->fileName();
139 if (!b && theBufferList().empty())
140 getDialogs().hideBufferDependent();
142 Buffer * newBuffer = work_area_->bufferView().setBuffer(b);
145 //Are we closing an oldBuffer which was a child document?
146 if (!b && oldBuffer && oldBuffer->getMasterBuffer() != oldBuffer)
147 // Update the labels and section numbering of its master Buffer.
148 updateLabels(*oldBuffer->getMasterBuffer());
149 //Are we opening a new child document?
150 else if (child_document && newBuffer->getMasterBuffer() != oldBuffer) {
151 // Set the parent name of the child document.
152 // This makes insertion of citations and references in the child work,
153 // when the target is in the parent or another child document.
154 newBuffer->setParentName(parentfilename);
155 // Update the labels and section numbering to the new master Buffer.
156 updateLabels(*newBuffer->getMasterBuffer());
158 //Now that all the updating of the old buffer has been done, we can
159 //connect the new buffer. Note that this will also disconnect the old
160 //buffer, if such there is.
161 //FIXME Is it clear that this should go right here? Or should it go
162 //earlier before the previous if (in which case we'd remove the "else")?
163 connectBuffer(*newBuffer);
165 /* FIXME: We need to rebuild the Toc dialog before the others even
166 if it will be rebuilt again in the next line. This avoid a crash when
167 other dialogs are rebuilt before the Toc dialog. The reason is
168 that closing a Buffer triggers an update of all opened dialogs
169 when dispatching LFUN_DIALOG_UPDATE (hence the patch).
170 The path is as following:
171 setBuffer() -> updateBufferDependent() -> RestoreButton() -> LFUN
172 The problem here is that the Toc dialog has not been
173 reconstructed (because it comes after in the list of dialogs). */
176 // Buffer-dependent dialogs should be updated or
177 // hidden. This should go here because some dialogs (eg ToC)
178 // require bv_->text.
179 getDialogs().updateBufferDependent(true);
181 //Disconnect the old buffer...there's no new one.
189 updateLayoutChoice();
194 work_area_->redraw();
198 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
199 bool child_document, bool auto_open)
203 BOOST_ASSERT(work_area_);
204 string parentfilename;
205 Buffer * oldBuffer = work_area_->bufferView().buffer();
207 parentfilename = oldBuffer->fileName();
209 Buffer * newBuffer = checkAndLoadLyXFile(filename);
212 message(_("Document not loaded."));
215 work_area_->redraw();
219 if (child_document && newBuffer != oldBuffer) {
220 // Set the parent name of the child document.
221 // This makes insertion of citations and references in the child work,
222 // when the target is in the parent or another child document.
223 newBuffer->setParentName(parentfilename);
224 message(bformat(_("Opening child document %1$s..."),
225 makeDisplayPath(filename.absFilename())));
228 // Update the labels and section numbering.
229 updateLabels(*newBuffer->getMasterBuffer());
231 bool const parse_error = !newBuffer->errorList("Parse").empty();
232 if (parse_error || !auto_open) {
233 setBuffer(newBuffer, child_document);
234 showErrorList("Parse");
237 // scroll to the position when the file was last closed
238 if (!auto_open && lyxrc.use_lastfilepos) {
241 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
242 // if successfully move to pit (returned par_id is not zero),
243 // update metrics and reset font
244 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
245 if (work_area_->bufferView().fitCursor())
246 work_area_->bufferView().updateMetrics(false);
247 newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
250 updateLayoutChoice();
252 work_area_->redraw();
257 LyX::ref().session().lastFiles().add(filename);
264 void LyXView::connectBuffer(Buffer & buf)
266 if (errorsConnection_.connected())
269 BOOST_ASSERT(work_area_);
270 bufferChangedConnection_ =
272 boost::bind(&WorkArea::redraw, work_area_));
274 bufferStructureChangedConnection_ =
275 buf.getMasterBuffer()->structureChanged.connect(
276 boost::bind(&LyXView::updateToc, this));
280 boost::bind(&LyXView::showErrorList, this, _1));
284 boost::bind(&LyXView::message, this, _1));
288 boost::bind(&LyXView::busy, this, _1));
291 buf.updateTitles.connect(
292 boost::bind(&LyXView::updateWindowTitle, this));
295 buf.resetAutosaveTimers.connect(
296 boost::bind(&LyXView::resetAutosaveTimer, this));
298 readonlyConnection_ =
299 buf.readonly.connect(
300 boost::bind(&LyXView::showReadonly, this, _1));
304 boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
308 void LyXView::disconnectBuffer()
310 errorsConnection_.disconnect();
311 bufferChangedConnection_.disconnect();
312 bufferStructureChangedConnection_.disconnect();
313 messageConnection_.disconnect();
314 busyConnection_.disconnect();
315 titleConnection_.disconnect();
316 timerConnection_.disconnect();
317 readonlyConnection_.disconnect();
318 closingConnection_.disconnect();
319 layout_changed_connection_.disconnect();
323 void LyXView::connectBufferView(BufferView & bv)
325 show_dialog_connection_ = bv.showDialog.connect(
326 boost::bind(&LyXView::showDialog, this, _1));
327 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
328 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
329 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
330 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
331 update_dialog_connection_ = bv.updateDialog.connect(
332 boost::bind(&LyXView::updateDialog, this, _1, _2));
333 layout_changed_connection_ = bv.layoutChanged.connect(
334 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
338 void LyXView::disconnectBufferView()
340 show_dialog_connection_.disconnect();
341 show_dialog_with_data_connection_.disconnect();
342 show_inset_dialog_connection_.disconnect();
343 update_dialog_connection_.disconnect();
347 void LyXView::showErrorList(string const & error_type)
349 ErrorList & el = buffer()->errorList(error_type);
351 getDialogs().show("errorlist", error_type);
356 void LyXView::showDialog(string const & name)
358 getDialogs().show(name);
362 void LyXView::showDialogWithData(string const & name, string const & data)
364 getDialogs().show(name, data);
368 void LyXView::showInsetDialog(string const & name, string const & data,
371 getDialogs().show(name, data, inset);
375 void LyXView::updateDialog(string const & name, string const & data)
377 if (getDialogs().visible(name))
378 getDialogs().update(name, data);
382 void LyXView::showReadonly(bool)
385 getDialogs().updateBufferDependent(false);
389 BufferView * LyXView::view() const
391 BOOST_ASSERT(work_area_);
392 return &work_area_->bufferView();
396 void LyXView::updateToc()
398 updateDialog("toc", "");
402 void LyXView::updateToolbars()
404 BOOST_ASSERT(work_area_);
406 work_area_->bufferView().cursor().inMathed();
408 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
410 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
411 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
413 toolbars_->update(math, table, review);
414 // update redaonly status of open dialogs. This could also be in
415 // updateMenubar(), but since updateToolbars() and updateMenubar()
416 // are always called together it is only here.
417 getDialogs().checkStatus();
421 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
423 return toolbars_->getToolbarInfo(name);
427 void LyXView::toggleToolbarState(string const & name, bool allowauto)
429 // it is possible to get current toolbar status like this,...
430 // but I decide to obey the order of ToolbarBackend::flags
431 // and disregard real toolbar status.
432 // toolbars_->saveToolbarInfo();
434 // toggle state on/off/auto
435 toolbars_->toggleToolbarState(name, allowauto);
441 void LyXView::updateMenubar()
447 void LyXView::autoSave()
449 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
451 if (view()->buffer())
452 lyx::autoSave(view());
456 void LyXView::resetAutosaveTimer()
459 autosave_timeout_->restart();
463 void LyXView::updateLayoutChoice()
465 // Don't show any layouts without a buffer
466 if (!view()->buffer()) {
467 toolbars_->clearLayoutList();
471 // Update the layout display
472 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
473 current_layout = buffer()->params().getTextClass().defaultLayoutName();
476 BOOST_ASSERT(work_area_);
477 docstring const & layout = work_area_->bufferView().cursor().
478 innerParagraph().layout()->name();
480 if (layout != current_layout) {
481 toolbars_->setLayout(layout);
482 current_layout = layout;
487 void LyXView::updateWindowTitle()
489 docstring maximize_title = lyx::from_ascii("LyX");
490 docstring minimize_title = lyx::from_ascii("LyX");
492 if (view()->buffer()) {
493 string const cur_title = buffer()->fileName();
494 if (!cur_title.empty()) {
495 maximize_title += ": " + makeDisplayPath(cur_title, 30);
496 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
497 if (!buffer()->isClean()) {
498 maximize_title += _(" (changed)");
499 minimize_title += lyx::char_type('*');
501 if (buffer()->isReadonly())
502 maximize_title += _(" (read only)");
506 setWindowTitle(maximize_title, minimize_title);
511 void LyXView::dispatch(FuncRequest const & cmd)
513 theLyXFunc().setLyXView(this);
518 Buffer const * const LyXView::updateInset(Inset const * inset) const
520 Buffer const * buffer_ptr = 0;
522 BOOST_ASSERT(work_area_);
523 work_area_->scheduleRedraw();
525 buffer_ptr = work_area_->bufferView().buffer();