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>
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
52 using lyx::frontend::WorkArea;
55 using lyx::support::bformat;
56 using lyx::support::makeDisplayPath;
57 using lyx::support::onlyFilename;
62 using lyx::frontend::ControlCommandBuffer;
64 string current_layout;
69 toolbars_(new Toolbars(*this)),
70 autosave_timeout_(new Timeout(5000)),
71 lyxfunc_(new LyXFunc(this)),
72 dialogs_(new Dialogs(*this)),
73 controlcommand_(new ControlCommandBuffer(*this))
75 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
84 void LyXView::setWorkArea(WorkArea * work_area)
86 work_area_ = work_area;
90 void LyXView::redrawWorkArea()
97 WorkArea * LyXView::workArea()
105 updateLayoutChoice();
108 // Start autosave timer
109 if (lyxrc.autosave) {
110 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
111 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
112 autosave_timeout_->start();
117 Buffer * LyXView::buffer() const
119 return work_area_->bufferView().buffer();
123 void LyXView::setBuffer(Buffer * b)
125 if (work_area_->bufferView().buffer())
129 getDialogs().hideBufferDependent();
131 work_area_->bufferView().setBuffer(b);
133 if (work_area_->bufferView().buffer()) {
134 // Buffer-dependent dialogs should be updated or
135 // hidden. This should go here because some dialogs (eg ToC)
136 // require bv_->text.
137 getDialogs().updateBufferDependent(true);
138 connectBuffer(*work_area_->bufferView().buffer());
143 updateLayoutChoice();
149 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
151 if (work_area_->bufferView().buffer())
154 bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
158 updateLayoutChoice();
161 connectBuffer(*work_area_->bufferView().buffer());
162 showErrorList("Parse");
169 void LyXView::connectBuffer(Buffer & buf)
171 if (errorsConnection_.connected())
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 messageConnection_.disconnect();
207 busyConnection_.disconnect();
208 titleConnection_.disconnect();
209 timerConnection_.disconnect();
210 readonlyConnection_.disconnect();
211 closingConnection_.disconnect();
215 void LyXView::connectBufferView(BufferView & bv)
217 show_dialog_connection_ = bv.showDialog.connect(
218 boost::bind(&LyXView::showDialog, this, _1));
219 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
220 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
221 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
222 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
223 update_dialog_connection_ = bv.updateDialog.connect(
224 boost::bind(&LyXView::updateDialog, this, _1, _2));
228 void LyXView::disconnectBufferView()
230 show_dialog_connection_.disconnect();
231 show_dialog_with_data_connection_.disconnect();
232 show_inset_dialog_connection_.disconnect();
233 update_dialog_connection_.disconnect();
237 void LyXView::showErrorList(string const & error_type)
239 ErrorList & el = buffer()->errorList(error_type);
241 getDialogs().show("errorlist", error_type);
246 void LyXView::showDialog(string const & name)
248 getDialogs().show(name);
252 void LyXView::showDialogWithData(string const & name,
255 getDialogs().show(name, data);
259 void LyXView::showInsetDialog(string const & name, string const & data,
262 getDialogs().show(name, data, 0);
266 void LyXView::updateDialog(string const & name, string const & data)
268 if (getDialogs().visible(name))
269 getDialogs().update(name, data);
273 void LyXView::showReadonly(bool)
276 getDialogs().updateBufferDependent(false);
280 BufferView * LyXView::view() const
282 return &work_area_->bufferView();
286 void LyXView::setLayout(string const & layout)
288 toolbars_->setLayout(layout);
292 void LyXView::updateToolbars()
294 bool const math = work_area_->bufferView().cursor().inMathed();
296 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
297 toolbars_->update(math, table);
298 // update redaonly status of open dialogs. This could also be in
299 // updateMenubar(), but since updateToolbars() and updateMenubar()
300 // are always called together it is only here.
301 getDialogs().checkStatus();
305 void LyXView::updateMenubar()
311 void LyXView::autoSave()
313 lyxerr[Debug::INFO] << "Running autoSave()" << endl;
315 if (view()->buffer()) {
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 += lyx::from_ascii(": ") + 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 getLyXFunc().dispatch(cmd);
387 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
389 Buffer const * buffer_ptr = 0;
391 buffer_ptr = work_area_->bufferView().buffer();
393 work_area_->bufferView().update(Update::Force);