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->getMasterBuffer());
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) {
147 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
148 // if successfully move to pit (returned par_id is not zero),
149 // update metrics and reset font
150 wa->bufferView().moveToPosition(pit, pos, 0, 0);
154 LyX::ref().session().lastFiles().add(filename);
161 void LyXView::connectBuffer(Buffer & buf)
163 buf.setGuiDelegate(this);
167 void LyXView::disconnectBuffer()
169 if (WorkArea * work_area = currentWorkArea())
170 work_area->bufferView().setGuiDelegate(0);
174 void LyXView::connectBufferView(BufferView & bv)
176 bv.setGuiDelegate(this);
180 void LyXView::disconnectBufferView()
182 if (WorkArea * work_area = currentWorkArea())
183 work_area->bufferView().setGuiDelegate(0);
187 void LyXView::showErrorList(string const & error_type)
189 ErrorList & el = buffer()->errorList(error_type);
191 getDialogs().show("errorlist", error_type);
195 void LyXView::showDialog(string const & name)
197 getDialogs().show(name);
201 void LyXView::showDialogWithData(string const & name, string const & data)
203 getDialogs().show(name, data);
207 void LyXView::showInsetDialog(string const & name, string const & data,
210 getDialogs().show(name, data, inset);
214 void LyXView::updateDialog(string const & name, string const & data)
216 if (getDialogs().visible(name))
217 getDialogs().update(name, data);
221 void LyXView::showReadonly(bool)
224 getDialogs().updateBufferDependent(false);
228 BufferView * LyXView::view()
230 WorkArea * wa = currentWorkArea();
231 return wa? &wa->bufferView() : 0;
235 void LyXView::updateToc()
237 updateDialog("toc", "");
241 void LyXView::updateEmbeddedFiles()
243 updateDialog("embedding", "");
247 void LyXView::autoSave()
249 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
252 view()->buffer().autoSave();
256 void LyXView::resetAutosaveTimer()
259 autosave_timeout_->restart();
263 void LyXView::updateWindowTitle()
265 docstring maximize_title = from_ascii("LyX");
266 docstring minimize_title = from_ascii("LyX");
268 Buffer * buf = buffer();
270 string const cur_title = buf->fileName();
271 if (!cur_title.empty()) {
272 maximize_title += ": " + makeDisplayPath(cur_title, 30);
273 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
274 if (!buf->isClean()) {
275 maximize_title += _(" (changed)");
276 minimize_title += char_type('*');
278 if (buf->isReadonly())
279 maximize_title += _(" (read only)");
283 setWindowTitle(maximize_title, minimize_title);
287 void LyXView::dispatch(FuncRequest const & cmd)
289 string const argument = to_utf8(cmd.argument());
291 case LFUN_BUFFER_SWITCH:
292 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
295 theLyXFunc().setLyXView(this);
301 Buffer const * LyXView::updateInset(Inset const * inset)
303 WorkArea * work_area = currentWorkArea();
308 BOOST_ASSERT(work_area);
309 work_area->scheduleRedraw();
311 return &work_area->bufferView().buffer();
314 } // namespace frontend