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 dispatch_result result = UNDISPATCHED;
120 switch (cmd.action) {
121 case LFUN_INSET_MODIFY: {
122 InsetInclude::Params p;
123 InsetIncludeMailer::string2params(cmd.argument, p);
124 if (p.cparams.getCmdName().empty())
128 params_.masterFilename_ = cmd.view()->buffer()->fileName();
130 cmd.view()->updateInset(this);
135 case LFUN_INSET_DIALOG_UPDATE: {
136 InsetIncludeMailer mailer(*this);
137 mailer.updateDialog(cmd.view());
141 case LFUN_MOUSE_RELEASE:
142 edit(cmd.view(), cmd.x, cmd.y, cmd.button());
153 InsetInclude::Params const & InsetInclude::params() const
159 bool InsetInclude::Params::operator==(Params const & o) const
161 if (cparams == o.cparams && flag == o.flag &&
162 masterFilename_ == o.masterFilename_)
169 bool InsetInclude::Params::operator!=(Params const & o) const
171 return !(*this == o);
175 void InsetInclude::set(Params const & p)
181 switch (params_.flag) {
186 command="verbatiminput";
192 command="verbatiminput*";
196 params_.cparams.setCmdName(command);
198 if (preview_->monitoring())
199 preview_->stopMonitoring();
201 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
202 preview_->generatePreview();
206 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
209 p.masterFilename_ = buffer.fileName();
211 return new InsetInclude(p);
215 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
217 InsetIncludeMailer mailer(*this);
218 mailer.showDialog(bv);
222 void InsetInclude::edit(BufferView * bv, bool)
224 edit(bv, 0, 0, mouse_button::none);
228 void InsetInclude::write(Buffer const *, ostream & os) const
230 os << "Include " << params_.cparams.getCommand() << '\n'
231 << "preview " << tostr(params_.cparams.preview()) << '\n';
235 void InsetInclude::read(Buffer const *, LyXLex & lex)
237 params_.cparams.read(lex);
239 if (params_.cparams.getCmdName() == "include")
240 params_.flag = INCLUDE;
241 else if (params_.cparams.getCmdName() == "input")
242 params_.flag = INPUT;
243 /* FIXME: is this logic necessary now ? */
244 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
246 if (params_.cparams.getCmdName() == "verbatiminput*")
247 params_.flag = VERBAST;
252 bool InsetInclude::display() const
254 return !(params_.flag == INPUT);
258 string const InsetInclude::getScreenLabel(Buffer const *) const
262 switch (params_.flag) {
263 case INPUT: temp += _("Input"); break;
264 case VERB: temp += _("Verbatim Input"); break;
265 case VERBAST: temp += _("Verbatim Input*"); break;
266 case INCLUDE: temp += _("Include"); break;
271 if (params_.cparams.getContents().empty())
274 temp += params_.cparams.getContents();
280 string const InsetInclude::getFileName() const
282 return MakeAbsPath(params_.cparams.getContents(),
283 OnlyPath(getMasterFilename()));
287 string const InsetInclude::getMasterFilename() const
289 return params_.masterFilename_;
293 bool InsetInclude::loadIfNeeded() const
298 if (!IsLyXFilename(getFileName()))
301 if (bufferlist.exists(getFileName()))
304 // the readonly flag can/will be wrong, not anymore I think.
305 FileInfo finfo(getFileName());
309 return bufferlist.loadLyXFile(getFileName(), false) != 0;
313 int InsetInclude::latex(Buffer const * buffer, ostream & os,
314 bool /*fragile*/, bool /*fs*/) const
316 string incfile(params_.cparams.getContents());
318 // Do nothing if no file name has been specified
322 if (loadIfNeeded()) {
323 Buffer * tmp = bufferlist.getBuffer(getFileName());
325 // FIXME: this should be a GUI warning
326 if (tmp->params.textclass != buffer->params.textclass) {
327 lyxerr << "WARNING: Included file `"
328 << MakeDisplayPath(getFileName())
329 << "' has textclass `"
330 << tmp->params.getLyXTextClass().name()
331 << "' while parent file has textclass `"
332 << buffer->params.getLyXTextClass().name()
337 // write it to a file (so far the complete file)
338 string writefile = ChangeExtension(getFileName(), ".tex");
340 if (!buffer->tmppath.empty()
341 && !buffer->niceFile) {
342 incfile = subst(incfile, '/','@');
344 incfile = subst(incfile, ':', '$');
346 writefile = AddName(buffer->tmppath, incfile);
348 writefile = getFileName();
349 writefile = ChangeExtension(writefile, ".tex");
350 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
351 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
353 tmp->markDepClean(buffer->tmppath);
355 tmp->makeLaTeXFile(writefile,
356 OnlyPath(getMasterFilename()),
357 buffer->niceFile, true);
361 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
362 } else if (params_.flag == INPUT) {
363 // \input wants file with extension (default is .tex)
364 if (!IsLyXFilename(getFileName())) {
365 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
367 os << '\\' << params_.cparams.getCmdName() << '{'
368 << ChangeExtension(incfile, ".tex")
372 // \include don't want extension and demands that the
373 // file really have .tex
374 os << '\\' << params_.cparams.getCmdName() << '{'
375 << ChangeExtension(incfile, string())
383 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
386 os << GetFileContents(getFileName());
391 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
393 string incfile(params_.cparams.getContents());
395 // Do nothing if no file name has been specified
399 if (loadIfNeeded()) {
400 Buffer * tmp = bufferlist.getBuffer(getFileName());
402 // write it to a file (so far the complete file)
403 string writefile = ChangeExtension(getFileName(), ".sgml");
404 if (!buffer->tmppath.empty() && !buffer->niceFile) {
405 incfile = subst(incfile, '/','@');
406 writefile = AddName(buffer->tmppath, incfile);
408 writefile = getFileName();
410 if (IsLyXFilename(getFileName()))
411 writefile = ChangeExtension(writefile, ".sgml");
413 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
414 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
416 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
421 << GetFileContents(getFileName())
424 os << '&' << include_label << ';';
430 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
431 bool /*mixcont*/) const
433 string incfile(params_.cparams.getContents());
435 // Do nothing if no file name has been specified
439 if (loadIfNeeded()) {
440 Buffer * tmp = bufferlist.getBuffer(getFileName());
442 // write it to a file (so far the complete file)
443 string writefile = ChangeExtension(getFileName(), ".sgml");
444 if (!buffer->tmppath.empty() && !buffer->niceFile) {
445 incfile = subst(incfile, '/','@');
446 writefile = AddName(buffer->tmppath, incfile);
448 writefile = getFileName();
449 if (IsLyXFilename(getFileName()))
450 writefile = ChangeExtension(writefile, ".sgml");
452 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
453 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
455 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
459 os << "<inlinegraphic fileref=\""
460 << '&' << include_label << ';'
461 << "\" format=\"linespecific\">";
463 os << '&' << include_label << ';';
469 void InsetInclude::validate(LaTeXFeatures & features) const
472 string incfile(params_.cparams.getContents());
475 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
477 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
478 incfile = subst(incfile, '/','@');
479 writefile = AddName(b->tmppath, incfile);
481 writefile = getFileName();
483 if (IsLyXFilename(getFileName()))
484 writefile = ChangeExtension(writefile, ".sgml");
486 features.includeFile(include_label, writefile);
489 features.require("verbatim");
491 // Here we must do the fun stuff...
492 // Load the file in the include if it needs
494 if (loadIfNeeded()) {
496 Buffer * const tmp = bufferlist.getBuffer(getFileName());
499 tmp->niceFile = b->niceFile;
500 tmp->validate(features);
506 vector<string> const InsetInclude::getLabelList() const
510 if (loadIfNeeded()) {
511 Buffer * tmp = bufferlist.getBuffer(getFileName());
512 tmp->setParentName("");
513 l = tmp->getLabelList();
514 tmp->setParentName(getMasterFilename());
521 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
523 if (loadIfNeeded()) {
524 Buffer * tmp = bufferlist.getBuffer(getFileName());
525 tmp->setParentName("");
526 tmp->fillWithBibKeys(keys);
527 tmp->setParentName(getMasterFilename());
532 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
534 return preview_->previewReady() ?
535 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
539 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
541 return preview_->previewReady() ?
542 preview_->pimage()->descent() : InsetButton::descent(bv, font);
546 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
548 return preview_->previewReady() ?
549 preview_->pimage()->width() : InsetButton::width(bv, font);
553 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
557 if (!preview_->previewReady()) {
558 InsetButton::draw(bv, font, y, xx);
562 if (!preview_->monitoring())
563 preview_->startMonitoring();
565 int const x = int(xx);
566 int const w = width(bv, font);
567 int const d = descent(bv, font);
568 int const a = ascent(bv, font);
571 bv->painter().image(x, y - a, w, h,
572 *(preview_->pimage()->image()));
582 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
584 preview_->addPreview(ploader);
588 bool InsetInclude::PreviewImpl::previewWanted() const
590 return parent().params_.flag == InsetInclude::INPUT &&
591 parent().params_.cparams.preview() &&
592 IsFileReadable(parent().getFileName());
596 string const InsetInclude::PreviewImpl::latexString() const
598 if (!view() || !view()->buffer())
602 parent().latex(view()->buffer(), os, false, false);
604 return STRCONV(os.str());
608 void InsetInclude::PreviewImpl::startMonitoring()
610 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
611 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
616 void InsetInclude::PreviewImpl::restartLoading()
618 lyxerr << "restartLoading()" << std::endl;
621 view()->updateInset(&parent());
626 string const InsetIncludeMailer::name_("include");
628 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
633 string const InsetIncludeMailer::inset2string() const
635 return params2string(inset_.params());
639 void InsetIncludeMailer::string2params(string const & in,
640 InsetInclude::Params & params)
642 params = InsetInclude::Params();
647 istringstream data(in);
653 string const token = lex.getString();
658 // This is part of the inset proper that is usually swallowed
659 // by Buffer::readInset
662 string const token = lex.getString();
663 if (token != "Include")
668 InsetInclude inset(params);
670 params = inset.params();
676 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
678 InsetInclude inset(params);
681 data << name_ << ' ';
682 inset.write(0, data);
683 data << "\\end_inset\n";