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();
92 // FIXME, there's only one WorkArea per LyXView possible for now.
93 void LyXView::setWorkArea(WorkArea * work_area)
95 work_area_ = work_area;
96 work_area_ids_.clear();
97 work_area_ids_.push_back(work_area_->id());
101 Buffer * LyXView::buffer() const
103 return work_area_->bufferView().buffer();
107 void LyXView::setBuffer(Buffer * b)
111 if (work_area_->bufferView().buffer())
115 getDialogs().hideBufferDependent();
117 work_area_->bufferView().setBuffer(b);
119 if (work_area_->bufferView().buffer()) {
120 // Buffer-dependent dialogs should be updated or
121 // hidden. This should go here because some dialogs (eg ToC)
122 // require bv_->text.
123 getDialogs().updateBufferDependent(true);
124 connectBuffer(*work_area_->bufferView().buffer());
132 updateLayoutChoice();
137 work_area_->redraw();
141 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
145 if (work_area_->bufferView().buffer())
148 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
152 updateLayoutChoice();
156 connectBuffer(*work_area_->bufferView().buffer());
157 showErrorList("Parse");
161 work_area_->redraw();
166 void LyXView::connectBuffer(Buffer & buf)
168 if (errorsConnection_.connected())
171 bufferChangedConnection_ =
173 boost::bind(&WorkArea::redraw, work_area_));
177 boost::bind(&LyXView::showErrorList, this, _1));
181 boost::bind(&LyXView::message, this, _1));
185 boost::bind(&LyXView::busy, this, _1));
188 buf.updateTitles.connect(
189 boost::bind(&LyXView::updateWindowTitle, this));
192 buf.resetAutosaveTimers.connect(
193 boost::bind(&LyXView::resetAutosaveTimer, this));
195 readonlyConnection_ =
196 buf.readonly.connect(
197 boost::bind(&LyXView::showReadonly, this, _1));
201 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
205 void LyXView::disconnectBuffer()
207 errorsConnection_.disconnect();
208 bufferChangedConnection_.disconnect();
209 messageConnection_.disconnect();
210 busyConnection_.disconnect();
211 titleConnection_.disconnect();
212 timerConnection_.disconnect();
213 readonlyConnection_.disconnect();
214 closingConnection_.disconnect();
215 layout_changed_connection_.disconnect();
219 void LyXView::connectBufferView(BufferView & bv)
221 show_dialog_connection_ = bv.showDialog.connect(
222 boost::bind(&LyXView::showDialog, this, _1));
223 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
224 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
225 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
226 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
227 update_dialog_connection_ = bv.updateDialog.connect(
228 boost::bind(&LyXView::updateDialog, this, _1, _2));
229 layout_changed_connection_ = bv.layoutChanged.connect(
230 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
234 void LyXView::disconnectBufferView()
236 show_dialog_connection_.disconnect();
237 show_dialog_with_data_connection_.disconnect();
238 show_inset_dialog_connection_.disconnect();
239 update_dialog_connection_.disconnect();
243 void LyXView::showErrorList(string const & error_type)
245 ErrorList & el = buffer()->errorList(error_type);
247 getDialogs().show("errorlist", error_type);
252 void LyXView::showDialog(string const & name)
254 getDialogs().show(name);
258 void LyXView::showDialogWithData(string const & name, string const & data)
260 getDialogs().show(name, data);
264 void LyXView::showInsetDialog(string const & name, string const & data,
267 getDialogs().show(name, data, inset);
271 void LyXView::updateDialog(string const & name, string const & data)
273 if (getDialogs().visible(name))
274 getDialogs().update(name, data);
278 void LyXView::showReadonly(bool)
281 getDialogs().updateBufferDependent(false);
285 BufferView * LyXView::view() const
287 return &work_area_->bufferView();
291 void LyXView::updateToolbars()
294 work_area_->bufferView().cursor().inMathed();
296 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
298 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
299 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
301 toolbars_->update(math, table, review);
302 // update redaonly status of open dialogs. This could also be in
303 // updateMenubar(), but since updateToolbars() and updateMenubar()
304 // are always called together it is only here.
305 getDialogs().checkStatus();
309 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
311 return toolbars_->getToolbarState(name);
315 void LyXView::toggleToolbarState(string const & name)
317 // it is possible to get current toolbar status like this,...
318 // but I decide to obey the order of ToolbarBackend::flags
319 // and disregard real toolbar status.
320 // toolbars_->saveToolbarInfo();
322 // toggle state on/off/auto
323 toolbars_->toggleToolbarState(name);
329 void LyXView::updateMenubar()
335 void LyXView::autoSave()
337 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
339 if (view()->buffer())
340 lyx::autoSave(view());
344 void LyXView::resetAutosaveTimer()
347 autosave_timeout_->restart();
351 void LyXView::updateLayoutChoice()
353 // Don't show any layouts without a buffer
354 if (!view()->buffer()) {
355 toolbars_->clearLayoutList();
359 // Update the layout display
360 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
361 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
364 if (work_area_->bufferView().cursor().inMathed())
367 string const & layout =
368 work_area_->bufferView().cursor().paragraph().layout()->name();
370 if (layout != current_layout) {
371 toolbars_->setLayout(layout);
372 current_layout = layout;
377 void LyXView::updateWindowTitle()
379 docstring maximize_title = lyx::from_ascii("LyX");
380 docstring minimize_title = lyx::from_ascii("LyX");
382 if (view()->buffer()) {
383 string const cur_title = buffer()->fileName();
384 if (!cur_title.empty()) {
385 maximize_title += ": " + makeDisplayPath(cur_title, 30);
386 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
387 if (!buffer()->isClean()) {
388 maximize_title += _(" (changed)");
389 minimize_title += lyx::char_type('*');
391 if (buffer()->isReadonly())
392 maximize_title += _(" (read only)");
396 setWindowTitle(maximize_title, minimize_title);
400 void LyXView::dispatch(FuncRequest const & cmd)
402 theLyXFunc().setLyXView(this);
407 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
409 Buffer const * buffer_ptr = 0;
411 buffer_ptr = work_area_->bufferView().buffer();
413 work_area_->bufferView().update(Update::Force);
414 work_area_->redraw();