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.
21 #include "buffer_funcs.h"
22 #include "BufferList.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
27 #include "ErrorList.h"
28 #include "FuncRequest.h"
35 #include "MenuBackend.h"
36 #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 LyXView::LyXView(int id)
67 : autosave_timeout_(new Timeout(5000)),
68 dialogs_(new Dialogs(*this)),
71 // Start autosave timer
73 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
74 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
75 autosave_timeout_->start();
83 delete autosave_timeout_;
87 Buffer * LyXView::buffer()
89 WorkArea * work_area = currentWorkArea();
91 return &work_area->bufferView().buffer();
96 Buffer const * LyXView::buffer() const
98 WorkArea const * work_area = currentWorkArea();
100 return &work_area->bufferView().buffer();
105 void LyXView::setBuffer(Buffer * newBuffer)
107 BOOST_ASSERT(newBuffer);
110 WorkArea * wa = workArea(*newBuffer);
112 updateLabels(*newBuffer->masterBuffer());
113 wa = addWorkArea(*newBuffer);
115 //Disconnect the old buffer...there's no new one.
118 connectBuffer(*newBuffer);
119 connectBufferView(wa->bufferView());
120 setCurrentWorkArea(wa);
126 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
130 Buffer * newBuffer = checkAndLoadLyXFile(filename);
133 message(_("Document not loaded."));
139 WorkArea * wa = workArea(*newBuffer);
141 wa = addWorkArea(*newBuffer);
143 // scroll to the position when the file was last closed
144 if (lyxrc.use_lastfilepos) {
145 LastFilePosSection::FilePos filepos =
146 LyX::ref().session().lastFilePos().load(filename);
147 // if successfully move to pit (returned par_id is not zero),
148 // update metrics and reset font
149 wa->bufferView().moveToPosition(filepos.pit, filepos.pos, 0, 0);
153 LyX::ref().session().lastFiles().add(filename);
160 void LyXView::connectBuffer(Buffer & buf)
162 buf.setGuiDelegate(this);
166 void LyXView::disconnectBuffer()
168 if (WorkArea * work_area = currentWorkArea())
169 work_area->bufferView().setGuiDelegate(0);
173 void LyXView::connectBufferView(BufferView & bv)
175 bv.setGuiDelegate(this);
179 void LyXView::disconnectBufferView()
181 if (WorkArea * work_area = currentWorkArea())
182 work_area->bufferView().setGuiDelegate(0);
186 void LyXView::showErrorList(string const & error_type)
188 ErrorList & el = buffer()->errorList(error_type);
190 getDialogs().show("errorlist", error_type);
194 void LyXView::showDialog(string const & name)
196 getDialogs().show(name);
200 void LyXView::showDialogWithData(string const & name, string const & data)
202 getDialogs().show(name, data);
206 void LyXView::showInsetDialog(string const & name, string const & data,
209 getDialogs().show(name, data, inset);
213 void LyXView::updateDialog(string const & name, string const & data)
215 if (getDialogs().visible(name))
216 getDialogs().update(name, data);
220 BufferView * LyXView::view()
222 WorkArea * wa = currentWorkArea();
223 return wa? &wa->bufferView() : 0;
227 void LyXView::updateToc()
229 updateDialog("toc", "");
233 void LyXView::updateEmbeddedFiles()
235 updateDialog("embedding", "");
239 void LyXView::autoSave()
241 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
244 view()->buffer().autoSave();
248 void LyXView::resetAutosaveTimer()
251 autosave_timeout_->restart();
255 void LyXView::dispatch(FuncRequest const & cmd)
257 string const argument = to_utf8(cmd.argument());
259 case LFUN_BUFFER_SWITCH:
260 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
263 theLyXFunc().setLyXView(this);
269 Buffer const * LyXView::updateInset(Inset const * inset)
271 WorkArea * work_area = currentWorkArea();
276 BOOST_ASSERT(work_area);
277 work_area->scheduleRedraw();
279 return &work_area->bufferView().buffer();
282 } // namespace frontend