7 #include "insetinclude.h"
9 #include "bufferlist.h"
10 #include "BufferView.h"
13 #include "LaTeXFeatures.h"
16 #include "frontends/Dialogs.h"
17 #include "frontends/LyXView.h"
18 #include "frontends/Painter.h"
20 #include "support/filetools.h"
21 #include "support/FileInfo.h"
22 #include "support/FileMonitor.h"
23 #include "support/lstrings.h"
25 #include "graphics/PreviewedInset.h"
26 #include "graphics/PreviewImage.h"
28 #include <boost/bind.hpp>
38 extern BufferList bufferlist;
41 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
44 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
47 bool previewWanted() const;
49 string const latexString() const;
51 InsetInclude & parent() const {
52 return *static_cast<InsetInclude*>(inset());
56 bool monitoring() const { return monitor_.get(); }
58 void startMonitoring();
60 void stopMonitoring() { monitor_.reset(); }
63 /// Invoked by monitor_ should the parent file change.
64 void restartLoading();
66 boost::scoped_ptr<FileMonitor> monitor_;
72 string const uniqueID()
74 static unsigned int seed = 1000;
77 ost << "file" << ++seed;
79 // Needed if we use lyxstring.
80 return ost.str().c_str();
86 InsetInclude::InsetInclude(Params const & p)
87 : params_(p), include_label(uniqueID()),
88 preview_(new PreviewImpl(*this))
92 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
93 : include_label(uniqueID()),
94 preview_(new PreviewImpl(*this))
97 params_.masterFilename_ = b.fileName();
101 InsetInclude::~InsetInclude()
107 InsetInclude::Params const & InsetInclude::params() const
113 bool InsetInclude::Params::operator==(Params const & o) const
115 if (cparams == o.cparams && flag == o.flag &&
116 masterFilename_ == o.masterFilename_)
123 bool InsetInclude::Params::operator!=(Params const & o) const
125 return !(*this == o);
129 void InsetInclude::set(Params const & p)
133 if (preview_->monitoring())
134 preview_->stopMonitoring();
136 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
137 preview_->generatePreview();
141 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
144 p.masterFilename_ = buffer.fileName();
146 return new InsetInclude(p);
150 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
152 bv->owner()->getDialogs().showInclude(this);
156 void InsetInclude::edit(BufferView * bv, bool)
158 edit(bv, 0, 0, mouse_button::none);
162 void InsetInclude::write(Buffer const *, ostream & os) const
164 os << "Include " << params_.cparams.getCommand() << '\n'
165 << "preview " << tostr(params_.cparams.preview()) << '\n';
169 void InsetInclude::read(Buffer const *, LyXLex & lex)
171 params_.cparams.read(lex);
173 if (params_.cparams.getCmdName() == "include")
174 params_.flag = INCLUDE;
175 else if (params_.cparams.getCmdName() == "input")
176 params_.flag = INPUT;
177 /* FIXME: is this logic necessary now ? */
178 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
180 if (params_.cparams.getCmdName() == "verbatiminput*")
181 params_.flag = VERBAST;
186 bool InsetInclude::display() const
188 return !(params_.flag == INPUT);
192 string const InsetInclude::getScreenLabel(Buffer const *) const
196 switch (params_.flag) {
197 case INPUT: temp += _("Input"); break;
198 case VERB: temp += _("Verbatim Input"); break;
199 case VERBAST: temp += _("Verbatim Input*"); break;
200 case INCLUDE: temp += _("Include"); break;
205 if (params_.cparams.getContents().empty())
208 temp += params_.cparams.getContents();
214 string const InsetInclude::getRelFileBaseName() const
216 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
220 string const InsetInclude::getFileName() const
222 return MakeAbsPath(params_.cparams.getContents(),
223 OnlyPath(getMasterFilename()));
227 string const InsetInclude::getMasterFilename() const
229 return params_.masterFilename_;
233 bool InsetInclude::loadIfNeeded() const
238 if (!IsLyXFilename(getFileName()))
241 if (bufferlist.exists(getFileName()))
244 // the readonly flag can/will be wrong, not anymore I think.
245 FileInfo finfo(getFileName());
249 return bufferlist.readFile(getFileName(), !finfo.writable()) != 0;
253 int InsetInclude::latex(Buffer const * buffer, ostream & os,
254 bool /*fragile*/, bool /*fs*/) const
256 string incfile(params_.cparams.getContents());
258 // Do nothing if no file name has been specified
262 if (loadIfNeeded()) {
263 Buffer * tmp = bufferlist.getBuffer(getFileName());
265 // FIXME: this should be a GUI warning
266 if (tmp->params.textclass != buffer->params.textclass) {
267 lyxerr << "WARNING: Included file `"
268 << MakeDisplayPath(getFileName())
269 << "' has textclass `"
270 << tmp->params.getLyXTextClass().name()
271 << "' while parent file has textclass `"
272 << buffer->params.getLyXTextClass().name()
277 // write it to a file (so far the complete file)
278 string writefile = ChangeExtension(getFileName(), ".tex");
280 if (!buffer->tmppath.empty()
281 && !buffer->niceFile) {
282 incfile = subst(incfile, '/','@');
284 incfile = subst(incfile, ':', '$');
286 writefile = AddName(buffer->tmppath, incfile);
288 writefile = getFileName();
289 writefile = ChangeExtension(writefile, ".tex");
290 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
291 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
293 tmp->markDepClean(buffer->tmppath);
295 tmp->makeLaTeXFile(writefile,
296 OnlyPath(getMasterFilename()),
297 buffer->niceFile, true);
301 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
302 } else if (params_.flag == INPUT) {
303 // \input wants file with extension (default is .tex)
304 if (!IsLyXFilename(getFileName())) {
305 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
307 os << '\\' << params_.cparams.getCmdName() << '{'
308 << ChangeExtension(incfile, ".tex")
312 // \include don't want extension and demands that the
313 // file really have .tex
314 os << '\\' << params_.cparams.getCmdName() << '{'
315 << ChangeExtension(incfile, string())
323 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
326 os << GetFileContents(getFileName());
331 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
333 string incfile(params_.cparams.getContents());
335 // Do nothing if no file name has been specified
339 if (loadIfNeeded()) {
340 Buffer * tmp = bufferlist.getBuffer(getFileName());
342 // write it to a file (so far the complete file)
343 string writefile = ChangeExtension(getFileName(), ".sgml");
344 if (!buffer->tmppath.empty() && !buffer->niceFile) {
345 incfile = subst(incfile, '/','@');
346 writefile = AddName(buffer->tmppath, incfile);
348 writefile = getFileName();
350 if (IsLyXFilename(getFileName()))
351 writefile = ChangeExtension(writefile, ".sgml");
353 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
354 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
356 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
361 << GetFileContents(getFileName())
364 os << '&' << include_label << ';';
370 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
371 bool /*mixcont*/) const
373 string incfile(params_.cparams.getContents());
375 // Do nothing if no file name has been specified
379 if (loadIfNeeded()) {
380 Buffer * tmp = bufferlist.getBuffer(getFileName());
382 // write it to a file (so far the complete file)
383 string writefile = ChangeExtension(getFileName(), ".sgml");
384 if (!buffer->tmppath.empty() && !buffer->niceFile) {
385 incfile = subst(incfile, '/','@');
386 writefile = AddName(buffer->tmppath, incfile);
388 writefile = getFileName();
389 if (IsLyXFilename(getFileName()))
390 writefile = ChangeExtension(writefile, ".sgml");
392 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
393 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
395 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
399 os << "<inlinegraphic fileref=\""
400 << '&' << include_label << ';'
401 << "\" format=\"linespecific\">";
403 os << '&' << include_label << ';';
409 void InsetInclude::validate(LaTeXFeatures & features) const
412 string incfile(params_.cparams.getContents());
415 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
417 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
418 incfile = subst(incfile, '/','@');
419 writefile = AddName(b->tmppath, incfile);
421 writefile = getFileName();
423 if (IsLyXFilename(getFileName()))
424 writefile = ChangeExtension(writefile, ".sgml");
426 features.includeFile(include_label, writefile);
429 features.require("verbatim");
431 // Here we must do the fun stuff...
432 // Load the file in the include if it needs
434 if (loadIfNeeded()) {
436 Buffer * const tmp = bufferlist.getBuffer(getFileName());
439 tmp->niceFile = b->niceFile;
440 tmp->validate(features);
446 vector<string> const InsetInclude::getLabelList() const
450 if (loadIfNeeded()) {
451 Buffer * tmp = bufferlist.getBuffer(getFileName());
452 tmp->setParentName("");
453 l = tmp->getLabelList();
454 tmp->setParentName(getMasterFilename());
461 vector<pair<string,string> > const InsetInclude::getKeys() const
463 vector<pair<string,string> > keys;
465 if (loadIfNeeded()) {
466 Buffer * tmp = bufferlist.getBuffer(getFileName());
467 tmp->setParentName("");
468 keys = tmp->getBibkeyList();
469 tmp->setParentName(getMasterFilename());
476 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
478 return preview_->previewReady() ?
479 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
483 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
485 return preview_->previewReady() ?
486 preview_->pimage()->descent() : InsetButton::descent(bv, font);
490 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
492 return preview_->previewReady() ?
493 preview_->pimage()->width() : InsetButton::width(bv, font);
497 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
498 float & xx, bool b) const
500 preview_->setView(bv);
501 if (!preview_->previewReady()) {
502 InsetButton::draw(bv, font, y, xx, b);
506 if (!preview_->monitoring())
507 preview_->startMonitoring();
509 int const x = int(xx);
510 int const w = width(bv, font);
511 int const d = descent(bv, font);
512 int const a = ascent(bv, font);
515 bv->painter().image(x, y - a, w, h,
516 *(preview_->pimage()->image(*this, *bv)));
526 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
528 preview_->addPreview(ploader);
532 bool InsetInclude::PreviewImpl::previewWanted() const
534 return parent().params_.flag == InsetInclude::INPUT &&
535 parent().params_.cparams.preview() &&
536 IsFileReadable(parent().getFileName());
540 string const InsetInclude::PreviewImpl::latexString() const
542 if (!view() || !view()->buffer())
546 parent().latex(view()->buffer(), os, false, false);
548 return os.str().c_str();
552 void InsetInclude::PreviewImpl::startMonitoring()
554 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
555 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
560 void InsetInclude::PreviewImpl::restartLoading()
562 lyxerr << "restartLoading()" << std::endl;
565 view()->updateInset(&parent(), false);