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 void LyXView::redrawWorkArea()
99 lyxerr[Debug::WORKAREA] << "LyXView::redrawWorkArea" << std::endl;
100 work_area_->redraw();
105 WorkArea * LyXView::workArea()
111 Buffer * LyXView::buffer() const
113 return work_area_->bufferView().buffer();
117 void LyXView::setBuffer(Buffer * b)
119 if (work_area_->bufferView().buffer())
123 getDialogs().hideBufferDependent();
125 work_area_->bufferView().setBuffer(b);
127 if (work_area_->bufferView().buffer()) {
128 // Buffer-dependent dialogs should be updated or
129 // hidden. This should go here because some dialogs (eg ToC)
130 // require bv_->text.
131 getDialogs().updateBufferDependent(true);
132 connectBuffer(*work_area_->bufferView().buffer());
137 updateLayoutChoice();
143 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
145 if (work_area_->bufferView().buffer())
148 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
152 updateLayoutChoice();
155 connectBuffer(*work_area_->bufferView().buffer());
156 showErrorList("Parse");
163 void LyXView::connectBuffer(Buffer & buf)
165 if (errorsConnection_.connected())
170 boost::bind(&LyXView::showErrorList, this, _1));
174 boost::bind(&LyXView::message, this, _1));
178 boost::bind(&LyXView::busy, this, _1));
181 buf.updateTitles.connect(
182 boost::bind(&LyXView::updateWindowTitle, this));
185 buf.resetAutosaveTimers.connect(
186 boost::bind(&LyXView::resetAutosaveTimer, this));
188 readonlyConnection_ =
189 buf.readonly.connect(
190 boost::bind(&LyXView::showReadonly, this, _1));
194 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
198 void LyXView::disconnectBuffer()
200 messageConnection_.disconnect();
201 busyConnection_.disconnect();
202 titleConnection_.disconnect();
203 timerConnection_.disconnect();
204 readonlyConnection_.disconnect();
205 closingConnection_.disconnect();
206 layout_changed_connection_.disconnect();
210 void LyXView::connectBufferView(BufferView & bv)
212 show_dialog_connection_ = bv.showDialog.connect(
213 boost::bind(&LyXView::showDialog, this, _1));
214 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
215 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
216 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
217 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
218 update_dialog_connection_ = bv.updateDialog.connect(
219 boost::bind(&LyXView::updateDialog, this, _1, _2));
220 layout_changed_connection_ = bv.layoutChanged.connect(
221 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
225 void LyXView::disconnectBufferView()
227 show_dialog_connection_.disconnect();
228 show_dialog_with_data_connection_.disconnect();
229 show_inset_dialog_connection_.disconnect();
230 update_dialog_connection_.disconnect();
234 void LyXView::showErrorList(string const & error_type)
236 ErrorList & el = buffer()->errorList(error_type);
238 getDialogs().show("errorlist", error_type);
243 void LyXView::showDialog(string const & name)
245 getDialogs().show(name);
249 void LyXView::showDialogWithData(string const & name, string const & data)
251 getDialogs().show(name, data);
255 void LyXView::showInsetDialog(string const & name, string const & data,
258 getDialogs().show(name, data, inset);
262 void LyXView::updateDialog(string const & name, string const & data)
264 if (getDialogs().visible(name))
265 getDialogs().update(name, data);
269 void LyXView::showReadonly(bool)
272 getDialogs().updateBufferDependent(false);
276 BufferView * LyXView::view() const
278 return &work_area_->bufferView();
282 void LyXView::updateToolbars()
284 bool const math = work_area_->bufferView().cursor().inMathed();
286 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
287 toolbars_->update(math, table);
288 // update redaonly status of open dialogs. This could also be in
289 // updateMenubar(), but since updateToolbars() and updateMenubar()
290 // are always called together it is only here.
291 getDialogs().checkStatus();
295 void LyXView::updateMenubar()
301 void LyXView::autoSave()
303 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
305 if (view()->buffer())
306 lyx::autoSave(view());
310 void LyXView::resetAutosaveTimer()
313 autosave_timeout_->restart();
317 void LyXView::updateLayoutChoice()
319 // Don't show any layouts without a buffer
320 if (!view()->buffer()) {
321 toolbars_->clearLayoutList();
325 // Update the layout display
326 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
327 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
330 if (work_area_->bufferView().cursor().inMathed())
333 string const & layout =
334 work_area_->bufferView().cursor().paragraph().layout()->name();
336 if (layout != current_layout) {
337 toolbars_->setLayout(layout);
338 current_layout = layout;
343 void LyXView::updateWindowTitle()
345 static docstring last_title = lyx::from_ascii("LyX");
346 docstring maximize_title = lyx::from_ascii("LyX");
347 docstring minimize_title = lyx::from_ascii("LyX");
349 if (view()->buffer()) {
350 string const cur_title = buffer()->fileName();
351 if (!cur_title.empty()) {
352 maximize_title += ": " + makeDisplayPath(cur_title, 30);
353 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
354 if (!buffer()->isClean()) {
355 maximize_title += _(" (changed)");
356 minimize_title += lyx::char_type('*');
358 if (buffer()->isReadonly())
359 maximize_title += _(" (read only)");
363 if (maximize_title != last_title) {
364 setWindowTitle(maximize_title, minimize_title);
365 last_title = maximize_title;
370 void LyXView::dispatch(FuncRequest const & cmd)
372 theLyXFunc().setLyXView(this);
377 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
379 Buffer const * buffer_ptr = 0;
381 buffer_ptr = work_area_->bufferView().buffer();
383 work_area_->bufferView().update(Update::Force);