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();
149 InsetInclude::Params const & InsetInclude::params() const
155 bool InsetInclude::Params::operator==(Params const & o) const
157 if (cparams == o.cparams && flag == o.flag &&
158 masterFilename_ == o.masterFilename_)
165 bool InsetInclude::Params::operator!=(Params const & o) const
167 return !(*this == o);
171 void InsetInclude::set(Params const & p)
177 switch (params_.flag) {
182 command="verbatiminput";
188 command="verbatiminput*";
192 params_.cparams.setCmdName(command);
194 if (preview_->monitoring())
195 preview_->stopMonitoring();
197 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
198 preview_->generatePreview();
202 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
205 p.masterFilename_ = buffer.fileName();
207 return new InsetInclude(p);
211 void InsetInclude::edit(BufferView *, int, int, mouse_button::state)
213 InsetIncludeMailer mailer(*this);
218 void InsetInclude::edit(BufferView * bv, bool)
220 edit(bv, 0, 0, mouse_button::none);
224 void InsetInclude::write(Buffer const *, ostream & os) const
226 os << "Include " << params_.cparams.getCommand() << '\n'
227 << "preview " << tostr(params_.cparams.preview()) << '\n';
231 void InsetInclude::read(Buffer const *, LyXLex & lex)
233 params_.cparams.read(lex);
235 if (params_.cparams.getCmdName() == "include")
236 params_.flag = INCLUDE;
237 else if (params_.cparams.getCmdName() == "input")
238 params_.flag = INPUT;
239 /* FIXME: is this logic necessary now ? */
240 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
242 if (params_.cparams.getCmdName() == "verbatiminput*")
243 params_.flag = VERBAST;
248 bool InsetInclude::display() const
250 return !(params_.flag == INPUT);
254 string const InsetInclude::getScreenLabel(Buffer const *) const
258 switch (params_.flag) {
259 case INPUT: temp += _("Input"); break;
260 case VERB: temp += _("Verbatim Input"); break;
261 case VERBAST: temp += _("Verbatim Input*"); break;
262 case INCLUDE: temp += _("Include"); break;
267 if (params_.cparams.getContents().empty())
270 temp += params_.cparams.getContents();
276 string const InsetInclude::getRelFileBaseName() const
278 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
282 string const InsetInclude::getFileName() const
284 return MakeAbsPath(params_.cparams.getContents(),
285 OnlyPath(getMasterFilename()));
289 string const InsetInclude::getMasterFilename() const
291 return params_.masterFilename_;
295 bool InsetInclude::loadIfNeeded() const
300 if (!IsLyXFilename(getFileName()))
303 if (bufferlist.exists(getFileName()))
306 // the readonly flag can/will be wrong, not anymore I think.
307 FileInfo finfo(getFileName());
311 return bufferlist.loadLyXFile(getFileName(), false) != 0;
315 int InsetInclude::latex(Buffer const * buffer, ostream & os,
316 bool /*fragile*/, bool /*fs*/) const
318 string incfile(params_.cparams.getContents());
320 // Do nothing if no file name has been specified
324 if (loadIfNeeded()) {
325 Buffer * tmp = bufferlist.getBuffer(getFileName());
327 // FIXME: this should be a GUI warning
328 if (tmp->params.textclass != buffer->params.textclass) {
329 lyxerr << "WARNING: Included file `"
330 << MakeDisplayPath(getFileName())
331 << "' has textclass `"
332 << tmp->params.getLyXTextClass().name()
333 << "' while parent file has textclass `"
334 << buffer->params.getLyXTextClass().name()
339 // write it to a file (so far the complete file)
340 string writefile = ChangeExtension(getFileName(), ".tex");
342 if (!buffer->tmppath.empty()
343 && !buffer->niceFile) {
344 incfile = subst(incfile, '/','@');
346 incfile = subst(incfile, ':', '$');
348 writefile = AddName(buffer->tmppath, incfile);
350 writefile = getFileName();
351 writefile = ChangeExtension(writefile, ".tex");
352 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
353 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
355 tmp->markDepClean(buffer->tmppath);
357 tmp->makeLaTeXFile(writefile,
358 OnlyPath(getMasterFilename()),
359 buffer->niceFile, true);
363 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
364 } else if (params_.flag == INPUT) {
365 // \input wants file with extension (default is .tex)
366 if (!IsLyXFilename(getFileName())) {
367 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
369 os << '\\' << params_.cparams.getCmdName() << '{'
370 << ChangeExtension(incfile, ".tex")
374 // \include don't want extension and demands that the
375 // file really have .tex
376 os << '\\' << params_.cparams.getCmdName() << '{'
377 << ChangeExtension(incfile, string())
385 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
388 os << GetFileContents(getFileName());
393 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
395 string incfile(params_.cparams.getContents());
397 // Do nothing if no file name has been specified
401 if (loadIfNeeded()) {
402 Buffer * tmp = bufferlist.getBuffer(getFileName());
404 // write it to a file (so far the complete file)
405 string writefile = ChangeExtension(getFileName(), ".sgml");
406 if (!buffer->tmppath.empty() && !buffer->niceFile) {
407 incfile = subst(incfile, '/','@');
408 writefile = AddName(buffer->tmppath, incfile);
410 writefile = getFileName();
412 if (IsLyXFilename(getFileName()))
413 writefile = ChangeExtension(writefile, ".sgml");
415 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
416 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
418 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
423 << GetFileContents(getFileName())
426 os << '&' << include_label << ';';
432 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
433 bool /*mixcont*/) const
435 string incfile(params_.cparams.getContents());
437 // Do nothing if no file name has been specified
441 if (loadIfNeeded()) {
442 Buffer * tmp = bufferlist.getBuffer(getFileName());
444 // write it to a file (so far the complete file)
445 string writefile = ChangeExtension(getFileName(), ".sgml");
446 if (!buffer->tmppath.empty() && !buffer->niceFile) {
447 incfile = subst(incfile, '/','@');
448 writefile = AddName(buffer->tmppath, incfile);
450 writefile = getFileName();
451 if (IsLyXFilename(getFileName()))
452 writefile = ChangeExtension(writefile, ".sgml");
454 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
455 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
457 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
461 os << "<inlinegraphic fileref=\""
462 << '&' << include_label << ';'
463 << "\" format=\"linespecific\">";
465 os << '&' << include_label << ';';
471 void InsetInclude::validate(LaTeXFeatures & features) const
474 string incfile(params_.cparams.getContents());
477 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
479 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
480 incfile = subst(incfile, '/','@');
481 writefile = AddName(b->tmppath, incfile);
483 writefile = getFileName();
485 if (IsLyXFilename(getFileName()))
486 writefile = ChangeExtension(writefile, ".sgml");
488 features.includeFile(include_label, writefile);
491 features.require("verbatim");
493 // Here we must do the fun stuff...
494 // Load the file in the include if it needs
496 if (loadIfNeeded()) {
498 Buffer * const tmp = bufferlist.getBuffer(getFileName());
501 tmp->niceFile = b->niceFile;
502 tmp->validate(features);
508 vector<string> const InsetInclude::getLabelList() const
512 if (loadIfNeeded()) {
513 Buffer * tmp = bufferlist.getBuffer(getFileName());
514 tmp->setParentName("");
515 l = tmp->getLabelList();
516 tmp->setParentName(getMasterFilename());
523 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
525 if (loadIfNeeded()) {
526 Buffer * tmp = bufferlist.getBuffer(getFileName());
527 tmp->setParentName("");
528 tmp->fillWithBibKeys(keys);
529 tmp->setParentName(getMasterFilename());
534 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
536 return preview_->previewReady() ?
537 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
541 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
543 return preview_->previewReady() ?
544 preview_->pimage()->descent() : InsetButton::descent(bv, font);
548 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
550 return preview_->previewReady() ?
551 preview_->pimage()->width() : InsetButton::width(bv, font);
555 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
556 float & xx, bool b) const
559 if (!preview_->previewReady()) {
560 InsetButton::draw(bv, font, y, xx, b);
564 if (!preview_->monitoring())
565 preview_->startMonitoring();
567 int const x = int(xx);
568 int const w = width(bv, font);
569 int const d = descent(bv, font);
570 int const a = ascent(bv, font);
573 bv->painter().image(x, y - a, w, h,
574 *(preview_->pimage()->image()));
584 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
586 preview_->addPreview(ploader);
590 bool InsetInclude::PreviewImpl::previewWanted() const
592 return parent().params_.flag == InsetInclude::INPUT &&
593 parent().params_.cparams.preview() &&
594 IsFileReadable(parent().getFileName());
598 string const InsetInclude::PreviewImpl::latexString() const
600 if (!view() || !view()->buffer())
604 parent().latex(view()->buffer(), os, false, false);
606 return STRCONV(os.str());
610 void InsetInclude::PreviewImpl::startMonitoring()
612 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
613 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
618 void InsetInclude::PreviewImpl::restartLoading()
620 lyxerr << "restartLoading()" << std::endl;
623 view()->updateInset(&parent(), false);
628 string const InsetIncludeMailer::name_("include");
630 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
635 string const InsetIncludeMailer::inset2string() const
637 return params2string(inset_.params());
641 void InsetIncludeMailer::string2params(string const & in,
642 InsetInclude::Params & params)
644 params = InsetInclude::Params();
646 istringstream data(in);
652 string const token = lex.getString();
657 // This is part of the inset proper that is usually swallowed
658 // by Buffer::readInset
661 string const token = lex.getString();
662 if (token != "Include")
666 InsetInclude inset(params);
668 params = inset.params();
673 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
675 InsetInclude inset(params);
678 data << name_ << ' ';
679 inset.write(0, data);
680 data << "\\end_inset\n";