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.
24 //#include "BufferList.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
30 #include "ErrorList.h"
31 #include "FuncRequest.h"
38 #include "MenuBackend.h"
39 #include "Paragraph.h"
41 #include "controllers/ControlCommandBuffer.h"
43 #include "support/lstrings.h"
44 #include "support/filetools.h" // OnlyFilename()
46 #include <boost/bind.hpp>
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
58 using frontend::WorkArea;
60 using support::bformat;
61 using support::FileName;
62 using support::makeDisplayPath;
63 using support::onlyFilename;
68 using lyx::frontend::ControlCommandBuffer;
70 string current_layout;
73 LyXView::LyXView(int id)
75 toolbars_(new Toolbars(*this)),
76 autosave_timeout_(new Timeout(5000)),
77 dialogs_(new Dialogs(*this)),
78 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
80 // Start autosave timer
82 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
83 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
84 autosave_timeout_->start();
95 // FIXME, there's only one WorkArea per LyXView possible for now.
96 void LyXView::setWorkArea(WorkArea * work_area)
98 BOOST_ASSERT(work_area);
99 work_area_ = work_area;
100 work_area_ids_.clear();
101 work_area_ids_.push_back(work_area_->id());
105 // FIXME, there's only one WorkArea per LyXView possible for now.
106 WorkArea const * LyXView::currentWorkArea() const
112 // FIXME, there's only one WorkArea per LyXView possible for now.
113 WorkArea * LyXView::currentWorkArea()
119 Buffer * LyXView::buffer() const
121 BOOST_ASSERT(work_area_);
122 return work_area_->bufferView().buffer();
126 void LyXView::setBuffer(Buffer * b)
130 BOOST_ASSERT(work_area_);
131 if (work_area_->bufferView().buffer())
135 if (!b) // && theBufferList().empty()
136 getDialogs().hideBufferDependent();
138 work_area_->bufferView().setBuffer(b);
139 // Make sure the TOC is updated before anything else.
142 if (work_area_->bufferView().buffer()) {
143 // Buffer-dependent dialogs should be updated or
144 // hidden. This should go here because some dialogs (eg ToC)
145 // require bv_->text.
146 getDialogs().updateBufferDependent(true);
147 connectBuffer(*work_area_->bufferView().buffer());
155 updateLayoutChoice();
160 work_area_->redraw();
164 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
168 BOOST_ASSERT(work_area_);
169 bool const hadBuffer = work_area_->bufferView().buffer();
174 work_area_->bufferView().loadLyXFile(filename, tolastfiles);
179 updateLayoutChoice();
183 connectBuffer(*work_area_->bufferView().buffer());
184 showErrorList("Parse");
185 } else if (hadBuffer)
186 //Need to reconnect the buffer if the load failed
187 connectBuffer(*work_area_->bufferView().buffer());
190 work_area_->redraw();
195 void LyXView::connectBuffer(Buffer & buf)
197 if (errorsConnection_.connected())
200 BOOST_ASSERT(work_area_);
201 bufferChangedConnection_ =
203 boost::bind(&WorkArea::redraw, work_area_));
205 bufferStructureChangedConnection_ =
206 buf.getMasterBuffer()->structureChanged.connect(
207 boost::bind(&LyXView::updateToc, this));
211 boost::bind(&LyXView::showErrorList, this, _1));
215 boost::bind(&LyXView::message, this, _1));
219 boost::bind(&LyXView::busy, this, _1));
222 buf.updateTitles.connect(
223 boost::bind(&LyXView::updateWindowTitle, this));
226 buf.resetAutosaveTimers.connect(
227 boost::bind(&LyXView::resetAutosaveTimer, this));
229 readonlyConnection_ =
230 buf.readonly.connect(
231 boost::bind(&LyXView::showReadonly, this, _1));
235 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
239 void LyXView::disconnectBuffer()
241 errorsConnection_.disconnect();
242 bufferChangedConnection_.disconnect();
243 bufferStructureChangedConnection_.disconnect();
244 messageConnection_.disconnect();
245 busyConnection_.disconnect();
246 titleConnection_.disconnect();
247 timerConnection_.disconnect();
248 readonlyConnection_.disconnect();
249 closingConnection_.disconnect();
250 layout_changed_connection_.disconnect();
254 void LyXView::connectBufferView(BufferView & bv)
256 show_dialog_connection_ = bv.showDialog.connect(
257 boost::bind(&LyXView::showDialog, this, _1));
258 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
259 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
260 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
261 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
262 update_dialog_connection_ = bv.updateDialog.connect(
263 boost::bind(&LyXView::updateDialog, this, _1, _2));
264 layout_changed_connection_ = bv.layoutChanged.connect(
265 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
269 void LyXView::disconnectBufferView()
271 show_dialog_connection_.disconnect();
272 show_dialog_with_data_connection_.disconnect();
273 show_inset_dialog_connection_.disconnect();
274 update_dialog_connection_.disconnect();
278 void LyXView::showErrorList(string const & error_type)
280 ErrorList & el = buffer()->errorList(error_type);
282 getDialogs().show("errorlist", error_type);
287 void LyXView::showDialog(string const & name)
289 getDialogs().show(name);
293 void LyXView::showDialogWithData(string const & name, string const & data)
295 getDialogs().show(name, data);
299 void LyXView::showInsetDialog(string const & name, string const & data,
302 getDialogs().show(name, data, inset);
306 void LyXView::updateDialog(string const & name, string const & data)
308 if (getDialogs().visible(name))
309 getDialogs().update(name, data);
313 void LyXView::showReadonly(bool)
316 getDialogs().updateBufferDependent(false);
320 BufferView * LyXView::view() const
322 BOOST_ASSERT(work_area_);
323 return &work_area_->bufferView();
327 void LyXView::updateToc()
329 updateDialog("toc", "");
333 void LyXView::updateToolbars()
335 BOOST_ASSERT(work_area_);
337 work_area_->bufferView().cursor().inMathed();
339 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
341 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
342 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
344 toolbars_->update(math, table, review);
345 // update redaonly status of open dialogs. This could also be in
346 // updateMenubar(), but since updateToolbars() and updateMenubar()
347 // are always called together it is only here.
348 getDialogs().checkStatus();
352 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
354 return toolbars_->getToolbarState(name);
358 void LyXView::toggleToolbarState(string const & name)
360 // it is possible to get current toolbar status like this,...
361 // but I decide to obey the order of ToolbarBackend::flags
362 // and disregard real toolbar status.
363 // toolbars_->saveToolbarInfo();
365 // toggle state on/off/auto
366 toolbars_->toggleToolbarState(name);
372 void LyXView::updateMenubar()
378 void LyXView::autoSave()
380 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
382 if (view()->buffer())
383 lyx::autoSave(view());
387 void LyXView::resetAutosaveTimer()
390 autosave_timeout_->restart();
394 void LyXView::updateLayoutChoice()
396 // Don't show any layouts without a buffer
397 if (!view()->buffer()) {
398 toolbars_->clearLayoutList();
402 // Update the layout display
403 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
404 current_layout = buffer()->params().getTextClass().defaultLayoutName();
407 BOOST_ASSERT(work_area_);
408 if (work_area_->bufferView().cursor().inMathed())
411 string const & layout =
412 work_area_->bufferView().cursor().paragraph().layout()->name();
414 if (layout != current_layout) {
415 toolbars_->setLayout(layout);
416 current_layout = layout;
421 void LyXView::updateWindowTitle()
423 docstring maximize_title = lyx::from_ascii("LyX");
424 docstring minimize_title = lyx::from_ascii("LyX");
426 if (view()->buffer()) {
427 string const cur_title = buffer()->fileName();
428 if (!cur_title.empty()) {
429 maximize_title += ": " + makeDisplayPath(cur_title, 30);
430 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
431 if (!buffer()->isClean()) {
432 maximize_title += _(" (changed)");
433 minimize_title += lyx::char_type('*');
435 if (buffer()->isReadonly())
436 maximize_title += _(" (read only)");
440 setWindowTitle(maximize_title, minimize_title);
445 void LyXView::dispatch(FuncRequest const & cmd)
447 theLyXFunc().setLyXView(this);
452 Buffer const * const LyXView::updateInset(Inset const * inset) const
454 Buffer const * buffer_ptr = 0;
456 BOOST_ASSERT(work_area_);
457 work_area_->scheduleRedraw();
459 buffer_ptr = work_area_->bufferView().buffer();