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"
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" // contains
33 #include "support/tostr.h"
35 #include "graphics/PreviewedInset.h"
36 #include "graphics/PreviewImage.h"
38 #include <boost/bind.hpp>
48 extern BufferList bufferlist;
51 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
54 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
57 bool previewWanted() const;
59 string const latexString() const;
61 InsetInclude & parent() const {
62 return *static_cast<InsetInclude*>(inset());
66 bool monitoring() const { return monitor_.get(); }
68 void startMonitoring();
70 void stopMonitoring() { monitor_.reset(); }
73 /// Invoked by monitor_ should the parent file change.
74 void restartLoading();
76 boost::scoped_ptr<FileMonitor> monitor_;
82 string const uniqueID()
84 static unsigned int seed = 1000;
87 ost << "file" << ++seed;
89 // Needed if we use lyxstring.
90 return STRCONV(ost.str());
96 InsetInclude::InsetInclude(Params const & p)
97 : params_(p), include_label(uniqueID()),
98 preview_(new PreviewImpl(*this))
102 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
103 : include_label(uniqueID()),
104 preview_(new PreviewImpl(*this))
107 params_.masterFilename_ = b.fileName();
111 InsetInclude::~InsetInclude()
113 InsetIncludeMailer mailer(*this);
118 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
120 dispatch_result result = UNDISPATCHED;
122 switch (cmd.action) {
123 case LFUN_INSET_MODIFY: {
124 InsetInclude::Params p;
125 InsetIncludeMailer::string2params(cmd.argument, p);
126 if (p.cparams.getCmdName().empty())
130 params_.masterFilename_ = cmd.view()->buffer()->fileName();
132 cmd.view()->updateInset(this);
137 case LFUN_INSET_DIALOG_UPDATE: {
138 InsetIncludeMailer mailer(*this);
139 mailer.updateDialog(cmd.view());
143 case LFUN_MOUSE_RELEASE:
144 edit(cmd.view(), cmd.x, cmd.y, cmd.button());
155 InsetInclude::Params const & InsetInclude::params() const
161 bool InsetInclude::Params::operator==(Params const & o) const
163 if (cparams == o.cparams && flag == o.flag &&
164 masterFilename_ == o.masterFilename_)
171 bool InsetInclude::Params::operator!=(Params const & o) const
173 return !(*this == o);
177 void InsetInclude::set(Params const & p)
183 switch (params_.flag) {
188 command="verbatiminput";
194 command="verbatiminput*";
198 params_.cparams.setCmdName(command);
200 if (preview_->monitoring())
201 preview_->stopMonitoring();
203 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
204 preview_->generatePreview();
208 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
211 p.masterFilename_ = buffer.fileName();
213 return new InsetInclude(p);
217 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
219 InsetIncludeMailer mailer(*this);
220 mailer.showDialog(bv);
224 void InsetInclude::edit(BufferView * bv, bool)
226 edit(bv, 0, 0, mouse_button::none);
230 void InsetInclude::write(Buffer const *, ostream & os) const
232 os << "Include " << params_.cparams.getCommand() << '\n'
233 << "preview " << tostr(params_.cparams.preview()) << '\n';
237 void InsetInclude::read(Buffer const *, LyXLex & lex)
239 params_.cparams.read(lex);
241 if (params_.cparams.getCmdName() == "include")
242 params_.flag = INCLUDE;
243 else if (params_.cparams.getCmdName() == "input")
244 params_.flag = INPUT;
245 /* FIXME: is this logic necessary now ? */
246 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
248 if (params_.cparams.getCmdName() == "verbatiminput*")
249 params_.flag = VERBAST;
254 bool InsetInclude::display() const
256 return !(params_.flag == INPUT);
260 string const InsetInclude::getScreenLabel(Buffer const *) const
264 switch (params_.flag) {
265 case INPUT: temp += _("Input"); break;
266 case VERB: temp += _("Verbatim Input"); break;
267 case VERBAST: temp += _("Verbatim Input*"); break;
268 case INCLUDE: temp += _("Include"); break;
273 if (params_.cparams.getContents().empty())
276 temp += params_.cparams.getContents();
282 string const InsetInclude::getFileName() const
284 return MakeAbsPath(params_.cparams.getContents(),
285 OnlyPath(getMasterFilename()));
289 string const InsetInclude::getMasterFilename() const
291 return params_.masterFilename_;
295 bool InsetInclude::loadIfNeeded() const
300 if (!IsLyXFilename(getFileName()))
303 if (bufferlist.exists(getFileName()))
306 // the readonly flag can/will be wrong, not anymore I think.
307 FileInfo finfo(getFileName());
311 return bufferlist.loadLyXFile(getFileName(), false) != 0;
315 int InsetInclude::latex(Buffer const * buffer, ostream & os,
316 bool /*fragile*/, bool /*fs*/) const
318 string incfile(params_.cparams.getContents());
320 // Do nothing if no file name has been specified
324 if (loadIfNeeded()) {
325 Buffer * tmp = bufferlist.getBuffer(getFileName());
327 // FIXME: this should be a GUI warning
328 if (tmp->params.textclass != buffer->params.textclass) {
329 lyxerr << "WARNING: Included file `"
330 << MakeDisplayPath(getFileName())
331 << "' has textclass `"
332 << tmp->params.getLyXTextClass().name()
333 << "' while parent file has textclass `"
334 << buffer->params.getLyXTextClass().name()
339 // write it to a file (so far the complete file)
340 string writefile = ChangeExtension(getFileName(), ".tex");
342 if (!buffer->tmppath.empty()
343 && !buffer->niceFile) {
344 incfile = subst(incfile, '/','@');
346 incfile = subst(incfile, ':', '$');
348 writefile = AddName(buffer->tmppath, incfile);
350 writefile = getFileName();
351 writefile = ChangeExtension(writefile, ".tex");
352 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
353 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
355 tmp->markDepClean(buffer->tmppath);
357 tmp->makeLaTeXFile(writefile,
358 OnlyPath(getMasterFilename()),
359 buffer->niceFile, true);
363 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
364 } else if (params_.flag == INPUT) {
365 // \input wants file with extension (default is .tex)
366 if (!IsLyXFilename(getFileName())) {
367 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
369 os << '\\' << params_.cparams.getCmdName() << '{'
370 << ChangeExtension(incfile, ".tex")
374 // \include don't want extension and demands that the
375 // file really have .tex
376 os << '\\' << params_.cparams.getCmdName() << '{'
377 << ChangeExtension(incfile, string())
385 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
388 os << GetFileContents(getFileName());
393 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
395 string incfile(params_.cparams.getContents());
397 // Do nothing if no file name has been specified
401 if (loadIfNeeded()) {
402 Buffer * tmp = bufferlist.getBuffer(getFileName());
404 // write it to a file (so far the complete file)
405 string writefile = ChangeExtension(getFileName(), ".sgml");
406 if (!buffer->tmppath.empty() && !buffer->niceFile) {
407 incfile = subst(incfile, '/','@');
408 writefile = AddName(buffer->tmppath, incfile);
410 writefile = getFileName();
412 if (IsLyXFilename(getFileName()))
413 writefile = ChangeExtension(writefile, ".sgml");
415 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
416 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
418 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
423 << GetFileContents(getFileName())
426 os << '&' << include_label << ';';
432 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
433 bool /*mixcont*/) const
435 string incfile(params_.cparams.getContents());
437 // Do nothing if no file name has been specified
441 if (loadIfNeeded()) {
442 Buffer * tmp = bufferlist.getBuffer(getFileName());
444 // write it to a file (so far the complete file)
445 string writefile = ChangeExtension(getFileName(), ".sgml");
446 if (!buffer->tmppath.empty() && !buffer->niceFile) {
447 incfile = subst(incfile, '/','@');
448 writefile = AddName(buffer->tmppath, incfile);
450 writefile = getFileName();
451 if (IsLyXFilename(getFileName()))
452 writefile = ChangeExtension(writefile, ".sgml");
454 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
455 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
457 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
461 os << "<inlinegraphic fileref=\""
462 << '&' << include_label << ';'
463 << "\" format=\"linespecific\">";
465 os << '&' << include_label << ';';
471 void InsetInclude::validate(LaTeXFeatures & features) const
474 string incfile(params_.cparams.getContents());
477 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
479 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
480 incfile = subst(incfile, '/','@');
481 writefile = AddName(b->tmppath, incfile);
483 writefile = getFileName();
485 if (IsLyXFilename(getFileName()))
486 writefile = ChangeExtension(writefile, ".sgml");
488 features.includeFile(include_label, writefile);
491 features.require("verbatim");
493 // Here we must do the fun stuff...
494 // Load the file in the include if it needs
496 if (loadIfNeeded()) {
498 Buffer * const tmp = bufferlist.getBuffer(getFileName());
501 tmp->niceFile = b->niceFile;
502 tmp->validate(features);
508 vector<string> const InsetInclude::getLabelList() const
512 if (loadIfNeeded()) {
513 Buffer * tmp = bufferlist.getBuffer(getFileName());
514 tmp->setParentName("");
515 l = tmp->getLabelList();
516 tmp->setParentName(getMasterFilename());
523 void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
525 if (loadIfNeeded()) {
526 Buffer * tmp = bufferlist.getBuffer(getFileName());
527 tmp->setParentName("");
528 tmp->fillWithBibKeys(keys);
529 tmp->setParentName(getMasterFilename());
534 int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
536 return preview_->previewReady() ?
537 preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
541 int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
543 return preview_->previewReady() ?
544 preview_->pimage()->descent() : InsetButton::descent(bv, font);
548 int InsetInclude::width(BufferView * bv, LyXFont const & font) const
550 return preview_->previewReady() ?
551 preview_->pimage()->width() : InsetButton::width(bv, font);
555 void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
559 if (!preview_->previewReady()) {
560 InsetButton::draw(bv, font, y, xx);
564 if (!preview_->monitoring())
565 preview_->startMonitoring();
567 int const x = int(xx);
568 int const w = width(bv, font);
569 int const d = descent(bv, font);
570 int const a = ascent(bv, font);
573 bv->painter().image(x, y - a, w, h,
574 *(preview_->pimage()->image()));
584 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
586 preview_->addPreview(ploader);
590 bool InsetInclude::PreviewImpl::previewWanted() const
592 return parent().params_.flag == InsetInclude::INPUT &&
593 parent().params_.cparams.preview() &&
594 IsFileReadable(parent().getFileName());
598 string const InsetInclude::PreviewImpl::latexString() const
600 if (!view() || !view()->buffer())
604 parent().latex(view()->buffer(), os, false, false);
606 return STRCONV(os.str());
610 void InsetInclude::PreviewImpl::startMonitoring()
612 monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
613 monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
618 void InsetInclude::PreviewImpl::restartLoading()
620 lyxerr << "restartLoading()" << std::endl;
623 view()->updateInset(&parent());
628 string const InsetIncludeMailer::name_("include");
630 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
635 string const InsetIncludeMailer::inset2string() const
637 return params2string(inset_.params());
641 void InsetIncludeMailer::string2params(string const & in,
642 InsetInclude::Params & params)
644 params = InsetInclude::Params();
649 istringstream data(in);
655 string const token = lex.getString();
660 // This is part of the inset proper that is usually swallowed
661 // by Buffer::readInset
664 string const token = lex.getString();
665 if (token != "Include")
670 InsetInclude inset(params);
672 params = inset.params();
678 InsetIncludeMailer::params2string(InsetInclude::Params const & params)
680 InsetInclude inset(params);
683 data << name_ << ' ';
684 inset.write(0, data);
685 data << "\\end_inset\n";