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 disconnectBufferView();
86 delete autosave_timeout_;
90 Buffer * LyXView::buffer()
92 WorkArea * work_area = currentWorkArea();
94 return &work_area->bufferView().buffer();
99 Buffer const * LyXView::buffer() const
101 WorkArea const * work_area = currentWorkArea();
103 return &work_area->bufferView().buffer();
108 void LyXView::setBuffer(Buffer * newBuffer)
110 BOOST_ASSERT(newBuffer);
113 WorkArea * wa = workArea(*newBuffer);
115 updateLabels(*newBuffer->getMasterBuffer());
116 wa = addWorkArea(*newBuffer);
118 //Disconnect the old buffer...there's no new one.
121 connectBuffer(*newBuffer);
122 connectBufferView(wa->bufferView());
123 setCurrentWorkArea(wa);
129 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
133 Buffer * newBuffer = checkAndLoadLyXFile(filename);
136 message(_("Document not loaded."));
142 WorkArea * wa = workArea(*newBuffer);
144 wa = addWorkArea(*newBuffer);
146 // scroll to the position when the file was last closed
147 if (lyxrc.use_lastfilepos) {
150 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
151 // if successfully move to pit (returned par_id is not zero),
152 // update metrics and reset font
153 wa->bufferView().moveToPosition(pit, pos, 0, 0);
157 LyX::ref().session().lastFiles().add(filename);
164 void LyXView::connectBuffer(Buffer & buf)
166 buf.setGuiDelegate(this);
170 void LyXView::disconnectBuffer()
172 if (WorkArea * work_area = currentWorkArea())
173 work_area->bufferView().setGuiDelegate(0);
177 void LyXView::connectBufferView(BufferView & bv)
179 bv.setGuiDelegate(this);
183 void LyXView::disconnectBufferView()
185 if (WorkArea * work_area = currentWorkArea())
186 work_area->bufferView().setGuiDelegate(0);
190 void LyXView::showErrorList(string const & error_type)
192 ErrorList & el = buffer()->errorList(error_type);
194 getDialogs().show("errorlist", error_type);
198 void LyXView::showDialog(string const & name)
200 getDialogs().show(name);
204 void LyXView::showDialogWithData(string const & name, string const & data)
206 getDialogs().show(name, data);
210 void LyXView::showInsetDialog(string const & name, string const & data,
213 getDialogs().show(name, data, inset);
217 void LyXView::updateDialog(string const & name, string const & data)
219 if (getDialogs().visible(name))
220 getDialogs().update(name, data);
224 void LyXView::showReadonly(bool)
227 getDialogs().updateBufferDependent(false);
231 BufferView * LyXView::view()
233 WorkArea * wa = currentWorkArea();
234 return wa? &wa->bufferView() : 0;
238 void LyXView::updateToc()
240 updateDialog("toc", "");
244 void LyXView::updateEmbeddedFiles()
246 updateDialog("embedding", "");
250 void LyXView::autoSave()
252 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
255 lyx::autoSave(view());
259 void LyXView::resetAutosaveTimer()
262 autosave_timeout_->restart();
266 void LyXView::updateWindowTitle()
268 docstring maximize_title = from_ascii("LyX");
269 docstring minimize_title = from_ascii("LyX");
271 Buffer * buf = buffer();
273 string const cur_title = buf->fileName();
274 if (!cur_title.empty()) {
275 maximize_title += ": " + makeDisplayPath(cur_title, 30);
276 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
277 if (!buf->isClean()) {
278 maximize_title += _(" (changed)");
279 minimize_title += char_type('*');
281 if (buf->isReadonly())
282 maximize_title += _(" (read only)");
286 setWindowTitle(maximize_title, minimize_title);
290 void LyXView::dispatch(FuncRequest const & cmd)
292 string const argument = to_utf8(cmd.argument());
294 case LFUN_BUFFER_SWITCH:
295 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
298 theLyXFunc().setLyXView(this);
304 Buffer const * LyXView::updateInset(Inset const * inset)
306 WorkArea * work_area = currentWorkArea();
311 BOOST_ASSERT(work_area);
312 work_area->scheduleRedraw();
314 return &work_area->bufferView().buffer();
317 } // namespace frontend