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)
112 void InsetExternalParams::settemplate(string const & name)
114 templatename_ = name;
118 void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
121 << "\ttemplate " << templatename() << '\n';
123 if (!filename.empty())
125 << filename.outputFilename(buffer.filePath())
128 if (display != defaultDisplayType)
130 << lyx::graphics::displayTranslator.find(display)
133 if (lyxscale != defaultLyxScale)
134 os << "\tlyxscale " << tostr(lyxscale) << '\n';
138 bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
148 keyword_item external_tags[] = {
149 { "\\end_inset", EX_END },
150 { "display", EX_DISPLAY},
151 { "filename", EX_FILENAME},
152 { "lyxscale", EX_LYXSCALE},
153 { "template", EX_TEMPLATE }
156 pushpophelper pph(lex, external_tags, EX_END);
158 bool found_end = false;
159 bool read_error = false;
165 templatename_ = lex.getString();
170 string const name = lex.getString();
171 filename.set(name, buffer.filePath());
177 string const name = lex.getString();
178 display = lyx::graphics::displayTranslator.find(name);
184 lyxscale = lex.getInteger();
192 lex.printError("ExternalInset::read: "
193 "Wrong tag: $$Token");
198 if (found_end || read_error)
203 lex.printError("ExternalInset::read: Missing \\end_inset.");
205 // This is a trick to make sure that the data are self-consistent.
206 settemplate(templatename_);
208 if (lyxerr.debugging(Debug::EXTERNAL)) {
209 lyxerr << "InsetExternalParams::read:\n";
210 write(buffer, lyxerr);
217 InsetExternal::InsetExternal()
218 : renderer_(new ButtonRenderer)
222 InsetExternal::InsetExternal(InsetExternal const & other)
224 boost::signals::trackable(),
225 params_(other.params_),
226 renderer_(other.renderer_->clone())
228 GraphicRenderer * ptr =
229 dynamic_cast<GraphicRenderer *>(renderer_.get());
231 ptr->connect(boost::bind(&InsetExternal::statusChanged, this));
235 auto_ptr<InsetBase> InsetExternal::clone() const
237 return auto_ptr<InsetBase>(new InsetExternal(*this));
241 InsetExternal::~InsetExternal()
243 InsetExternalMailer(*this).hideDialog();
247 void InsetExternal::statusChanged()
249 BufferView * const bv = renderer_->view();
251 bv->updateInset(this);
255 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
257 switch (cmd.action) {
259 case LFUN_EXTERNAL_EDIT: {
260 BOOST_ASSERT(cmd.view());
262 Buffer const & buffer = *cmd.view()->buffer();
263 InsetExternalParams p;
264 InsetExternalMailer::string2params(cmd.argument, buffer, p);
265 external::editExternal(p, buffer);
266 return DISPATCHED_NOUPDATE;
269 case LFUN_INSET_MODIFY: {
270 BOOST_ASSERT(cmd.view());
272 Buffer const & buffer = *cmd.view()->buffer();
273 InsetExternalParams p;
274 InsetExternalMailer::string2params(cmd.argument, buffer, p);
275 setParams(p, buffer);
276 cmd.view()->updateInset(this);
280 case LFUN_INSET_DIALOG_UPDATE:
281 InsetExternalMailer(*this).updateDialog(cmd.view());
284 case LFUN_MOUSE_RELEASE:
285 case LFUN_INSET_EDIT:
286 InsetExternalMailer(*this).showDialog(cmd.view());
295 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
297 renderer_->metrics(mi, dim);
302 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
304 renderer_->draw(pi, x, y);
310 lyx::graphics::Params get_grfx_params(InsetExternalParams const & eparams)
312 lyx::graphics::Params gparams;
314 gparams.filename = eparams.filename.absFilename();
315 gparams.scale = eparams.lyxscale;
317 gparams.display = eparams.display;
318 if (gparams.display == lyx::graphics::DefaultDisplay)
319 gparams.display = lyxrc.display_graphics;
320 // Override the above if we're not using a gui
321 if (!lyx_gui::use_gui)
322 gparams.display = lyx::graphics::NoDisplay;
328 string const getScreenLabel(InsetExternalParams const & params,
329 Buffer const & buffer)
331 external::Template const * const ptr =
332 external::getTemplatePtr(params);
334 return support::bformat(_("External template %1$s is not installed"),
335 params.templatename());
336 return external::doSubstitution(params, buffer, ptr->guiName);
342 InsetExternalParams const & InsetExternal::params() const
348 void InsetExternal::setParams(InsetExternalParams const & p,
349 Buffer const & buffer)
351 // The stored params; what we would like to happen in an ideal world.
354 // We display the inset as a button by default.
355 bool display_button = (!external::getTemplatePtr(params_) ||
356 params_.filename.empty() ||
357 params_.display == lyx::graphics::NoDisplay);
359 if (display_button) {
360 ButtonRenderer * button_ptr =
361 dynamic_cast<ButtonRenderer *>(renderer_.get());
363 button_ptr = new ButtonRenderer;
364 renderer_.reset(button_ptr);
367 button_ptr->update(getScreenLabel(params_, buffer), true);
370 GraphicRenderer * graphic_ptr =
371 dynamic_cast<GraphicRenderer *>(renderer_.get());
373 graphic_ptr = new GraphicRenderer;
374 graphic_ptr->connect(
375 boost::bind(&InsetExternal::statusChanged, this));
376 renderer_.reset(graphic_ptr);
379 graphic_ptr->update(get_grfx_params(params_));
384 void InsetExternal::write(Buffer const & buffer, ostream & os) const
386 params_.write(buffer, os);
390 void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
392 InsetExternalParams params;
393 if (params.read(buffer, lex))
394 setParams(params, buffer);
398 int InsetExternal::latex(Buffer const & buf, ostream & os,
399 LatexRunParams const & runparams) const
401 // "nice" means that the buffer is exported to LaTeX format but not
402 // run through the LaTeX compiler.
403 // If we're running through the LaTeX compiler, we should write the
404 // generated files in the bufer's temporary directory.
405 bool const external_in_tmpdir =
406 lyxrc.use_tempdir && !buf.temppath().empty() && !runparams.nice;
408 // If the template has specified a PDFLaTeX output, then we try and
410 if (runparams.flavor == LatexRunParams::PDFLATEX) {
411 external::Template const * const et_ptr =
412 external::getTemplatePtr(params_);
415 external::Template const & et = *et_ptr;
417 external::Template::Formats::const_iterator cit =
418 et.formats.find("PDFLaTeX");
419 if (cit != et.formats.end())
420 return external::writeExternal(params_, "PDFLaTeX",
421 buf, os, external_in_tmpdir);
424 return external::writeExternal(params_, "LaTeX",
425 buf, os, external_in_tmpdir);
429 int InsetExternal::ascii(Buffer const & buf, ostream & os, int) const
431 return external::writeExternal(params_, "Ascii", buf, os);
435 int InsetExternal::linuxdoc(Buffer const & buf, ostream & os) const
437 return external::writeExternal(params_, "LinuxDoc", buf, os);
441 int InsetExternal::docbook(Buffer const & buf, ostream & os, bool) const
443 return external::writeExternal(params_, "DocBook", buf, os);
447 void InsetExternal::validate(LaTeXFeatures & features) const
449 external::Template const * const et_ptr =
450 external::getTemplatePtr(params_);
453 external::Template const & et = *et_ptr;
455 external::Template::Formats::const_iterator cit =
456 et.formats.find("LaTeX");
457 if (cit == et.formats.end())
460 if (!cit->second.requirement.empty())
461 features.require(cit->second.requirement);
463 external::TemplateManager & etm = external::TemplateManager::get();
465 vector<string>::const_iterator it = cit->second.preambleNames.begin();
466 vector<string>::const_iterator end = cit->second.preambleNames.end();
467 for (; it != end; ++it) {
468 string const preamble = etm.getPreambleDefByName(*it);
469 if (!preamble.empty())
470 features.addExternalPreamble(preamble);
475 string const InsetExternalMailer::name_("external");
477 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
482 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
484 return params2string(inset_.params(), buffer);
488 void InsetExternalMailer::string2params(string const & in,
489 Buffer const & buffer,
490 InsetExternalParams & params)
492 params = InsetExternalParams();
497 istringstream data(in);
503 string const token = lex.getString();
508 // This is part of the inset proper that is usually swallowed
509 // by Buffer::readInset
512 string const token = lex.getString();
513 if (token != "External")
518 params.read(buffer, lex);
524 InsetExternalMailer::params2string(InsetExternalParams const & params,
525 Buffer const & buffer)
528 data << name_ << ' ';
529 params.write(buffer, data);
530 data << "\\end_inset\n";