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"
19 #include "LaTeXFeatures.h"
22 #include "frontends/Dialogs.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
26 #include "support/filetools.h"
27 #include "support/FileInfo.h"
28 #include "support/FileMonitor.h"
29 #include "support/lstrings.h"
31 #include "graphics/PreviewedInset.h"
32 #include "graphics/PreviewImage.h"
34 #include <boost/bind.hpp>
44 extern BufferList bufferlist;
47 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
50 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
53 bool previewWanted() const;
55 string const latexString() const;
57 InsetInclude & parent() const {
58 return *static_cast<InsetInclude*>(inset());
62 bool monitoring() const { return monitor_.get(); }
64 void startMonitoring();
66 void stopMonitoring() { monitor_.reset(); }
69 /// Invoked by monitor_ should the parent file change.
70 void restartLoading();
72 boost::scoped_ptr<FileMonitor> monitor_;
78 string const uniqueID()
80 static unsigned int seed = 1000;
83 ost << "file" << ++seed;
85 // Needed if we use lyxstring.
86 return STRCONV(ost.str());
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()
113 InsetInclude::Params const & InsetInclude::params() const
119 bool InsetInclude::Params::operator==(Params const & o) const
121 if (cparams == o.cparams && flag == o.flag &&
122 masterFilename_ == o.masterFilename_)
129 bool InsetInclude::Params::operator!=(Params const & o) const
131 return !(*this == o);
135 void InsetInclude::set(Params const & p)
141 switch (params_.flag) {
146 command="verbatiminput";
152 command="verbatiminput*";
156 params_.cparams.setCmdName(command);
158 if (preview_->monitoring())
159 preview_->stopMonitoring();
161 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
162 preview_->generatePreview();
166 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
169 p.masterFilename_ = buffer.fileName();
171 return new InsetInclude(p);
175 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
177 bv->owner()->getDialogs().showInclude(this);
181 void InsetInclude::edit(BufferView * bv, bool)
183 edit(bv, 0, 0, mouse_button::none);
187 void InsetInclude::write(Buffer const *, ostream & os) const
189 os << "Include " << params_.cparams.getCommand() << '\n'
190 << "preview " << tostr(params_.cparams.preview()) << '\n';
194 void InsetInclude::read(Buffer const *, LyXLex & lex)
196 params_.cparams.read(lex);
198 if (params_.cparams.getCmdName() == "include")
199 params_.flag = INCLUDE;
200 else if (params_.cparams.getCmdName() == "input")
201 params_.flag = INPUT;
202 /* FIXME: is this logic necessary now ? */
203 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
205 if (params_.cparams.getCmdName() == "verbatiminput*")
206 params_.flag = VERBAST;
211 bool InsetInclude::display() const
213 return !(params_.flag == INPUT);
217 string const InsetInclude::getScreenLabel(Buffer const *) const
221 switch (params_.flag) {
222 case INPUT: temp += _("Input"); break;
223 case VERB: temp += _("Verbatim Input"); break;
224 case VERBAST: temp += _("Verbatim Input*"); break;
225 case INCLUDE: temp += _("Include"); break;
230 if (params_.cparams.getContents().empty())
233 temp += params_.cparams.getContents();
239 string const InsetInclude::getRelFileBaseName() const
241 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
245 string const InsetInclude::getFileName() const
247 return MakeAbsPath(params_.cparams.getContents(),
248 OnlyPath(getMasterFilename()));
252 string const InsetInclude::getMasterFilename() const
254 return params_.masterFilename_;
258 bool InsetInclude::loadIfNeeded() const
263 if (!IsLyXFilename(getFileName()))
266 if (bufferlist.exists(getFileName()))
269 // the readonly flag can/will be wrong, not anymore I think.
270 FileInfo finfo(getFileName());
274 return bufferlist.loadLyXFile(getFileName(), false) != 0;
278 int InsetInclude::latex(Buffer const * buffer, ostream & os,
279 bool /*fragile*/, bool /*fs*/) const
281 string incfile(params_.cparams.getContents());
283 // Do nothing if no file name has been specified
287 if (loadIfNeeded()) {
288 Buffer * tmp = bufferlist.getBuffer(getFileName());
290 // FIXME: this should be a GUI warning
291 if (tmp->params.textclass != buffer->params.textclass) {
292 lyxerr << "WARNING: Included file `"
293 << MakeDisplayPath(getFileName())
294 << "' has textclass `"
295 << tmp->params.getLyXTextClass().name()
296 << "' while parent file has textclass `"
297 << buffer->params.getLyXTextClass().name()
302 // write it to a file (so far the complete file)
303 string writefile = ChangeExtension(getFileName(), ".tex");
305 if (!buffer->tmppath.empty()
306 && !buffer->niceFile) {
307 incfile = subst(incfile, '/','@');
309 incfile = subst(incfile, ':', '$');
311 writefile = AddName(buffer->tmppath, incfile);
313 writefile = getFileName();
314 writefile = ChangeExtension(writefile, ".tex");
315 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
316 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
318 tmp->markDepClean(buffer->tmppath);
320 tmp->makeLaTeXFile(writefile,
321 OnlyPath(getMasterFilename()),
322 buffer->niceFile, true);
326 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
327 } else if (params_.flag == INPUT) {
328 // \input wants file with extension (default is .tex)
329 if (!IsLyXFilename(getFileName())) {
330 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
332 os << '\\' << params_.cparams.getCmdName() << '{'
333 << ChangeExtension(incfile, ".tex")
337 // \include don't want extension and demands that the
338 // file really have .tex
339 os << '\\' << params_.cparams.getCmdName() << '{'
340 << ChangeExtension(incfile, string())
348 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
351 os << GetFileContents(getFileName());
356 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
358 string incfile(params_.cparams.getContents());
360 // Do nothing if no file name has been specified
364 if (loadIfNeeded()) {
365 Buffer * tmp = bufferlist.getBuffer(getFileName());
367 // write it to a file (so far the complete file)
368 string writefile = ChangeExtension(getFileName(), ".sgml");
369 if (!buffer->tmppath.empty() && !buffer->niceFile) {
370 incfile = subst(incfile, '/','@');
371 writefile = AddName(buffer->tmppath, incfile);
373 writefile = getFileName();
375 if (IsLyXFilename(getFileName()))
376 writefile = ChangeExtension(writefile, ".sgml");
378 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
379 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
381 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
386 << GetFileContents(getFileName())
389 os << '&' << include_label << ';';
395 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
396 bool /*mixcont*/) const
398 string incfile(params_.cparams.getContents());
400 // Do nothing if no file name has been specified
404 if (loadIfNeeded()) {
405 Buffer * tmp = bufferlist.getBuffer(getFileName());
407 // write it to a file (so far the complete file)
408 string writefile = ChangeExtension(getFileName(), ".sgml");
409 if (!buffer->tmppath.empty() && !buffer->niceFile) {
410 incfile = subst(incfile, '/','@');
411 writefile = AddName(buffer->tmppath, incfile);
413 writefile = getFileName();
414 if (IsLyXFilename(getFileName()))
415 writefile = ChangeExtension(writefile, ".sgml");
417 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
418 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
420 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
424 os << "<inlinegraphic fileref=\""
425 << '&' << include_label << ';'
426 << "\" format=\"linespecific\">";
428 os << '&' << include_label << ';';
434 void InsetInclude::validate(LaTeXFeatures & features) const
437 string incfile(params_.cparams.getContents());
440 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
442 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
443 incfile = subst(incfile, '/','@');
444 writefile = AddName(b->tmppath, incfile);
446 writefile = getFileName();
448 if (IsLyXFilename(getFileName()))
449 writefile = ChangeExtension(writefile, ".sgml");
451 features.includeFile(include_label, writefile);
454 features.require("verbatim");
456 // Here we must do the fun stuff...
457 // Load the file in the include if it needs
459 if (loadIfNeeded()) {
461 Buffer * const tmp = bufferlist.getBuffer(getFileName());
464 tmp->niceFile = b->niceFile;
465 tmp->validate(features);
471 vector<string> const InsetInclude::getLabelList() const
475 if (loadIfNeeded()) {
476 Buffer * tmp = bufferlist.getBuffer(getFileName());
477 tmp->setParentName("");
478 l = tmp->getLabelList();
479 tmp->setParentName(getMasterFilename());
486 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
488 if (loadIfNeeded()) {
489 Buffer * tmp = bufferlist.getBuffer(getFileName());
490 tmp->setParentName("");
491 tmp->fillWithBibKeys(keys);
492 tmp->setParentName(getMasterFilename());
497 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
499 return preview_->previewReady() ?
500 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
504 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
506 return preview_->previewReady() ?
507 preview_->pimage()->descent() : InsetButton::descent(bv, font);
511 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
513 return preview_->previewReady() ?
514 preview_->pimage()->width() : InsetButton::width(bv, font);
518 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
519 float & xx, bool b) const
522 if (!preview_->previewReady()) {
523 InsetButton::draw(bv, font, y, xx, b);
527 if (!preview_->monitoring())
528 preview_->startMonitoring();
530 int const x = int(xx);
531 int const w = width(bv, font);
532 int const d = descent(bv, font);
533 int const a = ascent(bv, font);
536 bv->painter().image(x, y - a, w, h,
537 *(preview_->pimage()->image()));
547 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
549 preview_->addPreview(ploader);
553 bool InsetInclude::PreviewImpl::previewWanted() const
555 return parent().params_.flag == InsetInclude::INPUT &&
556 parent().params_.cparams.preview() &&
557 IsFileReadable(parent().getFileName());
561 string const InsetInclude::PreviewImpl::latexString() const
563 if (!view() || !view()->buffer())
567 parent().latex(view()->buffer(), os, false, false);
569 return STRCONV(os.str());
573 void InsetInclude::PreviewImpl::startMonitoring()
575 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
576 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
581 void InsetInclude::PreviewImpl::restartLoading()
583 lyxerr << "restartLoading()" << std::endl;
586 view()->updateInset(&parent(), false);