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
8 * Full author contact details are available in file CREDITS
13 #include "insetinclude.h"
15 #include "buffer_funcs.h"
16 #include "bufferlist.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
26 #include "metricsinfo.h"
27 #include "dimension.h"
29 #include "frontends/Dialogs.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Painter.h"
33 #include "support/filetools.h"
34 #include "support/FileInfo.h"
35 #include "support/FileMonitor.h"
36 #include "support/lstrings.h" // contains
37 #include "support/tostr.h"
39 #include "graphics/PreviewedInset.h"
40 #include "graphics/PreviewImage.h"
42 #include <boost/bind.hpp>
51 extern BufferList bufferlist;
54 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
57 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
60 bool previewWanted() const;
62 string const latexString() const;
64 InsetInclude & parent() const {
65 return *static_cast<InsetInclude*>(inset());
69 bool monitoring() const { return monitor_.get(); }
71 void startMonitoring();
73 void stopMonitoring() { monitor_.reset(); }
76 /// Invoked by monitor_ should the parent file change.
77 void restartLoading();
79 boost::scoped_ptr<FileMonitor> monitor_;
85 string const uniqueID()
87 static unsigned int seed = 1000;
88 return "file" + tostr(++seed);
94 InsetInclude::InsetInclude(Params const & p)
95 : params_(p), include_label(uniqueID()),
96 preview_(new PreviewImpl(*this)),
101 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
102 : include_label(uniqueID()),
103 preview_(new PreviewImpl(*this)),
107 params_.masterFilename_ = b.fileName();
111 InsetInclude::InsetInclude(InsetInclude const & other)
113 params_(other.params_),
114 include_label(other.include_label),
115 preview_(new PreviewImpl(*this)),
116 set_label_(other.set_label_)
120 InsetInclude::~InsetInclude()
122 InsetIncludeMailer mailer(*this);
127 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
129 switch (cmd.action) {
131 case LFUN_INSET_MODIFY: {
132 InsetInclude::Params p;
133 InsetIncludeMailer::string2params(cmd.argument, p);
134 if (!p.cparams.getCmdName().empty()) {
136 params_.masterFilename_ = cmd.view()->buffer()->fileName();
137 cmd.view()->updateInset(this);
142 case LFUN_INSET_DIALOG_UPDATE:
143 InsetIncludeMailer(*this).updateDialog(cmd.view());
146 case LFUN_MOUSE_RELEASE:
147 case LFUN_INSET_EDIT:
148 InsetIncludeMailer(*this).showDialog(cmd.view());
152 return Inset::localDispatch(cmd);
157 InsetInclude::Params const & InsetInclude::params() const
163 bool InsetInclude::Params::operator==(Params const & o) const
165 return cparams == o.cparams && flag == o.flag &&
166 masterFilename_ == o.masterFilename_;
170 bool InsetInclude::Params::operator!=(Params const & o) const
172 return !(*this == o);
176 void InsetInclude::set(Params const & p)
182 switch (params_.flag) {
187 command="verbatiminput";
193 command="verbatiminput*";
197 params_.cparams.setCmdName(command);
199 if (preview_->monitoring())
200 preview_->stopMonitoring();
202 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
203 preview_->generatePreview();
207 InsetBase * InsetInclude::clone() const
210 //p.masterFilename_ = buffer.fileName();
211 #warning FIXME: broken cross-doc copy/paste - must fix
213 return new InsetInclude(params_);
217 void InsetInclude::write(Buffer const *, ostream & os) const
219 os << "Include " << params_.cparams.getCommand() << '\n'
220 << "preview " << tostr(params_.cparams.preview()) << '\n';
224 void InsetInclude::read(Buffer const *, LyXLex & lex)
226 params_.cparams.read(lex);
228 if (params_.cparams.getCmdName() == "include")
229 params_.flag = INCLUDE;
230 else if (params_.cparams.getCmdName() == "input")
231 params_.flag = INPUT;
232 /* FIXME: is this logic necessary now ? */
233 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
235 if (params_.cparams.getCmdName() == "verbatiminput*")
236 params_.flag = VERBAST;
241 bool InsetInclude::display() const
243 return !(params_.flag == INPUT);
247 string const InsetInclude::getScreenLabel(Buffer const *) const
251 switch (params_.flag) {
252 case INPUT: temp += _("Input"); break;
253 case VERB: temp += _("Verbatim Input"); break;
254 case VERBAST: temp += _("Verbatim Input*"); break;
255 case INCLUDE: temp += _("Include"); break;
260 if (params_.cparams.getContents().empty())
263 temp += params_.cparams.getContents();
269 string const InsetInclude::getFileName() const
271 return MakeAbsPath(params_.cparams.getContents(),
272 OnlyPath(getMasterFilename()));
276 string const InsetInclude::getMasterFilename() const
278 return params_.masterFilename_;
282 bool InsetInclude::loadIfNeeded() const
287 if (!IsLyXFilename(getFileName()))
290 if (bufferlist.exists(getFileName()))
293 // the readonly flag can/will be wrong, not anymore I think.
294 FileInfo finfo(getFileName());
297 return loadLyXFile(bufferlist.newBuffer(getFileName()),
302 int InsetInclude::latex(Buffer const * buffer, ostream & os,
303 LatexRunParams const & runparams) const
305 string incfile(params_.cparams.getContents());
307 // Do nothing if no file name has been specified
311 if (loadIfNeeded()) {
312 Buffer * tmp = bufferlist.getBuffer(getFileName());
314 // FIXME: this should be a GUI warning
315 if (tmp->params.textclass != buffer->params.textclass) {
316 lyxerr << "WARNING: Included file `"
317 << MakeDisplayPath(getFileName())
318 << "' has textclass `"
319 << tmp->params.getLyXTextClass().name()
320 << "' while parent file has textclass `"
321 << buffer->params.getLyXTextClass().name()
326 // write it to a file (so far the complete file)
327 string writefile = ChangeExtension(getFileName(), ".tex");
329 if (!buffer->tmppath.empty() && !runparams.nice) {
330 incfile = subst(incfile, '/','@');
332 incfile = subst(incfile, ':', '$');
334 writefile = AddName(buffer->tmppath, incfile);
336 writefile = getFileName();
337 writefile = ChangeExtension(writefile, ".tex");
338 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
339 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
341 tmp->markDepClean(buffer->tmppath);
343 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
348 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
349 } else if (params_.flag == INPUT) {
350 // \input wants file with extension (default is .tex)
351 if (!IsLyXFilename(getFileName())) {
352 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
354 os << '\\' << params_.cparams.getCmdName() << '{'
355 << ChangeExtension(incfile, ".tex")
359 // \include don't want extension and demands that the
360 // file really have .tex
361 os << '\\' << params_.cparams.getCmdName() << '{'
362 << ChangeExtension(incfile, string())
370 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
373 os << GetFileContents(getFileName());
378 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
380 string incfile(params_.cparams.getContents());
382 // Do nothing if no file name has been specified
386 if (loadIfNeeded()) {
387 Buffer * tmp = bufferlist.getBuffer(getFileName());
389 // write it to a file (so far the complete file)
390 string writefile = ChangeExtension(getFileName(), ".sgml");
391 if (!buffer->tmppath.empty() && !buffer->niceFile) {
392 incfile = subst(incfile, '/','@');
393 writefile = AddName(buffer->tmppath, incfile);
395 writefile = getFileName();
397 if (IsLyXFilename(getFileName()))
398 writefile = ChangeExtension(writefile, ".sgml");
400 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
401 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
403 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
408 << GetFileContents(getFileName())
411 os << '&' << include_label << ';';
417 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
418 bool /*mixcont*/) const
420 string incfile(params_.cparams.getContents());
422 // Do nothing if no file name has been specified
426 if (loadIfNeeded()) {
427 Buffer * tmp = bufferlist.getBuffer(getFileName());
429 // write it to a file (so far the complete file)
430 string writefile = ChangeExtension(getFileName(), ".sgml");
431 if (!buffer->tmppath.empty() && !buffer->niceFile) {
432 incfile = subst(incfile, '/','@');
433 writefile = AddName(buffer->tmppath, incfile);
435 writefile = getFileName();
436 if (IsLyXFilename(getFileName()))
437 writefile = ChangeExtension(writefile, ".sgml");
439 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
440 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
442 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
446 os << "<inlinegraphic fileref=\""
447 << '&' << include_label << ';'
448 << "\" format=\"linespecific\">";
450 os << '&' << include_label << ';';
456 void InsetInclude::validate(LaTeXFeatures & features) const
459 string incfile(params_.cparams.getContents());
462 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
464 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
465 incfile = subst(incfile, '/','@');
466 writefile = AddName(b->tmppath, incfile);
468 writefile = getFileName();
470 if (IsLyXFilename(getFileName()))
471 writefile = ChangeExtension(writefile, ".sgml");
473 features.includeFile(include_label, writefile);
476 features.require("verbatim");
478 // Here we must do the fun stuff...
479 // Load the file in the include if it needs
481 if (loadIfNeeded()) {
483 Buffer * const tmp = bufferlist.getBuffer(getFileName());
486 tmp->niceFile = b->niceFile;
487 tmp->validate(features);
493 void InsetInclude::getLabelList(std::vector<string> & list) const
495 if (loadIfNeeded()) {
496 Buffer * tmp = bufferlist.getBuffer(getFileName());
497 tmp->setParentName("");
498 tmp->getLabelList(list);
499 tmp->setParentName(getMasterFilename());
504 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
506 if (loadIfNeeded()) {
507 Buffer * tmp = bufferlist.getBuffer(getFileName());
508 tmp->setParentName("");
509 tmp->fillWithBibKeys(keys);
510 tmp->setParentName(getMasterFilename());
515 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
517 if (preview_->previewReady()) {
518 dim.asc = preview_->pimage()->ascent();
519 dim.des = preview_->pimage()->descent();
520 dim.wid = preview_->pimage()->width();
524 button_.update(getScreenLabel(mi.base.bv->buffer()),
525 editable() != NOT_EDITABLE);
527 button_.metrics(mi, dim);
533 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
536 if (!preview_->previewReady()) {
537 button_.draw(pi, x, y);
541 if (!preview_->monitoring())
542 preview_->startMonitoring();
544 pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
545 *(preview_->pimage()->image()));
549 BufferView * InsetInclude::view() const
551 return button_.view();
559 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
561 preview_->addPreview(ploader);
565 bool InsetInclude::PreviewImpl::previewWanted() const
567 return parent().params_.flag == InsetInclude::INPUT &&
568 parent().params_.cparams.preview() &&
569 IsFileReadable(parent().getFileName());
573 string const InsetInclude::PreviewImpl::latexString() const
575 if (!view() || !view()->buffer())
579 LatexRunParams runparams;
580 runparams.flavor = LatexRunParams::LATEX;
581 parent().latex(view()->buffer(), os, runparams);
583 return STRCONV(os.str());
587 void InsetInclude::PreviewImpl::startMonitoring()
589 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
590 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
595 void InsetInclude::PreviewImpl::restartLoading()
597 lyxerr << "restartLoading()" << std::endl;
600 view()->updateInset(&parent());
605 string const InsetIncludeMailer::name_("include");
607 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
612 string const InsetIncludeMailer::inset2string() const
614 return params2string(inset_.params());
618 void InsetIncludeMailer::string2params(string const & in,
619 InsetInclude::Params & params)
621 params = InsetInclude::Params();
626 istringstream data(STRCONV(in));
632 string const token = lex.getString();
637 // This is part of the inset proper that is usually swallowed
638 // by Buffer::readInset
641 string const token = lex.getString();
642 if (token != "Include")
647 InsetInclude inset(params);
649 params = inset.params();
655 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
657 InsetInclude inset(params);
660 data << name_ << ' ';
661 inset.write(0, data);
662 data << "\\end_inset\n";
663 return STRCONV(data.str());