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 Buffer * LyXView::buffer() const
105 BOOST_ASSERT(work_area_);
106 return work_area_->bufferView().buffer();
110 void LyXView::setBuffer(Buffer * b)
114 BOOST_ASSERT(work_area_);
115 if (work_area_->bufferView().buffer())
119 getDialogs().hideBufferDependent();
121 work_area_->bufferView().setBuffer(b);
123 if (work_area_->bufferView().buffer()) {
124 // Buffer-dependent dialogs should be updated or
125 // hidden. This should go here because some dialogs (eg ToC)
126 // require bv_->text.
127 getDialogs().updateBufferDependent(true);
128 connectBuffer(*work_area_->bufferView().buffer());
136 updateLayoutChoice();
141 work_area_->redraw();
145 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
149 BOOST_ASSERT(work_area_);
150 if (work_area_->bufferView().buffer())
153 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
157 updateLayoutChoice();
161 connectBuffer(*work_area_->bufferView().buffer());
162 showErrorList("Parse");
166 work_area_->redraw();
171 void LyXView::connectBuffer(Buffer & buf)
173 if (errorsConnection_.connected())
176 BOOST_ASSERT(work_area_);
177 bufferChangedConnection_ =
179 boost::bind(&WorkArea::redraw, work_area_));
183 boost::bind(&LyXView::showErrorList, this, _1));
187 boost::bind(&LyXView::message, this, _1));
191 boost::bind(&LyXView::busy, this, _1));
194 buf.updateTitles.connect(
195 boost::bind(&LyXView::updateWindowTitle, this));
198 buf.resetAutosaveTimers.connect(
199 boost::bind(&LyXView::resetAutosaveTimer, this));
201 readonlyConnection_ =
202 buf.readonly.connect(
203 boost::bind(&LyXView::showReadonly, this, _1));
207 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
211 void LyXView::disconnectBuffer()
213 errorsConnection_.disconnect();
214 bufferChangedConnection_.disconnect();
215 messageConnection_.disconnect();
216 busyConnection_.disconnect();
217 titleConnection_.disconnect();
218 timerConnection_.disconnect();
219 readonlyConnection_.disconnect();
220 closingConnection_.disconnect();
221 layout_changed_connection_.disconnect();
225 void LyXView::connectBufferView(BufferView & bv)
227 show_dialog_connection_ = bv.showDialog.connect(
228 boost::bind(&LyXView::showDialog, this, _1));
229 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
230 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
231 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
232 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
233 update_dialog_connection_ = bv.updateDialog.connect(
234 boost::bind(&LyXView::updateDialog, this, _1, _2));
235 layout_changed_connection_ = bv.layoutChanged.connect(
236 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
240 void LyXView::disconnectBufferView()
242 show_dialog_connection_.disconnect();
243 show_dialog_with_data_connection_.disconnect();
244 show_inset_dialog_connection_.disconnect();
245 update_dialog_connection_.disconnect();
249 void LyXView::showErrorList(string const & error_type)
251 ErrorList & el = buffer()->errorList(error_type);
253 getDialogs().show("errorlist", error_type);
258 void LyXView::showDialog(string const & name)
260 getDialogs().show(name);
264 void LyXView::showDialogWithData(string const & name, string const & data)
266 getDialogs().show(name, data);
270 void LyXView::showInsetDialog(string const & name, string const & data,
273 getDialogs().show(name, data, inset);
277 void LyXView::updateDialog(string const & name, string const & data)
279 if (getDialogs().visible(name))
280 getDialogs().update(name, data);
284 void LyXView::showReadonly(bool)
287 getDialogs().updateBufferDependent(false);
291 BufferView * LyXView::view() const
293 BOOST_ASSERT(work_area_);
294 return &work_area_->bufferView();
298 void LyXView::updateToolbars()
300 BOOST_ASSERT(work_area_);
302 work_area_->bufferView().cursor().inMathed();
304 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
306 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
307 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
309 toolbars_->update(math, table, review);
310 // update redaonly status of open dialogs. This could also be in
311 // updateMenubar(), but since updateToolbars() and updateMenubar()
312 // are always called together it is only here.
313 getDialogs().checkStatus();
317 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
319 return toolbars_->getToolbarState(name);
323 void LyXView::toggleToolbarState(string const & name)
325 // it is possible to get current toolbar status like this,...
326 // but I decide to obey the order of ToolbarBackend::flags
327 // and disregard real toolbar status.
328 // toolbars_->saveToolbarInfo();
330 // toggle state on/off/auto
331 toolbars_->toggleToolbarState(name);
337 void LyXView::updateMenubar()
343 void LyXView::autoSave()
345 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
347 if (view()->buffer())
348 lyx::autoSave(view());
352 void LyXView::resetAutosaveTimer()
355 autosave_timeout_->restart();
359 void LyXView::updateLayoutChoice()
361 // Don't show any layouts without a buffer
362 if (!view()->buffer()) {
363 toolbars_->clearLayoutList();
367 // Update the layout display
368 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
369 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
372 BOOST_ASSERT(work_area_);
373 if (work_area_->bufferView().cursor().inMathed())
376 string const & layout =
377 work_area_->bufferView().cursor().paragraph().layout()->name();
379 if (layout != current_layout) {
380 toolbars_->setLayout(layout);
381 current_layout = layout;
386 void LyXView::updateWindowTitle()
388 docstring maximize_title = lyx::from_ascii("LyX");
389 docstring minimize_title = lyx::from_ascii("LyX");
391 if (view()->buffer()) {
392 string const cur_title = buffer()->fileName();
393 if (!cur_title.empty()) {
394 maximize_title += ": " + makeDisplayPath(cur_title, 30);
395 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
396 if (!buffer()->isClean()) {
397 maximize_title += _(" (changed)");
398 minimize_title += lyx::char_type('*');
400 if (buffer()->isReadonly())
401 maximize_title += _(" (read only)");
405 setWindowTitle(maximize_title, minimize_title);
409 void LyXView::dispatch(FuncRequest const & cmd)
411 theLyXFunc().setLyXView(this);
416 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
418 Buffer const * buffer_ptr = 0;
420 buffer_ptr = work_area_->bufferView().buffer();
422 BOOST_ASSERT(work_area_);
423 work_area_->bufferView().update(Update::Force);
424 work_area_->redraw();