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"
28 #include "ErrorList.h"
29 #include "FuncRequest.h"
36 #include "MenuBackend.h"
37 #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 LyXView::LyXView(int id)
68 : autosave_timeout_(new Timeout(5000)),
69 dialogs_(new Dialogs(*this)),
72 // Start autosave timer
74 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
75 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
76 autosave_timeout_->start();
84 delete autosave_timeout_;
88 Buffer * LyXView::buffer()
90 WorkArea * work_area = currentWorkArea();
92 return &work_area->bufferView().buffer();
97 Buffer const * LyXView::buffer() const
99 WorkArea const * work_area = currentWorkArea();
101 return &work_area->bufferView().buffer();
106 void LyXView::setBuffer(Buffer * newBuffer)
108 BOOST_ASSERT(newBuffer);
111 WorkArea * wa = workArea(*newBuffer);
113 updateLabels(*newBuffer->getMasterBuffer());
114 wa = addWorkArea(*newBuffer);
116 //Disconnect the old buffer...there's no new one.
119 connectBuffer(*newBuffer);
120 connectBufferView(wa->bufferView());
121 setCurrentWorkArea(wa);
127 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
131 Buffer * newBuffer = checkAndLoadLyXFile(filename);
134 message(_("Document not loaded."));
140 WorkArea * wa = workArea(*newBuffer);
142 wa = addWorkArea(*newBuffer);
144 // scroll to the position when the file was last closed
145 if (lyxrc.use_lastfilepos) {
148 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
149 // if successfully move to pit (returned par_id is not zero),
150 // update metrics and reset font
151 wa->bufferView().moveToPosition(pit, pos, 0, 0);
155 LyX::ref().session().lastFiles().add(filename);
162 void LyXView::connectBuffer(Buffer & buf)
164 buf.setGuiDelegate(this);
168 void LyXView::disconnectBuffer()
170 if (WorkArea * work_area = currentWorkArea())
171 work_area->bufferView().setGuiDelegate(0);
175 void LyXView::connectBufferView(BufferView & bv)
177 bv.setGuiDelegate(this);
181 void LyXView::disconnectBufferView()
183 if (WorkArea * work_area = currentWorkArea())
184 work_area->bufferView().setGuiDelegate(0);
188 void LyXView::showErrorList(string const & error_type)
190 ErrorList & el = buffer()->errorList(error_type);
192 getDialogs().show("errorlist", error_type);
196 void LyXView::showDialog(string const & name)
198 getDialogs().show(name);
202 void LyXView::showDialogWithData(string const & name, string const & data)
204 getDialogs().show(name, data);
208 void LyXView::showInsetDialog(string const & name, string const & data,
211 getDialogs().show(name, data, inset);
215 void LyXView::updateDialog(string const & name, string const & data)
217 if (getDialogs().visible(name))
218 getDialogs().update(name, data);
222 void LyXView::showReadonly(bool)
225 getDialogs().updateBufferDependent(false);
229 BufferView * LyXView::view()
231 WorkArea * wa = currentWorkArea();
232 return wa? &wa->bufferView() : 0;
236 void LyXView::updateToc()
238 updateDialog("toc", "");
242 void LyXView::updateEmbeddedFiles()
244 updateDialog("embedding", "");
248 void LyXView::autoSave()
250 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
253 lyx::autoSave(view());
257 void LyXView::resetAutosaveTimer()
260 autosave_timeout_->restart();
264 void LyXView::updateWindowTitle()
266 docstring maximize_title = from_ascii("LyX");
267 docstring minimize_title = from_ascii("LyX");
269 Buffer * buf = buffer();
271 string const cur_title = buf->fileName();
272 if (!cur_title.empty()) {
273 maximize_title += ": " + makeDisplayPath(cur_title, 30);
274 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
275 if (!buf->isClean()) {
276 maximize_title += _(" (changed)");
277 minimize_title += char_type('*');
279 if (buf->isReadonly())
280 maximize_title += _(" (read only)");
284 setWindowTitle(maximize_title, minimize_title);
288 void LyXView::dispatch(FuncRequest const & cmd)
290 string const argument = to_utf8(cmd.argument());
292 case LFUN_BUFFER_SWITCH:
293 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
296 theLyXFunc().setLyXView(this);
302 Buffer const * LyXView::updateInset(Inset const * inset)
304 WorkArea * work_area = currentWorkArea();
309 BOOST_ASSERT(work_area);
310 work_area->scheduleRedraw();
312 return &work_area->bufferView().buffer();
315 } // namespace frontend