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"
25 #include "frontends/Dialogs.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/Painter.h"
29 #include "support/filetools.h"
30 #include "support/FileInfo.h"
31 #include "support/FileMonitor.h"
32 #include "support/lstrings.h"
34 #include "graphics/PreviewedInset.h"
35 #include "graphics/PreviewImage.h"
37 #include <boost/bind.hpp>
47 extern BufferList bufferlist;
50 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
53 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
56 bool previewWanted() const;
58 string const latexString() const;
60 InsetInclude & parent() const {
61 return *static_cast<InsetInclude*>(inset());
65 bool monitoring() const { return monitor_.get(); }
67 void startMonitoring();
69 void stopMonitoring() { monitor_.reset(); }
72 /// Invoked by monitor_ should the parent file change.
73 void restartLoading();
75 boost::scoped_ptr<FileMonitor> monitor_;
81 string const uniqueID()
83 static unsigned int seed = 1000;
86 ost << "file" << ++seed;
88 // Needed if we use lyxstring.
89 return STRCONV(ost.str());
95 InsetInclude::InsetInclude(Params const & p)
96 : params_(p), include_label(uniqueID()),
97 preview_(new PreviewImpl(*this))
101 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
102 : include_label(uniqueID()),
103 preview_(new PreviewImpl(*this))
106 params_.masterFilename_ = b.fileName();
110 InsetInclude::~InsetInclude()
112 InsetIncludeMailer mailer(*this);
117 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
119 dispatch_result result = UNDISPATCHED;
121 switch (cmd.action) {
122 case LFUN_INSET_MODIFY: {
123 InsetInclude::Params p;
124 InsetIncludeMailer::string2params(cmd.argument, p);
125 if (p.cparams.getCmdName().empty())
129 params_.masterFilename_ = cmd.view()->buffer()->fileName();
131 cmd.view()->updateInset(this);
136 case LFUN_INSET_DIALOG_UPDATE: {
137 InsetIncludeMailer mailer(*this);
138 mailer.updateDialog(cmd.view());
142 case LFUN_MOUSE_RELEASE:
143 edit(cmd.view(), cmd.x, cmd.y, cmd.button());
154 InsetInclude::Params const & InsetInclude::params() const
160 bool InsetInclude::Params::operator==(Params const & o) const
162 if (cparams == o.cparams && flag == o.flag &&
163 masterFilename_ == o.masterFilename_)
170 bool InsetInclude::Params::operator!=(Params const & o) const
172 return !(*this == o);
176 void InsetInclude::set(Params const & p)
182 switch (params_.flag) {
187 command="verbatiminput";
193 command="verbatiminput*";
197 params_.cparams.setCmdName(command);
199 if (preview_->monitoring())
200 preview_->stopMonitoring();
202 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
203 preview_->generatePreview();
207 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
210 p.masterFilename_ = buffer.fileName();
212 return new InsetInclude(p);
216 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
218 InsetIncludeMailer mailer(*this);
219 mailer.showDialog(bv);
223 void InsetInclude::edit(BufferView * bv, bool)
225 edit(bv, 0, 0, mouse_button::none);
229 void InsetInclude::write(Buffer const *, ostream & os) const
231 os << "Include " << params_.cparams.getCommand() << '\n'
232 << "preview " << tostr(params_.cparams.preview()) << '\n';
236 void InsetInclude::read(Buffer const *, LyXLex & lex)
238 params_.cparams.read(lex);
240 if (params_.cparams.getCmdName() == "include")
241 params_.flag = INCLUDE;
242 else if (params_.cparams.getCmdName() == "input")
243 params_.flag = INPUT;
244 /* FIXME: is this logic necessary now ? */
245 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
247 if (params_.cparams.getCmdName() == "verbatiminput*")
248 params_.flag = VERBAST;
253 bool InsetInclude::display() const
255 return !(params_.flag == INPUT);
259 string const InsetInclude::getScreenLabel(Buffer const *) const
263 switch (params_.flag) {
264 case INPUT: temp += _("Input"); break;
265 case VERB: temp += _("Verbatim Input"); break;
266 case VERBAST: temp += _("Verbatim Input*"); break;
267 case INCLUDE: temp += _("Include"); break;
272 if (params_.cparams.getContents().empty())
275 temp += params_.cparams.getContents();
281 string const InsetInclude::getFileName() const
283 return MakeAbsPath(params_.cparams.getContents(),
284 OnlyPath(getMasterFilename()));
288 string const InsetInclude::getMasterFilename() const
290 return params_.masterFilename_;
294 bool InsetInclude::loadIfNeeded() const
299 if (!IsLyXFilename(getFileName()))
302 if (bufferlist.exists(getFileName()))
305 // the readonly flag can/will be wrong, not anymore I think.
306 FileInfo finfo(getFileName());
310 return bufferlist.loadLyXFile(getFileName(), false) != 0;
314 int InsetInclude::latex(Buffer const * buffer, ostream & os,
315 bool /*fragile*/, bool /*fs*/) const
317 string incfile(params_.cparams.getContents());
319 // Do nothing if no file name has been specified
323 if (loadIfNeeded()) {
324 Buffer * tmp = bufferlist.getBuffer(getFileName());
326 // FIXME: this should be a GUI warning
327 if (tmp->params.textclass != buffer->params.textclass) {
328 lyxerr << "WARNING: Included file `"
329 << MakeDisplayPath(getFileName())
330 << "' has textclass `"
331 << tmp->params.getLyXTextClass().name()
332 << "' while parent file has textclass `"
333 << buffer->params.getLyXTextClass().name()
338 // write it to a file (so far the complete file)
339 string writefile = ChangeExtension(getFileName(), ".tex");
341 if (!buffer->tmppath.empty()
342 && !buffer->niceFile) {
343 incfile = subst(incfile, '/','@');
345 incfile = subst(incfile, ':', '$');
347 writefile = AddName(buffer->tmppath, incfile);
349 writefile = getFileName();
350 writefile = ChangeExtension(writefile, ".tex");
351 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
352 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
354 tmp->markDepClean(buffer->tmppath);
356 tmp->makeLaTeXFile(writefile,
357 OnlyPath(getMasterFilename()),
358 buffer->niceFile, true);
362 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
363 } else if (params_.flag == INPUT) {
364 // \input wants file with extension (default is .tex)
365 if (!IsLyXFilename(getFileName())) {
366 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
368 os << '\\' << params_.cparams.getCmdName() << '{'
369 << ChangeExtension(incfile, ".tex")
373 // \include don't want extension and demands that the
374 // file really have .tex
375 os << '\\' << params_.cparams.getCmdName() << '{'
376 << ChangeExtension(incfile, string())
384 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
387 os << GetFileContents(getFileName());
392 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
394 string incfile(params_.cparams.getContents());
396 // Do nothing if no file name has been specified
400 if (loadIfNeeded()) {
401 Buffer * tmp = bufferlist.getBuffer(getFileName());
403 // write it to a file (so far the complete file)
404 string writefile = ChangeExtension(getFileName(), ".sgml");
405 if (!buffer->tmppath.empty() && !buffer->niceFile) {
406 incfile = subst(incfile, '/','@');
407 writefile = AddName(buffer->tmppath, incfile);
409 writefile = getFileName();
411 if (IsLyXFilename(getFileName()))
412 writefile = ChangeExtension(writefile, ".sgml");
414 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
415 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
417 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
422 << GetFileContents(getFileName())
425 os << '&' << include_label << ';';
431 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
432 bool /*mixcont*/) const
434 string incfile(params_.cparams.getContents());
436 // Do nothing if no file name has been specified
440 if (loadIfNeeded()) {
441 Buffer * tmp = bufferlist.getBuffer(getFileName());
443 // write it to a file (so far the complete file)
444 string writefile = ChangeExtension(getFileName(), ".sgml");
445 if (!buffer->tmppath.empty() && !buffer->niceFile) {
446 incfile = subst(incfile, '/','@');
447 writefile = AddName(buffer->tmppath, incfile);
449 writefile = getFileName();
450 if (IsLyXFilename(getFileName()))
451 writefile = ChangeExtension(writefile, ".sgml");
453 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
454 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
456 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
460 os << "<inlinegraphic fileref=\""
461 << '&' << include_label << ';'
462 << "\" format=\"linespecific\">";
464 os << '&' << include_label << ';';
470 void InsetInclude::validate(LaTeXFeatures & features) const
473 string incfile(params_.cparams.getContents());
476 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
478 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
479 incfile = subst(incfile, '/','@');
480 writefile = AddName(b->tmppath, incfile);
482 writefile = getFileName();
484 if (IsLyXFilename(getFileName()))
485 writefile = ChangeExtension(writefile, ".sgml");
487 features.includeFile(include_label, writefile);
490 features.require("verbatim");
492 // Here we must do the fun stuff...
493 // Load the file in the include if it needs
495 if (loadIfNeeded()) {
497 Buffer * const tmp = bufferlist.getBuffer(getFileName());
500 tmp->niceFile = b->niceFile;
501 tmp->validate(features);
507 vector<string> const InsetInclude::getLabelList() const
511 if (loadIfNeeded()) {
512 Buffer * tmp = bufferlist.getBuffer(getFileName());
513 tmp->setParentName("");
514 l = tmp->getLabelList();
515 tmp->setParentName(getMasterFilename());
522 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
524 if (loadIfNeeded()) {
525 Buffer * tmp = bufferlist.getBuffer(getFileName());
526 tmp->setParentName("");
527 tmp->fillWithBibKeys(keys);
528 tmp->setParentName(getMasterFilename());
533 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
535 return preview_->previewReady() ?
536 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
540 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
542 return preview_->previewReady() ?
543 preview_->pimage()->descent() : InsetButton::descent(bv, font);
547 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
549 return preview_->previewReady() ?
550 preview_->pimage()->width() : InsetButton::width(bv, font);
554 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
558 if (!preview_->previewReady()) {
559 InsetButton::draw(bv, font, y, xx);
563 if (!preview_->monitoring())
564 preview_->startMonitoring();
566 int const x = int(xx);
567 int const w = width(bv, font);
568 int const d = descent(bv, font);
569 int const a = ascent(bv, font);
572 bv->painter().image(x, y - a, w, h,
573 *(preview_->pimage()->image()));
583 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
585 preview_->addPreview(ploader);
589 bool InsetInclude::PreviewImpl::previewWanted() const
591 return parent().params_.flag == InsetInclude::INPUT &&
592 parent().params_.cparams.preview() &&
593 IsFileReadable(parent().getFileName());
597 string const InsetInclude::PreviewImpl::latexString() const
599 if (!view() || !view()->buffer())
603 parent().latex(view()->buffer(), os, false, false);
605 return STRCONV(os.str());
609 void InsetInclude::PreviewImpl::startMonitoring()
611 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
612 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
617 void InsetInclude::PreviewImpl::restartLoading()
619 lyxerr << "restartLoading()" << std::endl;
622 view()->updateInset(&parent());
627 string const InsetIncludeMailer::name_("include");
629 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
634 string const InsetIncludeMailer::inset2string() const
636 return params2string(inset_.params());
640 void InsetIncludeMailer::string2params(string const & in,
641 InsetInclude::Params & params)
643 params = InsetInclude::Params();
648 istringstream data(in);
654 string const token = lex.getString();
659 // This is part of the inset proper that is usually swallowed
660 // by Buffer::readInset
663 string const token = lex.getString();
664 if (token != "Include")
669 InsetInclude inset(params);
671 params = inset.params();
677 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
679 InsetInclude inset(params);
682 data << name_ << ' ';
683 inset.write(0, data);
684 data << "\\end_inset\n";