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"
27 #include "frontends/Dialogs.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Painter.h"
31 #include "support/filetools.h"
32 #include "support/FileInfo.h"
33 #include "support/FileMonitor.h"
34 #include "support/lstrings.h" // contains
35 #include "support/tostr.h"
37 #include "graphics/PreviewedInset.h"
38 #include "graphics/PreviewImage.h"
40 #include <boost/bind.hpp>
49 extern BufferList bufferlist;
52 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
55 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
58 bool previewWanted() const;
60 string const latexString() const;
62 InsetInclude & parent() const {
63 return *static_cast<InsetInclude*>(inset());
67 bool monitoring() const { return monitor_.get(); }
69 void startMonitoring();
71 void stopMonitoring() { monitor_.reset(); }
74 /// Invoked by monitor_ should the parent file change.
75 void restartLoading();
77 boost::scoped_ptr<FileMonitor> monitor_;
83 string const uniqueID()
85 static unsigned int seed = 1000;
86 return "file" + tostr(++seed);
92 InsetInclude::InsetInclude(Params const & p)
93 : params_(p), include_label(uniqueID()),
94 preview_(new PreviewImpl(*this))
98 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
99 : include_label(uniqueID()),
100 preview_(new PreviewImpl(*this))
103 params_.masterFilename_ = b.fileName();
107 InsetInclude::~InsetInclude()
109 InsetIncludeMailer mailer(*this);
114 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
116 switch (cmd.action) {
118 case LFUN_INSET_MODIFY: {
119 InsetInclude::Params p;
120 InsetIncludeMailer::string2params(cmd.argument, p);
121 if (!p.cparams.getCmdName().empty()) {
123 params_.masterFilename_ = cmd.view()->buffer()->fileName();
124 cmd.view()->updateInset(this);
129 case LFUN_INSET_DIALOG_UPDATE:
130 InsetIncludeMailer(*this).updateDialog(cmd.view());
133 case LFUN_MOUSE_RELEASE:
134 case LFUN_INSET_EDIT:
135 InsetIncludeMailer(*this).showDialog(cmd.view());
144 InsetInclude::Params const & InsetInclude::params() const
150 bool InsetInclude::Params::operator==(Params const & o) const
152 return cparams == o.cparams && flag == o.flag &&
153 masterFilename_ == o.masterFilename_;
157 bool InsetInclude::Params::operator!=(Params const & o) const
159 return !(*this == o);
163 void InsetInclude::set(Params const & p)
169 switch (params_.flag) {
174 command="verbatiminput";
180 command="verbatiminput*";
184 params_.cparams.setCmdName(command);
186 if (preview_->monitoring())
187 preview_->stopMonitoring();
189 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
190 preview_->generatePreview();
194 Inset * InsetInclude::clone(Buffer const & buffer) const
197 p.masterFilename_ = buffer.fileName();
199 return new InsetInclude(p);
203 // Inset * InsetInclude::clone(Buffer const & buffer, bool) const
205 // Params p(params_);
206 // p.masterFilename_ = buffer.fileName();
208 // return new InsetInclude(p);
212 void InsetInclude::write(Buffer const *, ostream & os) const
214 os << "Include " << params_.cparams.getCommand() << '\n'
215 << "preview " << tostr(params_.cparams.preview()) << '\n';
219 void InsetInclude::read(Buffer const *, LyXLex & lex)
221 params_.cparams.read(lex);
223 if (params_.cparams.getCmdName() == "include")
224 params_.flag = INCLUDE;
225 else if (params_.cparams.getCmdName() == "input")
226 params_.flag = INPUT;
227 /* FIXME: is this logic necessary now ? */
228 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
230 if (params_.cparams.getCmdName() == "verbatiminput*")
231 params_.flag = VERBAST;
236 bool InsetInclude::display() const
238 return !(params_.flag == INPUT);
242 string const InsetInclude::getScreenLabel(Buffer const *) const
246 switch (params_.flag) {
247 case INPUT: temp += _("Input"); break;
248 case VERB: temp += _("Verbatim Input"); break;
249 case VERBAST: temp += _("Verbatim Input*"); break;
250 case INCLUDE: temp += _("Include"); break;
255 if (params_.cparams.getContents().empty())
258 temp += params_.cparams.getContents();
264 string const InsetInclude::getFileName() const
266 return MakeAbsPath(params_.cparams.getContents(),
267 OnlyPath(getMasterFilename()));
271 string const InsetInclude::getMasterFilename() const
273 return params_.masterFilename_;
277 bool InsetInclude::loadIfNeeded() const
282 if (!IsLyXFilename(getFileName()))
285 if (bufferlist.exists(getFileName()))
288 // the readonly flag can/will be wrong, not anymore I think.
289 FileInfo finfo(getFileName());
293 return bufferlist.loadLyXFile(getFileName(), false) != 0;
297 int InsetInclude::latex(Buffer const * buffer, ostream & os,
298 LatexRunParams const & runparams) const
300 string incfile(params_.cparams.getContents());
302 // Do nothing if no file name has been specified
306 if (loadIfNeeded()) {
307 Buffer * tmp = bufferlist.getBuffer(getFileName());
309 // FIXME: this should be a GUI warning
310 if (tmp->params.textclass != buffer->params.textclass) {
311 lyxerr << "WARNING: Included file `"
312 << MakeDisplayPath(getFileName())
313 << "' has textclass `"
314 << tmp->params.getLyXTextClass().name()
315 << "' while parent file has textclass `"
316 << buffer->params.getLyXTextClass().name()
321 // write it to a file (so far the complete file)
322 string writefile = ChangeExtension(getFileName(), ".tex");
324 if (!buffer->tmppath.empty() && !runparams.nice) {
325 incfile = subst(incfile, '/','@');
327 incfile = subst(incfile, ':', '$');
329 writefile = AddName(buffer->tmppath, incfile);
331 writefile = getFileName();
332 writefile = ChangeExtension(writefile, ".tex");
333 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
334 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
336 tmp->markDepClean(buffer->tmppath);
338 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
343 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
344 } else if (params_.flag == INPUT) {
345 // \input wants file with extension (default is .tex)
346 if (!IsLyXFilename(getFileName())) {
347 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
349 os << '\\' << params_.cparams.getCmdName() << '{'
350 << ChangeExtension(incfile, ".tex")
354 // \include don't want extension and demands that the
355 // file really have .tex
356 os << '\\' << params_.cparams.getCmdName() << '{'
357 << ChangeExtension(incfile, string())
365 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
368 os << GetFileContents(getFileName());
373 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
375 string incfile(params_.cparams.getContents());
377 // Do nothing if no file name has been specified
381 if (loadIfNeeded()) {
382 Buffer * tmp = bufferlist.getBuffer(getFileName());
384 // write it to a file (so far the complete file)
385 string writefile = ChangeExtension(getFileName(), ".sgml");
386 if (!buffer->tmppath.empty() && !buffer->niceFile) {
387 incfile = subst(incfile, '/','@');
388 writefile = AddName(buffer->tmppath, incfile);
390 writefile = getFileName();
392 if (IsLyXFilename(getFileName()))
393 writefile = ChangeExtension(writefile, ".sgml");
395 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
396 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
398 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
403 << GetFileContents(getFileName())
406 os << '&' << include_label << ';';
412 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
413 bool /*mixcont*/) const
415 string incfile(params_.cparams.getContents());
417 // Do nothing if no file name has been specified
421 if (loadIfNeeded()) {
422 Buffer * tmp = bufferlist.getBuffer(getFileName());
424 // write it to a file (so far the complete file)
425 string writefile = ChangeExtension(getFileName(), ".sgml");
426 if (!buffer->tmppath.empty() && !buffer->niceFile) {
427 incfile = subst(incfile, '/','@');
428 writefile = AddName(buffer->tmppath, incfile);
430 writefile = getFileName();
431 if (IsLyXFilename(getFileName()))
432 writefile = ChangeExtension(writefile, ".sgml");
434 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
435 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
437 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
441 os << "<inlinegraphic fileref=\""
442 << '&' << include_label << ';'
443 << "\" format=\"linespecific\">";
445 os << '&' << include_label << ';';
451 void InsetInclude::validate(LaTeXFeatures & features) const
454 string incfile(params_.cparams.getContents());
457 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
459 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
460 incfile = subst(incfile, '/','@');
461 writefile = AddName(b->tmppath, incfile);
463 writefile = getFileName();
465 if (IsLyXFilename(getFileName()))
466 writefile = ChangeExtension(writefile, ".sgml");
468 features.includeFile(include_label, writefile);
471 features.require("verbatim");
473 // Here we must do the fun stuff...
474 // Load the file in the include if it needs
476 if (loadIfNeeded()) {
478 Buffer * const tmp = bufferlist.getBuffer(getFileName());
481 tmp->niceFile = b->niceFile;
482 tmp->validate(features);
488 vector<string> const InsetInclude::getLabelList() const
492 if (loadIfNeeded()) {
493 Buffer * tmp = bufferlist.getBuffer(getFileName());
494 tmp->setParentName("");
495 l = tmp->getLabelList();
496 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 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
516 return preview_->previewReady() ?
517 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
521 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
523 return preview_->previewReady() ?
524 preview_->pimage()->descent() : InsetButton::descent(bv, font);
528 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
530 return preview_->previewReady() ?
531 preview_->pimage()->width() : InsetButton::width(bv, font);
535 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
539 if (!preview_->previewReady()) {
540 InsetButton::draw(bv, font, y, xx);
544 if (!preview_->monitoring())
545 preview_->startMonitoring();
547 int const x = int(xx);
548 int const w = width(bv, font);
549 int const d = descent(bv, font);
550 int const a = ascent(bv, font);
553 bv->painter().image(x, y - a, w, h,
554 *(preview_->pimage()->image()));
564 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
566 preview_->addPreview(ploader);
570 bool InsetInclude::PreviewImpl::previewWanted() const
572 return parent().params_.flag == InsetInclude::INPUT &&
573 parent().params_.cparams.preview() &&
574 IsFileReadable(parent().getFileName());
578 string const InsetInclude::PreviewImpl::latexString() const
580 if (!view() || !view()->buffer())
584 LatexRunParams runparams;
585 runparams.flavor = LatexRunParams::LATEX;
586 parent().latex(view()->buffer(), os, runparams);
588 return STRCONV(os.str());
592 void InsetInclude::PreviewImpl::startMonitoring()
594 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
595 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
600 void InsetInclude::PreviewImpl::restartLoading()
602 lyxerr << "restartLoading()" << std::endl;
605 view()->updateInset(&parent());
610 string const InsetIncludeMailer::name_("include");
612 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
617 string const InsetIncludeMailer::inset2string() const
619 return params2string(inset_.params());
623 void InsetIncludeMailer::string2params(string const & in,
624 InsetInclude::Params & params)
626 params = InsetInclude::Params();
631 istringstream data(STRCONV(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")
652 InsetInclude inset(params);
654 params = inset.params();
660 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
662 InsetInclude inset(params);
665 data << name_ << ' ';
666 inset.write(0, data);
667 data << "\\end_inset\n";
668 return STRCONV(data.str());