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/renderers.h"
15 #include "insets/ExternalSupport.h"
16 #include "insets/ExternalTemplate.h"
19 #include "BufferView.h"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
24 #include "latexrunparams.h"
28 #include "frontends/lyx_gui.h"
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/tostr.h"
33 #include "support/translator.h"
35 #include <boost/bind.hpp>
37 #include "support/std_sstream.h"
39 namespace support = lyx::support;
40 namespace external = lyx::external;
45 using std::istringstream;
47 using std::ostringstream;
54 /// The translator between the DisplayType and the corresponding lyx string.
55 extern Translator<DisplayType, string> displayTranslator;
57 } // namespace graphics
63 lyx::graphics::DisplayType const defaultDisplayType = lyx::graphics::NoDisplay;
65 unsigned int defaultLyxScale = 100;
75 tempname_ = support::tempName(string(), "lyxext");
76 support::unlink(tempname_);
77 // must have an extension for the converter code to work correctly.
82 TempName::TempName(TempName const &)
84 tempname_ = TempName()();
90 support::unlink(tempname_);
95 TempName::operator=(TempName const & other)
98 tempname_ = TempName()();
102 } // namespace external
106 InsetExternalParams::InsetExternalParams()
107 : display(defaultDisplayType),
108 lyxscale(defaultLyxScale)
114 template <typename T>
115 void clearIfNotFound(T & data, external::TransformID value,
116 vector<external::TransformID> const & ids)
118 typedef vector<external::TransformID>::const_iterator
121 const_iterator it = ids.begin();
122 const_iterator end = ids.end();
123 it = std::find(it, end, value);
131 void InsetExternalParams::settemplate(string const & name)
133 templatename_ = name;
135 external::TemplateManager const & etm =
136 external::TemplateManager::get();
137 external::Template const * const et = etm.getTemplateByName(name);
139 // Be safe. Don't lose data.
142 // Ascertain which transforms the template supports.
143 // Empty all those that it doesn't.
144 vector<external::TransformID> const & ids = et->transformIds;
145 clearIfNotFound(clipdata, external::Clip, ids);
146 clearIfNotFound(extradata, external::Extra, ids);
147 clearIfNotFound(resizedata, external::Resize, ids);
148 clearIfNotFound(rotationdata, external::Rotate, ids);
152 void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
155 << "\ttemplate " << templatename() << '\n';
157 if (!filename.empty())
159 << filename.outputFilename(buffer.filePath())
162 if (display != defaultDisplayType)
164 << lyx::graphics::displayTranslator.find(display)
167 if (lyxscale != defaultLyxScale)
168 os << "\tlyxscale " << tostr(lyxscale) << '\n';
170 if (!clipdata.bbox.empty())
171 os << "\tboundingBox " << clipdata.bbox << '\n';
175 external::ExtraData::const_iterator it = extradata.begin();
176 external::ExtraData::const_iterator end = extradata.end();
177 for (; it != end; ++it) {
178 if (!it->second.empty())
179 os << "\textra " << it->first << " \""
180 << it->second << "\"\n";
183 if (!rotationdata.no_rotation()) {
184 os << "\trotateAngle " << rotationdata.angle() << '\n';
185 if (rotationdata.origin() != external::RotationData::DEFAULT)
186 os << "\trotateOrigin "
187 << rotationdata.originString() << '\n';
190 if (!resizedata.no_resize()) {
191 using support::float_equal;
193 if (!float_equal(resizedata.scale, 0.0, 0.05)) {
194 if (!float_equal(resizedata.scale, 100.0, 0.05))
196 << resizedata.scale << '\n';
198 if (!resizedata.width.zero())
200 << resizedata.width.asString() << '\n';
201 if (!resizedata.height.zero())
203 << resizedata.height.asString() << '\n';
205 if (resizedata.keepAspectRatio)
206 os << "\tkeepAspectRatio\n";
211 bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
230 keyword_item external_tags[] = {
231 { "\\end_inset", EX_END },
232 { "boundingBox", EX_BOUNDINGBOX },
234 { "display", EX_DISPLAY},
235 { "extra", EX_EXTRA },
236 { "filename", EX_FILENAME},
237 { "height", EX_HEIGHT },
238 { "keepAspectRatio", EX_KEEPASPECTRATIO },
239 { "lyxscale", EX_LYXSCALE},
240 { "rotateAngle", EX_ROTATEANGLE },
241 { "rotateOrigin", EX_ROTATEORIGIN },
242 { "scale", EX_SCALE },
243 { "template", EX_TEMPLATE },
244 { "width", EX_WIDTH }
247 pushpophelper pph(lex, external_tags, EX_END);
249 bool found_end = false;
250 bool read_error = false;
256 templatename_ = lex.getString();
261 string const name = lex.getString();
262 filename.set(name, buffer.filePath());
268 string const name = lex.getString();
269 display = lyx::graphics::displayTranslator.find(name);
275 lyxscale = lex.getInteger();
280 clipdata.bbox.xl = lex.getInteger();
282 clipdata.bbox.yb = lex.getInteger();
284 clipdata.bbox.xr = lex.getInteger();
286 clipdata.bbox.yt = lex.getInteger();
290 clipdata.clip = true;
295 string const name = lex.getString();
297 extradata.set(name, lex.getString());
303 resizedata.height = LyXLength(lex.getString());
306 case EX_KEEPASPECTRATIO:
307 resizedata.keepAspectRatio = true;
312 rotationdata.angle(lex.getFloat());
315 case EX_ROTATEORIGIN:
317 rotationdata.origin(lex.getString());
322 resizedata.scale = lex.getFloat();
327 resizedata.width = LyXLength(lex.getString());
335 lex.printError("ExternalInset::read: "
336 "Wrong tag: $$Token");
341 if (found_end || read_error)
346 lex.printError("ExternalInset::read: Missing \\end_inset.");
348 // This is a trick to make sure that the data are self-consistent.
349 settemplate(templatename_);
351 if (lyxerr.debugging(Debug::EXTERNAL)) {
352 lyxerr << "InsetExternalParams::read:\n";
353 write(buffer, lyxerr);
360 InsetExternal::InsetExternal()
361 : renderer_(new ButtonRenderer)
365 InsetExternal::InsetExternal(InsetExternal const & other)
367 boost::signals::trackable(),
368 params_(other.params_),
369 renderer_(other.renderer_->clone())
371 GraphicRenderer * ptr =
372 dynamic_cast<GraphicRenderer *>(renderer_.get());
374 ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
378 auto_ptr<InsetBase> InsetExternal::clone() const
380 return auto_ptr<InsetBase>(new InsetExternal(*this));
384 InsetExternal::~InsetExternal()
386 InsetExternalMailer(*this).hideDialog();
390 void InsetExternal::statusChanged()
392 BufferView * const bv = renderer_->view();
394 bv->updateInset(this);
398 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
400 switch (cmd.action) {
402 case LFUN_EXTERNAL_EDIT: {
403 BOOST_ASSERT(cmd.view());
405 Buffer const & buffer = *cmd.view()->buffer();
406 InsetExternalParams p;
407 InsetExternalMailer::string2params(cmd.argument, buffer, p);
408 external::editExternal(p, buffer);
409 return DISPATCHED_NOUPDATE;
412 case LFUN_INSET_MODIFY: {
413 BOOST_ASSERT(cmd.view());
415 Buffer const & buffer = *cmd.view()->buffer();
416 InsetExternalParams p;
417 InsetExternalMailer::string2params(cmd.argument, buffer, p);
418 setParams(p, buffer);
419 cmd.view()->updateInset(this);
423 case LFUN_INSET_DIALOG_UPDATE:
424 InsetExternalMailer(*this).updateDialog(cmd.view());
427 case LFUN_MOUSE_RELEASE:
428 case LFUN_INSET_EDIT:
429 InsetExternalMailer(*this).showDialog(cmd.view());
438 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
440 renderer_->metrics(mi, dim);
445 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
447 renderer_->draw(pi, x, y);
453 lyx::graphics::Params get_grfx_params(InsetExternalParams const & eparams)
455 lyx::graphics::Params gparams;
457 gparams.filename = eparams.filename.absFilename();
458 gparams.scale = eparams.lyxscale;
459 if (eparams.clipdata.clip)
460 gparams.bb = eparams.clipdata.bbox;
461 gparams.angle = eparams.rotationdata.angle();
463 gparams.display = eparams.display;
464 if (gparams.display == lyx::graphics::DefaultDisplay)
465 gparams.display = lyxrc.display_graphics;
466 // Override the above if we're not using a gui
467 if (!lyx_gui::use_gui)
468 gparams.display = lyx::graphics::NoDisplay;
474 string const getScreenLabel(InsetExternalParams const & params,
475 Buffer const & buffer)
477 external::Template const * const ptr =
478 external::getTemplatePtr(params);
480 return support::bformat(_("External template %1$s is not installed"),
481 params.templatename());
482 return external::doSubstitution(params, buffer, ptr->guiName);
488 InsetExternalParams const & InsetExternal::params() const
494 void InsetExternal::setParams(InsetExternalParams const & p,
495 Buffer const & buffer)
497 // The stored params; what we would like to happen in an ideal world.
500 // We display the inset as a button by default.
501 bool display_button = (!external::getTemplatePtr(params_) ||
502 params_.filename.empty() ||
503 params_.display == lyx::graphics::NoDisplay);
505 if (display_button) {
506 ButtonRenderer * button_ptr =
507 dynamic_cast<ButtonRenderer *>(renderer_.get());
509 button_ptr = new ButtonRenderer;
510 renderer_.reset(button_ptr);
513 button_ptr->update(getScreenLabel(params_, buffer), true);
516 GraphicRenderer * graphic_ptr =
517 dynamic_cast<GraphicRenderer *>(renderer_.get());
519 graphic_ptr = new GraphicRenderer;
520 graphic_ptr->connect(
521 boost::bind(&InsetExternal::statusChanged, this));
522 renderer_.reset(graphic_ptr);
525 graphic_ptr->update(get_grfx_params(params_));
530 void InsetExternal::write(Buffer const & buffer, ostream & os) const
532 params_.write(buffer, os);
536 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
538 InsetExternalParams params;
539 if (params.read(buffer, lex))
540 setParams(params, buffer);
544 int InsetExternal::latex(Buffer const & buf, ostream & os,
545 LatexRunParams const & runparams) const
547 // "nice" means that the buffer is exported to LaTeX format but not
548 // run through the LaTeX compiler.
549 // If we're running through the LaTeX compiler, we should write the
550 // generated files in the bufer's temporary directory.
551 bool const external_in_tmpdir =
552 lyxrc.use_tempdir && !buf.temppath().empty() && !runparams.nice;
554 // If the template has specified a PDFLaTeX output, then we try and
556 if (runparams.flavor == LatexRunParams::PDFLATEX) {
557 external::Template const * const et_ptr =
558 external::getTemplatePtr(params_);
561 external::Template const & et = *et_ptr;
563 external::Template::Formats::const_iterator cit =
564 et.formats.find("PDFLaTeX");
565 if (cit != et.formats.end())
566 return external::writeExternal(params_, "PDFLaTeX",
567 buf, os, external_in_tmpdir);
570 return external::writeExternal(params_, "LaTeX",
571 buf, os, external_in_tmpdir);
575 int InsetExternal::ascii(Buffer const & buf, ostream & os, int) const
577 return external::writeExternal(params_, "Ascii", buf, os);
581 int InsetExternal::linuxdoc(Buffer const & buf, ostream & os) const
583 return external::writeExternal(params_, "LinuxDoc", buf, os);
587 int InsetExternal::docbook(Buffer const & buf, ostream & os, bool) const
589 return external::writeExternal(params_, "DocBook", buf, os);
593 void InsetExternal::validate(LaTeXFeatures & features) const
595 external::Template const * const et_ptr =
596 external::getTemplatePtr(params_);
599 external::Template const & et = *et_ptr;
601 external::Template::Formats::const_iterator cit =
602 et.formats.find("LaTeX");
603 if (cit == et.formats.end())
606 if (!cit->second.requirement.empty())
607 features.require(cit->second.requirement);
609 external::TemplateManager & etm = external::TemplateManager::get();
611 vector<string>::const_iterator it = cit->second.preambleNames.begin();
612 vector<string>::const_iterator end = cit->second.preambleNames.end();
613 for (; it != end; ++it) {
614 string const preamble = etm.getPreambleDefByName(*it);
615 if (!preamble.empty())
616 features.addExternalPreamble(preamble);
621 string const InsetExternalMailer::name_("external");
623 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
628 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
630 return params2string(inset_.params(), buffer);
634 void InsetExternalMailer::string2params(string const & in,
635 Buffer const & buffer,
636 InsetExternalParams & params)
638 params = InsetExternalParams();
643 istringstream data(in);
649 string const token = lex.getString();
654 // This is part of the inset proper that is usually swallowed
655 // by Buffer::readInset
658 string const token = lex.getString();
659 if (token != "External")
664 params.read(buffer, lex);
670 InsetExternalMailer::params2string(InsetExternalParams const & params,
671 Buffer const & buffer)
674 data << name_ << ' ';
675 params.write(buffer, data);
676 data << "\\end_inset\n";