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.
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"
37 #include "MenuBackend.h"
38 #include "Paragraph.h"
40 #include "controllers/ControlCommandBuffer.h"
42 #include "support/lstrings.h"
43 #include "support/filetools.h" // OnlyFilename()
45 #include <boost/bind.hpp>
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
57 using frontend::WorkArea;
59 using support::bformat;
60 using support::FileName;
61 using support::makeDisplayPath;
62 using support::onlyFilename;
67 using lyx::frontend::ControlCommandBuffer;
69 string current_layout;
72 LyXView::LyXView(int id)
74 toolbars_(new Toolbars(*this)),
75 autosave_timeout_(new Timeout(5000)),
76 dialogs_(new Dialogs(*this)),
77 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
79 // Start autosave timer
81 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
82 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
83 autosave_timeout_->start();
94 // FIXME, there's only one WorkArea per LyXView possible for now.
95 void LyXView::setWorkArea(WorkArea * work_area)
97 BOOST_ASSERT(work_area);
98 work_area_ = work_area;
99 work_area_ids_.clear();
100 work_area_ids_.push_back(work_area_->id());
104 // FIXME, there's only one WorkArea per LyXView possible for now.
105 WorkArea const * LyXView::currentWorkArea() const
111 // FIXME, there's only one WorkArea per LyXView possible for now.
112 WorkArea * LyXView::currentWorkArea()
118 Buffer * LyXView::buffer() const
120 BOOST_ASSERT(work_area_);
121 return work_area_->bufferView().buffer();
125 void LyXView::setBuffer(Buffer * b)
129 BOOST_ASSERT(work_area_);
130 if (work_area_->bufferView().buffer())
133 if (!b && theBufferList().empty())
134 getDialogs().hideBufferDependent();
136 work_area_->bufferView().setBuffer(b);
137 // Make sure the TOC is updated before anything else.
140 if (work_area_->bufferView().buffer()) {
141 // Buffer-dependent dialogs should be updated or
142 // hidden. This should go here because some dialogs (eg ToC)
143 // require bv_->text.
144 getDialogs().updateBufferDependent(true);
145 connectBuffer(*work_area_->bufferView().buffer());
153 updateLayoutChoice();
158 work_area_->redraw();
162 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
166 BOOST_ASSERT(work_area_);
167 bool const hadBuffer = work_area_->bufferView().buffer();
172 work_area_->bufferView().loadLyXFile(filename, tolastfiles);
177 updateLayoutChoice();
181 connectBuffer(*work_area_->bufferView().buffer());
182 showErrorList("Parse");
183 } else if (hadBuffer)
184 //Need to reconnect the buffer if the load failed
185 connectBuffer(*work_area_->bufferView().buffer());
188 work_area_->redraw();
193 void LyXView::connectBuffer(Buffer & buf)
195 if (errorsConnection_.connected())
198 BOOST_ASSERT(work_area_);
199 bufferChangedConnection_ =
201 boost::bind(&WorkArea::redraw, work_area_));
203 bufferStructureChangedConnection_ =
204 buf.getMasterBuffer()->structureChanged.connect(
205 boost::bind(&LyXView::updateToc, this));
209 boost::bind(&LyXView::showErrorList, this, _1));
213 boost::bind(&LyXView::message, this, _1));
217 boost::bind(&LyXView::busy, this, _1));
220 buf.updateTitles.connect(
221 boost::bind(&LyXView::updateWindowTitle, this));
224 buf.resetAutosaveTimers.connect(
225 boost::bind(&LyXView::resetAutosaveTimer, this));
227 readonlyConnection_ =
228 buf.readonly.connect(
229 boost::bind(&LyXView::showReadonly, this, _1));
233 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
237 void LyXView::disconnectBuffer()
239 errorsConnection_.disconnect();
240 bufferChangedConnection_.disconnect();
241 bufferStructureChangedConnection_.disconnect();
242 messageConnection_.disconnect();
243 busyConnection_.disconnect();
244 titleConnection_.disconnect();
245 timerConnection_.disconnect();
246 readonlyConnection_.disconnect();
247 closingConnection_.disconnect();
248 layout_changed_connection_.disconnect();
252 void LyXView::connectBufferView(BufferView & bv)
254 show_dialog_connection_ = bv.showDialog.connect(
255 boost::bind(&LyXView::showDialog, this, _1));
256 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
257 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
258 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
259 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
260 update_dialog_connection_ = bv.updateDialog.connect(
261 boost::bind(&LyXView::updateDialog, this, _1, _2));
262 layout_changed_connection_ = bv.layoutChanged.connect(
263 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
267 void LyXView::disconnectBufferView()
269 show_dialog_connection_.disconnect();
270 show_dialog_with_data_connection_.disconnect();
271 show_inset_dialog_connection_.disconnect();
272 update_dialog_connection_.disconnect();
276 void LyXView::showErrorList(string const & error_type)
278 ErrorList & el = buffer()->errorList(error_type);
280 getDialogs().show("errorlist", error_type);
285 void LyXView::showDialog(string const & name)
287 getDialogs().show(name);
291 void LyXView::showDialogWithData(string const & name, string const & data)
293 getDialogs().show(name, data);
297 void LyXView::showInsetDialog(string const & name, string const & data,
300 getDialogs().show(name, data, inset);
304 void LyXView::updateDialog(string const & name, string const & data)
306 if (getDialogs().visible(name))
307 getDialogs().update(name, data);
311 void LyXView::showReadonly(bool)
314 getDialogs().updateBufferDependent(false);
318 BufferView * LyXView::view() const
320 BOOST_ASSERT(work_area_);
321 return &work_area_->bufferView();
325 void LyXView::updateToc()
327 updateDialog("toc", "");
331 void LyXView::updateToolbars()
333 BOOST_ASSERT(work_area_);
335 work_area_->bufferView().cursor().inMathed();
337 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
339 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
340 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
342 toolbars_->update(math, table, review);
343 // update redaonly status of open dialogs. This could also be in
344 // updateMenubar(), but since updateToolbars() and updateMenubar()
345 // are always called together it is only here.
346 getDialogs().checkStatus();
350 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
352 return toolbars_->getToolbarState(name);
356 void LyXView::toggleToolbarState(string const & name, bool allowauto)
358 // it is possible to get current toolbar status like this,...
359 // but I decide to obey the order of ToolbarBackend::flags
360 // and disregard real toolbar status.
361 // toolbars_->saveToolbarInfo();
363 // toggle state on/off/auto
364 toolbars_->toggleToolbarState(name, allowauto);
370 void LyXView::updateMenubar()
376 void LyXView::autoSave()
378 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
380 if (view()->buffer())
381 lyx::autoSave(view());
385 void LyXView::resetAutosaveTimer()
388 autosave_timeout_->restart();
392 void LyXView::updateLayoutChoice()
394 // Don't show any layouts without a buffer
395 if (!view()->buffer()) {
396 toolbars_->clearLayoutList();
400 // Update the layout display
401 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
402 current_layout = buffer()->params().getTextClass().defaultLayoutName();
405 BOOST_ASSERT(work_area_);
406 if (work_area_->bufferView().cursor().inMathed())
409 string const & layout =
410 work_area_->bufferView().cursor().paragraph().layout()->name();
412 if (layout != current_layout) {
413 toolbars_->setLayout(layout);
414 current_layout = layout;
419 void LyXView::updateWindowTitle()
421 docstring maximize_title = lyx::from_ascii("LyX");
422 docstring minimize_title = lyx::from_ascii("LyX");
424 if (view()->buffer()) {
425 string const cur_title = buffer()->fileName();
426 if (!cur_title.empty()) {
427 maximize_title += ": " + makeDisplayPath(cur_title, 30);
428 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
429 if (!buffer()->isClean()) {
430 maximize_title += _(" (changed)");
431 minimize_title += lyx::char_type('*');
433 if (buffer()->isReadonly())
434 maximize_title += _(" (read only)");
438 setWindowTitle(maximize_title, minimize_title);
443 void LyXView::dispatch(FuncRequest const & cmd)
445 theLyXFunc().setLyXView(this);
450 Buffer const * const LyXView::updateInset(Inset const * inset) const
452 Buffer const * buffer_ptr = 0;
454 BOOST_ASSERT(work_area_);
455 work_area_->scheduleRedraw();
457 buffer_ptr = work_area_->bufferView().buffer();