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 if (errorsConnection_.connected())
169 bufferStructureChangedConnection_ =
170 buf.getMasterBuffer()->structureChanged.connect(
171 boost::bind(&LyXView::updateToc, this));
173 bufferEmbeddingChangedConnection_ =
174 buf.embeddingChanged.connect(
175 boost::bind(&LyXView::updateEmbeddedFiles, this));
179 boost::bind(&LyXView::showErrorList, this, _1));
183 boost::bind(&LyXView::message, this, _1));
187 boost::bind(&LyXView::busy, this, _1));
190 buf.updateTitles.connect(
191 boost::bind(&LyXView::updateWindowTitle, this));
194 buf.resetAutosaveTimers.connect(
195 boost::bind(&LyXView::resetAutosaveTimer, this));
197 readonlyConnection_ =
198 buf.readonly.connect(
199 boost::bind(&LyXView::showReadonly, this, _1));
203 void LyXView::disconnectBuffer()
205 errorsConnection_.disconnect();
206 bufferStructureChangedConnection_.disconnect();
207 bufferEmbeddingChangedConnection_.disconnect();
208 messageConnection_.disconnect();
209 busyConnection_.disconnect();
210 titleConnection_.disconnect();
211 timerConnection_.disconnect();
212 readonlyConnection_.disconnect();
216 void LyXView::connectBufferView(BufferView & bv)
218 message_connection_ = bv.message.connect(
219 boost::bind(&LyXView::message, this, _1));
220 show_dialog_connection_ = bv.showDialog.connect(
221 boost::bind(&LyXView::showDialog, this, _1));
222 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
223 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
224 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
225 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
226 update_dialog_connection_ = bv.updateDialog.connect(
227 boost::bind(&LyXView::updateDialog, this, _1, _2));
231 void LyXView::disconnectBufferView()
233 message_connection_.disconnect();
234 show_dialog_connection_.disconnect();
235 show_dialog_with_data_connection_.disconnect();
236 show_inset_dialog_connection_.disconnect();
237 update_dialog_connection_.disconnect();
241 void LyXView::showErrorList(string const & error_type)
243 ErrorList & el = buffer()->errorList(error_type);
245 getDialogs().show("errorlist", error_type);
250 void LyXView::showDialog(string const & name)
252 getDialogs().show(name);
256 void LyXView::showDialogWithData(string const & name, string const & data)
258 getDialogs().show(name, data);
262 void LyXView::showInsetDialog(string const & name, string const & data,
265 getDialogs().show(name, data, inset);
269 void LyXView::updateDialog(string const & name, string const & data)
271 if (getDialogs().visible(name))
272 getDialogs().update(name, data);
276 void LyXView::showReadonly(bool)
279 getDialogs().updateBufferDependent(false);
283 BufferView * LyXView::view()
285 WorkArea * wa = currentWorkArea();
286 return wa? &wa->bufferView() : 0;
290 void LyXView::updateToc()
292 updateDialog("toc", "");
296 void LyXView::updateEmbeddedFiles()
298 updateDialog("embedding", "");
302 void LyXView::autoSave()
304 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
307 lyx::autoSave(view());
311 void LyXView::resetAutosaveTimer()
314 autosave_timeout_->restart();
318 void LyXView::updateWindowTitle()
320 docstring maximize_title = from_ascii("LyX");
321 docstring minimize_title = from_ascii("LyX");
323 Buffer * buf = buffer();
325 string const cur_title = buf->fileName();
326 if (!cur_title.empty()) {
327 maximize_title += ": " + makeDisplayPath(cur_title, 30);
328 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
329 if (!buf->isClean()) {
330 maximize_title += _(" (changed)");
331 minimize_title += char_type('*');
333 if (buf->isReadonly())
334 maximize_title += _(" (read only)");
338 setWindowTitle(maximize_title, minimize_title);
342 void LyXView::dispatch(FuncRequest const & cmd)
344 string const argument = to_utf8(cmd.argument());
346 case LFUN_BUFFER_SWITCH:
347 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
350 theLyXFunc().setLyXView(this);
356 Buffer const * LyXView::updateInset(Inset const * inset)
358 WorkArea * work_area = currentWorkArea();
363 BOOST_ASSERT(work_area);
364 work_area->scheduleRedraw();
366 return &work_area->bufferView().buffer();
370 } // namespace frontend