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;
70 LyXView::LyXView(int id)
72 toolbars_(new Toolbars(*this)),
73 autosave_timeout_(new Timeout(5000)),
74 dialogs_(new Dialogs(*this)),
75 controlcommand_(new ControlCommandBuffer(*this)), id_(id)
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 // FIXME, there's only one WorkArea per LyXView possible for now.
92 void LyXView::setWorkArea(WorkArea * work_area)
94 work_area_ = work_area;
95 work_area_ids_.clear();
96 work_area_ids_.push_back(work_area_->id());
100 Buffer * LyXView::buffer() const
102 return work_area_->bufferView().buffer();
106 void LyXView::setBuffer(Buffer * b)
110 if (work_area_->bufferView().buffer())
114 getDialogs().hideBufferDependent();
116 work_area_->bufferView().setBuffer(b);
118 if (work_area_->bufferView().buffer()) {
119 // Buffer-dependent dialogs should be updated or
120 // hidden. This should go here because some dialogs (eg ToC)
121 // require bv_->text.
122 getDialogs().updateBufferDependent(true);
123 connectBuffer(*work_area_->bufferView().buffer());
131 updateLayoutChoice();
136 work_area_->redraw();
140 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
144 if (work_area_->bufferView().buffer())
147 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
151 updateLayoutChoice();
155 connectBuffer(*work_area_->bufferView().buffer());
156 showErrorList("Parse");
160 work_area_->redraw();
165 void LyXView::connectBuffer(Buffer & buf)
167 if (errorsConnection_.connected())
170 bufferChangedConnection_ =
172 boost::bind(&WorkArea::redraw, work_area_, _1));
176 boost::bind(&LyXView::showErrorList, this, _1));
180 boost::bind(&LyXView::message, this, _1));
184 boost::bind(&LyXView::busy, this, _1));
187 buf.updateTitles.connect(
188 boost::bind(&LyXView::updateWindowTitle, this));
191 buf.resetAutosaveTimers.connect(
192 boost::bind(&LyXView::resetAutosaveTimer, this));
194 readonlyConnection_ =
195 buf.readonly.connect(
196 boost::bind(&LyXView::showReadonly, this, _1));
200 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
204 void LyXView::disconnectBuffer()
206 errorsConnection_.disconnect();
207 bufferChangedConnection_.disconnect();
208 messageConnection_.disconnect();
209 busyConnection_.disconnect();
210 titleConnection_.disconnect();
211 timerConnection_.disconnect();
212 readonlyConnection_.disconnect();
213 closingConnection_.disconnect();
214 layout_changed_connection_.disconnect();
218 void LyXView::connectBufferView(BufferView & bv)
220 show_dialog_connection_ = bv.showDialog.connect(
221 boost::bind(&LyXView::showDialog, this, _1));
222 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
223 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
224 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
225 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
226 update_dialog_connection_ = bv.updateDialog.connect(
227 boost::bind(&LyXView::updateDialog, this, _1, _2));
228 layout_changed_connection_ = bv.layoutChanged.connect(
229 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
233 void LyXView::disconnectBufferView()
235 show_dialog_connection_.disconnect();
236 show_dialog_with_data_connection_.disconnect();
237 show_inset_dialog_connection_.disconnect();
238 update_dialog_connection_.disconnect();
242 void LyXView::showErrorList(string const & error_type)
244 ErrorList & el = buffer()->errorList(error_type);
246 getDialogs().show("errorlist", error_type);
251 void LyXView::showDialog(string const & name)
253 getDialogs().show(name);
257 void LyXView::showDialogWithData(string const & name, string const & data)
259 getDialogs().show(name, data);
263 void LyXView::showInsetDialog(string const & name, string const & data,
266 getDialogs().show(name, data, inset);
270 void LyXView::updateDialog(string const & name, string const & data)
272 if (getDialogs().visible(name))
273 getDialogs().update(name, data);
277 void LyXView::showReadonly(bool)
280 getDialogs().updateBufferDependent(false);
284 BufferView * LyXView::view() const
286 return &work_area_->bufferView();
290 void LyXView::updateToolbars()
293 work_area_->bufferView().cursor().inMathed();
295 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
297 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
298 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
300 toolbars_->update(math, table, review);
301 // update redaonly status of open dialogs. This could also be in
302 // updateMenubar(), but since updateToolbars() and updateMenubar()
303 // are always called together it is only here.
304 getDialogs().checkStatus();
308 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
310 return toolbars_->getToolbarState(name);
314 void LyXView::toggleToolbarState(string const & name)
316 // it is possible to get current toolbar status like this,...
317 // but I decide to obey the order of ToolbarBackend::flags
318 // and disregard real toolbar status.
319 // toolbars_->saveToolbarInfo();
321 // toggle state on/off/auto
322 toolbars_->toggleToolbarState(name);
328 void LyXView::updateMenubar()
334 void LyXView::autoSave()
336 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
338 if (view()->buffer())
339 lyx::autoSave(view());
343 void LyXView::resetAutosaveTimer()
346 autosave_timeout_->restart();
350 void LyXView::updateLayoutChoice()
352 // Don't show any layouts without a buffer
353 if (!view()->buffer()) {
354 toolbars_->clearLayoutList();
358 // Update the layout display
359 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
360 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
363 if (work_area_->bufferView().cursor().inMathed())
366 string const & layout =
367 work_area_->bufferView().cursor().paragraph().layout()->name();
369 if (layout != current_layout) {
370 toolbars_->setLayout(layout);
371 current_layout = layout;
376 void LyXView::updateWindowTitle()
378 static docstring last_title = lyx::from_ascii("LyX");
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 if (maximize_title != last_title) {
397 setWindowTitle(maximize_title, minimize_title);
398 last_title = maximize_title;
403 void LyXView::dispatch(FuncRequest const & cmd)
405 if (cmd.action == LFUN_WINDOW_CLOSE) {
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 work_area_->bufferView().update(Update::Force);