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.
22 #include "bufferparams.h"
23 #include "BufferView.h"
24 #include "bufferview_funcs.h"
27 #include "errorlist.h"
28 #include "funcrequest.h"
35 #include "MenuBackend.h"
36 #include "paragraph.h"
38 #include "controllers/ControlCommandBuffer.h"
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
43 #include <boost/bind.hpp>
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
55 using lyx::frontend::WorkArea;
58 using lyx::support::bformat;
59 using lyx::support::makeDisplayPath;
60 using lyx::support::onlyFilename;
65 using lyx::frontend::ControlCommandBuffer;
67 string current_layout;
72 toolbars_(new Toolbars(*this)),
73 autosave_timeout_(new Timeout(5000)),
74 dialogs_(new Dialogs(*this)),
75 controlcommand_(new ControlCommandBuffer(*this))
77 // Start autosave timer
79 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
80 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
81 autosave_timeout_->start();
91 void LyXView::setWorkArea(WorkArea * work_area)
93 work_area_ = work_area;
97 Buffer * LyXView::buffer() const
99 return work_area_->bufferView().buffer();
103 void LyXView::setBuffer(Buffer * b)
105 if (work_area_->bufferView().buffer())
109 getDialogs().hideBufferDependent();
111 work_area_->bufferView().setBuffer(b);
113 if (work_area_->bufferView().buffer()) {
114 // Buffer-dependent dialogs should be updated or
115 // hidden. This should go here because some dialogs (eg ToC)
116 // require bv_->text.
117 getDialogs().updateBufferDependent(true);
118 connectBuffer(*work_area_->bufferView().buffer());
123 updateLayoutChoice();
126 work_area_->redraw();
130 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
132 if (work_area_->bufferView().buffer())
135 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
139 updateLayoutChoice();
142 connectBuffer(*work_area_->bufferView().buffer());
143 showErrorList("Parse");
146 work_area_->redraw();
151 void LyXView::connectBuffer(Buffer & buf)
153 if (errorsConnection_.connected())
156 bufferChangedConnection_ =
158 boost::bind(&WorkArea::redraw, work_area_));
162 boost::bind(&LyXView::showErrorList, this, _1));
166 boost::bind(&LyXView::message, this, _1));
170 boost::bind(&LyXView::busy, this, _1));
173 buf.updateTitles.connect(
174 boost::bind(&LyXView::updateWindowTitle, this));
177 buf.resetAutosaveTimers.connect(
178 boost::bind(&LyXView::resetAutosaveTimer, this));
180 readonlyConnection_ =
181 buf.readonly.connect(
182 boost::bind(&LyXView::showReadonly, this, _1));
186 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
190 void LyXView::disconnectBuffer()
192 errorsConnection_.disconnect();
193 bufferChangedConnection_.disconnect();
194 messageConnection_.disconnect();
195 busyConnection_.disconnect();
196 titleConnection_.disconnect();
197 timerConnection_.disconnect();
198 readonlyConnection_.disconnect();
199 closingConnection_.disconnect();
200 layout_changed_connection_.disconnect();
204 void LyXView::connectBufferView(BufferView & bv)
206 show_dialog_connection_ = bv.showDialog.connect(
207 boost::bind(&LyXView::showDialog, this, _1));
208 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
209 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
210 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
211 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
212 update_dialog_connection_ = bv.updateDialog.connect(
213 boost::bind(&LyXView::updateDialog, this, _1, _2));
214 layout_changed_connection_ = bv.layoutChanged.connect(
215 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
219 void LyXView::disconnectBufferView()
221 show_dialog_connection_.disconnect();
222 show_dialog_with_data_connection_.disconnect();
223 show_inset_dialog_connection_.disconnect();
224 update_dialog_connection_.disconnect();
228 void LyXView::showErrorList(string const & error_type)
230 ErrorList & el = buffer()->errorList(error_type);
232 getDialogs().show("errorlist", error_type);
237 void LyXView::showDialog(string const & name)
239 getDialogs().show(name);
243 void LyXView::showDialogWithData(string const & name, string const & data)
245 getDialogs().show(name, data);
249 void LyXView::showInsetDialog(string const & name, string const & data,
252 getDialogs().show(name, data, inset);
256 void LyXView::updateDialog(string const & name, string const & data)
258 if (getDialogs().visible(name))
259 getDialogs().update(name, data);
263 void LyXView::showReadonly(bool)
266 getDialogs().updateBufferDependent(false);
270 BufferView * LyXView::view() const
272 return &work_area_->bufferView();
276 void LyXView::updateToolbars()
278 bool const math = work_area_->bufferView().cursor().inMathed();
280 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
281 toolbars_->update(math, table);
282 // update redaonly status of open dialogs. This could also be in
283 // updateMenubar(), but since updateToolbars() and updateMenubar()
284 // are always called together it is only here.
285 getDialogs().checkStatus();
289 void LyXView::updateMenubar()
295 void LyXView::autoSave()
297 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
299 if (view()->buffer())
300 lyx::autoSave(view());
304 void LyXView::resetAutosaveTimer()
307 autosave_timeout_->restart();
311 void LyXView::updateLayoutChoice()
313 // Don't show any layouts without a buffer
314 if (!view()->buffer()) {
315 toolbars_->clearLayoutList();
319 // Update the layout display
320 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
321 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
324 if (work_area_->bufferView().cursor().inMathed())
327 string const & layout =
328 work_area_->bufferView().cursor().paragraph().layout()->name();
330 if (layout != current_layout) {
331 toolbars_->setLayout(layout);
332 current_layout = layout;
337 void LyXView::updateWindowTitle()
339 static docstring last_title = lyx::from_ascii("LyX");
340 docstring maximize_title = lyx::from_ascii("LyX");
341 docstring minimize_title = lyx::from_ascii("LyX");
343 if (view()->buffer()) {
344 string const cur_title = buffer()->fileName();
345 if (!cur_title.empty()) {
346 maximize_title += ": " + makeDisplayPath(cur_title, 30);
347 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
348 if (!buffer()->isClean()) {
349 maximize_title += _(" (changed)");
350 minimize_title += lyx::char_type('*');
352 if (buffer()->isReadonly())
353 maximize_title += _(" (read only)");
357 if (maximize_title != last_title) {
358 setWindowTitle(maximize_title, minimize_title);
359 last_title = maximize_title;
364 void LyXView::dispatch(FuncRequest const & cmd)
366 theLyXFunc().setLyXView(this);
371 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
373 Buffer const * buffer_ptr = 0;
375 buffer_ptr = work_area_->bufferView().buffer();
377 work_area_->bufferView().update(Update::Force);