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>
46 using namespace lyx::support;
55 extern BufferList bufferlist;
58 class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
61 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
64 bool previewWanted() const;
66 string const latexString() const;
68 InsetInclude & parent() const {
69 return *static_cast<InsetInclude*>(inset());
73 bool monitoring() const { return monitor_.get(); }
75 void startMonitoring();
77 void stopMonitoring() { monitor_.reset(); }
80 /// Invoked by monitor_ should the parent file change.
81 void restartLoading();
83 boost::scoped_ptr<FileMonitor> monitor_;
89 string const uniqueID()
91 static unsigned int seed = 1000;
92 return "file" + tostr(++seed);
98 InsetInclude::InsetInclude(Params const & p)
99 : params_(p), include_label(uniqueID()),
100 preview_(new PreviewImpl(*this)),
105 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
106 : include_label(uniqueID()),
107 preview_(new PreviewImpl(*this)),
111 params_.masterFilename_ = b.fileName();
115 InsetInclude::InsetInclude(InsetInclude const & other)
117 params_(other.params_),
118 include_label(other.include_label),
119 preview_(new PreviewImpl(*this)),
120 set_label_(other.set_label_)
124 InsetInclude::~InsetInclude()
126 InsetIncludeMailer mailer(*this);
131 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
133 switch (cmd.action) {
135 case LFUN_INSET_MODIFY: {
136 InsetInclude::Params p;
137 InsetIncludeMailer::string2params(cmd.argument, p);
138 if (!p.cparams.getCmdName().empty()) {
140 params_.masterFilename_ = cmd.view()->buffer()->fileName();
141 cmd.view()->updateInset(this);
146 case LFUN_INSET_DIALOG_UPDATE:
147 InsetIncludeMailer(*this).updateDialog(cmd.view());
150 case LFUN_MOUSE_RELEASE:
151 if (button_.box().contains(cmd.x, cmd.y))
152 InsetIncludeMailer(*this).showDialog(cmd.view());
155 case LFUN_INSET_DIALOG_SHOW:
156 InsetIncludeMailer(*this).showDialog(cmd.view());
160 return InsetOld::localDispatch(cmd);
165 InsetInclude::Params const & InsetInclude::params() const
171 bool InsetInclude::Params::operator==(Params const & o) const
173 return cparams == o.cparams && flag == o.flag &&
174 masterFilename_ == o.masterFilename_;
178 bool InsetInclude::Params::operator!=(Params const & o) const
180 return !(*this == o);
184 void InsetInclude::set(Params const & p)
190 switch (params_.flag) {
195 command="verbatiminput";
201 command="verbatiminput*";
205 params_.cparams.setCmdName(command);
207 if (preview_->monitoring())
208 preview_->stopMonitoring();
210 if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
211 preview_->generatePreview();
215 auto_ptr<InsetBase> InsetInclude::clone() const
218 //p.masterFilename_ = buffer.fileName();
219 #warning FIXME: broken cross-doc copy/paste - must fix
221 return auto_ptr<InsetBase>(new InsetInclude(params_));
225 void InsetInclude::write(Buffer const &, ostream & os) const
231 void InsetInclude::write(ostream & os) const
233 os << "Include " << params_.cparams.getCommand() << '\n'
234 << "preview " << tostr(params_.cparams.preview()) << '\n';
238 void InsetInclude::read(Buffer const &, LyXLex & lex)
244 void InsetInclude::read(LyXLex & lex)
246 params_.cparams.read(lex);
248 if (params_.cparams.getCmdName() == "include")
249 params_.flag = INCLUDE;
250 else if (params_.cparams.getCmdName() == "input")
251 params_.flag = INPUT;
252 /* FIXME: is this logic necessary now ? */
253 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
255 if (params_.cparams.getCmdName() == "verbatiminput*")
256 params_.flag = VERBAST;
261 string const InsetInclude::getScreenLabel(Buffer const &) const
265 switch (params_.flag) {
266 case INPUT: temp += _("Input"); break;
267 case VERB: temp += _("Verbatim Input"); break;
268 case VERBAST: temp += _("Verbatim Input*"); break;
269 case INCLUDE: temp += _("Include"); break;
274 if (params_.cparams.getContents().empty())
277 temp += params_.cparams.getContents();
283 string const InsetInclude::getFileName() const
285 return MakeAbsPath(params_.cparams.getContents(),
286 OnlyPath(getMasterFilename()));
290 string const InsetInclude::getMasterFilename() const
292 return params_.masterFilename_;
296 bool InsetInclude::loadIfNeeded() const
301 if (!IsLyXFilename(getFileName()))
304 if (bufferlist.exists(getFileName()))
307 // the readonly flag can/will be wrong, not anymore I think.
308 FileInfo finfo(getFileName());
311 return loadLyXFile(bufferlist.newBuffer(getFileName()),
316 int InsetInclude::latex(Buffer const & buffer, ostream & os,
317 LatexRunParams const & runparams) const
319 string incfile(params_.cparams.getContents());
321 // Do nothing if no file name has been specified
325 if (loadIfNeeded()) {
326 Buffer * tmp = bufferlist.getBuffer(getFileName());
328 // FIXME: this should be a GUI warning
329 if (tmp->params.textclass != buffer.params.textclass) {
330 lyxerr << "WARNING: Included file `"
331 << MakeDisplayPath(getFileName())
332 << "' has textclass `"
333 << tmp->params.getLyXTextClass().name()
334 << "' while parent file has textclass `"
335 << buffer.params.getLyXTextClass().name()
340 // write it to a file (so far the complete file)
341 string writefile = ChangeExtension(getFileName(), ".tex");
343 if (!buffer.tmppath.empty() && !runparams.nice) {
344 incfile = subst(incfile, '/','@');
346 incfile = subst(incfile, ':', '$');
348 writefile = AddName(buffer.tmppath, incfile);
350 writefile = getFileName();
351 writefile = ChangeExtension(writefile, ".tex");
352 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
353 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
355 tmp->markDepClean(buffer.tmppath);
357 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
362 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
363 } else if (params_.flag == INPUT) {
364 // \input wants file with extension (default is .tex)
365 if (!IsLyXFilename(getFileName())) {
366 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
368 os << '\\' << params_.cparams.getCmdName() << '{'
369 << ChangeExtension(incfile, ".tex")
373 // \include don't want extension and demands that the
374 // file really have .tex
375 os << '\\' << params_.cparams.getCmdName() << '{'
376 << ChangeExtension(incfile, string())
384 int InsetInclude::ascii(Buffer const &, ostream & os, int) const
387 os << GetFileContents(getFileName());
392 int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
394 string incfile(params_.cparams.getContents());
396 // Do nothing if no file name has been specified
400 if (loadIfNeeded()) {
401 Buffer * tmp = bufferlist.getBuffer(getFileName());
403 // write it to a file (so far the complete file)
404 string writefile = ChangeExtension(getFileName(), ".sgml");
405 if (!buffer.tmppath.empty() && !buffer.niceFile) {
406 incfile = subst(incfile, '/','@');
407 writefile = AddName(buffer.tmppath, incfile);
409 writefile = getFileName();
411 if (IsLyXFilename(getFileName()))
412 writefile = ChangeExtension(writefile, ".sgml");
414 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
415 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
417 tmp->makeLinuxDocFile(writefile, buffer.niceFile, true);
422 << GetFileContents(getFileName())
425 os << '&' << include_label << ';';
431 int InsetInclude::docbook(Buffer const & buffer, ostream & os,
432 bool /*mixcont*/) const
434 string incfile(params_.cparams.getContents());
436 // Do nothing if no file name has been specified
440 if (loadIfNeeded()) {
441 Buffer * tmp = bufferlist.getBuffer(getFileName());
443 // write it to a file (so far the complete file)
444 string writefile = ChangeExtension(getFileName(), ".sgml");
445 if (!buffer.tmppath.empty() && !buffer.niceFile) {
446 incfile = subst(incfile, '/','@');
447 writefile = AddName(buffer.tmppath, incfile);
449 writefile = getFileName();
450 if (IsLyXFilename(getFileName()))
451 writefile = ChangeExtension(writefile, ".sgml");
453 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
454 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
456 tmp->makeDocBookFile(writefile, buffer.niceFile, true);
460 os << "<inlinegraphic fileref=\""
461 << '&' << include_label << ';'
462 << "\" format=\"linespecific\">";
464 os << '&' << include_label << ';';
470 void InsetInclude::validate(LaTeXFeatures & features) const
473 string incfile(params_.cparams.getContents());
476 Buffer const & b = *bufferlist.getBuffer(getMasterFilename());
478 if (!b.tmppath.empty() && !b.niceFile && !isVerbatim()) {
479 incfile = subst(incfile, '/','@');
480 writefile = AddName(b.tmppath, incfile);
482 writefile = getFileName();
484 if (IsLyXFilename(getFileName()))
485 writefile = ChangeExtension(writefile, ".sgml");
487 features.includeFile(include_label, writefile);
490 features.require("verbatim");
492 // Here we must do the fun stuff...
493 // Load the file in the include if it needs
495 if (loadIfNeeded()) {
497 Buffer * const tmp = bufferlist.getBuffer(getFileName());
499 tmp->niceFile = b.niceFile;
500 tmp->validate(features);
506 void InsetInclude::getLabelList(std::vector<string> & list) const
508 if (loadIfNeeded()) {
509 Buffer * tmp = bufferlist.getBuffer(getFileName());
510 tmp->setParentName("");
511 tmp->getLabelList(list);
512 tmp->setParentName(getMasterFilename());
517 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
519 if (loadIfNeeded()) {
520 Buffer * tmp = bufferlist.getBuffer(getFileName());
521 tmp->setParentName("");
522 tmp->fillWithBibKeys(keys);
523 tmp->setParentName(getMasterFilename());
528 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
530 if (preview_->previewReady()) {
531 dim.asc = preview_->pimage()->ascent();
532 dim.des = preview_->pimage()->descent();
533 dim.wid = preview_->pimage()->width();
537 button_.update(getScreenLabel(*mi.base.bv->buffer()),
538 editable() != NOT_EDITABLE);
540 button_.metrics(mi, dim);
542 int center_indent = (params_.flag == INPUT ? 0 :
543 (mi.base.textwidth - dim.wid) / 2);
544 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
547 dim.wid = mi.base.textwidth;
552 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
555 if (!preview_->previewReady()) {
556 button_.draw(pi, x + button_.box().x1, y);
560 if (!preview_->monitoring())
561 preview_->startMonitoring();
563 pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
564 *(preview_->pimage()->image()));
568 BufferView * InsetInclude::view() const
570 return button_.view();
578 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
580 preview_->addPreview(ploader);
584 bool InsetInclude::PreviewImpl::previewWanted() const
586 return parent().params_.flag == InsetInclude::INPUT &&
587 parent().params_.cparams.preview() &&
588 IsFileReadable(parent().getFileName());
592 string const InsetInclude::PreviewImpl::latexString() const
594 if (!view() || !view()->buffer())
598 LatexRunParams runparams;
599 runparams.flavor = LatexRunParams::LATEX;
600 parent().latex(*view()->buffer(), os, runparams);
602 return STRCONV(os.str());
606 void InsetInclude::PreviewImpl::startMonitoring()
608 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
609 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
614 void InsetInclude::PreviewImpl::restartLoading()
616 lyxerr << "restartLoading()" << std::endl;
619 view()->updateInset(&parent());
624 string const InsetIncludeMailer::name_("include");
626 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
631 string const InsetIncludeMailer::inset2string(Buffer const &) const
633 return params2string(inset_.params());
637 void InsetIncludeMailer::string2params(string const & in,
638 InsetInclude::Params & params)
640 params = InsetInclude::Params();
645 istringstream data(STRCONV(in));
651 string const token = lex.getString();
656 // This is part of the inset proper that is usually swallowed
657 // by Buffer::readInset
660 string const token = lex.getString();
661 if (token != "Include")
666 InsetInclude inset(params);
668 params = inset.params();
674 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
676 InsetInclude inset(params);
679 data << name_ << ' ';
681 data << "\\end_inset\n";
682 return STRCONV(data.str());