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()
46 #include "support/Timeout.h"
48 #include <boost/bind.hpp>
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
63 using support::bformat;
64 using support::FileName;
65 using support::makeDisplayPath;
66 using support::onlyFilename;
70 docstring current_layout;
72 LyXView::LyXView(int id)
74 toolbars_(new Toolbars(*this)),
75 autosave_timeout_(new Timeout(5000)),
76 dialogs_(new Dialogs(*this)),
77 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
79 // Start autosave timer
81 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
82 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
83 autosave_timeout_->start();
94 // FIXME, there's only one WorkArea per LyXView possible for now.
95 void LyXView::setWorkArea(WorkArea * work_area)
97 BOOST_ASSERT(work_area);
98 work_area_ = work_area;
99 work_area_ids_.clear();
100 work_area_ids_.push_back(work_area_->id());
104 // FIXME, there's only one WorkArea per LyXView possible for now.
105 WorkArea const * LyXView::currentWorkArea() const
111 // FIXME, there's only one WorkArea per LyXView possible for now.
112 WorkArea * LyXView::currentWorkArea()
118 Buffer * LyXView::buffer() const
120 BOOST_ASSERT(work_area_);
121 return work_area_->bufferView().buffer();
125 void LyXView::setBuffer(Buffer * b, bool child_document)
129 BOOST_ASSERT(work_area_);
130 Buffer * oldBuffer = work_area_->bufferView().buffer();
131 // parentfilename will be used in case when we switch to a child
132 // document (hence when child_document is true)
133 string parentfilename;
135 parentfilename = oldBuffer->fileName();
137 if (!b && theBufferList().empty())
138 getDialogs().hideBufferDependent();
140 Buffer * newBuffer = work_area_->bufferView().setBuffer(b);
143 //Are we closing an oldBuffer which was a child document?
144 if (!b && oldBuffer && oldBuffer->getMasterBuffer() != oldBuffer)
145 // Update the labels and section numbering of its master Buffer.
146 updateLabels(*oldBuffer->getMasterBuffer());
147 //Are we opening a new child document?
148 else if (child_document && newBuffer->getMasterBuffer() != oldBuffer) {
149 // Set the parent name of the child document.
150 // This makes insertion of citations and references in the child work,
151 // when the target is in the parent or another child document.
152 newBuffer->setParentName(parentfilename);
153 // Update the labels and section numbering to the new master Buffer.
154 updateLabels(*newBuffer->getMasterBuffer());
156 //Now that all the updating of the old buffer has been done, we can
157 //connect the new buffer. Note that this will also disconnect the old
158 //buffer, if such there is.
159 //FIXME Is it clear that this should go right here? Or should it go
160 //earlier before the previous if (in which case we'd remove the "else")?
161 connectBuffer(*newBuffer);
163 /* FIXME: We need to rebuild the Toc dialog before the others even
164 if it will be rebuilt again in the next line. This avoid a crash when
165 other dialogs are rebuilt before the Toc dialog. The reason is
166 that closing a Buffer triggers an update of all opened dialogs
167 when dispatching LFUN_DIALOG_UPDATE (hence the patch).
168 The path is as following:
169 setBuffer() -> updateBufferDependent() -> RestoreButton() -> LFUN
170 The problem here is that the Toc dialog has not been
171 reconstructed (because it comes after in the list of dialogs). */
174 // Buffer-dependent dialogs should be updated or
175 // hidden. This should go here because some dialogs (eg ToC)
176 // require bv_->text.
177 getDialogs().updateBufferDependent(true);
179 //Disconnect the old buffer...there's no new one.
187 updateLayoutChoice();
192 work_area_->redraw();
196 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
197 bool child_document, bool auto_open)
201 BOOST_ASSERT(work_area_);
202 string parentfilename;
203 Buffer * oldBuffer = work_area_->bufferView().buffer();
205 parentfilename = oldBuffer->fileName();
207 bool alreadyLoaded = checkIfLoaded(filename);
208 Buffer * newBuffer = checkAndLoadLyXFile(filename);
211 message(_("Document not loaded."));
214 work_area_->redraw();
218 if (child_document && newBuffer != oldBuffer) {
219 // Set the parent name of the child document.
220 // This makes insertion of citations and references in the child work,
221 // when the target is in the parent or another child document.
222 newBuffer->setParentName(parentfilename);
223 message(bformat(_("Opening child document %1$s..."),
224 makeDisplayPath(filename.absFilename())));
227 // Update the labels and section numbering.
228 updateLabels(*newBuffer->getMasterBuffer());
230 bool const parse_error = !newBuffer->errorList("Parse").empty();
231 bool const need_switch = parse_error || !auto_open;
233 setBuffer(newBuffer, child_document);
234 if (!alreadyLoaded) {
236 showErrorList("Parse");
237 // scroll to the position when the file was last closed
238 if (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();
256 LyX::ref().session().lastFiles().add(filename);
265 void LyXView::connectBuffer(Buffer & buf)
267 if (errorsConnection_.connected())
270 BOOST_ASSERT(work_area_);
271 bufferChangedConnection_ =
273 boost::bind(&WorkArea::redraw, work_area_));
275 bufferStructureChangedConnection_ =
276 buf.getMasterBuffer()->structureChanged.connect(
277 boost::bind(&LyXView::updateToc, this));
281 boost::bind(&LyXView::showErrorList, this, _1));
285 boost::bind(&LyXView::message, this, _1));
289 boost::bind(&LyXView::busy, this, _1));
292 buf.updateTitles.connect(
293 boost::bind(&LyXView::updateWindowTitle, this));
296 buf.resetAutosaveTimers.connect(
297 boost::bind(&LyXView::resetAutosaveTimer, this));
299 readonlyConnection_ =
300 buf.readonly.connect(
301 boost::bind(&LyXView::showReadonly, this, _1));
305 boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
309 void LyXView::disconnectBuffer()
311 errorsConnection_.disconnect();
312 bufferChangedConnection_.disconnect();
313 bufferStructureChangedConnection_.disconnect();
314 messageConnection_.disconnect();
315 busyConnection_.disconnect();
316 titleConnection_.disconnect();
317 timerConnection_.disconnect();
318 readonlyConnection_.disconnect();
319 closingConnection_.disconnect();
320 layout_changed_connection_.disconnect();
324 void LyXView::connectBufferView(BufferView & bv)
326 show_dialog_connection_ = bv.showDialog.connect(
327 boost::bind(&LyXView::showDialog, this, _1));
328 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
329 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
330 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
331 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
332 update_dialog_connection_ = bv.updateDialog.connect(
333 boost::bind(&LyXView::updateDialog, this, _1, _2));
334 layout_changed_connection_ = bv.layoutChanged.connect(
335 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
339 void LyXView::disconnectBufferView()
341 show_dialog_connection_.disconnect();
342 show_dialog_with_data_connection_.disconnect();
343 show_inset_dialog_connection_.disconnect();
344 update_dialog_connection_.disconnect();
348 void LyXView::showErrorList(string const & error_type)
350 ErrorList & el = buffer()->errorList(error_type);
352 getDialogs().show("errorlist", error_type);
357 void LyXView::showDialog(string const & name)
359 getDialogs().show(name);
363 void LyXView::showDialogWithData(string const & name, string const & data)
365 getDialogs().show(name, data);
369 void LyXView::showInsetDialog(string const & name, string const & data,
372 getDialogs().show(name, data, inset);
376 void LyXView::updateDialog(string const & name, string const & data)
378 if (getDialogs().visible(name))
379 getDialogs().update(name, data);
383 void LyXView::showReadonly(bool)
386 getDialogs().updateBufferDependent(false);
390 BufferView * LyXView::view() const
392 BOOST_ASSERT(work_area_);
393 return &work_area_->bufferView();
397 void LyXView::updateToc()
399 updateDialog("toc", "");
403 void LyXView::updateToolbars()
405 BOOST_ASSERT(work_area_);
407 work_area_->bufferView().cursor().inMathed();
409 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
411 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
412 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
414 toolbars_->update(math, table, review);
415 // update redaonly status of open dialogs. This could also be in
416 // updateMenubar(), but since updateToolbars() and updateMenubar()
417 // are always called together it is only here.
418 getDialogs().checkStatus();
422 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
424 return toolbars_->getToolbarInfo(name);
428 void LyXView::toggleToolbarState(string const & name, bool allowauto)
430 // it is possible to get current toolbar status like this,...
431 // but I decide to obey the order of ToolbarBackend::flags
432 // and disregard real toolbar status.
433 // toolbars_->saveToolbarInfo();
435 // toggle state on/off/auto
436 toolbars_->toggleToolbarState(name, allowauto);
442 void LyXView::updateMenubar()
448 void LyXView::autoSave()
450 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
452 if (view()->buffer())
453 lyx::autoSave(view());
457 void LyXView::resetAutosaveTimer()
460 autosave_timeout_->restart();
464 void LyXView::updateLayoutChoice()
466 // Don't show any layouts without a buffer
467 if (!view()->buffer()) {
468 toolbars_->clearLayoutList();
472 // Update the layout display
473 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
474 current_layout = buffer()->params().getTextClass().defaultLayoutName();
477 BOOST_ASSERT(work_area_);
478 docstring const & layout = work_area_->bufferView().cursor().
479 innerParagraph().layout()->name();
481 if (layout != current_layout) {
482 toolbars_->setLayout(layout);
483 current_layout = layout;
488 void LyXView::updateWindowTitle()
490 docstring maximize_title = lyx::from_ascii("LyX");
491 docstring minimize_title = lyx::from_ascii("LyX");
493 if (view()->buffer()) {
494 string const cur_title = buffer()->fileName();
495 if (!cur_title.empty()) {
496 maximize_title += ": " + makeDisplayPath(cur_title, 30);
497 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
498 if (!buffer()->isClean()) {
499 maximize_title += _(" (changed)");
500 minimize_title += lyx::char_type('*');
502 if (buffer()->isReadonly())
503 maximize_title += _(" (read only)");
507 setWindowTitle(maximize_title, minimize_title);
512 void LyXView::dispatch(FuncRequest const & cmd)
514 theLyXFunc().setLyXView(this);
519 Buffer const * const LyXView::updateInset(Inset const * inset) const
521 Buffer const * buffer_ptr = 0;
523 BOOST_ASSERT(work_area_);
524 work_area_->scheduleRedraw();
526 buffer_ptr = work_area_->bufferView().buffer();
531 } // namespace frontend