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 "bufferlist.h"
16 #include "BufferView.h"
18 #include "funcrequest.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
25 #include "metricsinfo.h"
26 #include "dimension.h"
28 #include "frontends/Dialogs.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/Painter.h"
32 #include "support/filetools.h"
33 #include "support/FileInfo.h"
34 #include "support/FileMonitor.h"
35 #include "support/lstrings.h" // contains
36 #include "support/tostr.h"
38 #include "graphics/PreviewedInset.h"
39 #include "graphics/PreviewImage.h"
41 #include <boost/bind.hpp>
50 extern BufferList bufferlist;
53 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
56 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
59 bool previewWanted() const;
61 string const latexString() const;
63 InsetInclude & parent() const {
64 return *static_cast<InsetInclude*>(inset());
68 bool monitoring() const { return monitor_.get(); }
70 void startMonitoring();
72 void stopMonitoring() { monitor_.reset(); }
75 /// Invoked by monitor_ should the parent file change.
76 void restartLoading();
78 boost::scoped_ptr<FileMonitor> monitor_;
84 string const uniqueID()
86 static unsigned int seed = 1000;
87 return "file" + tostr(++seed);
93 InsetInclude::InsetInclude(Params const & p)
94 : params_(p), include_label(uniqueID()),
95 preview_(new PreviewImpl(*this)),
100 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
101 : include_label(uniqueID()),
102 preview_(new PreviewImpl(*this)),
106 params_.masterFilename_ = b.fileName();
110 InsetInclude::InsetInclude(InsetInclude const & other)
112 params_(other.params_),
113 include_label(other.include_label),
114 preview_(new PreviewImpl(*this)),
115 set_label_(other.set_label_)
119 InsetInclude::~InsetInclude()
121 InsetIncludeMailer mailer(*this);
126 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
128 switch (cmd.action) {
130 case LFUN_INSET_MODIFY: {
131 InsetInclude::Params p;
132 InsetIncludeMailer::string2params(cmd.argument, p);
133 if (!p.cparams.getCmdName().empty()) {
135 params_.masterFilename_ = cmd.view()->buffer()->fileName();
136 cmd.view()->updateInset(this);
141 case LFUN_INSET_DIALOG_UPDATE:
142 InsetIncludeMailer(*this).updateDialog(cmd.view());
145 case LFUN_MOUSE_RELEASE:
146 case LFUN_INSET_EDIT:
147 InsetIncludeMailer(*this).showDialog(cmd.view());
151 return Inset::localDispatch(cmd);
156 InsetInclude::Params const & InsetInclude::params() const
162 bool InsetInclude::Params::operator==(Params const & o) const
164 return cparams == o.cparams && flag == o.flag &&
165 masterFilename_ == o.masterFilename_;
169 bool InsetInclude::Params::operator!=(Params const & o) const
171 return !(*this == o);
175 void InsetInclude::set(Params const & p)
181 switch (params_.flag) {
186 command="verbatiminput";
192 command="verbatiminput*";
196 params_.cparams.setCmdName(command);
198 if (preview_->monitoring())
199 preview_->stopMonitoring();
201 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
202 preview_->generatePreview();
206 InsetBase * InsetInclude::clone() const
209 //p.masterFilename_ = buffer.fileName();
210 #warning FIXME: broken cross-doc copy/paste - must fix
212 return new InsetInclude(params_);
216 void InsetInclude::write(Buffer const *, ostream & os) const
218 os << "Include " << params_.cparams.getCommand() << '\n'
219 << "preview " << tostr(params_.cparams.preview()) << '\n';
223 void InsetInclude::read(Buffer const *, LyXLex & lex)
225 params_.cparams.read(lex);
227 if (params_.cparams.getCmdName() == "include")
228 params_.flag = INCLUDE;
229 else if (params_.cparams.getCmdName() == "input")
230 params_.flag = INPUT;
231 /* FIXME: is this logic necessary now ? */
232 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
234 if (params_.cparams.getCmdName() == "verbatiminput*")
235 params_.flag = VERBAST;
240 bool InsetInclude::display() const
242 return !(params_.flag == INPUT);
246 string const InsetInclude::getScreenLabel(Buffer const *) const
250 switch (params_.flag) {
251 case INPUT: temp += _("Input"); break;
252 case VERB: temp += _("Verbatim Input"); break;
253 case VERBAST: temp += _("Verbatim Input*"); break;
254 case INCLUDE: temp += _("Include"); break;
259 if (params_.cparams.getContents().empty())
262 temp += params_.cparams.getContents();
268 string const InsetInclude::getFileName() const
270 return MakeAbsPath(params_.cparams.getContents(),
271 OnlyPath(getMasterFilename()));
275 string const InsetInclude::getMasterFilename() const
277 return params_.masterFilename_;
281 bool InsetInclude::loadIfNeeded() const
286 if (!IsLyXFilename(getFileName()))
289 if (bufferlist.exists(getFileName()))
292 // the readonly flag can/will be wrong, not anymore I think.
293 FileInfo finfo(getFileName());
297 return bufferlist.loadLyXFile(getFileName(), false) != 0;
301 int InsetInclude::latex(Buffer const * buffer, ostream & os,
302 LatexRunParams const & runparams) const
304 string incfile(params_.cparams.getContents());
306 // Do nothing if no file name has been specified
310 if (loadIfNeeded()) {
311 Buffer * tmp = bufferlist.getBuffer(getFileName());
313 // FIXME: this should be a GUI warning
314 if (tmp->params.textclass != buffer->params.textclass) {
315 lyxerr << "WARNING: Included file `"
316 << MakeDisplayPath(getFileName())
317 << "' has textclass `"
318 << tmp->params.getLyXTextClass().name()
319 << "' while parent file has textclass `"
320 << buffer->params.getLyXTextClass().name()
325 // write it to a file (so far the complete file)
326 string writefile = ChangeExtension(getFileName(), ".tex");
328 if (!buffer->tmppath.empty() && !runparams.nice) {
329 incfile = subst(incfile, '/','@');
331 incfile = subst(incfile, ':', '$');
333 writefile = AddName(buffer->tmppath, incfile);
335 writefile = getFileName();
336 writefile = ChangeExtension(writefile, ".tex");
337 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
338 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
340 tmp->markDepClean(buffer->tmppath);
342 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
347 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
348 } else if (params_.flag == INPUT) {
349 // \input wants file with extension (default is .tex)
350 if (!IsLyXFilename(getFileName())) {
351 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
353 os << '\\' << params_.cparams.getCmdName() << '{'
354 << ChangeExtension(incfile, ".tex")
358 // \include don't want extension and demands that the
359 // file really have .tex
360 os << '\\' << params_.cparams.getCmdName() << '{'
361 << ChangeExtension(incfile, string())
369 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
372 os << GetFileContents(getFileName());
377 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
379 string incfile(params_.cparams.getContents());
381 // Do nothing if no file name has been specified
385 if (loadIfNeeded()) {
386 Buffer * tmp = bufferlist.getBuffer(getFileName());
388 // write it to a file (so far the complete file)
389 string writefile = ChangeExtension(getFileName(), ".sgml");
390 if (!buffer->tmppath.empty() && !buffer->niceFile) {
391 incfile = subst(incfile, '/','@');
392 writefile = AddName(buffer->tmppath, incfile);
394 writefile = getFileName();
396 if (IsLyXFilename(getFileName()))
397 writefile = ChangeExtension(writefile, ".sgml");
399 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
400 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
402 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
407 << GetFileContents(getFileName())
410 os << '&' << include_label << ';';
416 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
417 bool /*mixcont*/) const
419 string incfile(params_.cparams.getContents());
421 // Do nothing if no file name has been specified
425 if (loadIfNeeded()) {
426 Buffer * tmp = bufferlist.getBuffer(getFileName());
428 // write it to a file (so far the complete file)
429 string writefile = ChangeExtension(getFileName(), ".sgml");
430 if (!buffer->tmppath.empty() && !buffer->niceFile) {
431 incfile = subst(incfile, '/','@');
432 writefile = AddName(buffer->tmppath, incfile);
434 writefile = getFileName();
435 if (IsLyXFilename(getFileName()))
436 writefile = ChangeExtension(writefile, ".sgml");
438 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
439 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
441 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
445 os << "<inlinegraphic fileref=\""
446 << '&' << include_label << ';'
447 << "\" format=\"linespecific\">";
449 os << '&' << include_label << ';';
455 void InsetInclude::validate(LaTeXFeatures & features) const
458 string incfile(params_.cparams.getContents());
461 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
463 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
464 incfile = subst(incfile, '/','@');
465 writefile = AddName(b->tmppath, incfile);
467 writefile = getFileName();
469 if (IsLyXFilename(getFileName()))
470 writefile = ChangeExtension(writefile, ".sgml");
472 features.includeFile(include_label, writefile);
475 features.require("verbatim");
477 // Here we must do the fun stuff...
478 // Load the file in the include if it needs
480 if (loadIfNeeded()) {
482 Buffer * const tmp = bufferlist.getBuffer(getFileName());
485 tmp->niceFile = b->niceFile;
486 tmp->validate(features);
492 void InsetInclude::getLabelList(std::vector<string> & list) const
494 if (loadIfNeeded()) {
495 Buffer * tmp = bufferlist.getBuffer(getFileName());
496 tmp->setParentName("");
497 tmp->getLabelList(list);
498 tmp->setParentName(getMasterFilename());
503 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
505 if (loadIfNeeded()) {
506 Buffer * tmp = bufferlist.getBuffer(getFileName());
507 tmp->setParentName("");
508 tmp->fillWithBibKeys(keys);
509 tmp->setParentName(getMasterFilename());
514 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
516 if (preview_->previewReady()) {
517 dim.asc = preview_->pimage()->ascent();
518 dim.des = preview_->pimage()->descent();
519 dim.wid = preview_->pimage()->width();
523 button_.update(getScreenLabel(mi.base.bv->buffer()),
524 editable() != NOT_EDITABLE);
526 button_.metrics(mi, dim);
532 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
535 if (!preview_->previewReady()) {
536 button_.draw(pi, x, y);
540 if (!preview_->monitoring())
541 preview_->startMonitoring();
543 pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
544 *(preview_->pimage()->image()));
548 BufferView * InsetInclude::view() const
550 return button_.view();
558 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
560 preview_->addPreview(ploader);
564 bool InsetInclude::PreviewImpl::previewWanted() const
566 return parent().params_.flag == InsetInclude::INPUT &&
567 parent().params_.cparams.preview() &&
568 IsFileReadable(parent().getFileName());
572 string const InsetInclude::PreviewImpl::latexString() const
574 if (!view() || !view()->buffer())
578 LatexRunParams runparams;
579 runparams.flavor = LatexRunParams::LATEX;
580 parent().latex(view()->buffer(), os, runparams);
582 return STRCONV(os.str());
586 void InsetInclude::PreviewImpl::startMonitoring()
588 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
589 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
594 void InsetInclude::PreviewImpl::restartLoading()
596 lyxerr << "restartLoading()" << std::endl;
599 view()->updateInset(&parent());
604 string const InsetIncludeMailer::name_("include");
606 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
611 string const InsetIncludeMailer::inset2string() const
613 return params2string(inset_.params());
617 void InsetIncludeMailer::string2params(string const & in,
618 InsetInclude::Params & params)
620 params = InsetInclude::Params();
625 istringstream data(STRCONV(in));
631 string const token = lex.getString();
636 // This is part of the inset proper that is usually swallowed
637 // by Buffer::readInset
640 string const token = lex.getString();
641 if (token != "Include")
646 InsetInclude inset(params);
648 params = inset.params();
654 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
656 InsetInclude inset(params);
659 data << name_ << ' ';
660 inset.write(0, data);
661 data << "\\end_inset\n";
662 return STRCONV(data.str());