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();
135 work_area_->redraw();
139 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
143 if (work_area_->bufferView().buffer())
146 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
150 updateLayoutChoice();
153 connectBuffer(*work_area_->bufferView().buffer());
154 showErrorList("Parse");
158 work_area_->redraw();
163 void LyXView::connectBuffer(Buffer & buf)
165 if (errorsConnection_.connected())
168 bufferChangedConnection_ =
170 boost::bind(&WorkArea::redraw, work_area_));
174 boost::bind(&LyXView::showErrorList, this, _1));
178 boost::bind(&LyXView::message, this, _1));
182 boost::bind(&LyXView::busy, this, _1));
185 buf.updateTitles.connect(
186 boost::bind(&LyXView::updateWindowTitle, this));
189 buf.resetAutosaveTimers.connect(
190 boost::bind(&LyXView::resetAutosaveTimer, this));
192 readonlyConnection_ =
193 buf.readonly.connect(
194 boost::bind(&LyXView::showReadonly, this, _1));
198 boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
202 void LyXView::disconnectBuffer()
204 errorsConnection_.disconnect();
205 bufferChangedConnection_.disconnect();
206 messageConnection_.disconnect();
207 busyConnection_.disconnect();
208 titleConnection_.disconnect();
209 timerConnection_.disconnect();
210 readonlyConnection_.disconnect();
211 closingConnection_.disconnect();
212 layout_changed_connection_.disconnect();
216 void LyXView::connectBufferView(BufferView & bv)
218 show_dialog_connection_ = bv.showDialog.connect(
219 boost::bind(&LyXView::showDialog, this, _1));
220 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
221 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
222 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
223 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
224 update_dialog_connection_ = bv.updateDialog.connect(
225 boost::bind(&LyXView::updateDialog, this, _1, _2));
226 layout_changed_connection_ = bv.layoutChanged.connect(
227 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
231 void LyXView::disconnectBufferView()
233 show_dialog_connection_.disconnect();
234 show_dialog_with_data_connection_.disconnect();
235 show_inset_dialog_connection_.disconnect();
236 update_dialog_connection_.disconnect();
240 void LyXView::showErrorList(string const & error_type)
242 ErrorList & el = buffer()->errorList(error_type);
244 getDialogs().show("errorlist", error_type);
249 void LyXView::showDialog(string const & name)
251 getDialogs().show(name);
255 void LyXView::showDialogWithData(string const & name, string const & data)
257 getDialogs().show(name, data);
261 void LyXView::showInsetDialog(string const & name, string const & data,
264 getDialogs().show(name, data, inset);
268 void LyXView::updateDialog(string const & name, string const & data)
270 if (getDialogs().visible(name))
271 getDialogs().update(name, data);
275 void LyXView::showReadonly(bool)
278 getDialogs().updateBufferDependent(false);
282 BufferView * LyXView::view() const
284 return &work_area_->bufferView();
288 void LyXView::updateToolbars()
291 work_area_->bufferView().cursor().inMathed();
293 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
295 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
296 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
298 toolbars_->update(math, table, review);
299 // update redaonly status of open dialogs. This could also be in
300 // updateMenubar(), but since updateToolbars() and updateMenubar()
301 // are always called together it is only here.
302 getDialogs().checkStatus();
306 void LyXView::updateMenubar()
312 void LyXView::autoSave()
314 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
316 if (view()->buffer())
317 lyx::autoSave(view());
321 void LyXView::resetAutosaveTimer()
324 autosave_timeout_->restart();
328 void LyXView::updateLayoutChoice()
330 // Don't show any layouts without a buffer
331 if (!view()->buffer()) {
332 toolbars_->clearLayoutList();
336 // Update the layout display
337 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
338 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
341 if (work_area_->bufferView().cursor().inMathed())
344 string const & layout =
345 work_area_->bufferView().cursor().paragraph().layout()->name();
347 if (layout != current_layout) {
348 toolbars_->setLayout(layout);
349 current_layout = layout;
354 void LyXView::updateWindowTitle()
356 static docstring last_title = lyx::from_ascii("LyX");
357 docstring maximize_title = lyx::from_ascii("LyX");
358 docstring minimize_title = lyx::from_ascii("LyX");
360 if (view()->buffer()) {
361 string const cur_title = buffer()->fileName();
362 if (!cur_title.empty()) {
363 maximize_title += ": " + makeDisplayPath(cur_title, 30);
364 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
365 if (!buffer()->isClean()) {
366 maximize_title += _(" (changed)");
367 minimize_title += lyx::char_type('*');
369 if (buffer()->isReadonly())
370 maximize_title += _(" (read only)");
374 if (maximize_title != last_title) {
375 setWindowTitle(maximize_title, minimize_title);
376 last_title = maximize_title;
381 void LyXView::dispatch(FuncRequest const & cmd)
383 if (cmd.action == LFUN_WINDOW_CLOSE) {
389 theLyXFunc().setLyXView(this);
394 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
396 Buffer const * buffer_ptr = 0;
398 buffer_ptr = work_area_->bufferView().buffer();
400 work_area_->bufferView().update(Update::Force);