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 "buffer_funcs.h"
24 #include "BufferList.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
30 #include "ErrorList.h"
31 #include "FuncRequest.h"
39 #include "MenuBackend.h"
40 #include "Paragraph.h"
42 #include "controllers/ControlCommandBuffer.h"
44 #include "support/lstrings.h"
45 #include "support/filetools.h" // OnlyFilename()
46 #include "support/Timeout.h"
48 #include <boost/bind.hpp>
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
63 using support::bformat;
64 using support::FileName;
65 using support::makeDisplayPath;
66 using support::onlyFilename;
70 docstring current_layout;
72 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();
90 disconnectBufferView();
94 Buffer * LyXView::buffer()
96 WorkArea * work_area = currentWorkArea();
98 return &work_area->bufferView().buffer();
103 Buffer const * LyXView::buffer() const
105 WorkArea const * work_area = currentWorkArea();
107 return &work_area->bufferView().buffer();
112 void LyXView::setBuffer(Buffer * newBuffer)
114 BOOST_ASSERT(newBuffer);
117 WorkArea * wa = workArea(*newBuffer);
119 updateLabels(*newBuffer->getMasterBuffer());
120 wa = addWorkArea(*newBuffer);
122 //Disconnect the old buffer...there's no new one.
124 connectBuffer(*newBuffer);
125 connectBufferView(wa->bufferView());
126 setCurrentWorkArea(wa);
132 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
136 Buffer * newBuffer = checkAndLoadLyXFile(filename);
139 message(_("Document not loaded."));
145 WorkArea * wa = addWorkArea(*newBuffer);
147 // scroll to the position when the file was last closed
148 if (lyxrc.use_lastfilepos) {
151 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
152 // if successfully move to pit (returned par_id is not zero),
153 // update metrics and reset font
154 wa->bufferView().moveToPosition(pit, pos, 0, 0);
158 LyX::ref().session().lastFiles().add(filename);
165 void LyXView::connectBuffer(Buffer & buf)
167 if (errorsConnection_.connected())
170 bufferStructureChangedConnection_ =
171 buf.getMasterBuffer()->structureChanged.connect(
172 boost::bind(&LyXView::updateToc, this));
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 void LyXView::disconnectBuffer()
202 errorsConnection_.disconnect();
203 bufferStructureChangedConnection_.disconnect();
204 messageConnection_.disconnect();
205 busyConnection_.disconnect();
206 titleConnection_.disconnect();
207 timerConnection_.disconnect();
208 readonlyConnection_.disconnect();
209 layout_changed_connection_.disconnect();
213 void LyXView::connectBufferView(BufferView & bv)
215 message_connection_ = bv.message.connect(
216 boost::bind(&LyXView::message, this, _1));
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));
225 layout_changed_connection_ = bv.layoutChanged.connect(
226 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
230 void LyXView::disconnectBufferView()
232 message_connection_.disconnect();
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()
284 WorkArea * wa = currentWorkArea();
285 return wa? &wa->bufferView() : 0;
289 void LyXView::updateToc()
291 updateDialog("toc", "");
295 void LyXView::updateToolbars()
297 WorkArea * wa = currentWorkArea();
300 wa->bufferView().cursor().inMathed();
302 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
304 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
305 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
307 toolbars_->update(math, table, review);
309 toolbars_->update(false, false, false);
311 // update redaonly status of open dialogs. This could also be in
312 // updateMenubar(), but since updateToolbars() and updateMenubar()
313 // are always called together it is only here.
314 getDialogs().checkStatus();
318 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
320 return toolbars_->getToolbarInfo(name);
324 void LyXView::toggleToolbarState(string const & name, bool allowauto)
326 // it is possible to get current toolbar status like this,...
327 // but I decide to obey the order of ToolbarBackend::flags
328 // and disregard real toolbar status.
329 // toolbars_->saveToolbarInfo();
331 // toggle state on/off/auto
332 toolbars_->toggleToolbarState(name, allowauto);
338 void LyXView::updateMenubar()
344 void LyXView::autoSave()
346 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
349 lyx::autoSave(view());
353 void LyXView::resetAutosaveTimer()
356 autosave_timeout_->restart();
360 void LyXView::updateLayoutChoice()
362 // Don't show any layouts without a buffer
364 toolbars_->clearLayoutList();
368 // Update the layout display
369 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
370 current_layout = buffer()->params().getTextClass().defaultLayoutName();
373 docstring const & layout = currentWorkArea()->bufferView().cursor().
374 innerParagraph().layout()->name();
376 if (layout != current_layout) {
377 toolbars_->setLayout(layout);
378 current_layout = layout;
383 void LyXView::updateWindowTitle()
385 docstring maximize_title = lyx::from_ascii("LyX");
386 docstring minimize_title = lyx::from_ascii("LyX");
388 Buffer * buf = buffer();
390 string const cur_title = buf->fileName();
391 if (!cur_title.empty()) {
392 maximize_title += ": " + makeDisplayPath(cur_title, 30);
393 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
394 if (!buf->isClean()) {
395 maximize_title += _(" (changed)");
396 minimize_title += lyx::char_type('*');
398 if (buf->isReadonly())
399 maximize_title += _(" (read only)");
403 setWindowTitle(maximize_title, minimize_title);
407 void LyXView::dispatch(FuncRequest const & cmd)
409 string const argument = to_utf8(cmd.argument());
411 case LFUN_BUFFER_SWITCH:
412 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
415 theLyXFunc().setLyXView(this);
421 Buffer const * const LyXView::updateInset(Inset const * inset)
423 WorkArea * work_area = currentWorkArea();
428 BOOST_ASSERT(work_area);
429 work_area->scheduleRedraw();
431 return &work_area->bufferView().buffer();
435 void LyXView::openLayoutList()
437 toolbars_->openLayoutList();
441 bool LyXView::isToolbarVisible(std::string const & id)
443 return toolbars_->visible(id);
446 } // namespace frontend