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 work_area_ = work_area;
97 work_area_ids_.clear();
98 work_area_ids_.push_back(work_area_->id());
102 Buffer * LyXView::buffer() const
104 return work_area_->bufferView().buffer();
108 void LyXView::setBuffer(Buffer * b)
112 if (work_area_->bufferView().buffer())
116 getDialogs().hideBufferDependent();
118 work_area_->bufferView().setBuffer(b);
120 if (work_area_->bufferView().buffer()) {
121 // Buffer-dependent dialogs should be updated or
122 // hidden. This should go here because some dialogs (eg ToC)
123 // require bv_->text.
124 getDialogs().updateBufferDependent(true);
125 connectBuffer(*work_area_->bufferView().buffer());
133 updateLayoutChoice();
138 work_area_->redraw();
142 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
146 if (work_area_->bufferView().buffer())
149 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
153 updateLayoutChoice();
157 connectBuffer(*work_area_->bufferView().buffer());
158 showErrorList("Parse");
162 work_area_->redraw();
167 void LyXView::connectBuffer(Buffer & buf)
169 if (errorsConnection_.connected())
172 bufferChangedConnection_ =
174 boost::bind(&WorkArea::redraw, work_area_));
178 boost::bind(&LyXView::showErrorList, this, _1));
182 boost::bind(&LyXView::message, this, _1));
186 boost::bind(&LyXView::busy, this, _1));
189 buf.updateTitles.connect(
190 boost::bind(&LyXView::updateWindowTitle, this));
193 buf.resetAutosaveTimers.connect(
194 boost::bind(&LyXView::resetAutosaveTimer, this));
196 readonlyConnection_ =
197 buf.readonly.connect(
198 boost::bind(&LyXView::showReadonly, this, _1));
202 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
206 void LyXView::disconnectBuffer()
208 errorsConnection_.disconnect();
209 bufferChangedConnection_.disconnect();
210 messageConnection_.disconnect();
211 busyConnection_.disconnect();
212 titleConnection_.disconnect();
213 timerConnection_.disconnect();
214 readonlyConnection_.disconnect();
215 closingConnection_.disconnect();
216 layout_changed_connection_.disconnect();
220 void LyXView::connectBufferView(BufferView & bv)
222 show_dialog_connection_ = bv.showDialog.connect(
223 boost::bind(&LyXView::showDialog, this, _1));
224 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
225 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
226 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
227 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
228 update_dialog_connection_ = bv.updateDialog.connect(
229 boost::bind(&LyXView::updateDialog, this, _1, _2));
230 layout_changed_connection_ = bv.layoutChanged.connect(
231 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
235 void LyXView::disconnectBufferView()
237 show_dialog_connection_.disconnect();
238 show_dialog_with_data_connection_.disconnect();
239 show_inset_dialog_connection_.disconnect();
240 update_dialog_connection_.disconnect();
244 void LyXView::showErrorList(string const & error_type)
246 ErrorList & el = buffer()->errorList(error_type);
248 getDialogs().show("errorlist", error_type);
253 void LyXView::showDialog(string const & name)
255 getDialogs().show(name);
259 void LyXView::showDialogWithData(string const & name, string const & data)
261 getDialogs().show(name, data);
265 void LyXView::showInsetDialog(string const & name, string const & data,
268 getDialogs().show(name, data, inset);
272 void LyXView::updateDialog(string const & name, string const & data)
274 if (getDialogs().visible(name))
275 getDialogs().update(name, data);
279 void LyXView::showReadonly(bool)
282 getDialogs().updateBufferDependent(false);
286 BufferView * LyXView::view() const
288 return &work_area_->bufferView();
292 void LyXView::updateToolbars()
295 work_area_->bufferView().cursor().inMathed();
297 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
299 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
300 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
302 toolbars_->update(math, table, review);
303 // update redaonly status of open dialogs. This could also be in
304 // updateMenubar(), but since updateToolbars() and updateMenubar()
305 // are always called together it is only here.
306 getDialogs().checkStatus();
310 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
312 return toolbars_->getToolbarState(name);
316 void LyXView::toggleToolbarState(string const & name)
318 // it is possible to get current toolbar status like this,...
319 // but I decide to obey the order of ToolbarBackend::flags
320 // and disregard real toolbar status.
321 // toolbars_->saveToolbarInfo();
323 // toggle state on/off/auto
324 toolbars_->toggleToolbarState(name);
330 void LyXView::updateMenubar()
336 void LyXView::autoSave()
338 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
340 if (view()->buffer())
341 lyx::autoSave(view());
345 void LyXView::resetAutosaveTimer()
348 autosave_timeout_->restart();
352 void LyXView::updateLayoutChoice()
354 // Don't show any layouts without a buffer
355 if (!view()->buffer()) {
356 toolbars_->clearLayoutList();
360 // Update the layout display
361 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
362 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
365 if (work_area_->bufferView().cursor().inMathed())
368 string const & layout =
369 work_area_->bufferView().cursor().paragraph().layout()->name();
371 if (layout != current_layout) {
372 toolbars_->setLayout(layout);
373 current_layout = layout;
378 void LyXView::updateWindowTitle()
380 docstring maximize_title = lyx::from_ascii("LyX");
381 docstring minimize_title = lyx::from_ascii("LyX");
383 if (view()->buffer()) {
384 string const cur_title = buffer()->fileName();
385 if (!cur_title.empty()) {
386 maximize_title += ": " + makeDisplayPath(cur_title, 30);
387 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
388 if (!buffer()->isClean()) {
389 maximize_title += _(" (changed)");
390 minimize_title += lyx::char_type('*');
392 if (buffer()->isReadonly())
393 maximize_title += _(" (read only)");
397 setWindowTitle(maximize_title, minimize_title);
401 void LyXView::dispatch(FuncRequest const & cmd)
403 theLyXFunc().setLyXView(this);
408 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
410 Buffer const * buffer_ptr = 0;
412 buffer_ptr = work_area_->bufferView().buffer();
414 work_area_->bufferView().update(Update::Force);
415 work_area_->redraw();