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 string 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();
141 if (!b && theBufferList().empty())
142 getDialogs().hideBufferDependent();
144 work_area_->bufferView().setBuffer(b);
146 //FIXME This would be a little simpler if setBuffer returned the buffer.
147 Buffer * newBuffer = work_area_->bufferView().buffer();
149 if (child_document && newBuffer != oldBuffer) {
150 // Set the parent name of the child document.
151 // This makes insertion of citations and references in the child work,
152 // when the target is in the parent or another child document.
153 newBuffer->setParentName(parentfilename);
154 // updateLabels() will emit Buffer::structureChanged() so better
155 // connect it before.
156 connectBuffer(*newBuffer);
157 // Update the labels and section numbering.
158 updateLabels(*newBuffer->getMasterBuffer());
160 connectBuffer(*newBuffer);
162 // Buffer-dependent dialogs should be updated or
163 // hidden. This should go here because some dialogs (eg ToC)
164 // require bv_->text.
165 getDialogs().updateBufferDependent(true);
173 updateLayoutChoice();
178 work_area_->redraw();
182 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
183 bool child_document, bool auto_open)
187 BOOST_ASSERT(work_area_);
188 string parentfilename;
189 Buffer * oldBuffer = work_area_->bufferView().buffer();
191 parentfilename = oldBuffer->fileName();
194 work_area_->bufferView().loadLyXFile(filename, auto_open);
197 message(_("Document not loaded."));
200 work_area_->redraw();
206 connectBuffer(*newBuffer);
209 showErrorList("Parse");
211 if (child_document && newBuffer != oldBuffer) {
212 // Set the parent name of the child document.
213 // This makes insertion of citations and references in the child work,
214 // when the target is in the parent or another child document.
215 newBuffer->setParentName(parentfilename);
216 message(bformat(_("Opening child document %1$s..."),
217 makeDisplayPath(filename.absFilename())));
220 // Update the labels and section numbering.
221 updateLabels(*newBuffer->getMasterBuffer());
223 // scroll to the position when the file was last closed
224 if (!auto_open && lyxrc.use_lastfilepos) {
227 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
228 // if successfully move to pit (returned par_id is not zero),
229 // update metrics and reset font
230 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
231 if (work_area_->bufferView().fitCursor())
232 work_area_->bufferView().updateMetrics(false);
233 newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
238 LyX::ref().session().lastFiles().add(filename);
240 // FIXME We definitely don't have to do all of this if auto_open...
241 // The question is: What do we have to do here if we've loaded a new
242 // file but haven't switched buffers? I'm guessing we can skip the
243 // layout bit and the title.
244 // and if we have already switched buffers...won't all of this have been
245 // done already in setBuffer()? So why does it also need doing here?
246 // In fact, won't a lot of what's above already have been done? E.g.,
247 // the connecting and disconnecting of buffers will have been done
248 // already in setBuffer().
249 // This bit of cleanup is post-1.5.0....
252 updateLayoutChoice();
257 work_area_->redraw();
262 void LyXView::connectBuffer(Buffer & buf)
264 if (errorsConnection_.connected())
267 BOOST_ASSERT(work_area_);
268 bufferChangedConnection_ =
270 boost::bind(&WorkArea::redraw, work_area_));
272 bufferStructureChangedConnection_ =
273 buf.getMasterBuffer()->structureChanged.connect(
274 boost::bind(&LyXView::updateToc, this));
278 boost::bind(&LyXView::showErrorList, this, _1));
282 boost::bind(&LyXView::message, this, _1));
286 boost::bind(&LyXView::busy, this, _1));
289 buf.updateTitles.connect(
290 boost::bind(&LyXView::updateWindowTitle, this));
293 buf.resetAutosaveTimers.connect(
294 boost::bind(&LyXView::resetAutosaveTimer, this));
296 readonlyConnection_ =
297 buf.readonly.connect(
298 boost::bind(&LyXView::showReadonly, this, _1));
302 boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
306 void LyXView::disconnectBuffer()
308 errorsConnection_.disconnect();
309 bufferChangedConnection_.disconnect();
310 bufferStructureChangedConnection_.disconnect();
311 messageConnection_.disconnect();
312 busyConnection_.disconnect();
313 titleConnection_.disconnect();
314 timerConnection_.disconnect();
315 readonlyConnection_.disconnect();
316 closingConnection_.disconnect();
317 layout_changed_connection_.disconnect();
321 void LyXView::connectBufferView(BufferView & bv)
323 show_dialog_connection_ = bv.showDialog.connect(
324 boost::bind(&LyXView::showDialog, this, _1));
325 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
326 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
327 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
328 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
329 update_dialog_connection_ = bv.updateDialog.connect(
330 boost::bind(&LyXView::updateDialog, this, _1, _2));
331 layout_changed_connection_ = bv.layoutChanged.connect(
332 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
336 void LyXView::disconnectBufferView()
338 show_dialog_connection_.disconnect();
339 show_dialog_with_data_connection_.disconnect();
340 show_inset_dialog_connection_.disconnect();
341 update_dialog_connection_.disconnect();
345 void LyXView::showErrorList(string const & error_type)
347 ErrorList & el = buffer()->errorList(error_type);
349 getDialogs().show("errorlist", error_type);
354 void LyXView::showDialog(string const & name)
356 getDialogs().show(name);
360 void LyXView::showDialogWithData(string const & name, string const & data)
362 getDialogs().show(name, data);
366 void LyXView::showInsetDialog(string const & name, string const & data,
369 getDialogs().show(name, data, inset);
373 void LyXView::updateDialog(string const & name, string const & data)
375 if (getDialogs().visible(name))
376 getDialogs().update(name, data);
380 void LyXView::showReadonly(bool)
383 getDialogs().updateBufferDependent(false);
387 BufferView * LyXView::view() const
389 BOOST_ASSERT(work_area_);
390 return &work_area_->bufferView();
394 void LyXView::updateToc()
396 updateDialog("toc", "");
400 void LyXView::updateToolbars()
402 BOOST_ASSERT(work_area_);
404 work_area_->bufferView().cursor().inMathed();
406 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
408 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
409 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
411 toolbars_->update(math, table, review);
412 // update redaonly status of open dialogs. This could also be in
413 // updateMenubar(), but since updateToolbars() and updateMenubar()
414 // are always called together it is only here.
415 getDialogs().checkStatus();
419 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
421 return toolbars_->getToolbarState(name);
425 void LyXView::toggleToolbarState(string const & name, bool allowauto)
427 // it is possible to get current toolbar status like this,...
428 // but I decide to obey the order of ToolbarBackend::flags
429 // and disregard real toolbar status.
430 // toolbars_->saveToolbarInfo();
432 // toggle state on/off/auto
433 toolbars_->toggleToolbarState(name, allowauto);
439 void LyXView::updateMenubar()
445 void LyXView::autoSave()
447 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
449 if (view()->buffer())
450 lyx::autoSave(view());
454 void LyXView::resetAutosaveTimer()
457 autosave_timeout_->restart();
461 void LyXView::updateLayoutChoice()
463 // Don't show any layouts without a buffer
464 if (!view()->buffer()) {
465 toolbars_->clearLayoutList();
469 // Update the layout display
470 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
471 current_layout = buffer()->params().getTextClass().defaultLayoutName();
474 BOOST_ASSERT(work_area_);
475 string const & layout = work_area_->bufferView().cursor().
476 innerParagraph().layout()->name();
478 if (layout != current_layout) {
479 toolbars_->setLayout(layout);
480 current_layout = layout;
485 void LyXView::updateWindowTitle()
487 docstring maximize_title = lyx::from_ascii("LyX");
488 docstring minimize_title = lyx::from_ascii("LyX");
490 if (view()->buffer()) {
491 string const cur_title = buffer()->fileName();
492 if (!cur_title.empty()) {
493 maximize_title += ": " + makeDisplayPath(cur_title, 30);
494 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
495 if (!buffer()->isClean()) {
496 maximize_title += _(" (changed)");
497 minimize_title += lyx::char_type('*');
499 if (buffer()->isReadonly())
500 maximize_title += _(" (read only)");
504 setWindowTitle(maximize_title, minimize_title);
509 void LyXView::dispatch(FuncRequest const & cmd)
511 theLyXFunc().setLyXView(this);
516 Buffer const * const LyXView::updateInset(Inset const * inset) const
518 Buffer const * buffer_ptr = 0;
520 BOOST_ASSERT(work_area_);
521 work_area_->scheduleRedraw();
523 buffer_ptr = work_area_->bufferView().buffer();