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);
137 if (work_area_->bufferView().buffer()) {
138 // Buffer-dependent dialogs should be updated or
139 // hidden. This should go here because some dialogs (eg ToC)
140 // require bv_->text.
141 getDialogs().updateBufferDependent(true);
142 connectBuffer(*work_area_->bufferView().buffer());
150 updateLayoutChoice();
155 work_area_->redraw();
159 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
163 BOOST_ASSERT(work_area_);
164 if (work_area_->bufferView().buffer())
167 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
171 updateLayoutChoice();
175 connectBuffer(*work_area_->bufferView().buffer());
176 showErrorList("Parse");
180 work_area_->redraw();
185 void LyXView::connectBuffer(Buffer & buf)
187 if (errorsConnection_.connected())
190 BOOST_ASSERT(work_area_);
191 bufferChangedConnection_ =
193 boost::bind(&WorkArea::redraw, work_area_));
197 boost::bind(&LyXView::showErrorList, this, _1));
201 boost::bind(&LyXView::message, this, _1));
205 boost::bind(&LyXView::busy, this, _1));
208 buf.updateTitles.connect(
209 boost::bind(&LyXView::updateWindowTitle, this));
212 buf.resetAutosaveTimers.connect(
213 boost::bind(&LyXView::resetAutosaveTimer, this));
215 readonlyConnection_ =
216 buf.readonly.connect(
217 boost::bind(&LyXView::showReadonly, this, _1));
221 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
225 void LyXView::disconnectBuffer()
227 errorsConnection_.disconnect();
228 bufferChangedConnection_.disconnect();
229 messageConnection_.disconnect();
230 busyConnection_.disconnect();
231 titleConnection_.disconnect();
232 timerConnection_.disconnect();
233 readonlyConnection_.disconnect();
234 closingConnection_.disconnect();
235 layout_changed_connection_.disconnect();
239 void LyXView::connectBufferView(BufferView & bv)
241 show_dialog_connection_ = bv.showDialog.connect(
242 boost::bind(&LyXView::showDialog, this, _1));
243 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
244 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
245 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
246 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
247 update_dialog_connection_ = bv.updateDialog.connect(
248 boost::bind(&LyXView::updateDialog, this, _1, _2));
249 layout_changed_connection_ = bv.layoutChanged.connect(
250 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
254 void LyXView::disconnectBufferView()
256 show_dialog_connection_.disconnect();
257 show_dialog_with_data_connection_.disconnect();
258 show_inset_dialog_connection_.disconnect();
259 update_dialog_connection_.disconnect();
263 void LyXView::showErrorList(string const & error_type)
265 ErrorList & el = buffer()->errorList(error_type);
267 getDialogs().show("errorlist", error_type);
272 void LyXView::showDialog(string const & name)
274 getDialogs().show(name);
278 void LyXView::showDialogWithData(string const & name, string const & data)
280 getDialogs().show(name, data);
284 void LyXView::showInsetDialog(string const & name, string const & data,
287 getDialogs().show(name, data, inset);
291 void LyXView::updateDialog(string const & name, string const & data)
293 if (getDialogs().visible(name))
294 getDialogs().update(name, data);
298 void LyXView::showReadonly(bool)
301 getDialogs().updateBufferDependent(false);
305 BufferView * LyXView::view() const
307 BOOST_ASSERT(work_area_);
308 return &work_area_->bufferView();
312 void LyXView::updateToolbars()
314 BOOST_ASSERT(work_area_);
316 work_area_->bufferView().cursor().inMathed();
318 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
320 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
321 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
323 toolbars_->update(math, table, review);
324 // update redaonly status of open dialogs. This could also be in
325 // updateMenubar(), but since updateToolbars() and updateMenubar()
326 // are always called together it is only here.
327 getDialogs().checkStatus();
331 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
333 return toolbars_->getToolbarState(name);
337 void LyXView::toggleToolbarState(string const & name)
339 // it is possible to get current toolbar status like this,...
340 // but I decide to obey the order of ToolbarBackend::flags
341 // and disregard real toolbar status.
342 // toolbars_->saveToolbarInfo();
344 // toggle state on/off/auto
345 toolbars_->toggleToolbarState(name);
351 void LyXView::updateMenubar()
357 void LyXView::autoSave()
359 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
361 if (view()->buffer())
362 lyx::autoSave(view());
366 void LyXView::resetAutosaveTimer()
369 autosave_timeout_->restart();
373 void LyXView::updateLayoutChoice()
375 // Don't show any layouts without a buffer
376 if (!view()->buffer()) {
377 toolbars_->clearLayoutList();
381 // Update the layout display
382 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
383 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
386 BOOST_ASSERT(work_area_);
387 if (work_area_->bufferView().cursor().inMathed())
390 string const & layout =
391 work_area_->bufferView().cursor().paragraph().layout()->name();
393 if (layout != current_layout) {
394 toolbars_->setLayout(layout);
395 current_layout = layout;
400 void LyXView::updateWindowTitle()
402 docstring maximize_title = lyx::from_ascii("LyX");
403 docstring minimize_title = lyx::from_ascii("LyX");
405 if (view()->buffer()) {
406 string const cur_title = buffer()->fileName();
407 if (!cur_title.empty()) {
408 maximize_title += ": " + makeDisplayPath(cur_title, 30);
409 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
410 if (!buffer()->isClean()) {
411 maximize_title += _(" (changed)");
412 minimize_title += lyx::char_type('*');
414 if (buffer()->isReadonly())
415 maximize_title += _(" (read only)");
419 setWindowTitle(maximize_title, minimize_title);
423 void LyXView::dispatch(FuncRequest const & cmd)
425 theLyXFunc().setLyXView(this);
430 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
432 Buffer const * buffer_ptr = 0;
434 buffer_ptr = work_area_->bufferView().buffer();
436 BOOST_ASSERT(work_area_);
437 work_area_->bufferView().update(Update::Force);
438 work_area_->redraw();