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 // parentfilename will be used in case when we switch to a child
123 // document (hence when child_document is true)
124 string parentfilename;
126 parentfilename = oldBuffer->fileName();
128 WorkArea * wa = workArea(*newBuffer);
130 updateLabels(*newBuffer->getMasterBuffer());
131 wa = addWorkArea(*newBuffer);
133 //Disconnect the old buffer...there's no new one.
135 connectBuffer(*newBuffer);
136 connectBufferView(wa->bufferView());
137 setCurrentWorkArea(wa);
143 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
146 string parentfilename;
147 Buffer * oldBuffer = buffer();
149 parentfilename = oldBuffer->fileName();
151 Buffer * newBuffer = checkAndLoadLyXFile(filename);
154 message(_("Document not loaded."));
160 WorkArea * wa = addWorkArea(*newBuffer);
162 // scroll to the position when the file was last closed
163 if (lyxrc.use_lastfilepos) {
166 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
167 // if successfully move to pit (returned par_id is not zero),
168 // update metrics and reset font
169 BufferView & bv = wa->bufferView();
170 if (bv.moveToPosition(pit, pos, 0, 0).get<1>()) {
172 bv.updateMetrics(false);
173 newBuffer->text().setCurrentFont(bv.cursor());
182 void LyXView::connectBuffer(Buffer & buf)
184 if (errorsConnection_.connected())
187 bufferStructureChangedConnection_ =
188 buf.getMasterBuffer()->structureChanged.connect(
189 boost::bind(&LyXView::updateToc, this));
193 boost::bind(&LyXView::showErrorList, this, _1));
197 boost::bind(&LyXView::message, this, _1));
201 boost::bind(&LyXView::busy, this, _1));
204 buf.updateTitles.connect(
205 boost::bind(&LyXView::updateWindowTitle, this));
208 buf.resetAutosaveTimers.connect(
209 boost::bind(&LyXView::resetAutosaveTimer, this));
211 readonlyConnection_ =
212 buf.readonly.connect(
213 boost::bind(&LyXView::showReadonly, this, _1));
217 void LyXView::disconnectBuffer()
219 errorsConnection_.disconnect();
220 bufferStructureChangedConnection_.disconnect();
221 messageConnection_.disconnect();
222 busyConnection_.disconnect();
223 titleConnection_.disconnect();
224 timerConnection_.disconnect();
225 readonlyConnection_.disconnect();
226 layout_changed_connection_.disconnect();
230 void LyXView::connectBufferView(BufferView & bv)
232 message_connection_ = bv.message.connect(
233 boost::bind(&LyXView::message, this, _1));
234 show_dialog_connection_ = bv.showDialog.connect(
235 boost::bind(&LyXView::showDialog, this, _1));
236 show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
237 boost::bind(&LyXView::showDialogWithData, this, _1, _2));
238 show_inset_dialog_connection_ = bv.showInsetDialog.connect(
239 boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
240 update_dialog_connection_ = bv.updateDialog.connect(
241 boost::bind(&LyXView::updateDialog, this, _1, _2));
242 layout_changed_connection_ = bv.layoutChanged.connect(
243 boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
247 void LyXView::disconnectBufferView()
249 message_connection_.disconnect();
250 show_dialog_connection_.disconnect();
251 show_dialog_with_data_connection_.disconnect();
252 show_inset_dialog_connection_.disconnect();
253 update_dialog_connection_.disconnect();
257 void LyXView::showErrorList(string const & error_type)
259 ErrorList & el = buffer()->errorList(error_type);
261 getDialogs().show("errorlist", error_type);
266 void LyXView::showDialog(string const & name)
268 getDialogs().show(name);
272 void LyXView::showDialogWithData(string const & name, string const & data)
274 getDialogs().show(name, data);
278 void LyXView::showInsetDialog(string const & name, string const & data,
281 getDialogs().show(name, data, inset);
285 void LyXView::updateDialog(string const & name, string const & data)
287 if (getDialogs().visible(name))
288 getDialogs().update(name, data);
292 void LyXView::showReadonly(bool)
295 getDialogs().updateBufferDependent(false);
299 BufferView * LyXView::view()
301 WorkArea * wa = currentWorkArea();
302 return wa? &wa->bufferView() : 0;
306 void LyXView::updateToc()
308 updateDialog("toc", "");
312 void LyXView::updateToolbars()
314 WorkArea * wa = currentWorkArea();
317 wa->bufferView().cursor().inMathed();
319 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
321 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
322 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
324 toolbars_->update(math, table, review);
326 toolbars_->update(false, false, false);
328 // update redaonly status of open dialogs. This could also be in
329 // updateMenubar(), but since updateToolbars() and updateMenubar()
330 // are always called together it is only here.
331 getDialogs().checkStatus();
335 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
337 return toolbars_->getToolbarInfo(name);
341 void LyXView::toggleToolbarState(string const & name, bool allowauto)
343 // it is possible to get current toolbar status like this,...
344 // but I decide to obey the order of ToolbarBackend::flags
345 // and disregard real toolbar status.
346 // toolbars_->saveToolbarInfo();
348 // toggle state on/off/auto
349 toolbars_->toggleToolbarState(name, allowauto);
355 void LyXView::updateMenubar()
361 void LyXView::autoSave()
363 LYXERR(Debug::INFO) << "Running autoSave()" << endl;
366 lyx::autoSave(view());
370 void LyXView::resetAutosaveTimer()
373 autosave_timeout_->restart();
377 void LyXView::updateLayoutChoice()
379 // Don't show any layouts without a buffer
381 toolbars_->clearLayoutList();
385 // Update the layout display
386 if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
387 current_layout = buffer()->params().getTextClass().defaultLayoutName();
390 docstring const & layout = currentWorkArea()->bufferView().cursor().
391 innerParagraph().layout()->name();
393 if (layout != current_layout) {
394 toolbars_->setLayout(layout);
395 current_layout = layout;
400 void LyXView::updateWindowTitle()
402 docstring maximize_title = lyx::from_ascii("LyX");
403 docstring minimize_title = lyx::from_ascii("LyX");
405 Buffer * buf = buffer();
407 string const cur_title = buf->fileName();
408 if (!cur_title.empty()) {
409 maximize_title += ": " + makeDisplayPath(cur_title, 30);
410 minimize_title = lyx::from_utf8(onlyFilename(cur_title));
411 if (!buf->isClean()) {
412 maximize_title += _(" (changed)");
413 minimize_title += lyx::char_type('*');
415 if (buf->isReadonly())
416 maximize_title += _(" (read only)");
420 setWindowTitle(maximize_title, minimize_title);
424 void LyXView::dispatch(FuncRequest const & cmd)
426 string const argument = to_utf8(cmd.argument());
428 case LFUN_BUFFER_SWITCH:
429 setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
432 theLyXFunc().setLyXView(this);
438 Buffer const * const LyXView::updateInset(Inset const * inset)
440 WorkArea * work_area = currentWorkArea();
445 BOOST_ASSERT(work_area);
446 work_area->scheduleRedraw();
448 return &work_area->bufferView().buffer();
451 } // namespace frontend