]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
Fix text frame drawing.
[lyx.git] / src / insets / InsetExternal.cpp
1 /**
2  * \file InsetExternal.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetExternal.h"
14 #include "insets/ExternalSupport.h"
15 #include "insets/ExternalTemplate.h"
16 #include "insets/RenderButton.h"
17 #include "insets/RenderGraphic.h"
18 #include "insets/RenderPreview.h"
19
20 #include "Buffer.h"
21 #include "Cursor.h"
22 #include "debug.h"
23 #include "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncStatus.h"
26 #include "FuncRequest.h"
27 #include "gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "LyX.h"
30 #include "Lexer.h"
31 #include "LyXRC.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.h"
34
35 #include "graphics/PreviewLoader.h"
36
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "support/lyxlib.h"
40 #include "support/convert.h"
41 #include "support/Translator.h"
42
43 #include <boost/bind.hpp>
44
45 #include <sstream>
46
47 using std::string;
48 using std::istringstream;
49 using std::ostream;
50 using std::ostringstream;
51 using std::vector;
52
53
54 namespace {
55
56 lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
57
58 unsigned int const defaultLyxScale = 100;
59
60 string defaultTemplateName;
61
62 } // namespace anon
63
64
65 namespace lyx {
66
67 namespace external {
68
69 TempName::TempName()
70 {
71         support::FileName const tempname(support::tempName(support::FileName(), "lyxext"));
72         // FIXME: This is unsafe
73         support::unlink(tempname);
74         // must have an extension for the converter code to work correctly.
75         tempname_ = support::FileName(tempname.absFilename() + ".tmp");
76 }
77
78
79 TempName::TempName(TempName const &)
80 {
81         tempname_ = TempName()();
82 }
83
84
85 TempName::~TempName()
86 {
87         support::unlink(tempname_);
88 }
89
90
91 TempName &
92 TempName::operator=(TempName const & other)
93 {
94         if (this != &other)
95                 tempname_ = TempName()();
96         return *this;
97 }
98
99
100 namespace {
101
102 /// The translator between the Display enum and corresponding lyx string.
103 Translator<DisplayType, string> const initTranslator()
104 {
105         Translator<DisplayType, string> translator(DefaultDisplay, "default");
106
107         // Fill the display translator
108         translator.addPair(MonochromeDisplay, "monochrome");
109         translator.addPair(GrayscaleDisplay, "grayscale");
110         translator.addPair(ColorDisplay, "color");
111         translator.addPair(PreviewDisplay, "preview");
112         translator.addPair(NoDisplay, "none");
113
114         return translator;
115 }
116
117 } // namespace anon
118
119
120 Translator<DisplayType, string> const & displayTranslator()
121 {
122         static Translator<DisplayType, string> const translator =
123                 initTranslator();
124         return translator;
125 }
126
127 } // namespace external
128
129
130 InsetExternalParams::InsetExternalParams()
131         : display(defaultDisplayType),
132           lyxscale(defaultLyxScale),
133           draft(false)
134 {
135         if (defaultTemplateName.empty()) {
136                 external::TemplateManager const & etm =
137                         external::TemplateManager::get();
138                 templatename_ = etm.getTemplates().begin()->first;
139         } else
140                 templatename_ = defaultTemplateName;
141 }
142
143
144 namespace {
145
146 template <typename T>
147 void clearIfNotFound(T & data, external::TransformID value,
148                      vector<external::TransformID> const & ids)
149 {
150         typedef vector<external::TransformID>::const_iterator
151                 const_iterator;
152
153         const_iterator it  = ids.begin();
154         const_iterator end = ids.end();
155         it = std::find(it, end, value);
156         if (it == end)
157                 data = T();
158 }
159
160 } // namespace anon
161
162
163 void InsetExternalParams::settemplate(string const & name)
164 {
165         templatename_ = name;
166
167         external::TemplateManager const & etm =
168                 external::TemplateManager::get();
169         external::Template const * const et = etm.getTemplateByName(name);
170         if (!et)
171                 // Be safe. Don't lose data.
172                 return;
173
174         // Ascertain which transforms the template supports.
175         // Empty all those that it doesn't.
176         vector<external::TransformID> const & ids = et->transformIds;
177         clearIfNotFound(clipdata,     external::Clip,   ids);
178         clearIfNotFound(extradata,    external::Extra,  ids);
179         clearIfNotFound(resizedata,   external::Resize, ids);
180         clearIfNotFound(rotationdata, external::Rotate, ids);
181 }
182
183
184 void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
185 {
186         os << "External\n"
187            << "\ttemplate " << templatename() << '\n';
188
189         if (!filename.empty())
190                 os << "\tfilename "
191                    << filename.outputFilename(buffer.filePath())
192                    << '\n';
193
194         if (display != defaultDisplayType)
195                 os << "\tdisplay "
196                    << external::displayTranslator().find(display)
197                    << '\n';
198
199         if (lyxscale != defaultLyxScale)
200                 os << "\tlyxscale " << convert<string>(lyxscale) << '\n';
201
202         if (draft)
203                 os << "\tdraft\n";
204
205         if (!clipdata.bbox.empty())
206                 os << "\tboundingBox " << clipdata.bbox << '\n';
207         if (clipdata.clip)
208                 os << "\tclip\n";
209
210         external::ExtraData::const_iterator it  = extradata.begin();
211         external::ExtraData::const_iterator end = extradata.end();
212         for (; it != end; ++it) {
213                 if (!it->second.empty())
214                         os << "\textra " << it->first << " \""
215                            << it->second << "\"\n";
216         }
217
218         if (!rotationdata.no_rotation()) {
219                 os << "\trotateAngle " << rotationdata.adjAngle() << '\n';
220                 if (rotationdata.origin() != external::RotationData::DEFAULT)
221                         os << "\trotateOrigin "
222                            << rotationdata.originString() << '\n';
223         }
224
225         if (!resizedata.no_resize()) {
226                 using support::float_equal;
227                 double const scl = convert<double>(resizedata.scale);
228                 if (!float_equal(scl, 0.0, 0.05)) {
229                         if (!float_equal(scl, 100.0, 0.05))
230                                 os << "\tscale "
231                                    << resizedata.scale << '\n';
232                 } else {
233                         if (!resizedata.width.zero())
234                                 os << "\twidth "
235                                    << resizedata.width.asString() << '\n';
236                         if (!resizedata.height.zero())
237                                 os << "\theight "
238                                    << resizedata.height.asString() << '\n';
239                 }
240                 if (resizedata.keepAspectRatio)
241                         os << "\tkeepAspectRatio\n";
242         }
243 }
244
245
246 bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
247 {
248         enum ExternalTags {
249                 EX_TEMPLATE = 1,
250                 EX_FILENAME,
251                 EX_DISPLAY,
252                 EX_LYXSCALE,
253                 EX_DRAFT,
254                 EX_BOUNDINGBOX,
255                 EX_CLIP,
256                 EX_EXTRA,
257                 EX_HEIGHT,
258                 EX_KEEPASPECTRATIO,
259                 EX_ROTATEANGLE,
260                 EX_ROTATEORIGIN,
261                 EX_SCALE,
262                 EX_WIDTH,
263                 EX_END
264         };
265
266         keyword_item external_tags[] = {
267                 { "\\end_inset",     EX_END },
268                 { "boundingBox",     EX_BOUNDINGBOX },
269                 { "clip",            EX_CLIP },
270                 { "display",         EX_DISPLAY},
271                 { "draft",           EX_DRAFT},
272                 { "extra",           EX_EXTRA },
273                 { "filename",        EX_FILENAME},
274                 { "height",          EX_HEIGHT },
275                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
276                 { "lyxscale",        EX_LYXSCALE},
277                 { "rotateAngle",     EX_ROTATEANGLE },
278                 { "rotateOrigin",    EX_ROTATEORIGIN },
279                 { "scale",           EX_SCALE },
280                 { "template",        EX_TEMPLATE },
281                 { "width",           EX_WIDTH }
282         };
283
284         PushPopHelper pph(lex, external_tags, EX_END);
285
286         bool found_end  = false;
287         bool read_error = false;
288
289         while (lex.isOK()) {
290                 switch (lex.lex()) {
291                 case EX_TEMPLATE:
292                         lex.next();
293                         templatename_ = lex.getString();
294                         break;
295
296                 case EX_FILENAME: {
297                         lex.eatLine();
298                         string const name = lex.getString();
299                         filename.set(name, buffer.filePath());
300                         break;
301                 }
302
303                 case EX_DISPLAY: {
304                         lex.next();
305                         string const name = lex.getString();
306                         display = external::displayTranslator().find(name);
307                         break;
308                 }
309
310                 case EX_LYXSCALE:
311                         lex.next();
312                         lyxscale = lex.getInteger();
313                         break;
314
315                 case EX_DRAFT:
316                         draft = true;
317                         break;
318
319                 case EX_BOUNDINGBOX:
320                         lex.next();
321                         clipdata.bbox.xl = lex.getInteger();
322                         lex.next();
323                         clipdata.bbox.yb = lex.getInteger();
324                         lex.next();
325                         clipdata.bbox.xr = lex.getInteger();
326                         lex.next();
327                         clipdata.bbox.yt = lex.getInteger();
328                         break;
329
330                 case EX_CLIP:
331                         clipdata.clip = true;
332                         break;
333
334                 case EX_EXTRA: {
335                         lex.next();
336                         string const name = lex.getString();
337                         lex.next();
338                         extradata.set(name, lex.getString());
339                         break;
340                 }
341
342                 case EX_HEIGHT:
343                         lex.next();
344                         resizedata.height = Length(lex.getString());
345                         break;
346
347                 case EX_KEEPASPECTRATIO:
348                         resizedata.keepAspectRatio = true;
349                         break;
350
351                 case EX_ROTATEANGLE:
352                         lex.next();
353                         rotationdata.angle = lex.getString();
354                         break;
355
356                 case EX_ROTATEORIGIN:
357                         lex.next();
358                         rotationdata.origin(lex.getString());
359                         break;
360
361                 case EX_SCALE:
362                         lex.next();
363                         resizedata.scale = lex.getString();
364                         break;
365
366                 case EX_WIDTH:
367                         lex.next();
368                         resizedata.width = Length(lex.getString());
369                         break;
370
371                 case EX_END:
372                         found_end = true;
373                         break;
374
375                 default:
376                         lex.printError("ExternalInset::read: "
377                                        "Wrong tag: $$Token");
378                         read_error = true;
379                         break;
380                 }
381
382                 if (found_end || read_error)
383                         break;
384         }
385
386         if (!found_end)
387                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
388
389         // This is a trick to make sure that the data are self-consistent.
390         settemplate(templatename_);
391
392         if (lyxerr.debugging(Debug::EXTERNAL)) {
393                 lyxerr  << "InsetExternalParams::read:\n";
394                 write(buffer, lyxerr);
395         }
396
397         return !read_error;
398 }
399
400
401 InsetExternal::InsetExternal()
402         : renderer_(new RenderButton)
403 {}
404
405
406 InsetExternal::InsetExternal(InsetExternal const & other)
407         : Inset(other),
408           boost::signals::trackable(),
409           params_(other.params_),
410           renderer_(other.renderer_->clone(this))
411 {}
412
413
414 Inset * InsetExternal::clone() const
415 {
416         return new InsetExternal(*this);
417 }
418
419
420 InsetExternal::~InsetExternal()
421 {
422         InsetExternalMailer(*this).hideDialog();
423 }
424
425
426 void InsetExternal::statusChanged() const
427 {
428         LyX::cref().updateInset(this);
429 }
430
431
432 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
433 {
434         switch (cmd.action) {
435
436         case LFUN_EXTERNAL_EDIT: {
437                 Buffer const & buffer = cur.buffer();
438                 InsetExternalParams p;
439                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
440                 external::editExternal(p, buffer);
441                 break;
442         }
443
444         case LFUN_INSET_MODIFY: {
445                 Buffer const & buffer = cur.buffer();
446                 InsetExternalParams p;
447                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
448                 setParams(p, buffer);
449                 break;
450         }
451
452         case LFUN_INSET_DIALOG_UPDATE:
453                 InsetExternalMailer(*this).updateDialog(&cur.bv());
454                 break;
455
456         case LFUN_MOUSE_RELEASE:
457                 if (!cur.selection())
458                         InsetExternalMailer(*this).showDialog(&cur.bv());
459                 break;
460
461         default:
462                 Inset::doDispatch(cur, cmd);
463         }
464 }
465
466
467 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
468                 FuncStatus & flag) const
469 {
470         switch (cmd.action) {
471
472         case LFUN_EXTERNAL_EDIT:
473         case LFUN_INSET_MODIFY:
474         case LFUN_INSET_DIALOG_UPDATE:
475                 flag.enabled(true);
476                 return true;
477
478         default:
479                 return Inset::getStatus(cur, cmd, flag);
480         }
481 }
482
483
484 void InsetExternal::registerEmbeddedFiles(Buffer const &,
485         EmbeddedFiles & files, ParConstIterator const & pit) const
486 {
487         files.registerFile(params_.filename.absFilename(), EmbeddedFile::AUTO, pit);
488 }
489
490
491 void InsetExternal::edit(Cursor & cur, bool)
492 {
493         InsetExternalMailer(*this).showDialog(&cur.bv());
494 }
495
496
497 bool InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
498 {
499         renderer_->metrics(mi, dim);
500         bool const changed = dim_ != dim;
501         dim_ = dim;
502         return changed;
503 }
504
505
506 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
507 {
508         setPosCache(pi, x, y);
509         renderer_->draw(pi, x, y);
510 }
511
512
513 namespace {
514
515 enum RenderType {
516         RENDERBUTTON,
517         RENDERGRAPHIC,
518         RENDERPREVIEW
519 };
520
521
522 RenderType getRenderType(InsetExternalParams const & p)
523 {
524         if (!external::getTemplatePtr(p) ||
525             p.filename.empty() ||
526             p.display == external::NoDisplay)
527                 return RENDERBUTTON;
528
529         if (p.display == external::PreviewDisplay) {
530                 if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
531                         return RENDERPREVIEW;
532                 return RENDERBUTTON;
533         }
534
535         if (p.display == external::DefaultDisplay &&
536             lyxrc.display_graphics == graphics::NoDisplay)
537                 return RENDERBUTTON;
538         return RENDERGRAPHIC;
539 }
540
541
542 graphics::Params get_grfx_params(InsetExternalParams const & eparams)
543 {
544         graphics::Params gparams;
545
546         gparams.filename = eparams.filename;
547         gparams.scale = eparams.lyxscale;
548         if (eparams.clipdata.clip)
549                 gparams.bb = eparams.clipdata.bbox;
550         gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
551
552         switch (eparams.display) {
553         case external::DefaultDisplay:
554                 gparams.display = graphics::DefaultDisplay;
555                 break;
556         case external::MonochromeDisplay:
557                 gparams.display = graphics::MonochromeDisplay;
558                 break;
559         case external::GrayscaleDisplay:
560                 gparams.display = graphics::GrayscaleDisplay;
561                 break;
562         case external::ColorDisplay:
563                 gparams.display = graphics::ColorDisplay;
564                 break;
565         case external::NoDisplay:
566                 gparams.display = graphics::NoDisplay;
567                 break;
568         default:
569                 BOOST_ASSERT(false);
570         }
571         if (gparams.display == graphics::DefaultDisplay)
572                 gparams.display = lyxrc.display_graphics;
573         // Override the above if we're not using a gui
574         if (!use_gui)
575                 gparams.display = graphics::NoDisplay;
576
577         return gparams;
578 }
579
580
581 docstring const getScreenLabel(InsetExternalParams const & params,
582                             Buffer const & buffer)
583 {
584         external::Template const * const ptr =
585                 external::getTemplatePtr(params);
586         if (!ptr)
587                 // FIXME UNICODE
588                 return support::bformat((_("External template %1$s is not installed")),
589                                         from_utf8(params.templatename()));
590         // FIXME UNICODE
591         docstring gui = _(ptr->guiName);
592         return from_utf8(external::doSubstitution(params, buffer,
593                                 to_utf8(gui), false));
594 }
595
596 void add_preview_and_start_loading(RenderMonitoredPreview &,
597                                    InsetExternal const &,
598                                    Buffer const &);
599
600 } // namespace anon
601
602
603 InsetExternalParams const & InsetExternal::params() const
604 {
605         return params_;
606 }
607
608
609 void InsetExternal::setParams(InsetExternalParams const & p,
610                               Buffer const & buffer)
611 {
612         params_ = p;
613
614         // Subsequent calls to the InsetExternal::Params default constructor
615         // will use this.
616         defaultTemplateName = params_.templatename();
617
618         switch (getRenderType(params_)) {
619         case RENDERBUTTON: {
620                 RenderButton * button_ptr = renderer_->asButton();
621                 if (!button_ptr) {
622                         renderer_.reset(new RenderButton);
623                         button_ptr = renderer_->asButton();
624                 }
625
626                 button_ptr->update(getScreenLabel(params_, buffer), true);
627                 break;
628
629         } case RENDERGRAPHIC: {
630                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
631                 if (!graphic_ptr) {
632                         renderer_.reset(new RenderGraphic(this));
633                         graphic_ptr = renderer_->asGraphic();
634                 }
635
636                 graphic_ptr->update(get_grfx_params(params_));
637
638                 break;
639
640         } case RENDERPREVIEW: {
641                 RenderMonitoredPreview * preview_ptr =
642                         renderer_->asMonitoredPreview();
643                 if (!preview_ptr) {
644                         renderer_.reset(new RenderMonitoredPreview(this));
645                         preview_ptr = renderer_->asMonitoredPreview();
646                         preview_ptr->fileChanged(
647                                 boost::bind(&InsetExternal::fileChanged, this));
648                 }
649
650                 if (preview_ptr->monitoring())
651                         preview_ptr->stopMonitoring();
652                 add_preview_and_start_loading(*preview_ptr, *this, buffer);
653
654                 break;
655         }
656         }
657 }
658
659
660 void InsetExternal::fileChanged() const
661 {
662         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
663         if (!buffer_ptr)
664                 return;
665
666         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
667         BOOST_ASSERT(ptr);
668
669         Buffer const & buffer = *buffer_ptr;
670         ptr->removePreview(buffer);
671         add_preview_and_start_loading(*ptr, *this, buffer);
672 }
673
674
675 void InsetExternal::write(Buffer const & buffer, ostream & os) const
676 {
677         params_.write(buffer, os);
678 }
679
680
681 void InsetExternal::read(Buffer const & buffer, Lexer & lex)
682 {
683         InsetExternalParams params;
684         if (params.read(buffer, lex))
685                 setParams(params, buffer);
686 }
687
688
689 int InsetExternal::latex(Buffer const & buf, odocstream & os,
690                          OutputParams const & runparams) const
691 {
692         if (params_.draft) {
693                 // FIXME UNICODE
694                 os << "\\fbox{\\ttfamily{}"
695                    << from_utf8(params_.filename.outputFilename(buf.filePath()))
696                    << "}\n";
697                 return 1;
698         }
699
700         // "nice" means that the buffer is exported to LaTeX format but not
701         // run through the LaTeX compiler.
702         // If we're running through the LaTeX compiler, we should write the
703         // generated files in the buffer's temporary directory.
704         bool const external_in_tmpdir = !runparams.nice;
705         bool const dryrun = runparams.dryrun || runparams.inComment;
706
707         // If the template has specified a PDFLaTeX output, then we try and
708         // use that.
709         if (runparams.flavor == OutputParams::PDFLATEX) {
710                 external::Template const * const et_ptr =
711                         external::getTemplatePtr(params_);
712                 if (!et_ptr)
713                         return 0;
714                 external::Template const & et = *et_ptr;
715
716                 external::Template::Formats::const_iterator cit =
717                         et.formats.find("PDFLaTeX");
718
719                 if (cit != et.formats.end()) {
720                         return external::writeExternal(params_, "PDFLaTeX",
721                                                        buf, os,
722                                                        *(runparams.exportdata),
723                                                        external_in_tmpdir,
724                                                        dryrun);
725                 }
726         }
727
728         return external::writeExternal(params_, "LaTeX", buf, os,
729                                        *(runparams.exportdata),
730                                        external_in_tmpdir,
731                                        dryrun);
732 }
733
734
735 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
736                              OutputParams const & runparams) const
737 {
738         os << '\n'; // output external material on a new line
739         external::writeExternal(params_, "Ascii", buf, os,
740                                 *(runparams.exportdata), false,
741                                 runparams.dryrun || runparams.inComment);
742         return PLAINTEXT_NEWLINE;
743 }
744
745
746 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
747                            OutputParams const & runparams) const
748 {
749         return external::writeExternal(params_, "DocBook", buf, os,
750                                        *(runparams.exportdata), false,
751                                        runparams.dryrun || runparams.inComment);
752 }
753
754
755 void InsetExternal::validate(LaTeXFeatures & features) const
756 {
757         if (params_.draft)
758                 return;
759
760         external::Template const * const et_ptr =
761                 external::getTemplatePtr(params_);
762         if (!et_ptr)
763                 return;
764         external::Template const & et = *et_ptr;
765
766         string format;
767         switch (features.runparams().flavor) {
768         case OutputParams::LATEX:
769                 format = "LaTeX";
770                 break;
771         case OutputParams::PDFLATEX:
772                 format = "PDFLaTeX";
773                 break;
774         case OutputParams::XML:
775                 format = "DocBook";
776                 break;
777         }
778         external::Template::Formats::const_iterator cit =
779                 et.formats.find(format);
780         if (cit == et.formats.end())
781                 return;
782
783         // FIXME: We don't need that always
784         features.require("lyxdot");
785
786         vector<string>::const_iterator it  = cit->second.requirements.begin();
787         vector<string>::const_iterator end = cit->second.requirements.end();
788         for (; it != end; ++it)
789                 features.require(*it);
790
791         external::TemplateManager & etm = external::TemplateManager::get();
792
793         it  = cit->second.preambleNames.begin();
794         end = cit->second.preambleNames.end();
795         for (; it != end; ++it) {
796                 string const preamble = etm.getPreambleDefByName(*it);
797                 if (!preamble.empty())
798                         features.addPreambleSnippet(preamble);
799         }
800 }
801
802
803 //
804 // preview stuff
805 //
806
807 namespace {
808
809 bool preview_wanted(InsetExternalParams const & params)
810 {
811         return params.display == external::PreviewDisplay &&
812                 support::isFileReadable(params.filename);
813 }
814
815
816 docstring const latex_string(InsetExternal const & inset, Buffer const & buffer)
817 {
818         odocstringstream os;
819         // We don't need to set runparams.encoding since it is not used by
820         // latex().
821         OutputParams runparams(0);
822         runparams.flavor = OutputParams::LATEX;
823         inset.latex(buffer, os, runparams);
824         return os.str();
825 }
826
827
828 void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
829                                    InsetExternal const & inset,
830                                    Buffer const & buffer)
831 {
832         InsetExternalParams const & params = inset.params();
833
834         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
835             preview_wanted(params)) {
836                 renderer.setAbsFile(params.filename);
837                 docstring const snippet = latex_string(inset, buffer);
838                 renderer.addPreview(snippet, buffer);
839                 renderer.startLoading(buffer);
840         }
841 }
842
843 } // namespace anon
844
845
846 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
847 {
848         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
849         if (!ptr)
850                 return;
851
852         if (preview_wanted(params())) {
853                 ptr->setAbsFile(params_.filename);
854                 docstring const snippet = latex_string(*this, ploader.buffer());
855                 ptr->addPreview(snippet, ploader);
856         }
857 }
858
859
860 /// Mailer stuff
861
862 string const InsetExternalMailer::name_("external");
863
864 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
865         : inset_(inset)
866 {}
867
868
869 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
870 {
871         return params2string(inset_.params(), buffer);
872 }
873
874
875 void InsetExternalMailer::string2params(string const & in,
876                                         Buffer const & buffer,
877                                         InsetExternalParams & params)
878 {
879         params = InsetExternalParams();
880         if (in.empty())
881                 return;
882
883         istringstream data(in);
884         Lexer lex(0,0);
885         lex.setStream(data);
886
887         string name;
888         lex >> name;
889         if (!lex || name != name_)
890                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
891
892         // This is part of the inset proper that is usually swallowed
893         // by Text::readInset
894         string id;
895         lex >> id;
896         if (!lex || id != "External")
897                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
898
899         params.read(buffer, lex);
900 }
901
902
903 string const
904 InsetExternalMailer::params2string(InsetExternalParams const & params,
905                                    Buffer const & buffer)
906 {
907         ostringstream data;
908         data << name_ << ' ';
909         params.write(buffer, data);
910         data << "\\end_inset\n";
911         return data.str();
912 }
913
914 } // namespace lyx