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"
28 #include "ErrorList.h"
29 #include "FuncRequest.h"
37 #include "MenuBackend.h"
38 #include "Paragraph.h"
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
42 #include "support/Timeout.h"
44 #include <boost/bind.hpp>
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
59 using support::bformat;
60 using support::FileName;
61 using support::makeDisplayPath;
62 using support::onlyFilename;
66 docstring current_layout;
68 LyXView::LyXView(int id)
69 : toolbars_(new Toolbars(*this)),
70 autosave_timeout_(new Timeout(5000)),
71 dialogs_(new Dialogs(*this)),
74 // Start autosave timer
76 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
77 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
78 autosave_timeout_->start();
86 disconnectBufferView();
89 delete autosave_timeout_;
93 Buffer * LyXView::buffer()
95 WorkArea * work_area = currentWorkArea();
97 return &work_area->bufferView().buffer();
102 Buffer const * LyXView::buffer() const
104 WorkArea const * work_area = currentWorkArea();
106 return &work_area->bufferView().buffer();
111 void LyXView::setBuffer(Buffer * newBuffer)
113 BOOST_ASSERT(newBuffer);
116 WorkArea * wa = workArea(*newBuffer);
118 updateLabels(*newBuffer->getMasterBuffer());
119 wa = addWorkArea(*newBuffer);
121 //Disconnect the old buffer...there's no new one.
124 connectBuffer(*newBuffer);
125 connectBufferView(wa->bufferView());
126 setCurrentWorkArea(wa);
132 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
136 Buffer * newBuffer = checkAndLoadLyXFile(filename);
139 message(_("Document not loaded."));
145 WorkArea * wa = workArea(*newBuffer);
147 wa = addWorkArea(*newBuffer);
149 // scroll to the position when the file was last closed
150 if (lyxrc.use_lastfilepos) {
153 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
154 // if successfully move to pit (returned par_id is not zero),
155 // update metrics and reset font
156 wa->bufferView().moveToPosition(pit, pos, 0, 0);
160 LyX::ref().session().lastFiles().add(filename);
167 void LyXView::connectBuffer(Buffer & buf)
169 if (errorsConnection_.connected())
172 bufferStructureChangedConnection_ =
173 buf.getMasterBuffer()->structureChanged.connect(
174 boost::bind(&LyXView::updateToc, this));
176 bufferEmbeddingChangedConnection_ =
177 buf.embeddingChanged.connect(
178 boost::bind(&LyXView::updateEmbeddedFiles, this));
182 boost::bind(&LyXView::showErrorList, this, _1));
186 boost::bind(&LyXView::message, this, _1));
190 boost::bind(&LyXView::busy, this, _1));
193 buf.updateTitles.connect(
194 boost::bind(&LyXView::updateWindowTitle, this));
197 buf.resetAutosaveTimers.connect(
198 boost::bind(&LyXView::resetAutosaveTimer, this));
200 readonlyConnection_ =
201 buf.readonly.connect(
202 boost::bind(&LyXView::showReadonly, this, _1));
206 void LyXView::disconnectBuffer()
208 errorsConnection_.disconnect();
209 bufferStructureChangedConnection_.disconnect();
210 bufferEmbeddingChangedConnection_.disconnect();
211 messageConnection_.disconnect();
212 busyConnection_.disconnect();
213 titleConnection_.disconnect();
214 timerConnection_.disconnect();
215 readonlyConnection_.disconnect();
216 layout_changed_connection_.disconnect();
220 void LyXView::connectBufferView(BufferView & bv)
222 message_connection_ = bv.message.connect(
223 boost::bind(&LyXView::message, this, _1));
224 show_dialog_connection_ = bv.showDialog.connect(
225 boost::bind(&LyXView::showDialog, this, _1));
226 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
227 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
228 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
229 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
230 update_dialog_connection_ = bv.updateDialog.connect(
231 boost::bind(&LyXView::updateDialog, this, _1, _2));
232 layout_changed_connection_ = bv.layoutChanged.connect(
233 boost::bind(&Toolbars::setLayout, toolbars_, _1));
237 void LyXView::disconnectBufferView()
239 message_connection_.disconnect();
240 show_dialog_connection_.disconnect();
241 show_dialog_with_data_connection_.disconnect();
242 show_inset_dialog_connection_.disconnect();
243 update_dialog_connection_.disconnect();
247 void LyXView::showErrorList(string const & error_type)
249 ErrorList & el = buffer()->errorList(error_type);
251 getDialogs().show("errorlist", error_type);
256 void LyXView::showDialog(string const & name)
258 getDialogs().show(name);
262 void LyXView::showDialogWithData(string const & name, string const & data)
264 getDialogs().show(name, data);
268 void LyXView::showInsetDialog(string const & name, string const & data,
271 getDialogs().show(name, data, inset);
275 void LyXView::updateDialog(string const & name, string const & data)
277 if (getDialogs().visible(name))
278 getDialogs().update(name, data);
282 void LyXView::showReadonly(bool)
285 getDialogs().updateBufferDependent(false);
289 BufferView * LyXView::view()
291 WorkArea * wa = currentWorkArea();
292 return wa? &wa->bufferView() : 0;
296 void LyXView::updateToc()
298 updateDialog("toc", "");
302 void LyXView::updateEmbeddedFiles()
304 updateDialog("embedding", "");
308 void LyXView::updateToolbars()
310 WorkArea * wa = currentWorkArea();
313 wa->bufferView().cursor().inMathed();
315 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
317 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
318 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
320 toolbars_->update(math, table, review);
322 toolbars_->update(false, false, false);
324 // update redaonly status of open dialogs.
325 getDialogs().checkStatus();
329 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
331 return toolbars_->getToolbarInfo(name);
335 void LyXView::toggleToolbarState(string const & name, bool allowauto)
337 // it is possible to get current toolbar status like this,...
338 // but I decide to obey the order of ToolbarBackend::flags
339 // and disregard real toolbar status.
340 // toolbars_->saveToolbarInfo();
342 // toggle state on/off/auto
343 toolbars_->toggleToolbarState(name, allowauto);
349 void LyXView::autoSave()
351 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
354 lyx::autoSave(view());
358 void LyXView::resetAutosaveTimer()
361 autosave_timeout_->restart();
365 void LyXView::updateLayoutChoice()
367 // Don't show any layouts without a buffer
369 toolbars_->clearLayoutList();
373 // Update the layout display
374 if (toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
375 current_layout = buffer()->params().getTextClass().defaultLayoutName();
378 docstring const & layout = currentWorkArea()->bufferView().cursor().
379 innerParagraph().layout()->name();
381 if (layout != current_layout) {
382 toolbars_->setLayout(layout);
383 current_layout = layout;
388 void LyXView::updateWindowTitle()
390 docstring maximize_title = from_ascii("LyX");
391 docstring minimize_title = from_ascii("LyX");
393 Buffer * buf = buffer();
395 string const cur_title = buf->fileName();
396 if (!cur_title.empty()) {
397 maximize_title += ": " + makeDisplayPath(cur_title, 30);
398 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
399 if (!buf->isClean()) {
400 maximize_title += _(" (changed)");
401 minimize_title += char_type('*');
403 if (buf->isReadonly())
404 maximize_title += _(" (read only)");
408 setWindowTitle(maximize_title, minimize_title);
412 void LyXView::dispatch(FuncRequest const & cmd)
414 string const argument = to_utf8(cmd.argument());
416 case LFUN_BUFFER_SWITCH:
417 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
420 theLyXFunc().setLyXView(this);
426 Buffer const * LyXView::updateInset(Inset const * inset)
428 WorkArea * work_area = currentWorkArea();
433 BOOST_ASSERT(work_area);
434 work_area->scheduleRedraw();
436 return &work_area->bufferView().buffer();
440 void LyXView::openLayoutList()
442 toolbars_->openLayoutList();
446 bool LyXView::isToolbarVisible(std::string const & id)
448 return toolbars_->visible(id);
451 } // namespace frontend