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"
24 #include "frontends/Dialogs.h"
25 #include "frontends/LyXView.h"
26 #include "frontends/Painter.h"
28 #include "support/filetools.h"
29 #include "support/FileInfo.h"
30 #include "support/FileMonitor.h"
31 #include "support/lstrings.h"
33 #include "graphics/PreviewedInset.h"
34 #include "graphics/PreviewImage.h"
36 #include <boost/bind.hpp>
46 extern BufferList bufferlist;
49 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
52 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
55 bool previewWanted() const;
57 string const latexString() const;
59 InsetInclude & parent() const {
60 return *static_cast<InsetInclude*>(inset());
64 bool monitoring() const { return monitor_.get(); }
66 void startMonitoring();
68 void stopMonitoring() { monitor_.reset(); }
71 /// Invoked by monitor_ should the parent file change.
72 void restartLoading();
74 boost::scoped_ptr<FileMonitor> monitor_;
80 string const uniqueID()
82 static unsigned int seed = 1000;
85 ost << "file" << ++seed;
87 // Needed if we use lyxstring.
88 return STRCONV(ost.str());
94 InsetInclude::InsetInclude(Params const & p)
95 : params_(p), include_label(uniqueID()),
96 preview_(new PreviewImpl(*this))
100 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
101 : include_label(uniqueID()),
102 preview_(new PreviewImpl(*this))
105 params_.masterFilename_ = b.fileName();
109 InsetInclude::~InsetInclude()
111 InsetIncludeMailer mailer(*this);
116 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
118 if (cmd.action != LFUN_INSET_MODIFY)
121 InsetInclude::Params p;
122 InsetIncludeMailer::string2params(cmd.argument, p);
123 if (p.cparams.getCmdName().empty())
127 params_.masterFilename_ = cmd.view()->buffer()->fileName();
129 cmd.view()->updateInset(this, true);
134 InsetInclude::Params const & InsetInclude::params() const
140 bool InsetInclude::Params::operator==(Params const & o) const
142 if (cparams == o.cparams && flag == o.flag &&
143 masterFilename_ == o.masterFilename_)
150 bool InsetInclude::Params::operator!=(Params const & o) const
152 return !(*this == o);
156 void InsetInclude::set(Params const & p)
162 switch (params_.flag) {
167 command="verbatiminput";
173 command="verbatiminput*";
177 params_.cparams.setCmdName(command);
179 if (preview_->monitoring())
180 preview_->stopMonitoring();
182 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
183 preview_->generatePreview();
187 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
190 p.masterFilename_ = buffer.fileName();
192 return new InsetInclude(p);
196 void InsetInclude::edit(BufferView *, int, int, mouse_button::state)
198 InsetIncludeMailer mailer(*this);
203 void InsetInclude::edit(BufferView * bv, bool)
205 edit(bv, 0, 0, mouse_button::none);
209 void InsetInclude::write(Buffer const *, ostream & os) const
211 os << "Include " << params_.cparams.getCommand() << '\n'
212 << "preview " << tostr(params_.cparams.preview()) << '\n';
216 void InsetInclude::read(Buffer const *, LyXLex & lex)
218 params_.cparams.read(lex);
220 if (params_.cparams.getCmdName() == "include")
221 params_.flag = INCLUDE;
222 else if (params_.cparams.getCmdName() == "input")
223 params_.flag = INPUT;
224 /* FIXME: is this logic necessary now ? */
225 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
227 if (params_.cparams.getCmdName() == "verbatiminput*")
228 params_.flag = VERBAST;
233 bool InsetInclude::display() const
235 return !(params_.flag == INPUT);
239 string const InsetInclude::getScreenLabel(Buffer const *) const
243 switch (params_.flag) {
244 case INPUT: temp += _("Input"); break;
245 case VERB: temp += _("Verbatim Input"); break;
246 case VERBAST: temp += _("Verbatim Input*"); break;
247 case INCLUDE: temp += _("Include"); break;
252 if (params_.cparams.getContents().empty())
255 temp += params_.cparams.getContents();
261 string const InsetInclude::getRelFileBaseName() const
263 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
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());
296 return bufferlist.loadLyXFile(getFileName(), false) != 0;
300 int InsetInclude::latex(Buffer const * buffer, ostream & os,
301 bool /*fragile*/, bool /*fs*/) 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()
328 && !buffer->niceFile) {
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,
343 OnlyPath(getMasterFilename()),
344 buffer->niceFile, true);
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 vector<string> const InsetInclude::getLabelList() const
497 if (loadIfNeeded()) {
498 Buffer * tmp = bufferlist.getBuffer(getFileName());
499 tmp->setParentName("");
500 l = tmp->getLabelList();
501 tmp->setParentName(getMasterFilename());
508 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
510 if (loadIfNeeded()) {
511 Buffer * tmp = bufferlist.getBuffer(getFileName());
512 tmp->setParentName("");
513 tmp->fillWithBibKeys(keys);
514 tmp->setParentName(getMasterFilename());
519 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
521 return preview_->previewReady() ?
522 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
526 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
528 return preview_->previewReady() ?
529 preview_->pimage()->descent() : InsetButton::descent(bv, font);
533 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
535 return preview_->previewReady() ?
536 preview_->pimage()->width() : InsetButton::width(bv, font);
540 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
541 float & xx, bool b) const
544 if (!preview_->previewReady()) {
545 InsetButton::draw(bv, font, y, xx, b);
549 if (!preview_->monitoring())
550 preview_->startMonitoring();
552 int const x = int(xx);
553 int const w = width(bv, font);
554 int const d = descent(bv, font);
555 int const a = ascent(bv, font);
558 bv->painter().image(x, y - a, w, h,
559 *(preview_->pimage()->image()));
569 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
571 preview_->addPreview(ploader);
575 bool InsetInclude::PreviewImpl::previewWanted() const
577 return parent().params_.flag == InsetInclude::INPUT &&
578 parent().params_.cparams.preview() &&
579 IsFileReadable(parent().getFileName());
583 string const InsetInclude::PreviewImpl::latexString() const
585 if (!view() || !view()->buffer())
589 parent().latex(view()->buffer(), os, false, false);
591 return STRCONV(os.str());
595 void InsetInclude::PreviewImpl::startMonitoring()
597 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
598 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
603 void InsetInclude::PreviewImpl::restartLoading()
605 lyxerr << "restartLoading()" << std::endl;
608 view()->updateInset(&parent(), false);
613 string const InsetIncludeMailer::name_("include");
615 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
620 string const InsetIncludeMailer::inset2string() const
622 return params2string(inset_.params());
626 void InsetIncludeMailer::string2params(string const & in,
627 InsetInclude::Params & params)
629 params = InsetInclude::Params();
631 istringstream data(in);
637 string const token = lex.getString();
642 // This is part of the inset proper that is usually swallowed
643 // by Buffer::readInset
646 string const token = lex.getString();
647 if (token != "Include")
651 InsetInclude inset(params);
653 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";