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;
53 extern BufferList bufferlist;
56 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
59 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
62 bool previewWanted() const;
64 string const latexString() const;
66 InsetInclude & parent() const {
67 return *static_cast<InsetInclude*>(inset());
71 bool monitoring() const { return monitor_.get(); }
73 void startMonitoring();
75 void stopMonitoring() { monitor_.reset(); }
78 /// Invoked by monitor_ should the parent file change.
79 void restartLoading();
81 boost::scoped_ptr<FileMonitor> monitor_;
87 string const uniqueID()
89 static unsigned int seed = 1000;
90 return "file" + tostr(++seed);
96 InsetInclude::InsetInclude(Params const & p)
97 : params_(p), include_label(uniqueID()),
98 preview_(new PreviewImpl(*this)),
103 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
104 : include_label(uniqueID()),
105 preview_(new PreviewImpl(*this)),
109 params_.masterFilename_ = b.fileName();
113 InsetInclude::InsetInclude(InsetInclude const & other)
115 params_(other.params_),
116 include_label(other.include_label),
117 preview_(new PreviewImpl(*this)),
118 set_label_(other.set_label_)
122 InsetInclude::~InsetInclude()
124 InsetIncludeMailer mailer(*this);
129 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
131 switch (cmd.action) {
133 case LFUN_INSET_MODIFY: {
134 InsetInclude::Params p;
135 InsetIncludeMailer::string2params(cmd.argument, p);
136 if (!p.cparams.getCmdName().empty()) {
138 params_.masterFilename_ = cmd.view()->buffer()->fileName();
139 cmd.view()->updateInset(this);
144 case LFUN_INSET_DIALOG_UPDATE:
145 InsetIncludeMailer(*this).updateDialog(cmd.view());
148 case LFUN_MOUSE_RELEASE:
149 case LFUN_INSET_EDIT:
150 InsetIncludeMailer(*this).showDialog(cmd.view());
154 return Inset::localDispatch(cmd);
159 InsetInclude::Params const & InsetInclude::params() const
165 bool InsetInclude::Params::operator==(Params const & o) const
167 return cparams == o.cparams && flag == o.flag &&
168 masterFilename_ == o.masterFilename_;
172 bool InsetInclude::Params::operator!=(Params const & o) const
174 return !(*this == o);
178 void InsetInclude::set(Params const & p)
184 switch (params_.flag) {
189 command="verbatiminput";
195 command="verbatiminput*";
199 params_.cparams.setCmdName(command);
201 if (preview_->monitoring())
202 preview_->stopMonitoring();
204 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
205 preview_->generatePreview();
209 InsetBase * InsetInclude::clone() const
212 //p.masterFilename_ = buffer.fileName();
213 #warning FIXME: broken cross-doc copy/paste - must fix
215 return new InsetInclude(params_);
219 void InsetInclude::write(Buffer const *, ostream & os) const
221 os << "Include " << params_.cparams.getCommand() << '\n'
222 << "preview " << tostr(params_.cparams.preview()) << '\n';
226 void InsetInclude::read(Buffer const *, LyXLex & lex)
228 params_.cparams.read(lex);
230 if (params_.cparams.getCmdName() == "include")
231 params_.flag = INCLUDE;
232 else if (params_.cparams.getCmdName() == "input")
233 params_.flag = INPUT;
234 /* FIXME: is this logic necessary now ? */
235 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
237 if (params_.cparams.getCmdName() == "verbatiminput*")
238 params_.flag = VERBAST;
243 bool InsetInclude::display() const
245 return !(params_.flag == INPUT);
249 string const InsetInclude::getScreenLabel(Buffer const *) const
253 switch (params_.flag) {
254 case INPUT: temp += _("Input"); break;
255 case VERB: temp += _("Verbatim Input"); break;
256 case VERBAST: temp += _("Verbatim Input*"); break;
257 case INCLUDE: temp += _("Include"); break;
262 if (params_.cparams.getContents().empty())
265 temp += params_.cparams.getContents();
271 string const InsetInclude::getFileName() const
273 return MakeAbsPath(params_.cparams.getContents(),
274 OnlyPath(getMasterFilename()));
278 string const InsetInclude::getMasterFilename() const
280 return params_.masterFilename_;
284 bool InsetInclude::loadIfNeeded() const
289 if (!IsLyXFilename(getFileName()))
292 if (bufferlist.exists(getFileName()))
295 // the readonly flag can/will be wrong, not anymore I think.
296 FileInfo finfo(getFileName());
299 return loadLyXFile(bufferlist.newBuffer(getFileName()),
304 int InsetInclude::latex(Buffer const * buffer, ostream & os,
305 LatexRunParams const & runparams) const
307 string incfile(params_.cparams.getContents());
309 // Do nothing if no file name has been specified
313 if (loadIfNeeded()) {
314 Buffer * tmp = bufferlist.getBuffer(getFileName());
316 // FIXME: this should be a GUI warning
317 if (tmp->params.textclass != buffer->params.textclass) {
318 lyxerr << "WARNING: Included file `"
319 << MakeDisplayPath(getFileName())
320 << "' has textclass `"
321 << tmp->params.getLyXTextClass().name()
322 << "' while parent file has textclass `"
323 << buffer->params.getLyXTextClass().name()
328 // write it to a file (so far the complete file)
329 string writefile = ChangeExtension(getFileName(), ".tex");
331 if (!buffer->tmppath.empty() && !runparams.nice) {
332 incfile = subst(incfile, '/','@');
334 incfile = subst(incfile, ':', '$');
336 writefile = AddName(buffer->tmppath, incfile);
338 writefile = getFileName();
339 writefile = ChangeExtension(writefile, ".tex");
340 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
341 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
343 tmp->markDepClean(buffer->tmppath);
345 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
350 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
351 } else if (params_.flag == INPUT) {
352 // \input wants file with extension (default is .tex)
353 if (!IsLyXFilename(getFileName())) {
354 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
356 os << '\\' << params_.cparams.getCmdName() << '{'
357 << ChangeExtension(incfile, ".tex")
361 // \include don't want extension and demands that the
362 // file really have .tex
363 os << '\\' << params_.cparams.getCmdName() << '{'
364 << ChangeExtension(incfile, string())
372 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
375 os << GetFileContents(getFileName());
380 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
382 string incfile(params_.cparams.getContents());
384 // Do nothing if no file name has been specified
388 if (loadIfNeeded()) {
389 Buffer * tmp = bufferlist.getBuffer(getFileName());
391 // write it to a file (so far the complete file)
392 string writefile = ChangeExtension(getFileName(), ".sgml");
393 if (!buffer->tmppath.empty() && !buffer->niceFile) {
394 incfile = subst(incfile, '/','@');
395 writefile = AddName(buffer->tmppath, incfile);
397 writefile = getFileName();
399 if (IsLyXFilename(getFileName()))
400 writefile = ChangeExtension(writefile, ".sgml");
402 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
403 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
405 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
410 << GetFileContents(getFileName())
413 os << '&' << include_label << ';';
419 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
420 bool /*mixcont*/) const
422 string incfile(params_.cparams.getContents());
424 // Do nothing if no file name has been specified
428 if (loadIfNeeded()) {
429 Buffer * tmp = bufferlist.getBuffer(getFileName());
431 // write it to a file (so far the complete file)
432 string writefile = ChangeExtension(getFileName(), ".sgml");
433 if (!buffer->tmppath.empty() && !buffer->niceFile) {
434 incfile = subst(incfile, '/','@');
435 writefile = AddName(buffer->tmppath, incfile);
437 writefile = getFileName();
438 if (IsLyXFilename(getFileName()))
439 writefile = ChangeExtension(writefile, ".sgml");
441 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
442 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
444 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
448 os << "<inlinegraphic fileref=\""
449 << '&' << include_label << ';'
450 << "\" format=\"linespecific\">";
452 os << '&' << include_label << ';';
458 void InsetInclude::validate(LaTeXFeatures & features) const
461 string incfile(params_.cparams.getContents());
464 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
466 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
467 incfile = subst(incfile, '/','@');
468 writefile = AddName(b->tmppath, incfile);
470 writefile = getFileName();
472 if (IsLyXFilename(getFileName()))
473 writefile = ChangeExtension(writefile, ".sgml");
475 features.includeFile(include_label, writefile);
478 features.require("verbatim");
480 // Here we must do the fun stuff...
481 // Load the file in the include if it needs
483 if (loadIfNeeded()) {
485 Buffer * const tmp = bufferlist.getBuffer(getFileName());
488 tmp->niceFile = b->niceFile;
489 tmp->validate(features);
495 void InsetInclude::getLabelList(std::vector<string> & list) const
497 if (loadIfNeeded()) {
498 Buffer * tmp = bufferlist.getBuffer(getFileName());
499 tmp->setParentName("");
500 tmp->getLabelList(list);
501 tmp->setParentName(getMasterFilename());
506 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
508 if (loadIfNeeded()) {
509 Buffer * tmp = bufferlist.getBuffer(getFileName());
510 tmp->setParentName("");
511 tmp->fillWithBibKeys(keys);
512 tmp->setParentName(getMasterFilename());
517 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
519 if (preview_->previewReady()) {
520 dim.asc = preview_->pimage()->ascent();
521 dim.des = preview_->pimage()->descent();
522 dim.wid = preview_->pimage()->width();
526 button_.update(getScreenLabel(mi.base.bv->buffer()),
527 editable() != NOT_EDITABLE);
529 button_.metrics(mi, dim);
535 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
538 if (!preview_->previewReady()) {
539 button_.draw(pi, x, y);
543 if (!preview_->monitoring())
544 preview_->startMonitoring();
546 pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
547 *(preview_->pimage()->image()));
551 BufferView * InsetInclude::view() const
553 return button_.view();
561 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
563 preview_->addPreview(ploader);
567 bool InsetInclude::PreviewImpl::previewWanted() const
569 return parent().params_.flag == InsetInclude::INPUT &&
570 parent().params_.cparams.preview() &&
571 IsFileReadable(parent().getFileName());
575 string const InsetInclude::PreviewImpl::latexString() const
577 if (!view() || !view()->buffer())
581 LatexRunParams runparams;
582 runparams.flavor = LatexRunParams::LATEX;
583 parent().latex(view()->buffer(), os, runparams);
585 return STRCONV(os.str());
589 void InsetInclude::PreviewImpl::startMonitoring()
591 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
592 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
597 void InsetInclude::PreviewImpl::restartLoading()
599 lyxerr << "restartLoading()" << std::endl;
602 view()->updateInset(&parent());
607 string const InsetIncludeMailer::name_("include");
609 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
614 string const InsetIncludeMailer::inset2string() const
616 return params2string(inset_.params());
620 void InsetIncludeMailer::string2params(string const & in,
621 InsetInclude::Params & params)
623 params = InsetInclude::Params();
628 istringstream data(STRCONV(in));
634 string const token = lex.getString();
639 // This is part of the inset proper that is usually swallowed
640 // by Buffer::readInset
643 string const token = lex.getString();
644 if (token != "Include")
649 InsetInclude inset(params);
651 params = inset.params();
657 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
659 InsetInclude inset(params);
662 data << name_ << ' ';
663 inset.write(0, data);
664 data << "\\end_inset\n";
665 return STRCONV(data.str());