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 "BufferParams.h"
24 #include "BufferView.h"
25 #include "bufferview_funcs.h"
28 #include "ErrorList.h"
29 #include "FuncRequest.h"
36 #include "MenuBackend.h"
37 #include "Paragraph.h"
39 #include "controllers/ControlCommandBuffer.h"
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
44 #include <boost/bind.hpp>
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
56 using frontend::WorkArea;
58 using support::bformat;
59 using support::FileName;
60 using support::makeDisplayPath;
61 using support::onlyFilename;
66 using lyx::frontend::ControlCommandBuffer;
68 string current_layout;
71 LyXView::LyXView(int id)
73 toolbars_(new Toolbars(*this)),
74 autosave_timeout_(new Timeout(5000)),
75 dialogs_(new Dialogs(*this)),
76 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
78 // Start autosave timer
80 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
81 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
82 autosave_timeout_->start();
93 // FIXME, there's only one WorkArea per LyXView possible for now.
94 void LyXView::setWorkArea(WorkArea * work_area)
96 BOOST_ASSERT(work_area);
97 work_area_ = work_area;
98 work_area_ids_.clear();
99 work_area_ids_.push_back(work_area_->id());
103 // FIXME, there's only one WorkArea per LyXView possible for now.
104 WorkArea const * LyXView::currentWorkArea() const
110 // FIXME, there's only one WorkArea per LyXView possible for now.
111 WorkArea * LyXView::currentWorkArea()
117 Buffer * LyXView::buffer() const
119 BOOST_ASSERT(work_area_);
120 return work_area_->bufferView().buffer();
124 void LyXView::setBuffer(Buffer * b)
128 BOOST_ASSERT(work_area_);
129 if (work_area_->bufferView().buffer())
133 getDialogs().hideBufferDependent();
135 work_area_->bufferView().setBuffer(b);
136 // Make sure the TOC is updated before anything else.
139 if (work_area_->bufferView().buffer()) {
140 // Buffer-dependent dialogs should be updated or
141 // hidden. This should go here because some dialogs (eg ToC)
142 // require bv_->text.
143 getDialogs().updateBufferDependent(true);
144 connectBuffer(*work_area_->bufferView().buffer());
152 updateLayoutChoice();
157 work_area_->redraw();
161 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
165 BOOST_ASSERT(work_area_);
166 if (work_area_->bufferView().buffer())
169 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
174 updateLayoutChoice();
178 connectBuffer(*work_area_->bufferView().buffer());
179 showErrorList("Parse");
183 work_area_->redraw();
188 void LyXView::connectBuffer(Buffer & buf)
190 if (errorsConnection_.connected())
193 BOOST_ASSERT(work_area_);
194 bufferChangedConnection_ =
196 boost::bind(&WorkArea::redraw, work_area_));
198 bufferStructureChangedConnection_ =
199 buf.getMasterBuffer()->structureChanged.connect(
200 boost::bind(&LyXView::updateToc, this));
204 boost::bind(&LyXView::showErrorList, this, _1));
208 boost::bind(&LyXView::message, this, _1));
212 boost::bind(&LyXView::busy, this, _1));
215 buf.updateTitles.connect(
216 boost::bind(&LyXView::updateWindowTitle, this));
219 buf.resetAutosaveTimers.connect(
220 boost::bind(&LyXView::resetAutosaveTimer, this));
222 readonlyConnection_ =
223 buf.readonly.connect(
224 boost::bind(&LyXView::showReadonly, this, _1));
228 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
232 void LyXView::disconnectBuffer()
234 errorsConnection_.disconnect();
235 bufferChangedConnection_.disconnect();
236 bufferStructureChangedConnection_.disconnect();
237 messageConnection_.disconnect();
238 busyConnection_.disconnect();
239 titleConnection_.disconnect();
240 timerConnection_.disconnect();
241 readonlyConnection_.disconnect();
242 closingConnection_.disconnect();
243 layout_changed_connection_.disconnect();
247 void LyXView::connectBufferView(BufferView & bv)
249 show_dialog_connection_ = bv.showDialog.connect(
250 boost::bind(&LyXView::showDialog, this, _1));
251 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
252 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
253 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
254 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
255 update_dialog_connection_ = bv.updateDialog.connect(
256 boost::bind(&LyXView::updateDialog, this, _1, _2));
257 layout_changed_connection_ = bv.layoutChanged.connect(
258 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
262 void LyXView::disconnectBufferView()
264 show_dialog_connection_.disconnect();
265 show_dialog_with_data_connection_.disconnect();
266 show_inset_dialog_connection_.disconnect();
267 update_dialog_connection_.disconnect();
271 void LyXView::showErrorList(string const & error_type)
273 ErrorList & el = buffer()->errorList(error_type);
275 getDialogs().show("errorlist", error_type);
280 void LyXView::showDialog(string const & name)
282 getDialogs().show(name);
286 void LyXView::showDialogWithData(string const & name, string const & data)
288 getDialogs().show(name, data);
292 void LyXView::showInsetDialog(string const & name, string const & data,
295 getDialogs().show(name, data, inset);
299 void LyXView::updateDialog(string const & name, string const & data)
301 if (getDialogs().visible(name))
302 getDialogs().update(name, data);
306 void LyXView::showReadonly(bool)
309 getDialogs().updateBufferDependent(false);
313 BufferView * LyXView::view() const
315 BOOST_ASSERT(work_area_);
316 return &work_area_->bufferView();
320 void LyXView::updateToc()
322 updateDialog("toc", "");
326 void LyXView::updateToolbars()
328 BOOST_ASSERT(work_area_);
330 work_area_->bufferView().cursor().inMathed();
332 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
334 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
335 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
337 toolbars_->update(math, table, review);
338 // update redaonly status of open dialogs. This could also be in
339 // updateMenubar(), but since updateToolbars() and updateMenubar()
340 // are always called together it is only here.
341 getDialogs().checkStatus();
345 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
347 return toolbars_->getToolbarState(name);
351 void LyXView::toggleToolbarState(string const & name)
353 // it is possible to get current toolbar status like this,...
354 // but I decide to obey the order of ToolbarBackend::flags
355 // and disregard real toolbar status.
356 // toolbars_->saveToolbarInfo();
358 // toggle state on/off/auto
359 toolbars_->toggleToolbarState(name);
365 void LyXView::updateMenubar()
371 void LyXView::autoSave()
373 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
375 if (view()->buffer())
376 lyx::autoSave(view());
380 void LyXView::resetAutosaveTimer()
383 autosave_timeout_->restart();
387 void LyXView::updateLayoutChoice()
389 // Don't show any layouts without a buffer
390 if (!view()->buffer()) {
391 toolbars_->clearLayoutList();
395 // Update the layout display
396 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
397 current_layout = buffer()->params().getTextClass().defaultLayoutName();
400 BOOST_ASSERT(work_area_);
401 if (work_area_->bufferView().cursor().inMathed())
404 string const & layout =
405 work_area_->bufferView().cursor().paragraph().layout()->name();
407 if (layout != current_layout) {
408 toolbars_->setLayout(layout);
409 current_layout = layout;
414 void LyXView::updateWindowTitle()
416 docstring maximize_title = lyx::from_ascii("LyX");
417 docstring minimize_title = lyx::from_ascii("LyX");
419 if (view()->buffer()) {
420 string const cur_title = buffer()->fileName();
421 if (!cur_title.empty()) {
422 maximize_title += ": " + makeDisplayPath(cur_title, 30);
423 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
424 if (!buffer()->isClean()) {
425 maximize_title += _(" (changed)");
426 minimize_title += lyx::char_type('*');
428 if (buffer()->isReadonly())
429 maximize_title += _(" (read only)");
433 setWindowTitle(maximize_title, minimize_title);
438 void LyXView::dispatch(FuncRequest const & cmd)
440 theLyXFunc().setLyXView(this);
445 Buffer const * const LyXView::updateInset(Inset const * inset) const
447 Buffer const * buffer_ptr = 0;
449 BOOST_ASSERT(work_area_);
450 work_area_->scheduleRedraw();
452 buffer_ptr = work_area_->bufferView().buffer();