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, bool) const
197 p.masterFilename_ = buffer.fileName();
199 return new InsetInclude(p);
203 void InsetInclude::write(Buffer const *, ostream & os) const
205 os << "Include " << params_.cparams.getCommand() << '\n'
206 << "preview " << tostr(params_.cparams.preview()) << '\n';
210 void InsetInclude::read(Buffer const *, LyXLex & lex)
212 params_.cparams.read(lex);
214 if (params_.cparams.getCmdName() == "include")
215 params_.flag = INCLUDE;
216 else if (params_.cparams.getCmdName() == "input")
217 params_.flag = INPUT;
218 /* FIXME: is this logic necessary now ? */
219 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
221 if (params_.cparams.getCmdName() == "verbatiminput*")
222 params_.flag = VERBAST;
227 bool InsetInclude::display() const
229 return !(params_.flag == INPUT);
233 string const InsetInclude::getScreenLabel(Buffer const *) const
237 switch (params_.flag) {
238 case INPUT: temp += _("Input"); break;
239 case VERB: temp += _("Verbatim Input"); break;
240 case VERBAST: temp += _("Verbatim Input*"); break;
241 case INCLUDE: temp += _("Include"); break;
246 if (params_.cparams.getContents().empty())
249 temp += params_.cparams.getContents();
255 string const InsetInclude::getFileName() const
257 return MakeAbsPath(params_.cparams.getContents(),
258 OnlyPath(getMasterFilename()));
262 string const InsetInclude::getMasterFilename() const
264 return params_.masterFilename_;
268 bool InsetInclude::loadIfNeeded() const
273 if (!IsLyXFilename(getFileName()))
276 if (bufferlist.exists(getFileName()))
279 // the readonly flag can/will be wrong, not anymore I think.
280 FileInfo finfo(getFileName());
284 return bufferlist.loadLyXFile(getFileName(), false) != 0;
288 int InsetInclude::latex(Buffer const * buffer, ostream & os,
289 LatexRunParams const & runparams) const
291 string incfile(params_.cparams.getContents());
293 // Do nothing if no file name has been specified
297 if (loadIfNeeded()) {
298 Buffer * tmp = bufferlist.getBuffer(getFileName());
300 // FIXME: this should be a GUI warning
301 if (tmp->params.textclass != buffer->params.textclass) {
302 lyxerr << "WARNING: Included file `"
303 << MakeDisplayPath(getFileName())
304 << "' has textclass `"
305 << tmp->params.getLyXTextClass().name()
306 << "' while parent file has textclass `"
307 << buffer->params.getLyXTextClass().name()
312 // write it to a file (so far the complete file)
313 string writefile = ChangeExtension(getFileName(), ".tex");
315 if (!buffer->tmppath.empty() && !runparams.nice) {
316 incfile = subst(incfile, '/','@');
318 incfile = subst(incfile, ':', '$');
320 writefile = AddName(buffer->tmppath, incfile);
322 writefile = getFileName();
323 writefile = ChangeExtension(writefile, ".tex");
324 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
325 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
327 tmp->markDepClean(buffer->tmppath);
329 tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
334 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
335 } else if (params_.flag == INPUT) {
336 // \input wants file with extension (default is .tex)
337 if (!IsLyXFilename(getFileName())) {
338 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
340 os << '\\' << params_.cparams.getCmdName() << '{'
341 << ChangeExtension(incfile, ".tex")
345 // \include don't want extension and demands that the
346 // file really have .tex
347 os << '\\' << params_.cparams.getCmdName() << '{'
348 << ChangeExtension(incfile, string())
356 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
359 os << GetFileContents(getFileName());
364 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
366 string incfile(params_.cparams.getContents());
368 // Do nothing if no file name has been specified
372 if (loadIfNeeded()) {
373 Buffer * tmp = bufferlist.getBuffer(getFileName());
375 // write it to a file (so far the complete file)
376 string writefile = ChangeExtension(getFileName(), ".sgml");
377 if (!buffer->tmppath.empty() && !buffer->niceFile) {
378 incfile = subst(incfile, '/','@');
379 writefile = AddName(buffer->tmppath, incfile);
381 writefile = getFileName();
383 if (IsLyXFilename(getFileName()))
384 writefile = ChangeExtension(writefile, ".sgml");
386 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
387 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
389 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
394 << GetFileContents(getFileName())
397 os << '&' << include_label << ';';
403 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
404 bool /*mixcont*/) const
406 string incfile(params_.cparams.getContents());
408 // Do nothing if no file name has been specified
412 if (loadIfNeeded()) {
413 Buffer * tmp = bufferlist.getBuffer(getFileName());
415 // write it to a file (so far the complete file)
416 string writefile = ChangeExtension(getFileName(), ".sgml");
417 if (!buffer->tmppath.empty() && !buffer->niceFile) {
418 incfile = subst(incfile, '/','@');
419 writefile = AddName(buffer->tmppath, incfile);
421 writefile = getFileName();
422 if (IsLyXFilename(getFileName()))
423 writefile = ChangeExtension(writefile, ".sgml");
425 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
426 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
428 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
432 os << "<inlinegraphic fileref=\""
433 << '&' << include_label << ';'
434 << "\" format=\"linespecific\">";
436 os << '&' << include_label << ';';
442 void InsetInclude::validate(LaTeXFeatures & features) const
445 string incfile(params_.cparams.getContents());
448 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
450 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
451 incfile = subst(incfile, '/','@');
452 writefile = AddName(b->tmppath, incfile);
454 writefile = getFileName();
456 if (IsLyXFilename(getFileName()))
457 writefile = ChangeExtension(writefile, ".sgml");
459 features.includeFile(include_label, writefile);
462 features.require("verbatim");
464 // Here we must do the fun stuff...
465 // Load the file in the include if it needs
467 if (loadIfNeeded()) {
469 Buffer * const tmp = bufferlist.getBuffer(getFileName());
472 tmp->niceFile = b->niceFile;
473 tmp->validate(features);
479 vector<string> const InsetInclude::getLabelList() const
483 if (loadIfNeeded()) {
484 Buffer * tmp = bufferlist.getBuffer(getFileName());
485 tmp->setParentName("");
486 l = tmp->getLabelList();
487 tmp->setParentName(getMasterFilename());
494 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
496 if (loadIfNeeded()) {
497 Buffer * tmp = bufferlist.getBuffer(getFileName());
498 tmp->setParentName("");
499 tmp->fillWithBibKeys(keys);
500 tmp->setParentName(getMasterFilename());
505 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
507 return preview_->previewReady() ?
508 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
512 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
514 return preview_->previewReady() ?
515 preview_->pimage()->descent() : InsetButton::descent(bv, font);
519 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
521 return preview_->previewReady() ?
522 preview_->pimage()->width() : InsetButton::width(bv, font);
526 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
530 if (!preview_->previewReady()) {
531 InsetButton::draw(bv, font, y, xx);
535 if (!preview_->monitoring())
536 preview_->startMonitoring();
538 int const x = int(xx);
539 int const w = width(bv, font);
540 int const d = descent(bv, font);
541 int const a = ascent(bv, font);
544 bv->painter().image(x, y - a, w, h,
545 *(preview_->pimage()->image()));
555 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
557 preview_->addPreview(ploader);
561 bool InsetInclude::PreviewImpl::previewWanted() const
563 return parent().params_.flag == InsetInclude::INPUT &&
564 parent().params_.cparams.preview() &&
565 IsFileReadable(parent().getFileName());
569 string const InsetInclude::PreviewImpl::latexString() const
571 if (!view() || !view()->buffer())
575 LatexRunParams runparams;
576 runparams.flavor = LatexRunParams::LATEX;
577 parent().latex(view()->buffer(), os, runparams);
579 return STRCONV(os.str());
583 void InsetInclude::PreviewImpl::startMonitoring()
585 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
586 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
591 void InsetInclude::PreviewImpl::restartLoading()
593 lyxerr << "restartLoading()" << std::endl;
596 view()->updateInset(&parent());
601 string const InsetIncludeMailer::name_("include");
603 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
608 string const InsetIncludeMailer::inset2string() const
610 return params2string(inset_.params());
614 void InsetIncludeMailer::string2params(string const & in,
615 InsetInclude::Params & params)
617 params = InsetInclude::Params();
622 istringstream data(STRCONV(in));
628 string const token = lex.getString();
633 // This is part of the inset proper that is usually swallowed
634 // by Buffer::readInset
637 string const token = lex.getString();
638 if (token != "Include")
643 InsetInclude inset(params);
645 params = inset.params();
651 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
653 InsetInclude inset(params);
656 data << name_ << ' ';
657 inset.write(0, data);
658 data << "\\end_inset\n";
659 return STRCONV(data.str());