2 * \file insetexternal.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
8 * Full author contact details are available in file CREDITS.
13 #include "insetexternal.h"
14 #include "insets/ExternalSupport.h"
15 #include "insets/ExternalTemplate.h"
16 #include "insets/render_button.h"
17 #include "insets/render_graphic.h"
20 #include "BufferView.h"
22 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
25 #include "latexrunparams.h"
29 #include "metricsinfo.h"
31 #include "frontends/lyx_gui.h"
32 #include "frontends/LyXView.h"
34 #include "support/lstrings.h"
35 #include "support/lyxlib.h"
36 #include "support/tostr.h"
37 #include "support/translator.h"
39 #include <boost/bind.hpp>
41 #include "support/std_sstream.h"
43 namespace support = lyx::support;
44 namespace external = lyx::external;
49 using std::istringstream;
51 using std::ostringstream;
57 lyx::graphics::DisplayType const defaultDisplayType = lyx::graphics::NoDisplay;
59 unsigned int defaultLyxScale = 100;
69 tempname_ = support::tempName(string(), "lyxext");
70 support::unlink(tempname_);
71 // must have an extension for the converter code to work correctly.
76 TempName::TempName(TempName const &)
78 tempname_ = TempName()();
84 support::unlink(tempname_);
89 TempName::operator=(TempName const & other)
92 tempname_ = TempName()();
96 } // namespace external
100 InsetExternalParams::InsetExternalParams()
101 : display(defaultDisplayType),
102 lyxscale(defaultLyxScale)
108 template <typename T>
109 void clearIfNotFound(T & data, external::TransformID value,
110 vector<external::TransformID> const & ids)
112 typedef vector<external::TransformID>::const_iterator
115 const_iterator it = ids.begin();
116 const_iterator end = ids.end();
117 it = std::find(it, end, value);
125 void InsetExternalParams::settemplate(string const & name)
127 templatename_ = name;
129 external::TemplateManager const & etm =
130 external::TemplateManager::get();
131 external::Template const * const et = etm.getTemplateByName(name);
133 // Be safe. Don't lose data.
136 // Ascertain which transforms the template supports.
137 // Empty all those that it doesn't.
138 vector<external::TransformID> const & ids = et->transformIds;
139 clearIfNotFound(clipdata, external::Clip, ids);
140 clearIfNotFound(extradata, external::Extra, ids);
141 clearIfNotFound(resizedata, external::Resize, ids);
142 clearIfNotFound(rotationdata, external::Rotate, ids);
146 void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
149 << "\ttemplate " << templatename() << '\n';
151 if (!filename.empty())
153 << filename.outputFilename(buffer.filePath())
156 if (display != defaultDisplayType)
158 << lyx::graphics::displayTranslator().find(display)
161 if (lyxscale != defaultLyxScale)
162 os << "\tlyxscale " << tostr(lyxscale) << '\n';
164 if (!clipdata.bbox.empty())
165 os << "\tboundingBox " << clipdata.bbox << '\n';
169 external::ExtraData::const_iterator it = extradata.begin();
170 external::ExtraData::const_iterator end = extradata.end();
171 for (; it != end; ++it) {
172 if (!it->second.empty())
173 os << "\textra " << it->first << " \""
174 << it->second << "\"\n";
177 if (!rotationdata.no_rotation()) {
178 os << "\trotateAngle " << rotationdata.angle() << '\n';
179 if (rotationdata.origin() != external::RotationData::DEFAULT)
180 os << "\trotateOrigin "
181 << rotationdata.originString() << '\n';
184 if (!resizedata.no_resize()) {
185 using support::float_equal;
187 if (!float_equal(resizedata.scale, 0.0, 0.05)) {
188 if (!float_equal(resizedata.scale, 100.0, 0.05))
190 << resizedata.scale << '\n';
192 if (!resizedata.width.zero())
194 << resizedata.width.asString() << '\n';
195 if (!resizedata.height.zero())
197 << resizedata.height.asString() << '\n';
199 if (resizedata.keepAspectRatio)
200 os << "\tkeepAspectRatio\n";
205 bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
224 keyword_item external_tags[] = {
225 { "\\end_inset", EX_END },
226 { "boundingBox", EX_BOUNDINGBOX },
228 { "display", EX_DISPLAY},
229 { "extra", EX_EXTRA },
230 { "filename", EX_FILENAME},
231 { "height", EX_HEIGHT },
232 { "keepAspectRatio", EX_KEEPASPECTRATIO },
233 { "lyxscale", EX_LYXSCALE},
234 { "rotateAngle", EX_ROTATEANGLE },
235 { "rotateOrigin", EX_ROTATEORIGIN },
236 { "scale", EX_SCALE },
237 { "template", EX_TEMPLATE },
238 { "width", EX_WIDTH }
241 pushpophelper pph(lex, external_tags, EX_END);
243 bool found_end = false;
244 bool read_error = false;
250 templatename_ = lex.getString();
255 string const name = lex.getString();
256 filename.set(name, buffer.filePath());
262 string const name = lex.getString();
263 display = lyx::graphics::displayTranslator().find(name);
269 lyxscale = lex.getInteger();
274 clipdata.bbox.xl = lex.getInteger();
276 clipdata.bbox.yb = lex.getInteger();
278 clipdata.bbox.xr = lex.getInteger();
280 clipdata.bbox.yt = lex.getInteger();
284 clipdata.clip = true;
289 string const name = lex.getString();
291 extradata.set(name, lex.getString());
297 resizedata.height = LyXLength(lex.getString());
300 case EX_KEEPASPECTRATIO:
301 resizedata.keepAspectRatio = true;
306 rotationdata.angle(lex.getFloat());
309 case EX_ROTATEORIGIN:
311 rotationdata.origin(lex.getString());
316 resizedata.scale = lex.getFloat();
321 resizedata.width = LyXLength(lex.getString());
329 lex.printError("ExternalInset::read: "
330 "Wrong tag: $$Token");
335 if (found_end || read_error)
340 lex.printError("ExternalInset::read: Missing \\end_inset.");
342 // This is a trick to make sure that the data are self-consistent.
343 settemplate(templatename_);
345 if (lyxerr.debugging(Debug::EXTERNAL)) {
346 lyxerr << "InsetExternalParams::read:\n";
347 write(buffer, lyxerr);
354 InsetExternal::InsetExternal()
355 : renderer_(new RenderButton)
359 InsetExternal::InsetExternal(InsetExternal const & other)
361 boost::signals::trackable(),
362 params_(other.params_),
363 renderer_(other.renderer_->clone())
365 RenderGraphic * ptr =
366 dynamic_cast<RenderGraphic *>(renderer_.get());
368 ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
372 auto_ptr<InsetBase> InsetExternal::clone() const
374 return auto_ptr<InsetBase>(new InsetExternal(*this));
378 InsetExternal::~InsetExternal()
380 InsetExternalMailer(*this).hideDialog();
384 void InsetExternal::statusChanged() const
386 LyX::cref().updateInset(this);
391 InsetExternal::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
393 switch (cmd.action) {
395 case LFUN_EXTERNAL_EDIT: {
396 BOOST_ASSERT(cmd.view());
398 Buffer const & buffer = *cmd.view()->buffer();
399 InsetExternalParams p;
400 InsetExternalMailer::string2params(cmd.argument, buffer, p);
401 external::editExternal(p, buffer);
402 return DISPATCHED_NOUPDATE;
405 case LFUN_INSET_MODIFY: {
406 BOOST_ASSERT(cmd.view());
408 Buffer const & buffer = *cmd.view()->buffer();
409 InsetExternalParams p;
410 InsetExternalMailer::string2params(cmd.argument, buffer, p);
411 setParams(p, buffer);
412 cmd.view()->updateInset(this);
416 case LFUN_INSET_DIALOG_UPDATE:
417 InsetExternalMailer(*this).updateDialog(cmd.view());
420 case LFUN_MOUSE_RELEASE:
421 case LFUN_INSET_EDIT:
422 InsetExternalMailer(*this).showDialog(cmd.view());
431 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
433 renderer_->metrics(mi, dim);
438 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
440 renderer_->draw(pi, x, y);
446 lyx::graphics::Params get_grfx_params(InsetExternalParams const & eparams)
448 lyx::graphics::Params gparams;
450 gparams.filename = eparams.filename.absFilename();
451 gparams.scale = eparams.lyxscale;
452 if (eparams.clipdata.clip)
453 gparams.bb = eparams.clipdata.bbox;
454 gparams.angle = eparams.rotationdata.angle();
456 gparams.display = eparams.display;
457 if (gparams.display == lyx::graphics::DefaultDisplay)
458 gparams.display = lyxrc.display_graphics;
459 // Override the above if we're not using a gui
460 if (!lyx_gui::use_gui)
461 gparams.display = lyx::graphics::NoDisplay;
467 string const getScreenLabel(InsetExternalParams const & params,
468 Buffer const & buffer)
470 external::Template const * const ptr =
471 external::getTemplatePtr(params);
473 return support::bformat(_("External template %1$s is not installed"),
474 params.templatename());
475 return external::doSubstitution(params, buffer, ptr->guiName);
481 InsetExternalParams const & InsetExternal::params() const
487 void InsetExternal::setParams(InsetExternalParams const & p,
488 Buffer const & buffer)
490 // The stored params; what we would like to happen in an ideal world.
493 // We display the inset as a button by default.
494 bool display_button = (!external::getTemplatePtr(params_) ||
495 params_.filename.empty() ||
496 params_.display == lyx::graphics::NoDisplay);
498 if (display_button) {
499 RenderButton * button_ptr =
500 dynamic_cast<RenderButton *>(renderer_.get());
502 button_ptr = new RenderButton;
503 renderer_.reset(button_ptr);
506 button_ptr->update(getScreenLabel(params_, buffer), true);
509 RenderGraphic * graphic_ptr =
510 dynamic_cast<RenderGraphic *>(renderer_.get());
512 graphic_ptr = new RenderGraphic;
513 graphic_ptr->connect(
514 boost::bind(&InsetExternal::statusChanged, this));
515 renderer_.reset(graphic_ptr);
518 graphic_ptr->update(get_grfx_params(params_));
523 void InsetExternal::write(Buffer const & buffer, ostream & os) const
525 params_.write(buffer, os);
529 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
531 InsetExternalParams params;
532 if (params.read(buffer, lex))
533 setParams(params, buffer);
537 int InsetExternal::latex(Buffer const & buf, ostream & os,
538 LatexRunParams const & runparams) const
540 // "nice" means that the buffer is exported to LaTeX format but not
541 // run through the LaTeX compiler.
542 // If we're running through the LaTeX compiler, we should write the
543 // generated files in the bufer's temporary directory.
544 bool const external_in_tmpdir =
545 lyxrc.use_tempdir && !buf.temppath().empty() && !runparams.nice;
547 // If the template has specified a PDFLaTeX output, then we try and
549 if (runparams.flavor == LatexRunParams::PDFLATEX) {
550 external::Template const * const et_ptr =
551 external::getTemplatePtr(params_);
554 external::Template const & et = *et_ptr;
556 external::Template::Formats::const_iterator cit =
557 et.formats.find("PDFLaTeX");
558 if (cit != et.formats.end())
559 return external::writeExternal(params_, "PDFLaTeX",
560 buf, os, external_in_tmpdir);
563 return external::writeExternal(params_, "LaTeX",
564 buf, os, external_in_tmpdir);
568 int InsetExternal::ascii(Buffer const & buf, ostream & os, int) const
570 return external::writeExternal(params_, "Ascii", buf, os);
574 int InsetExternal::linuxdoc(Buffer const & buf, ostream & os) const
576 return external::writeExternal(params_, "LinuxDoc", buf, os);
580 int InsetExternal::docbook(Buffer const & buf, ostream & os, bool) const
582 return external::writeExternal(params_, "DocBook", buf, os);
586 void InsetExternal::validate(LaTeXFeatures & features) const
588 external::Template const * const et_ptr =
589 external::getTemplatePtr(params_);
592 external::Template const & et = *et_ptr;
594 external::Template::Formats::const_iterator cit =
595 et.formats.find("LaTeX");
596 if (cit == et.formats.end())
599 if (!cit->second.requirement.empty())
600 features.require(cit->second.requirement);
602 external::TemplateManager & etm = external::TemplateManager::get();
604 vector<string>::const_iterator it = cit->second.preambleNames.begin();
605 vector<string>::const_iterator end = cit->second.preambleNames.end();
606 for (; it != end; ++it) {
607 string const preamble = etm.getPreambleDefByName(*it);
608 if (!preamble.empty())
609 features.addExternalPreamble(preamble);
614 string const InsetExternalMailer::name_("external");
616 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
621 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
623 return params2string(inset_.params(), buffer);
627 void InsetExternalMailer::string2params(string const & in,
628 Buffer const & buffer,
629 InsetExternalParams & params)
631 params = InsetExternalParams();
636 istringstream data(in);
642 string const token = lex.getString();
647 // This is part of the inset proper that is usually swallowed
648 // by Buffer::readInset
651 string const token = lex.getString();
652 if (token != "External")
657 params.read(buffer, lex);
663 InsetExternalMailer::params2string(InsetExternalParams const & params,
664 Buffer const & buffer)
667 data << name_ << ' ';
668 params.write(buffer, data);
669 data << "\\end_inset\n";