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 noload == o.noload && masterFilename_ == o.masterFilename_)
123 bool InsetInclude::Params::operator!=(Params const & o) const
125 return !(*this == o);
129 void InsetInclude::set(Params const & p)
133 // Just to be safe...
136 switch (params_.flag) {
141 command="verbatiminput";
147 command="verbatiminput*";
151 params_.cparams.setCmdName(command);
153 if (preview_->monitoring())
154 preview_->stopMonitoring();
156 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
157 preview_->generatePreview();
161 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
164 p.masterFilename_ = buffer.fileName();
166 return new InsetInclude(p);
170 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
172 bv->owner()->getDialogs()->showInclude(this);
176 void InsetInclude::edit(BufferView * bv, bool)
178 edit(bv, 0, 0, mouse_button::none);
182 void InsetInclude::write(Buffer const *, ostream & os) const
184 os << "Include " << params_.cparams.getCommand() << '\n'
185 << "preview " << tostr(params_.cparams.preview()) << '\n';
189 void InsetInclude::read(Buffer const *, LyXLex & lex)
191 params_.cparams.read(lex);
193 if (params_.cparams.getCmdName() == "include")
194 params_.flag = INCLUDE;
195 else if (params_.cparams.getCmdName() == "input")
196 params_.flag = INPUT;
197 /* FIXME: is this logic necessary now ? */
198 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
200 if (params_.cparams.getCmdName() == "verbatiminput*")
201 params_.flag = VERBAST;
206 bool InsetInclude::display() const
208 return !(params_.flag == INPUT);
212 string const InsetInclude::getScreenLabel(Buffer const *) const
216 switch (params_.flag) {
217 case INPUT: temp += _("Input"); break;
218 case VERB: temp += _("Verbatim Input"); break;
219 case VERBAST: temp += _("Verbatim Input*"); break;
220 case INCLUDE: temp += _("Include"); break;
225 if (params_.cparams.getContents().empty())
228 temp += params_.cparams.getContents();
234 string const InsetInclude::getRelFileBaseName() const
236 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
240 string const InsetInclude::getFileName() const
242 return MakeAbsPath(params_.cparams.getContents(),
243 OnlyPath(getMasterFilename()));
247 string const InsetInclude::getMasterFilename() const
249 return params_.masterFilename_;
253 bool InsetInclude::loadIfNeeded() const
255 if (params_.noload || isVerbatim())
258 if (!IsLyXFilename(getFileName()))
261 if (bufferlist.exists(getFileName()))
264 // the readonly flag can/will be wrong, not anymore I think.
265 FileInfo finfo(getFileName());
269 return bufferlist.readFile(getFileName(), !finfo.writable()) != 0;
273 int InsetInclude::latex(Buffer const * buffer, ostream & os,
274 bool /*fragile*/, bool /*fs*/) const
276 string incfile(params_.cparams.getContents());
278 // Do nothing if no file name has been specified
282 if (loadIfNeeded()) {
283 Buffer * tmp = bufferlist.getBuffer(getFileName());
285 // FIXME: this should be a GUI warning
286 if (tmp->params.textclass != buffer->params.textclass) {
287 lyxerr << "WARNING: Included file `"
288 << MakeDisplayPath(getFileName())
289 << "' has textclass `"
290 << tmp->params.getLyXTextClass().name()
291 << "' while parent file has textclass `"
292 << buffer->params.getLyXTextClass().name()
297 // write it to a file (so far the complete file)
298 string writefile = ChangeExtension(getFileName(), ".tex");
300 if (!buffer->tmppath.empty()
301 && !buffer->niceFile) {
302 incfile = subst(incfile, '/','@');
304 incfile = subst(incfile, ':', '$');
306 writefile = AddName(buffer->tmppath, incfile);
308 writefile = getFileName();
309 writefile = ChangeExtension(writefile, ".tex");
310 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
311 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
313 tmp->markDepClean(buffer->tmppath);
315 tmp->makeLaTeXFile(writefile,
316 OnlyPath(getMasterFilename()),
317 buffer->niceFile, true);
321 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
322 } else if (params_.flag == INPUT) {
323 // \input wants file with extension (default is .tex)
324 if (!IsLyXFilename(getFileName())) {
325 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
327 os << '\\' << params_.cparams.getCmdName() << '{'
328 << ChangeExtension(incfile, ".tex")
332 // \include don't want extension and demands that the
333 // file really have .tex
334 os << '\\' << params_.cparams.getCmdName() << '{'
335 << ChangeExtension(incfile, string())
343 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
346 os << GetFileContents(getFileName());
351 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
353 string incfile(params_.cparams.getContents());
355 // Do nothing if no file name has been specified
359 if (loadIfNeeded()) {
360 Buffer * tmp = bufferlist.getBuffer(getFileName());
362 // write it to a file (so far the complete file)
363 string writefile = ChangeExtension(getFileName(), ".sgml");
364 if (!buffer->tmppath.empty() && !buffer->niceFile) {
365 incfile = subst(incfile, '/','@');
366 writefile = AddName(buffer->tmppath, incfile);
368 writefile = getFileName();
370 if (IsLyXFilename(getFileName()))
371 writefile = ChangeExtension(writefile, ".sgml");
373 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
374 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
376 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
381 << GetFileContents(getFileName())
384 os << '&' << include_label << ';';
390 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
391 bool /*mixcont*/) const
393 string incfile(params_.cparams.getContents());
395 // Do nothing if no file name has been specified
399 if (loadIfNeeded()) {
400 Buffer * tmp = bufferlist.getBuffer(getFileName());
402 // write it to a file (so far the complete file)
403 string writefile = ChangeExtension(getFileName(), ".sgml");
404 if (!buffer->tmppath.empty() && !buffer->niceFile) {
405 incfile = subst(incfile, '/','@');
406 writefile = AddName(buffer->tmppath, incfile);
408 writefile = getFileName();
409 if (IsLyXFilename(getFileName()))
410 writefile = ChangeExtension(writefile, ".sgml");
412 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
413 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
415 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
419 os << "<inlinegraphic fileref=\""
420 << '&' << include_label << ';'
421 << "\" format=\"linespecific\">";
423 os << '&' << include_label << ';';
429 void InsetInclude::validate(LaTeXFeatures & features) const
432 string incfile(params_.cparams.getContents());
435 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
437 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
438 incfile = subst(incfile, '/','@');
439 writefile = AddName(b->tmppath, incfile);
441 writefile = getFileName();
443 if (IsLyXFilename(getFileName()))
444 writefile = ChangeExtension(writefile, ".sgml");
446 features.includeFile(include_label, writefile);
449 features.require("verbatim");
451 // Here we must do the fun stuff...
452 // Load the file in the include if it needs
454 if (loadIfNeeded()) {
456 Buffer * const tmp = bufferlist.getBuffer(getFileName());
459 tmp->niceFile = b->niceFile;
460 tmp->validate(features);
466 vector<string> const InsetInclude::getLabelList() const
470 if (loadIfNeeded()) {
471 Buffer * tmp = bufferlist.getBuffer(getFileName());
472 tmp->setParentName("");
473 l = tmp->getLabelList();
474 tmp->setParentName(getMasterFilename());
481 vector<pair<string,string> > const InsetInclude::getKeys() const
483 vector<pair<string,string> > keys;
485 if (loadIfNeeded()) {
486 Buffer * tmp = bufferlist.getBuffer(getFileName());
487 tmp->setParentName("");
488 keys = tmp->getBibkeyList();
489 tmp->setParentName(getMasterFilename());
496 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
498 return preview_->previewReady() ?
499 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
503 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
505 return preview_->previewReady() ?
506 preview_->pimage()->descent() : InsetButton::descent(bv, font);
510 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
512 return preview_->previewReady() ?
513 preview_->pimage()->width() : InsetButton::width(bv, font);
517 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
518 float & xx, bool b) const
520 preview_->setView(bv);
521 if (!preview_->previewReady()) {
522 InsetButton::draw(bv, font, y, xx, b);
526 if (!preview_->monitoring())
527 preview_->startMonitoring();
529 int const x = int(xx);
530 int const w = width(bv, font);
531 int const d = descent(bv, font);
532 int const a = ascent(bv, font);
535 bv->painter().image(x, y - a, w, h,
536 *(preview_->pimage()->image(*this, *bv)));
546 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
548 preview_->addPreview(ploader);
552 bool InsetInclude::PreviewImpl::previewWanted() const
554 return parent().params_.flag == InsetInclude::INPUT &&
555 parent().params_.cparams.preview() &&
556 IsFileReadable(parent().getFileName());
560 string const InsetInclude::PreviewImpl::latexString() const
562 if (!view() || !view()->buffer())
566 parent().latex(view()->buffer(), os, false, false);
568 // This fails if the file has a relative path.
569 // return os.str().c_str();
571 // I would /really/ like not to do this, but don't know how to tell
572 // LaTeX where to find a \input-ed file...
575 string file = rtrim(split(os.str().c_str(), command, '{'), "}");
577 if (!AbsolutePath(file))
578 file = MakeAbsPath(file, view()->buffer()->filePath());
581 out << command << '{' << file << '}' << endl;
583 return out.str().c_str();
587 void InsetInclude::PreviewImpl::startMonitoring()
589 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
590 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
595 void InsetInclude::PreviewImpl::restartLoading()
597 lyxerr << "restartLoading()" << std::endl;
600 view()->updateInset(&parent(), false);