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/lstrings.h"
24 #include "graphics/PreviewedInset.h"
25 #include "graphics/PreviewImage.h"
35 extern BufferList bufferlist;
38 class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
41 PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
44 bool previewWanted() const;
46 string const latexString() const;
48 InsetInclude & parent() const {
49 return *static_cast<InsetInclude*>(inset());
56 string const uniqueID()
58 static unsigned int seed = 1000;
61 ost << "file" << ++seed;
63 // Needed if we use lyxstring.
64 return ost.str().c_str();
70 InsetInclude::InsetInclude(Params const & p)
71 : params_(p), include_label(uniqueID()),
72 preview_(new PreviewImpl(*this))
76 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
77 : include_label(uniqueID()),
78 preview_(new PreviewImpl(*this))
81 params_.masterFilename_ = b.fileName();
85 InsetInclude::~InsetInclude()
91 InsetInclude::Params const & InsetInclude::params() const
97 bool InsetInclude::Params::operator==(Params const & o) const
99 if (cparams == o.cparams && flag == o.flag &&
100 noload == o.noload && masterFilename_ == o.masterFilename_)
107 bool InsetInclude::Params::operator!=(Params const & o) const
109 return !(*this == o);
113 void InsetInclude::set(Params const & p)
117 // Just to be safe...
120 switch (params_.flag) {
125 command="verbatiminput";
131 command="verbatiminput*";
135 params_.cparams.setCmdName(command);
137 if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
138 preview_->generatePreview();
142 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
145 p.masterFilename_ = buffer.fileName();
147 return new InsetInclude(p);
151 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
153 bv->owner()->getDialogs()->showInclude(this);
157 void InsetInclude::edit(BufferView * bv, bool)
159 edit(bv, 0, 0, mouse_button::none);
163 void InsetInclude::write(Buffer const *, ostream & os) const
165 os << "Include " << params_.cparams.getCommand() << "\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
235 if (params_.noload || isVerbatim())
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 int const x = int(xx);
507 int const w = width(bv, font);
508 int const d = descent(bv, font);
509 int const a = ascent(bv, font);
512 bv->painter().image(x, y - a, w, h,
513 *(preview_->pimage()->image(*this, *bv)));
523 void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
525 preview_->addPreview(ploader);
529 bool InsetInclude::PreviewImpl::previewWanted() const
531 return parent().params_.flag == InsetInclude::INPUT;
535 string const InsetInclude::PreviewImpl::latexString() const
537 if (!view() || !view()->buffer())
541 parent().latex(view()->buffer(), os, false, false);
543 // This fails if the file has a relative path.
544 // return os.str().c_str();
546 // I would /really/ like not to do this...
549 string file = rtrim(split(os.str().c_str(), command, '{'), "}");
551 if (!AbsolutePath(file))
552 file = MakeAbsPath(file, view()->buffer()->filePath());
555 out << command << '{' << file << '}' << endl;
557 return out.str().c_str();