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, true);
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::getRelFileBaseName() const
282 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
286 string const InsetInclude::getFileName() const
288 return MakeAbsPath(params_.cparams.getContents(),
289 OnlyPath(getMasterFilename()));
293 string const InsetInclude::getMasterFilename() const
295 return params_.masterFilename_;
299 bool InsetInclude::loadIfNeeded() const
304 if (!IsLyXFilename(getFileName()))
307 if (bufferlist.exists(getFileName()))
310 // the readonly flag can/will be wrong, not anymore I think.
311 FileInfo finfo(getFileName());
315 return bufferlist.loadLyXFile(getFileName(), false) != 0;
319 int InsetInclude::latex(Buffer const * buffer, ostream & os,
320 bool /*fragile*/, bool /*fs*/) const
322 string incfile(params_.cparams.getContents());
324 // Do nothing if no file name has been specified
328 if (loadIfNeeded()) {
329 Buffer * tmp = bufferlist.getBuffer(getFileName());
331 // FIXME: this should be a GUI warning
332 if (tmp->params.textclass != buffer->params.textclass) {
333 lyxerr << "WARNING: Included file `"
334 << MakeDisplayPath(getFileName())
335 << "' has textclass `"
336 << tmp->params.getLyXTextClass().name()
337 << "' while parent file has textclass `"
338 << buffer->params.getLyXTextClass().name()
343 // write it to a file (so far the complete file)
344 string writefile = ChangeExtension(getFileName(), ".tex");
346 if (!buffer->tmppath.empty()
347 && !buffer->niceFile) {
348 incfile = subst(incfile, '/','@');
350 incfile = subst(incfile, ':', '$');
352 writefile = AddName(buffer->tmppath, incfile);
354 writefile = getFileName();
355 writefile = ChangeExtension(writefile, ".tex");
356 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
357 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
359 tmp->markDepClean(buffer->tmppath);
361 tmp->makeLaTeXFile(writefile,
362 OnlyPath(getMasterFilename()),
363 buffer->niceFile, true);
367 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
368 } else if (params_.flag == INPUT) {
369 // \input wants file with extension (default is .tex)
370 if (!IsLyXFilename(getFileName())) {
371 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
373 os << '\\' << params_.cparams.getCmdName() << '{'
374 << ChangeExtension(incfile, ".tex")
378 // \include don't want extension and demands that the
379 // file really have .tex
380 os << '\\' << params_.cparams.getCmdName() << '{'
381 << ChangeExtension(incfile, string())
389 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
392 os << GetFileContents(getFileName());
397 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
399 string incfile(params_.cparams.getContents());
401 // Do nothing if no file name has been specified
405 if (loadIfNeeded()) {
406 Buffer * tmp = bufferlist.getBuffer(getFileName());
408 // write it to a file (so far the complete file)
409 string writefile = ChangeExtension(getFileName(), ".sgml");
410 if (!buffer->tmppath.empty() && !buffer->niceFile) {
411 incfile = subst(incfile, '/','@');
412 writefile = AddName(buffer->tmppath, incfile);
414 writefile = getFileName();
416 if (IsLyXFilename(getFileName()))
417 writefile = ChangeExtension(writefile, ".sgml");
419 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
420 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
422 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
427 << GetFileContents(getFileName())
430 os << '&' << include_label << ';';
436 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
437 bool /*mixcont*/) const
439 string incfile(params_.cparams.getContents());
441 // Do nothing if no file name has been specified
445 if (loadIfNeeded()) {
446 Buffer * tmp = bufferlist.getBuffer(getFileName());
448 // write it to a file (so far the complete file)
449 string writefile = ChangeExtension(getFileName(), ".sgml");
450 if (!buffer->tmppath.empty() && !buffer->niceFile) {
451 incfile = subst(incfile, '/','@');
452 writefile = AddName(buffer->tmppath, incfile);
454 writefile = getFileName();
455 if (IsLyXFilename(getFileName()))
456 writefile = ChangeExtension(writefile, ".sgml");
458 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
459 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
461 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
465 os << "<inlinegraphic fileref=\""
466 << '&' << include_label << ';'
467 << "\" format=\"linespecific\">";
469 os << '&' << include_label << ';';
475 void InsetInclude::validate(LaTeXFeatures & features) const
478 string incfile(params_.cparams.getContents());
481 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
483 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
484 incfile = subst(incfile, '/','@');
485 writefile = AddName(b->tmppath, incfile);
487 writefile = getFileName();
489 if (IsLyXFilename(getFileName()))
490 writefile = ChangeExtension(writefile, ".sgml");
492 features.includeFile(include_label, writefile);
495 features.require("verbatim");
497 // Here we must do the fun stuff...
498 // Load the file in the include if it needs
500 if (loadIfNeeded()) {
502 Buffer * const tmp = bufferlist.getBuffer(getFileName());
505 tmp->niceFile = b->niceFile;
506 tmp->validate(features);
512 vector<string> const InsetInclude::getLabelList() const
516 if (loadIfNeeded()) {
517 Buffer * tmp = bufferlist.getBuffer(getFileName());
518 tmp->setParentName("");
519 l = tmp->getLabelList();
520 tmp->setParentName(getMasterFilename());
527 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
529 if (loadIfNeeded()) {
530 Buffer * tmp = bufferlist.getBuffer(getFileName());
531 tmp->setParentName("");
532 tmp->fillWithBibKeys(keys);
533 tmp->setParentName(getMasterFilename());
538 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
540 return preview_->previewReady() ?
541 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
545 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
547 return preview_->previewReady() ?
548 preview_->pimage()->descent() : InsetButton::descent(bv, font);
552 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
554 return preview_->previewReady() ?
555 preview_->pimage()->width() : InsetButton::width(bv, font);
559 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
563 if (!preview_->previewReady()) {
564 InsetButton::draw(bv, font, y, xx);
568 if (!preview_->monitoring())
569 preview_->startMonitoring();
571 int const x = int(xx);
572 int const w = width(bv, font);
573 int const d = descent(bv, font);
574 int const a = ascent(bv, font);
577 bv->painter().image(x, y - a, w, h,
578 *(preview_->pimage()->image()));
588 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
590 preview_->addPreview(ploader);
594 bool InsetInclude::PreviewImpl::previewWanted() const
596 return parent().params_.flag == InsetInclude::INPUT &&
597 parent().params_.cparams.preview() &&
598 IsFileReadable(parent().getFileName());
602 string const InsetInclude::PreviewImpl::latexString() const
604 if (!view() || !view()->buffer())
608 parent().latex(view()->buffer(), os, false, false);
610 return STRCONV(os.str());
614 void InsetInclude::PreviewImpl::startMonitoring()
616 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
617 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
622 void InsetInclude::PreviewImpl::restartLoading()
624 lyxerr << "restartLoading()" << std::endl;
627 view()->updateInset(&parent(), false);
632 string const InsetIncludeMailer::name_("include");
634 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
639 string const InsetIncludeMailer::inset2string() const
641 return params2string(inset_.params());
645 void InsetIncludeMailer::string2params(string const & in,
646 InsetInclude::Params & params)
648 params = InsetInclude::Params();
650 istringstream data(in);
656 string const token = lex.getString();
661 // This is part of the inset proper that is usually swallowed
662 // by Buffer::readInset
665 string const token = lex.getString();
666 if (token != "Include")
671 InsetInclude inset(params);
673 params = inset.params();
679 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
681 InsetInclude inset(params);
684 data << name_ << ' ';
685 inset.write(0, data);
686 data << "\\end_inset\n";