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.
22 #include "buffer_funcs.h"
23 #include "BufferList.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "bufferview_funcs.h"
29 #include "ErrorList.h"
30 #include "FuncRequest.h"
38 #include "MenuBackend.h"
39 #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 docstring current_layout;
69 LyXView::LyXView(int id)
70 : toolbars_(new Toolbars(*this)),
71 autosave_timeout_(new Timeout(5000)),
72 dialogs_(new Dialogs(*this)),
75 // Start autosave timer
77 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
78 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
79 autosave_timeout_->start();
87 disconnectBufferView();
91 Buffer * LyXView::buffer()
93 WorkArea * work_area = currentWorkArea();
95 return &work_area->bufferView().buffer();
100 Buffer const * LyXView::buffer() const
102 WorkArea const * work_area = currentWorkArea();
104 return &work_area->bufferView().buffer();
109 void LyXView::setBuffer(Buffer * newBuffer)
111 BOOST_ASSERT(newBuffer);
114 WorkArea * wa = workArea(*newBuffer);
116 updateLabels(*newBuffer->getMasterBuffer());
117 wa = addWorkArea(*newBuffer);
119 //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 = 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 if (errorsConnection_.connected())
167 bufferStructureChangedConnection_ =
168 buf.getMasterBuffer()->structureChanged.connect(
169 boost::bind(&LyXView::updateToc, this));
173 boost::bind(&LyXView::showErrorList, this, _1));
177 boost::bind(&LyXView::message, this, _1));
181 boost::bind(&LyXView::busy, this, _1));
184 buf.updateTitles.connect(
185 boost::bind(&LyXView::updateWindowTitle, this));
188 buf.resetAutosaveTimers.connect(
189 boost::bind(&LyXView::resetAutosaveTimer, this));
191 readonlyConnection_ =
192 buf.readonly.connect(
193 boost::bind(&LyXView::showReadonly, this, _1));
197 void LyXView::disconnectBuffer()
199 errorsConnection_.disconnect();
200 bufferStructureChangedConnection_.disconnect();
201 messageConnection_.disconnect();
202 busyConnection_.disconnect();
203 titleConnection_.disconnect();
204 timerConnection_.disconnect();
205 readonlyConnection_.disconnect();
206 layout_changed_connection_.disconnect();
210 void LyXView::connectBufferView(BufferView & bv)
212 message_connection_ = bv.message.connect(
213 boost::bind(&LyXView::message, this, _1));
214 show_dialog_connection_ = bv.showDialog.connect(
215 boost::bind(&LyXView::showDialog, this, _1));
216 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
217 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
218 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
219 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
220 update_dialog_connection_ = bv.updateDialog.connect(
221 boost::bind(&LyXView::updateDialog, this, _1, _2));
222 layout_changed_connection_ = bv.layoutChanged.connect(
223 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
227 void LyXView::disconnectBufferView()
229 message_connection_.disconnect();
230 show_dialog_connection_.disconnect();
231 show_dialog_with_data_connection_.disconnect();
232 show_inset_dialog_connection_.disconnect();
233 update_dialog_connection_.disconnect();
237 void LyXView::showErrorList(string const & error_type)
239 ErrorList & el = buffer()->errorList(error_type);
241 getDialogs().show("errorlist", error_type);
246 void LyXView::showDialog(string const & name)
248 getDialogs().show(name);
252 void LyXView::showDialogWithData(string const & name, string const & data)
254 getDialogs().show(name, data);
258 void LyXView::showInsetDialog(string const & name, string const & data,
261 getDialogs().show(name, data, inset);
265 void LyXView::updateDialog(string const & name, string const & data)
267 if (getDialogs().visible(name))
268 getDialogs().update(name, data);
272 void LyXView::showReadonly(bool)
275 getDialogs().updateBufferDependent(false);
279 BufferView * LyXView::view()
281 WorkArea * wa = currentWorkArea();
282 return wa? &wa->bufferView() : 0;
286 void LyXView::updateToc()
288 updateDialog("toc", "");
292 void LyXView::updateToolbars()
294 WorkArea * wa = currentWorkArea();
297 wa->bufferView().cursor().inMathed();
299 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
301 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
302 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
304 toolbars_->update(math, table, review);
306 toolbars_->update(false, false, false);
308 // update redaonly status of open dialogs.
309 getDialogs().checkStatus();
313 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
315 return toolbars_->getToolbarInfo(name);
319 void LyXView::toggleToolbarState(string const & name, bool allowauto)
321 // it is possible to get current toolbar status like this,...
322 // but I decide to obey the order of ToolbarBackend::flags
323 // and disregard real toolbar status.
324 // toolbars_->saveToolbarInfo();
326 // toggle state on/off/auto
327 toolbars_->toggleToolbarState(name, allowauto);
333 void LyXView::autoSave()
335 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
338 lyx::autoSave(view());
342 void LyXView::resetAutosaveTimer()
345 autosave_timeout_->restart();
349 void LyXView::updateLayoutChoice()
351 // Don't show any layouts without a buffer
353 toolbars_->clearLayoutList();
357 // Update the layout display
358 if (toolbars_->updateLayoutList(buffer()->params().getTextClass_ptr())) {
359 current_layout = buffer()->params().getTextClass().defaultLayoutName();
362 docstring const & layout = currentWorkArea()->bufferView().cursor().
363 innerParagraph().layout()->name();
365 if (layout != current_layout) {
366 toolbars_->setLayout(layout);
367 current_layout = layout;
372 void LyXView::updateWindowTitle()
374 docstring maximize_title = lyx::from_ascii("LyX");
375 docstring minimize_title = lyx::from_ascii("LyX");
377 Buffer * buf = buffer();
379 string const cur_title = buf->fileName();
380 if (!cur_title.empty()) {
381 maximize_title += ": " + makeDisplayPath(cur_title, 30);
382 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
383 if (!buf->isClean()) {
384 maximize_title += _(" (changed)");
385 minimize_title += lyx::char_type('*');
387 if (buf->isReadonly())
388 maximize_title += _(" (read only)");
392 setWindowTitle(maximize_title, minimize_title);
396 void LyXView::dispatch(FuncRequest const & cmd)
398 string const argument = to_utf8(cmd.argument());
400 case LFUN_BUFFER_SWITCH:
401 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
404 theLyXFunc().setLyXView(this);
410 Buffer const * const LyXView::updateInset(Inset const * inset)
412 WorkArea * work_area = currentWorkArea();
417 BOOST_ASSERT(work_area);
418 work_area->scheduleRedraw();
420 return &work_area->bufferView().buffer();
424 void LyXView::openLayoutList()
426 toolbars_->openLayoutList();
430 bool LyXView::isToolbarVisible(std::string const & id)
432 return toolbars_->visible(id);
435 } // namespace frontend