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 bool alreadyLoaded = checkIfLoaded(filename);
210 Buffer * newBuffer = checkAndLoadLyXFile(filename);
213 message(_("Document not loaded."));
216 work_area_->redraw();
220 if (child_document && newBuffer != oldBuffer) {
221 // Set the parent name of the child document.
222 // This makes insertion of citations and references in the child work,
223 // when the target is in the parent or another child document.
224 newBuffer->setParentName(parentfilename);
225 message(bformat(_("Opening child document %1$s..."),
226 makeDisplayPath(filename.absFilename())));
229 // Update the labels and section numbering.
230 updateLabels(*newBuffer->getMasterBuffer());
232 bool const parse_error = !newBuffer->errorList("Parse").empty();
233 bool const need_switch = parse_error || !auto_open;
235 setBuffer(newBuffer, child_document);
236 if (!alreadyLoaded) {
238 showErrorList("Parse");
239 // scroll to the position when the file was last closed
240 if (lyxrc.use_lastfilepos) {
243 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
244 // if successfully move to pit (returned par_id is not zero),
245 // update metrics and reset font
246 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
247 if (work_area_->bufferView().fitCursor())
248 work_area_->bufferView().updateMetrics(false);
249 newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
252 updateLayoutChoice();
254 work_area_->redraw();
258 LyX::ref().session().lastFiles().add(filename);
267 void LyXView::connectBuffer(Buffer & buf)
269 if (errorsConnection_.connected())
272 BOOST_ASSERT(work_area_);
273 bufferChangedConnection_ =
275 boost::bind(&WorkArea::redraw, work_area_));
277 bufferStructureChangedConnection_ =
278 buf.getMasterBuffer()->structureChanged.connect(
279 boost::bind(&LyXView::updateToc, this));
283 boost::bind(&LyXView::showErrorList, this, _1));
287 boost::bind(&LyXView::message, this, _1));
291 boost::bind(&LyXView::busy, this, _1));
294 buf.updateTitles.connect(
295 boost::bind(&LyXView::updateWindowTitle, this));
298 buf.resetAutosaveTimers.connect(
299 boost::bind(&LyXView::resetAutosaveTimer, this));
301 readonlyConnection_ =
302 buf.readonly.connect(
303 boost::bind(&LyXView::showReadonly, this, _1));
307 boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
311 void LyXView::disconnectBuffer()
313 errorsConnection_.disconnect();
314 bufferChangedConnection_.disconnect();
315 bufferStructureChangedConnection_.disconnect();
316 messageConnection_.disconnect();
317 busyConnection_.disconnect();
318 titleConnection_.disconnect();
319 timerConnection_.disconnect();
320 readonlyConnection_.disconnect();
321 closingConnection_.disconnect();
322 layout_changed_connection_.disconnect();
326 void LyXView::connectBufferView(BufferView & bv)
328 show_dialog_connection_ = bv.showDialog.connect(
329 boost::bind(&LyXView::showDialog, this, _1));
330 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
331 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
332 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
333 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
334 update_dialog_connection_ = bv.updateDialog.connect(
335 boost::bind(&LyXView::updateDialog, this, _1, _2));
336 layout_changed_connection_ = bv.layoutChanged.connect(
337 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
341 void LyXView::disconnectBufferView()
343 show_dialog_connection_.disconnect();
344 show_dialog_with_data_connection_.disconnect();
345 show_inset_dialog_connection_.disconnect();
346 update_dialog_connection_.disconnect();
350 void LyXView::showErrorList(string const & error_type)
352 ErrorList & el = buffer()->errorList(error_type);
354 getDialogs().show("errorlist", error_type);
359 void LyXView::showDialog(string const & name)
361 getDialogs().show(name);
365 void LyXView::showDialogWithData(string const & name, string const & data)
367 getDialogs().show(name, data);
371 void LyXView::showInsetDialog(string const & name, string const & data,
374 getDialogs().show(name, data, inset);
378 void LyXView::updateDialog(string const & name, string const & data)
380 if (getDialogs().visible(name))
381 getDialogs().update(name, data);
385 void LyXView::showReadonly(bool)
388 getDialogs().updateBufferDependent(false);
392 BufferView * LyXView::view() const
394 BOOST_ASSERT(work_area_);
395 return &work_area_->bufferView();
399 void LyXView::updateToc()
401 updateDialog("toc", "");
405 void LyXView::updateToolbars()
407 BOOST_ASSERT(work_area_);
409 work_area_->bufferView().cursor().inMathed();
411 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
413 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
414 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
416 toolbars_->update(math, table, review);
417 // update redaonly status of open dialogs. This could also be in
418 // updateMenubar(), but since updateToolbars() and updateMenubar()
419 // are always called together it is only here.
420 getDialogs().checkStatus();
424 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
426 return toolbars_->getToolbarInfo(name);
430 void LyXView::toggleToolbarState(string const & name, bool allowauto)
432 // it is possible to get current toolbar status like this,...
433 // but I decide to obey the order of ToolbarBackend::flags
434 // and disregard real toolbar status.
435 // toolbars_->saveToolbarInfo();
437 // toggle state on/off/auto
438 toolbars_->toggleToolbarState(name, allowauto);
444 void LyXView::updateMenubar()
450 void LyXView::autoSave()
452 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
454 if (view()->buffer())
455 lyx::autoSave(view());
459 void LyXView::resetAutosaveTimer()
462 autosave_timeout_->restart();
466 void LyXView::updateLayoutChoice()
468 // Don't show any layouts without a buffer
469 if (!view()->buffer()) {
470 toolbars_->clearLayoutList();
474 // Update the layout display
475 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
476 current_layout = buffer()->params().getTextClass().defaultLayoutName();
479 BOOST_ASSERT(work_area_);
480 docstring const & layout = work_area_->bufferView().cursor().
481 innerParagraph().layout()->name();
483 if (layout != current_layout) {
484 toolbars_->setLayout(layout);
485 current_layout = layout;
490 void LyXView::updateWindowTitle()
492 docstring maximize_title = lyx::from_ascii("LyX");
493 docstring minimize_title = lyx::from_ascii("LyX");
495 if (view()->buffer()) {
496 string const cur_title = buffer()->fileName();
497 if (!cur_title.empty()) {
498 maximize_title += ": " + makeDisplayPath(cur_title, 30);
499 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
500 if (!buffer()->isClean()) {
501 maximize_title += _(" (changed)");
502 minimize_title += lyx::char_type('*');
504 if (buffer()->isReadonly())
505 maximize_title += _(" (read only)");
509 setWindowTitle(maximize_title, minimize_title);
514 void LyXView::dispatch(FuncRequest const & cmd)
516 theLyXFunc().setLyXView(this);
521 Buffer const * const LyXView::updateInset(Inset const * inset) const
523 Buffer const * buffer_ptr = 0;
525 BOOST_ASSERT(work_area_);
526 work_area_->scheduleRedraw();
528 buffer_ptr = work_area_->bufferView().buffer();