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.
22 #include "buffer_funcs.h"
23 #include "BufferList.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
29 #include "ErrorList.h"
30 #include "FuncRequest.h"
38 #include "MenuBackend.h"
39 #include "Paragraph.h"
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
43 #include "support/Timeout.h"
45 #include <boost/bind.hpp>
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
60 using support::bformat;
61 using support::FileName;
62 using support::makeDisplayPath;
63 using support::onlyFilename;
67 docstring current_layout;
69 LyXView::LyXView(int id)
70 : toolbars_(new Toolbars(*this)),
71 autosave_timeout_(new Timeout(5000)),
72 dialogs_(new Dialogs(*this)),
75 // Start autosave timer
77 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
78 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
79 autosave_timeout_->start();
87 disconnectBufferView();
90 delete autosave_timeout_;
94 Buffer * LyXView::buffer()
96 WorkArea * work_area = currentWorkArea();
98 return &work_area->bufferView().buffer();
103 Buffer const * LyXView::buffer() const
105 WorkArea const * work_area = currentWorkArea();
107 return &work_area->bufferView().buffer();
112 void LyXView::setBuffer(Buffer * newBuffer)
114 BOOST_ASSERT(newBuffer);
117 WorkArea * wa = workArea(*newBuffer);
119 updateLabels(*newBuffer->getMasterBuffer());
120 wa = addWorkArea(*newBuffer);
122 //Disconnect the old buffer...there's no new one.
125 connectBuffer(*newBuffer);
126 connectBufferView(wa->bufferView());
127 setCurrentWorkArea(wa);
133 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
137 Buffer * newBuffer = checkAndLoadLyXFile(filename);
140 message(_("Document not loaded."));
146 WorkArea * wa = workArea(*newBuffer);
148 wa = addWorkArea(*newBuffer);
150 // scroll to the position when the file was last closed
151 if (lyxrc.use_lastfilepos) {
154 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
155 // if successfully move to pit (returned par_id is not zero),
156 // update metrics and reset font
157 wa->bufferView().moveToPosition(pit, pos, 0, 0);
161 LyX::ref().session().lastFiles().add(filename);
168 void LyXView::connectBuffer(Buffer & buf)
170 if (errorsConnection_.connected())
173 bufferStructureChangedConnection_ =
174 buf.getMasterBuffer()->structureChanged.connect(
175 boost::bind(&LyXView::updateToc, this));
177 bufferEmbeddingChangedConnection_ =
178 buf.embeddingChanged.connect(
179 boost::bind(&LyXView::updateEmbeddedFiles, this));
183 boost::bind(&LyXView::showErrorList, this, _1));
187 boost::bind(&LyXView::message, this, _1));
191 boost::bind(&LyXView::busy, this, _1));
194 buf.updateTitles.connect(
195 boost::bind(&LyXView::updateWindowTitle, this));
198 buf.resetAutosaveTimers.connect(
199 boost::bind(&LyXView::resetAutosaveTimer, this));
201 readonlyConnection_ =
202 buf.readonly.connect(
203 boost::bind(&LyXView::showReadonly, this, _1));
207 void LyXView::disconnectBuffer()
209 errorsConnection_.disconnect();
210 bufferStructureChangedConnection_.disconnect();
211 bufferEmbeddingChangedConnection_.disconnect();
212 messageConnection_.disconnect();
213 busyConnection_.disconnect();
214 titleConnection_.disconnect();
215 timerConnection_.disconnect();
216 readonlyConnection_.disconnect();
217 layout_changed_connection_.disconnect();
221 void LyXView::connectBufferView(BufferView & bv)
223 message_connection_ = bv.message.connect(
224 boost::bind(&LyXView::message, this, _1));
225 show_dialog_connection_ = bv.showDialog.connect(
226 boost::bind(&LyXView::showDialog, this, _1));
227 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
228 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
229 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
230 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
231 update_dialog_connection_ = bv.updateDialog.connect(
232 boost::bind(&LyXView::updateDialog, this, _1, _2));
233 layout_changed_connection_ = bv.layoutChanged.connect(
234 boost::bind(&Toolbars::setLayout, toolbars_, _1));
238 void LyXView::disconnectBufferView()
240 message_connection_.disconnect();
241 show_dialog_connection_.disconnect();
242 show_dialog_with_data_connection_.disconnect();
243 show_inset_dialog_connection_.disconnect();
244 update_dialog_connection_.disconnect();
248 void LyXView::showErrorList(string const & error_type)
250 ErrorList & el = buffer()->errorList(error_type);
252 getDialogs().show("errorlist", error_type);
257 void LyXView::showDialog(string const & name)
259 getDialogs().show(name);
263 void LyXView::showDialogWithData(string const & name, string const & data)
265 getDialogs().show(name, data);
269 void LyXView::showInsetDialog(string const & name, string const & data,
272 getDialogs().show(name, data, inset);
276 void LyXView::updateDialog(string const & name, string const & data)
278 if (getDialogs().visible(name))
279 getDialogs().update(name, data);
283 void LyXView::showReadonly(bool)
286 getDialogs().updateBufferDependent(false);
290 BufferView * LyXView::view()
292 WorkArea * wa = currentWorkArea();
293 return wa? &wa->bufferView() : 0;
297 void LyXView::updateToc()
299 updateDialog("toc", "");
303 void LyXView::updateEmbeddedFiles()
305 updateDialog("embedding", "");
309 void LyXView::updateToolbars()
311 WorkArea * wa = currentWorkArea();
314 wa->bufferView().cursor().inMathed();
316 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
318 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
319 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
321 toolbars_->update(math, table, review);
323 toolbars_->update(false, false, false);
325 // update redaonly status of open dialogs.
326 getDialogs().checkStatus();
330 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
332 return toolbars_->getToolbarInfo(name);
336 void LyXView::toggleToolbarState(string const & name, bool allowauto)
338 // it is possible to get current toolbar status like this,...
339 // but I decide to obey the order of ToolbarBackend::flags
340 // and disregard real toolbar status.
341 // toolbars_->saveToolbarInfo();
343 // toggle state on/off/auto
344 toolbars_->toggleToolbarState(name, allowauto);
350 void LyXView::autoSave()
352 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
355 lyx::autoSave(view());
359 void LyXView::resetAutosaveTimer()
362 autosave_timeout_->restart();
366 void LyXView::updateLayoutChoice()
368 // Don't show any layouts without a buffer
370 toolbars_->clearLayoutList();
374 // Update the layout display
375 if (toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
376 current_layout = buffer()->params().getTextClass().defaultLayoutName();
379 docstring const & layout = currentWorkArea()->bufferView().cursor().
380 innerParagraph().layout()->name();
382 if (layout != current_layout) {
383 toolbars_->setLayout(layout);
384 current_layout = layout;
389 void LyXView::updateWindowTitle()
391 docstring maximize_title = from_ascii("LyX");
392 docstring minimize_title = from_ascii("LyX");
394 Buffer * buf = buffer();
396 string const cur_title = buf->fileName();
397 if (!cur_title.empty()) {
398 maximize_title += ": " + makeDisplayPath(cur_title, 30);
399 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
400 if (!buf->isClean()) {
401 maximize_title += _(" (changed)");
402 minimize_title += char_type('*');
404 if (buf->isReadonly())
405 maximize_title += _(" (read only)");
409 setWindowTitle(maximize_title, minimize_title);
413 void LyXView::dispatch(FuncRequest const & cmd)
415 string const argument = to_utf8(cmd.argument());
417 case LFUN_BUFFER_SWITCH:
418 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
421 theLyXFunc().setLyXView(this);
427 Buffer const * LyXView::updateInset(Inset const * inset)
429 WorkArea * work_area = currentWorkArea();
434 BOOST_ASSERT(work_area);
435 work_area->scheduleRedraw();
437 return &work_area->bufferView().buffer();
441 void LyXView::openLayoutList()
443 toolbars_->openLayoutList();
447 bool LyXView::isToolbarVisible(std::string const & id)
449 return toolbars_->visible(id);
452 } // namespace frontend