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 #pragma implementation
16 #include "insetinclude.h"
18 #include "bufferlist.h"
19 #include "BufferView.h"
22 #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 ost.str().c_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()
116 InsetInclude::Params const & InsetInclude::params() const
122 bool InsetInclude::Params::operator==(Params const & o) const
124 if (cparams == o.cparams && flag == o.flag &&
125 masterFilename_ == o.masterFilename_)
132 bool InsetInclude::Params::operator!=(Params const & o) const
134 return !(*this == o);
138 void InsetInclude::set(Params const & p)
142 if (preview_->monitoring())
143 preview_->stopMonitoring();
145 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
146 preview_->generatePreview();
150 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
153 p.masterFilename_ = buffer.fileName();
155 return new InsetInclude(p);
159 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
161 bv->owner()->getDialogs().showInclude(this);
165 void InsetInclude::edit(BufferView * bv, bool)
167 edit(bv, 0, 0, mouse_button::none);
171 void InsetInclude::write(Buffer const *, ostream & os) const
173 os << "Include " << params_.cparams.getCommand() << '\n'
174 << "preview " << tostr(params_.cparams.preview()) << '\n';
178 void InsetInclude::read(Buffer const *, LyXLex & lex)
180 params_.cparams.read(lex);
182 if (params_.cparams.getCmdName() == "include")
183 params_.flag = INCLUDE;
184 else if (params_.cparams.getCmdName() == "input")
185 params_.flag = INPUT;
186 /* FIXME: is this logic necessary now ? */
187 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
189 if (params_.cparams.getCmdName() == "verbatiminput*")
190 params_.flag = VERBAST;
195 bool InsetInclude::display() const
197 return !(params_.flag == INPUT);
201 string const InsetInclude::getScreenLabel(Buffer const *) const
205 switch (params_.flag) {
206 case INPUT: temp += _("Input"); break;
207 case VERB: temp += _("Verbatim Input"); break;
208 case VERBAST: temp += _("Verbatim Input*"); break;
209 case INCLUDE: temp += _("Include"); break;
214 if (params_.cparams.getContents().empty())
217 temp += params_.cparams.getContents();
223 string const InsetInclude::getRelFileBaseName() const
225 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
229 string const InsetInclude::getFileName() const
231 return MakeAbsPath(params_.cparams.getContents(),
232 OnlyPath(getMasterFilename()));
236 string const InsetInclude::getMasterFilename() const
238 return params_.masterFilename_;
242 bool InsetInclude::loadIfNeeded() const
247 if (!IsLyXFilename(getFileName()))
250 if (bufferlist.exists(getFileName()))
253 // the readonly flag can/will be wrong, not anymore I think.
254 FileInfo finfo(getFileName());
258 return bufferlist.readFile(getFileName(), !finfo.writable()) != 0;
262 int InsetInclude::latex(Buffer const * buffer, ostream & os,
263 bool /*fragile*/, bool /*fs*/) const
265 string incfile(params_.cparams.getContents());
267 // Do nothing if no file name has been specified
271 if (loadIfNeeded()) {
272 Buffer * tmp = bufferlist.getBuffer(getFileName());
274 // FIXME: this should be a GUI warning
275 if (tmp->params.textclass != buffer->params.textclass) {
276 lyxerr << "WARNING: Included file `"
277 << MakeDisplayPath(getFileName())
278 << "' has textclass `"
279 << tmp->params.getLyXTextClass().name()
280 << "' while parent file has textclass `"
281 << buffer->params.getLyXTextClass().name()
286 // write it to a file (so far the complete file)
287 string writefile = ChangeExtension(getFileName(), ".tex");
289 if (!buffer->tmppath.empty()
290 && !buffer->niceFile) {
291 incfile = subst(incfile, '/','@');
293 incfile = subst(incfile, ':', '$');
295 writefile = AddName(buffer->tmppath, incfile);
297 writefile = getFileName();
298 writefile = ChangeExtension(writefile, ".tex");
299 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
300 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
302 tmp->markDepClean(buffer->tmppath);
304 tmp->makeLaTeXFile(writefile,
305 OnlyPath(getMasterFilename()),
306 buffer->niceFile, true);
310 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
311 } else if (params_.flag == INPUT) {
312 // \input wants file with extension (default is .tex)
313 if (!IsLyXFilename(getFileName())) {
314 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
316 os << '\\' << params_.cparams.getCmdName() << '{'
317 << ChangeExtension(incfile, ".tex")
321 // \include don't want extension and demands that the
322 // file really have .tex
323 os << '\\' << params_.cparams.getCmdName() << '{'
324 << ChangeExtension(incfile, string())
332 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
335 os << GetFileContents(getFileName());
340 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
342 string incfile(params_.cparams.getContents());
344 // Do nothing if no file name has been specified
348 if (loadIfNeeded()) {
349 Buffer * tmp = bufferlist.getBuffer(getFileName());
351 // write it to a file (so far the complete file)
352 string writefile = ChangeExtension(getFileName(), ".sgml");
353 if (!buffer->tmppath.empty() && !buffer->niceFile) {
354 incfile = subst(incfile, '/','@');
355 writefile = AddName(buffer->tmppath, incfile);
357 writefile = getFileName();
359 if (IsLyXFilename(getFileName()))
360 writefile = ChangeExtension(writefile, ".sgml");
362 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
363 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
365 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
370 << GetFileContents(getFileName())
373 os << '&' << include_label << ';';
379 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
380 bool /*mixcont*/) const
382 string incfile(params_.cparams.getContents());
384 // Do nothing if no file name has been specified
388 if (loadIfNeeded()) {
389 Buffer * tmp = bufferlist.getBuffer(getFileName());
391 // write it to a file (so far the complete file)
392 string writefile = ChangeExtension(getFileName(), ".sgml");
393 if (!buffer->tmppath.empty() && !buffer->niceFile) {
394 incfile = subst(incfile, '/','@');
395 writefile = AddName(buffer->tmppath, incfile);
397 writefile = getFileName();
398 if (IsLyXFilename(getFileName()))
399 writefile = ChangeExtension(writefile, ".sgml");
401 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
402 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
404 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
408 os << "<inlinegraphic fileref=\""
409 << '&' << include_label << ';'
410 << "\" format=\"linespecific\">";
412 os << '&' << include_label << ';';
418 void InsetInclude::validate(LaTeXFeatures & features) const
421 string incfile(params_.cparams.getContents());
424 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
426 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
427 incfile = subst(incfile, '/','@');
428 writefile = AddName(b->tmppath, incfile);
430 writefile = getFileName();
432 if (IsLyXFilename(getFileName()))
433 writefile = ChangeExtension(writefile, ".sgml");
435 features.includeFile(include_label, writefile);
438 features.require("verbatim");
440 // Here we must do the fun stuff...
441 // Load the file in the include if it needs
443 if (loadIfNeeded()) {
445 Buffer * const tmp = bufferlist.getBuffer(getFileName());
448 tmp->niceFile = b->niceFile;
449 tmp->validate(features);
455 vector<string> const InsetInclude::getLabelList() const
459 if (loadIfNeeded()) {
460 Buffer * tmp = bufferlist.getBuffer(getFileName());
461 tmp->setParentName("");
462 l = tmp->getLabelList();
463 tmp->setParentName(getMasterFilename());
470 vector<pair<string,string> > const InsetInclude::getKeys() const
472 vector<pair<string,string> > keys;
474 if (loadIfNeeded()) {
475 Buffer * tmp = bufferlist.getBuffer(getFileName());
476 tmp->setParentName("");
477 keys = tmp->getBibkeyList();
478 tmp->setParentName(getMasterFilename());
485 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
487 return preview_->previewReady() ?
488 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
492 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
494 return preview_->previewReady() ?
495 preview_->pimage()->descent() : InsetButton::descent(bv, font);
499 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
501 return preview_->previewReady() ?
502 preview_->pimage()->width() : InsetButton::width(bv, font);
506 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
507 float & xx, bool b) const
509 preview_->setView(bv);
510 if (!preview_->previewReady()) {
511 InsetButton::draw(bv, font, y, xx, b);
515 if (!preview_->monitoring())
516 preview_->startMonitoring();
518 int const x = int(xx);
519 int const w = width(bv, font);
520 int const d = descent(bv, font);
521 int const a = ascent(bv, font);
524 bv->painter().image(x, y - a, w, h,
525 *(preview_->pimage()->image(*this, *bv)));
535 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
537 preview_->addPreview(ploader);
541 bool InsetInclude::PreviewImpl::previewWanted() const
543 return parent().params_.flag == InsetInclude::INPUT &&
544 parent().params_.cparams.preview() &&
545 IsFileReadable(parent().getFileName());
549 string const InsetInclude::PreviewImpl::latexString() const
551 if (!view() || !view()->buffer())
555 parent().latex(view()->buffer(), os, false, false);
557 return os.str().c_str();
561 void InsetInclude::PreviewImpl::startMonitoring()
563 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
564 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
569 void InsetInclude::PreviewImpl::restartLoading()
571 lyxerr << "restartLoading()" << std::endl;
574 view()->updateInset(&parent(), false);