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();
146 case LFUN_INSET_DIALOG_UPDATE:
147 InsetIncludeMailer(*this).updateDialog(cmd.view());
150 case LFUN_MOUSE_RELEASE:
151 case LFUN_INSET_EDIT:
152 InsetIncludeMailer(*this).showDialog(cmd.view());
156 return InsetOld::localDispatch(cmd);
161 InsetInclude::Params const & InsetInclude::params() const
167 bool InsetInclude::Params::operator==(Params const & o) const
169 return cparams == o.cparams && flag == o.flag &&
170 masterFilename_ == o.masterFilename_;
174 bool InsetInclude::Params::operator!=(Params const & o) const
176 return !(*this == o);
180 void InsetInclude::set(Params const & p)
186 switch (params_.flag) {
191 command="verbatiminput";
197 command="verbatiminput*";
201 params_.cparams.setCmdName(command);
203 if (preview_->monitoring())
204 preview_->stopMonitoring();
206 if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
207 preview_->generatePreview();
211 auto_ptr<InsetBase> InsetInclude::clone() const
214 //p.masterFilename_ = buffer.fileName();
215 #warning FIXME: broken cross-doc copy/paste - must fix
217 return auto_ptr<InsetBase>(new InsetInclude(params_));
221 void InsetInclude::write(Buffer const *, ostream & os) const
223 os << "Include " << params_.cparams.getCommand() << '\n'
224 << "preview " << tostr(params_.cparams.preview()) << '\n';
228 void InsetInclude::read(Buffer const *, LyXLex & lex)
230 params_.cparams.read(lex);
232 if (params_.cparams.getCmdName() == "include")
233 params_.flag = INCLUDE;
234 else if (params_.cparams.getCmdName() == "input")
235 params_.flag = INPUT;
236 /* FIXME: is this logic necessary now ? */
237 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
239 if (params_.cparams.getCmdName() == "verbatiminput*")
240 params_.flag = VERBAST;
245 string const InsetInclude::getScreenLabel(Buffer const *) const
249 switch (params_.flag) {
250 case INPUT: temp += _("Input"); break;
251 case VERB: temp += _("Verbatim Input"); break;
252 case VERBAST: temp += _("Verbatim Input*"); break;
253 case INCLUDE: temp += _("Include"); break;
258 if (params_.cparams.getContents().empty())
261 temp += params_.cparams.getContents();
267 string const InsetInclude::getFileName() const
269 return MakeAbsPath(params_.cparams.getContents(),
270 OnlyPath(getMasterFilename()));
274 string const InsetInclude::getMasterFilename() const
276 return params_.masterFilename_;
280 bool InsetInclude::loadIfNeeded() const
285 if (!IsLyXFilename(getFileName()))
288 if (bufferlist.exists(getFileName()))
291 // the readonly flag can/will be wrong, not anymore I think.
292 FileInfo finfo(getFileName());
295 return loadLyXFile(bufferlist.newBuffer(getFileName()),
300 int InsetInclude::latex(Buffer const * buffer, ostream & os,
301 LatexRunParams const & runparams) const
303 string incfile(params_.cparams.getContents());
305 // Do nothing if no file name has been specified
309 if (loadIfNeeded()) {
310 Buffer * tmp = bufferlist.getBuffer(getFileName());
312 // FIXME: this should be a GUI warning
313 if (tmp->params.textclass != buffer->params.textclass) {
314 lyxerr << "WARNING: Included file `"
315 << MakeDisplayPath(getFileName())
316 << "' has textclass `"
317 << tmp->params.getLyXTextClass().name()
318 << "' while parent file has textclass `"
319 << buffer->params.getLyXTextClass().name()
324 // write it to a file (so far the complete file)
325 string writefile = ChangeExtension(getFileName(), ".tex");
327 if (!buffer->tmppath.empty() && !runparams.nice) {
328 incfile = subst(incfile, '/','@');
330 incfile = subst(incfile, ':', '$');
332 writefile = AddName(buffer->tmppath, incfile);
334 writefile = getFileName();
335 writefile = ChangeExtension(writefile, ".tex");
336 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
337 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
339 tmp->markDepClean(buffer->tmppath);
341 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
346 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
347 } else if (params_.flag == INPUT) {
348 // \input wants file with extension (default is .tex)
349 if (!IsLyXFilename(getFileName())) {
350 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
352 os << '\\' << params_.cparams.getCmdName() << '{'
353 << ChangeExtension(incfile, ".tex")
357 // \include don't want extension and demands that the
358 // file really have .tex
359 os << '\\' << params_.cparams.getCmdName() << '{'
360 << ChangeExtension(incfile, string())
368 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
371 os << GetFileContents(getFileName());
376 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
378 string incfile(params_.cparams.getContents());
380 // Do nothing if no file name has been specified
384 if (loadIfNeeded()) {
385 Buffer * tmp = bufferlist.getBuffer(getFileName());
387 // write it to a file (so far the complete file)
388 string writefile = ChangeExtension(getFileName(), ".sgml");
389 if (!buffer->tmppath.empty() && !buffer->niceFile) {
390 incfile = subst(incfile, '/','@');
391 writefile = AddName(buffer->tmppath, incfile);
393 writefile = getFileName();
395 if (IsLyXFilename(getFileName()))
396 writefile = ChangeExtension(writefile, ".sgml");
398 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
399 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
401 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
406 << GetFileContents(getFileName())
409 os << '&' << include_label << ';';
415 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
416 bool /*mixcont*/) const
418 string incfile(params_.cparams.getContents());
420 // Do nothing if no file name has been specified
424 if (loadIfNeeded()) {
425 Buffer * tmp = bufferlist.getBuffer(getFileName());
427 // write it to a file (so far the complete file)
428 string writefile = ChangeExtension(getFileName(), ".sgml");
429 if (!buffer->tmppath.empty() && !buffer->niceFile) {
430 incfile = subst(incfile, '/','@');
431 writefile = AddName(buffer->tmppath, incfile);
433 writefile = getFileName();
434 if (IsLyXFilename(getFileName()))
435 writefile = ChangeExtension(writefile, ".sgml");
437 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
438 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
440 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
444 os << "<inlinegraphic fileref=\""
445 << '&' << include_label << ';'
446 << "\" format=\"linespecific\">";
448 os << '&' << include_label << ';';
454 void InsetInclude::validate(LaTeXFeatures & features) const
457 string incfile(params_.cparams.getContents());
460 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
462 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
463 incfile = subst(incfile, '/','@');
464 writefile = AddName(b->tmppath, incfile);
466 writefile = getFileName();
468 if (IsLyXFilename(getFileName()))
469 writefile = ChangeExtension(writefile, ".sgml");
471 features.includeFile(include_label, writefile);
474 features.require("verbatim");
476 // Here we must do the fun stuff...
477 // Load the file in the include if it needs
479 if (loadIfNeeded()) {
481 Buffer * const tmp = bufferlist.getBuffer(getFileName());
484 tmp->niceFile = b->niceFile;
485 tmp->validate(features);
491 void InsetInclude::getLabelList(std::vector<string> & list) const
493 if (loadIfNeeded()) {
494 Buffer * tmp = bufferlist.getBuffer(getFileName());
495 tmp->setParentName("");
496 tmp->getLabelList(list);
497 tmp->setParentName(getMasterFilename());
502 void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
504 if (loadIfNeeded()) {
505 Buffer * tmp = bufferlist.getBuffer(getFileName());
506 tmp->setParentName("");
507 tmp->fillWithBibKeys(keys);
508 tmp->setParentName(getMasterFilename());
513 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
515 if (preview_->previewReady()) {
516 dim.asc = preview_->pimage()->ascent();
517 dim.des = preview_->pimage()->descent();
518 dim.wid = preview_->pimage()->width();
522 button_.update(getScreenLabel(mi.base.bv->buffer()),
523 editable() != NOT_EDITABLE);
525 button_.metrics(mi, dim);
527 if (params_.flag == INPUT)
530 center_indent_ = (mi.base.textwidth - dim.wid) / 2;
531 dim.wid = mi.base.textwidth;
536 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
539 if (!preview_->previewReady()) {
540 button_.draw(pi, x + center_indent_, y);
544 if (!preview_->monitoring())
545 preview_->startMonitoring();
547 pi.pain.image(x + center_indent_, y - dim_.asc, dim_.wid, dim_.height(),
548 *(preview_->pimage()->image()));
552 BufferView * InsetInclude::view() const
554 return button_.view();
562 void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
564 preview_->addPreview(ploader);
568 bool InsetInclude::PreviewImpl::previewWanted() const
570 return parent().params_.flag == InsetInclude::INPUT &&
571 parent().params_.cparams.preview() &&
572 IsFileReadable(parent().getFileName());
576 string const InsetInclude::PreviewImpl::latexString() const
578 if (!view() || !view()->buffer())
582 LatexRunParams runparams;
583 runparams.flavor = LatexRunParams::LATEX;
584 parent().latex(view()->buffer(), os, runparams);
586 return STRCONV(os.str());
590 void InsetInclude::PreviewImpl::startMonitoring()
592 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
593 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
598 void InsetInclude::PreviewImpl::restartLoading()
600 lyxerr << "restartLoading()" << std::endl;
603 view()->updateInset();
608 string const InsetIncludeMailer::name_("include");
610 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
615 string const InsetIncludeMailer::inset2string(Buffer const &) const
617 return params2string(inset_.params());
621 void InsetIncludeMailer::string2params(string const & in,
622 InsetInclude::Params & params)
624 params = InsetInclude::Params();
629 istringstream data(STRCONV(in));
635 string const token = lex.getString();
640 // This is part of the inset proper that is usually swallowed
641 // by Buffer::readInset
644 string const token = lex.getString();
645 if (token != "Include")
650 InsetInclude inset(params);
652 params = inset.params();
658 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
660 InsetInclude inset(params);
663 data << name_ << ' ';
664 inset.write(0, data);
665 data << "\\end_inset\n";
666 return STRCONV(data.str());