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)
116 Buffer * oldBuffer = buffer();
117 if (oldBuffer == newBuffer) {
122 WorkArea * wa = workArea(*newBuffer);
124 updateLabels(*newBuffer->getMasterBuffer());
125 wa = addWorkArea(*newBuffer);
127 //Disconnect the old buffer...there's no new one.
129 connectBuffer(*newBuffer);
130 connectBufferView(wa->bufferView());
131 setCurrentWorkArea(wa);
137 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
141 Buffer * newBuffer = checkAndLoadLyXFile(filename);
144 message(_("Document not loaded."));
150 WorkArea * wa = addWorkArea(*newBuffer);
152 // scroll to the position when the file was last closed
153 if (lyxrc.use_lastfilepos) {
156 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
157 // if successfully move to pit (returned par_id is not zero),
158 // update metrics and reset font
159 BufferView & bv = wa->bufferView();
160 if (bv.moveToPosition(pit, pos, 0, 0).get<1>()) {
162 bv.updateMetrics(false);
163 newBuffer->text().setCurrentFont(bv.cursor());
172 void LyXView::connectBuffer(Buffer & buf)
174 if (errorsConnection_.connected())
177 bufferStructureChangedConnection_ =
178 buf.getMasterBuffer()->structureChanged.connect(
179 boost::bind(&LyXView::updateToc, this));
183 boost::bind(&LyXView::showErrorList, this, _1));
187 boost::bind(&LyXView::message, this, _1));
191 boost::bind(&LyXView::busy, this, _1));
194 buf.updateTitles.connect(
195 boost::bind(&LyXView::updateWindowTitle, this));
198 buf.resetAutosaveTimers.connect(
199 boost::bind(&LyXView::resetAutosaveTimer, this));
201 readonlyConnection_ =
202 buf.readonly.connect(
203 boost::bind(&LyXView::showReadonly, this, _1));
207 void LyXView::disconnectBuffer()
209 errorsConnection_.disconnect();
210 bufferStructureChangedConnection_.disconnect();
211 messageConnection_.disconnect();
212 busyConnection_.disconnect();
213 titleConnection_.disconnect();
214 timerConnection_.disconnect();
215 readonlyConnection_.disconnect();
216 layout_changed_connection_.disconnect();
220 void LyXView::connectBufferView(BufferView & bv)
222 message_connection_ = bv.message.connect(
223 boost::bind(&LyXView::message, this, _1));
224 show_dialog_connection_ = bv.showDialog.connect(
225 boost::bind(&LyXView::showDialog, this, _1));
226 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
227 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
228 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
229 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
230 update_dialog_connection_ = bv.updateDialog.connect(
231 boost::bind(&LyXView::updateDialog, this, _1, _2));
232 layout_changed_connection_ = bv.layoutChanged.connect(
233 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
237 void LyXView::disconnectBufferView()
239 message_connection_.disconnect();
240 show_dialog_connection_.disconnect();
241 show_dialog_with_data_connection_.disconnect();
242 show_inset_dialog_connection_.disconnect();
243 update_dialog_connection_.disconnect();
247 void LyXView::showErrorList(string const & error_type)
249 ErrorList & el = buffer()->errorList(error_type);
251 getDialogs().show("errorlist", error_type);
256 void LyXView::showDialog(string const & name)
258 getDialogs().show(name);
262 void LyXView::showDialogWithData(string const & name, string const & data)
264 getDialogs().show(name, data);
268 void LyXView::showInsetDialog(string const & name, string const & data,
271 getDialogs().show(name, data, inset);
275 void LyXView::updateDialog(string const & name, string const & data)
277 if (getDialogs().visible(name))
278 getDialogs().update(name, data);
282 void LyXView::showReadonly(bool)
285 getDialogs().updateBufferDependent(false);
289 BufferView * LyXView::view()
291 WorkArea * wa = currentWorkArea();
292 return wa? &wa->bufferView() : 0;
296 void LyXView::updateToc()
298 updateDialog("toc", "");
302 void LyXView::updateToolbars()
304 WorkArea * wa = currentWorkArea();
307 wa->bufferView().cursor().inMathed();
309 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
311 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
312 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
314 toolbars_->update(math, table, review);
316 toolbars_->update(false, false, false);
318 // update redaonly status of open dialogs. This could also be in
319 // updateMenubar(), but since updateToolbars() and updateMenubar()
320 // are always called together it is only here.
321 getDialogs().checkStatus();
325 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
327 return toolbars_->getToolbarInfo(name);
331 void LyXView::toggleToolbarState(string const & name, bool allowauto)
333 // it is possible to get current toolbar status like this,...
334 // but I decide to obey the order of ToolbarBackend::flags
335 // and disregard real toolbar status.
336 // toolbars_->saveToolbarInfo();
338 // toggle state on/off/auto
339 toolbars_->toggleToolbarState(name, allowauto);
345 void LyXView::updateMenubar()
351 void LyXView::autoSave()
353 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
356 lyx::autoSave(view());
360 void LyXView::resetAutosaveTimer()
363 autosave_timeout_->restart();
367 void LyXView::updateLayoutChoice()
369 // Don't show any layouts without a buffer
371 toolbars_->clearLayoutList();
375 // Update the layout display
376 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
377 current_layout = buffer()->params().getTextClass().defaultLayoutName();
380 docstring const & layout = currentWorkArea()->bufferView().cursor().
381 innerParagraph().layout()->name();
383 if (layout != current_layout) {
384 toolbars_->setLayout(layout);
385 current_layout = layout;
390 void LyXView::updateWindowTitle()
392 docstring maximize_title = lyx::from_ascii("LyX");
393 docstring minimize_title = lyx::from_ascii("LyX");
395 Buffer * buf = buffer();
397 string const cur_title = buf->fileName();
398 if (!cur_title.empty()) {
399 maximize_title += ": " + makeDisplayPath(cur_title, 30);
400 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
401 if (!buf->isClean()) {
402 maximize_title += _(" (changed)");
403 minimize_title += lyx::char_type('*');
405 if (buf->isReadonly())
406 maximize_title += _(" (read only)");
410 setWindowTitle(maximize_title, minimize_title);
414 void LyXView::dispatch(FuncRequest const & cmd)
416 string const argument = to_utf8(cmd.argument());
418 case LFUN_BUFFER_SWITCH:
419 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
422 theLyXFunc().setLyXView(this);
428 Buffer const * const LyXView::updateInset(Inset const * inset)
430 WorkArea * work_area = currentWorkArea();
435 BOOST_ASSERT(work_area);
436 work_area->scheduleRedraw();
438 return &work_area->bufferView().buffer();
441 } // namespace frontend