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