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->getMasterBuffer() != 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 // Update the labels and section numbering to the new master Buffer.
155 updateLabels(*newBuffer->getMasterBuffer());
158 connectBuffer(*newBuffer);
160 // Buffer-dependent dialogs should be updated or
161 // hidden. This should go here because some dialogs (eg ToC)
162 // require bv_->text.
163 getDialogs().updateBufferDependent(true);
171 updateLayoutChoice();
176 work_area_->redraw();
180 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
181 bool child_document, bool auto_open)
185 BOOST_ASSERT(work_area_);
186 string parentfilename;
187 Buffer * oldBuffer = work_area_->bufferView().buffer();
189 parentfilename = oldBuffer->fileName();
191 Buffer * newBuffer = checkAndLoadLyXFile(filename);
194 message(_("Document not loaded."));
197 work_area_->redraw();
201 if (child_document && newBuffer != oldBuffer) {
202 // Set the parent name of the child document.
203 // This makes insertion of citations and references in the child work,
204 // when the target is in the parent or another child document.
205 newBuffer->setParentName(parentfilename);
206 message(bformat(_("Opening child document %1$s..."),
207 makeDisplayPath(filename.absFilename())));
210 // Update the labels and section numbering.
211 updateLabels(*newBuffer->getMasterBuffer());
213 bool const parse_error = !newBuffer->errorList("Parse").empty();
214 if (parse_error || !auto_open) {
215 setBuffer(newBuffer, child_document);
216 showErrorList("Parse");
219 // scroll to the position when the file was last closed
220 if (!auto_open && lyxrc.use_lastfilepos) {
223 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
224 // if successfully move to pit (returned par_id is not zero),
225 // update metrics and reset font
226 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
227 if (work_area_->bufferView().fitCursor())
228 work_area_->bufferView().updateMetrics(false);
229 newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
232 updateLayoutChoice();
234 work_area_->redraw();
239 LyX::ref().session().lastFiles().add(filename);
246 void LyXView::connectBuffer(Buffer & buf)
248 if (errorsConnection_.connected())
251 BOOST_ASSERT(work_area_);
252 bufferChangedConnection_ =
254 boost::bind(&WorkArea::redraw, work_area_));
256 bufferStructureChangedConnection_ =
257 buf.getMasterBuffer()->structureChanged.connect(
258 boost::bind(&LyXView::updateToc, this));
262 boost::bind(&LyXView::showErrorList, this, _1));
266 boost::bind(&LyXView::message, this, _1));
270 boost::bind(&LyXView::busy, this, _1));
273 buf.updateTitles.connect(
274 boost::bind(&LyXView::updateWindowTitle, this));
277 buf.resetAutosaveTimers.connect(
278 boost::bind(&LyXView::resetAutosaveTimer, this));
280 readonlyConnection_ =
281 buf.readonly.connect(
282 boost::bind(&LyXView::showReadonly, this, _1));
286 boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
290 void LyXView::disconnectBuffer()
292 errorsConnection_.disconnect();
293 bufferChangedConnection_.disconnect();
294 bufferStructureChangedConnection_.disconnect();
295 messageConnection_.disconnect();
296 busyConnection_.disconnect();
297 titleConnection_.disconnect();
298 timerConnection_.disconnect();
299 readonlyConnection_.disconnect();
300 closingConnection_.disconnect();
301 layout_changed_connection_.disconnect();
305 void LyXView::connectBufferView(BufferView & bv)
307 show_dialog_connection_ = bv.showDialog.connect(
308 boost::bind(&LyXView::showDialog, this, _1));
309 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
310 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
311 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
312 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
313 update_dialog_connection_ = bv.updateDialog.connect(
314 boost::bind(&LyXView::updateDialog, this, _1, _2));
315 layout_changed_connection_ = bv.layoutChanged.connect(
316 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
320 void LyXView::disconnectBufferView()
322 show_dialog_connection_.disconnect();
323 show_dialog_with_data_connection_.disconnect();
324 show_inset_dialog_connection_.disconnect();
325 update_dialog_connection_.disconnect();
329 void LyXView::showErrorList(string const & error_type)
331 ErrorList & el = buffer()->errorList(error_type);
333 getDialogs().show("errorlist", error_type);
338 void LyXView::showDialog(string const & name)
340 getDialogs().show(name);
344 void LyXView::showDialogWithData(string const & name, string const & data)
346 getDialogs().show(name, data);
350 void LyXView::showInsetDialog(string const & name, string const & data,
353 getDialogs().show(name, data, inset);
357 void LyXView::updateDialog(string const & name, string const & data)
359 if (getDialogs().visible(name))
360 getDialogs().update(name, data);
364 void LyXView::showReadonly(bool)
367 getDialogs().updateBufferDependent(false);
371 BufferView * LyXView::view() const
373 BOOST_ASSERT(work_area_);
374 return &work_area_->bufferView();
378 void LyXView::updateToc()
380 updateDialog("toc", "");
384 void LyXView::updateToolbars()
386 BOOST_ASSERT(work_area_);
388 work_area_->bufferView().cursor().inMathed();
390 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
392 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
393 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
395 toolbars_->update(math, table, review);
396 // update redaonly status of open dialogs. This could also be in
397 // updateMenubar(), but since updateToolbars() and updateMenubar()
398 // are always called together it is only here.
399 getDialogs().checkStatus();
403 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
405 return toolbars_->getToolbarState(name);
409 void LyXView::toggleToolbarState(string const & name, bool allowauto)
411 // it is possible to get current toolbar status like this,...
412 // but I decide to obey the order of ToolbarBackend::flags
413 // and disregard real toolbar status.
414 // toolbars_->saveToolbarInfo();
416 // toggle state on/off/auto
417 toolbars_->toggleToolbarState(name, allowauto);
423 void LyXView::updateMenubar()
429 void LyXView::autoSave()
431 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
433 if (view()->buffer())
434 lyx::autoSave(view());
438 void LyXView::resetAutosaveTimer()
441 autosave_timeout_->restart();
445 void LyXView::updateLayoutChoice()
447 // Don't show any layouts without a buffer
448 if (!view()->buffer()) {
449 toolbars_->clearLayoutList();
453 // Update the layout display
454 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
455 current_layout = buffer()->params().getTextClass().defaultLayoutName();
458 BOOST_ASSERT(work_area_);
459 string const & layout = work_area_->bufferView().cursor().
460 innerParagraph().layout()->name();
462 if (layout != current_layout) {
463 toolbars_->setLayout(layout);
464 current_layout = layout;
469 void LyXView::updateWindowTitle()
471 docstring maximize_title = lyx::from_ascii("LyX");
472 docstring minimize_title = lyx::from_ascii("LyX");
474 if (view()->buffer()) {
475 string const cur_title = buffer()->fileName();
476 if (!cur_title.empty()) {
477 maximize_title += ": " + makeDisplayPath(cur_title, 30);
478 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
479 if (!buffer()->isClean()) {
480 maximize_title += _(" (changed)");
481 minimize_title += lyx::char_type('*');
483 if (buffer()->isReadonly())
484 maximize_title += _(" (read only)");
488 setWindowTitle(maximize_title, minimize_title);
493 void LyXView::dispatch(FuncRequest const & cmd)
495 theLyXFunc().setLyXView(this);
500 Buffer const * const LyXView::updateInset(Inset const * inset) const
502 Buffer const * buffer_ptr = 0;
504 BOOST_ASSERT(work_area_);
505 work_area_->scheduleRedraw();
507 buffer_ptr = work_area_->bufferView().buffer();