]> git.lyx.org Git - lyx.git/blob - src/insets/InsetExternal.cpp
5e04597b3a8eb355ce315a7bb14d71ee6b6becc8
[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 "support/debug.h"
23 #include "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncStatus.h"
26 #include "FuncRequest.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "LyXRC.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33
34 #include "graphics/PreviewLoader.h"
35
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
38 #include "support/lyxlib.h"
39 #include "support/convert.h"
40 #include "support/Translator.h"
41
42 #include <boost/bind.hpp>
43
44 #include <sstream>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace {
50
51 lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
52
53 unsigned int const defaultLyxScale = 100;
54
55 string defaultTemplateName;
56
57 } // namespace anon
58
59
60 namespace lyx {
61
62 extern bool use_gui;
63
64 namespace external {
65
66 TempName::TempName()
67 {
68         FileName const tempname = FileName::tempName("lyxext");
69         // FIXME: This is unsafe
70         tempname.removeFile();
71         // must have an extension for the converter code to work correctly.
72         tempname_ = FileName(tempname.absFilename() + ".tmp");
73 }
74
75
76 TempName::TempName(TempName const &)
77 {
78         tempname_ = TempName()();
79 }
80
81
82 TempName::~TempName()
83 {
84         tempname_.removeFile();
85 }
86
87
88 TempName & TempName::operator=(TempName const & other)
89 {
90         if (this != &other)
91                 tempname_ = TempName()();
92         return *this;
93 }
94
95
96 namespace {
97
98 /// The translator between the Display enum and corresponding lyx string.
99 Translator<DisplayType, string> const initTranslator()
100 {
101         Translator<DisplayType, string> translator(DefaultDisplay, "default");
102
103         // Fill the display translator
104         translator.addPair(MonochromeDisplay, "monochrome");
105         translator.addPair(GrayscaleDisplay, "grayscale");
106         translator.addPair(ColorDisplay, "color");
107         translator.addPair(PreviewDisplay, "preview");
108         translator.addPair(NoDisplay, "none");
109
110         return translator;
111 }
112
113 } // namespace anon
114
115
116 Translator<DisplayType, string> const & displayTranslator()
117 {
118         static Translator<DisplayType, string> const translator =
119                 initTranslator();
120         return translator;
121 }
122
123 } // namespace external
124
125
126 InsetExternalParams::InsetExternalParams()
127         : display(defaultDisplayType),
128           lyxscale(defaultLyxScale),
129           draft(false)
130 {
131         if (defaultTemplateName.empty()) {
132                 external::TemplateManager const & etm =
133                         external::TemplateManager::get();
134                 templatename_ = etm.getTemplates().begin()->first;
135         } else
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 = 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 " << filename.outputFilename(buffer.filePath()) << '\n';
187
188         if (display != defaultDisplayType)
189                 os << "\tdisplay "
190                    << external::displayTranslator().find(display)
191                    << '\n';
192
193         if (lyxscale != defaultLyxScale)
194                 os << "\tlyxscale " << convert<string>(lyxscale) << '\n';
195
196         if (draft)
197                 os << "\tdraft\n";
198
199         if (!clipdata.bbox.empty())
200                 os << "\tboundingBox " << clipdata.bbox << '\n';
201         if (clipdata.clip)
202                 os << "\tclip\n";
203
204         external::ExtraData::const_iterator it  = extradata.begin();
205         external::ExtraData::const_iterator end = extradata.end();
206         for (; it != end; ++it) {
207                 if (!it->second.empty())
208                         os << "\textra " << it->first << " \""
209                            << it->second << "\"\n";
210         }
211
212         if (!rotationdata.no_rotation()) {
213                 os << "\trotateAngle " << rotationdata.adjAngle() << '\n';
214                 if (rotationdata.origin() != external::RotationData::DEFAULT)
215                         os << "\trotateOrigin "
216                            << rotationdata.originString() << '\n';
217         }
218
219         if (!resizedata.no_resize()) {
220                 double const scl = convert<double>(resizedata.scale);
221                 if (!float_equal(scl, 0.0, 0.05)) {
222                         if (!float_equal(scl, 100.0, 0.05))
223                                 os << "\tscale "
224                                    << resizedata.scale << '\n';
225                 } else {
226                         if (!resizedata.width.zero())
227                                 os << "\twidth "
228                                    << resizedata.width.asString() << '\n';
229                         if (!resizedata.height.zero())
230                                 os << "\theight "
231                                    << resizedata.height.asString() << '\n';
232                 }
233                 if (resizedata.keepAspectRatio)
234                         os << "\tkeepAspectRatio\n";
235         }
236 }
237
238
239 bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
240 {
241         enum ExternalTags {
242                 EX_TEMPLATE = 1,
243                 EX_FILENAME,
244                 EX_DISPLAY,
245                 EX_LYXSCALE,
246                 EX_DRAFT,
247                 EX_BOUNDINGBOX,
248                 EX_CLIP,
249                 EX_EXTRA,
250                 EX_HEIGHT,
251                 EX_KEEPASPECTRATIO,
252                 EX_ROTATEANGLE,
253                 EX_ROTATEORIGIN,
254                 EX_SCALE,
255                 EX_WIDTH,
256                 EX_END
257         };
258
259         keyword_item external_tags[] = {
260                 { "\\end_inset",     EX_END },
261                 { "boundingBox",     EX_BOUNDINGBOX },
262                 { "clip",            EX_CLIP },
263                 { "display",         EX_DISPLAY},
264                 { "draft",           EX_DRAFT},
265                 { "extra",           EX_EXTRA },
266                 { "filename",        EX_FILENAME},
267                 { "height",          EX_HEIGHT },
268                 { "keepAspectRatio", EX_KEEPASPECTRATIO },
269                 { "lyxscale",        EX_LYXSCALE},
270                 { "rotateAngle",     EX_ROTATEANGLE },
271                 { "rotateOrigin",    EX_ROTATEORIGIN },
272                 { "scale",           EX_SCALE },
273                 { "template",        EX_TEMPLATE },
274                 { "width",           EX_WIDTH }
275         };
276
277         PushPopHelper pph(lex, external_tags, EX_END);
278
279         bool found_end  = false;
280         bool read_error = false;
281
282         while (lex.isOK()) {
283                 switch (lex.lex()) {
284                 case EX_TEMPLATE:
285                         lex.next();
286                         templatename_ = lex.getString();
287                         break;
288
289                 case EX_FILENAME: {
290                         lex.eatLine();
291                         string const name = lex.getString();
292                         filename.set(name, buffer.filePath());
293                         break;
294                 }
295
296                 case EX_DISPLAY: {
297                         lex.next();
298                         string const name = lex.getString();
299                         display = external::displayTranslator().find(name);
300                         break;
301                 }
302
303                 case EX_LYXSCALE:
304                         lex.next();
305                         lyxscale = lex.getInteger();
306                         break;
307
308                 case EX_DRAFT:
309                         draft = true;
310                         break;
311
312                 case EX_BOUNDINGBOX:
313                         lex.next();
314                         clipdata.bbox.xl = lex.getInteger();
315                         lex.next();
316                         clipdata.bbox.yb = lex.getInteger();
317                         lex.next();
318                         clipdata.bbox.xr = lex.getInteger();
319                         lex.next();
320                         clipdata.bbox.yt = lex.getInteger();
321                         break;
322
323                 case EX_CLIP:
324                         clipdata.clip = true;
325                         break;
326
327                 case EX_EXTRA: {
328                         lex.next();
329                         string const name = lex.getString();
330                         lex.next();
331                         extradata.set(name, lex.getString());
332                         break;
333                 }
334
335                 case EX_HEIGHT:
336                         lex.next();
337                         resizedata.height = Length(lex.getString());
338                         break;
339
340                 case EX_KEEPASPECTRATIO:
341                         resizedata.keepAspectRatio = true;
342                         break;
343
344                 case EX_ROTATEANGLE:
345                         lex.next();
346                         rotationdata.angle = lex.getString();
347                         break;
348
349                 case EX_ROTATEORIGIN:
350                         lex.next();
351                         rotationdata.origin(lex.getString());
352                         break;
353
354                 case EX_SCALE:
355                         lex.next();
356                         resizedata.scale = lex.getString();
357                         break;
358
359                 case EX_WIDTH:
360                         lex.next();
361                         resizedata.width = Length(lex.getString());
362                         break;
363
364                 case EX_END:
365                         found_end = true;
366                         break;
367
368                 default:
369                         lex.printError("ExternalInset::read: Wrong tag: $$Token");
370                         read_error = true;
371                         break;
372                 }
373
374                 if (found_end || read_error)
375                         break;
376         }
377
378         if (!found_end)
379                 lex.printError("ExternalInsetParams::read: Missing \\end_inset.");
380
381         // This is a trick to make sure that the data are self-consistent.
382         settemplate(templatename_);
383
384         if (lyxerr.debugging(Debug::EXTERNAL)) {
385                 lyxerr  << "InsetExternalParams::read:\n";
386                 write(buffer, lyxerr);
387         }
388
389         return !read_error;
390 }
391
392
393 InsetExternal::InsetExternal()
394         : renderer_(new RenderButton)
395 {}
396
397
398 InsetExternal::InsetExternal(InsetExternal const & other)
399         : Inset(other),
400           boost::signals::trackable(),
401           params_(other.params_),
402           renderer_(other.renderer_->clone(this))
403 {}
404
405
406 Inset * InsetExternal::clone() const
407 {
408         return new InsetExternal(*this);
409 }
410
411
412 InsetExternal::~InsetExternal()
413 {
414         InsetExternalMailer(*this).hideDialog();
415 }
416
417
418 void InsetExternal::statusChanged() const
419 {
420         updateFrontend();
421 }
422
423
424 void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
425 {
426         switch (cmd.action) {
427
428         case LFUN_EXTERNAL_EDIT: {
429                 Buffer const & buffer = cur.buffer();
430                 InsetExternalParams p;
431                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
432                 external::editExternal(p, buffer);
433                 break;
434         }
435
436         case LFUN_INSET_MODIFY: {
437                 Buffer const & buffer = cur.buffer();
438                 InsetExternalParams p;
439                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
440                 setParams(p, buffer);
441                 break;
442         }
443
444         case LFUN_INSET_DIALOG_UPDATE:
445                 InsetExternalMailer(*this).updateDialog(&cur.bv());
446                 break;
447
448         case LFUN_MOUSE_RELEASE:
449                 if (!cur.selection())
450                         InsetExternalMailer(*this).showDialog(&cur.bv());
451                 break;
452
453         default:
454                 Inset::doDispatch(cur, cmd);
455         }
456 }
457
458
459 bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
460                 FuncStatus & flag) const
461 {
462         switch (cmd.action) {
463
464         case LFUN_EXTERNAL_EDIT:
465         case LFUN_INSET_MODIFY:
466         case LFUN_INSET_DIALOG_UPDATE:
467                 flag.enabled(true);
468                 return true;
469
470         default:
471                 return Inset::getStatus(cur, cmd, flag);
472         }
473 }
474
475
476 void InsetExternal::registerEmbeddedFiles(Buffer const &,
477         EmbeddedFiles & files) const
478 {
479         // temporarily disable embedding for this inset
480         /* files.registerFile(params_.filename, this); */
481 }
482
483
484 void InsetExternal::edit(Cursor & cur, bool)
485 {
486         InsetExternalMailer(*this).showDialog(&cur.bv());
487 }
488
489
490 void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
491 {
492         renderer_->metrics(mi, dim);
493 }
494
495
496 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
497 {
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 = graphics::DisplayType(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 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
619         case RENDERGRAPHIC: {
620                 RenderGraphic * graphic_ptr = renderer_->asGraphic();
621                 if (!graphic_ptr) {
622                         renderer_.reset(new RenderGraphic(this));
623                         graphic_ptr = renderer_->asGraphic();
624                 }
625
626                 graphic_ptr->update(get_grfx_params(params_));
627
628                 break;
629         }
630
631         case RENDERPREVIEW: {
632                 RenderMonitoredPreview * preview_ptr =
633                         renderer_->asMonitoredPreview();
634                 if (!preview_ptr) {
635                         renderer_.reset(new RenderMonitoredPreview(this));
636                         preview_ptr = renderer_->asMonitoredPreview();
637                         preview_ptr->fileChanged(
638                                 boost::bind(&InsetExternal::fileChanged, this));
639                 }
640
641                 if (preview_ptr->monitoring())
642                         preview_ptr->stopMonitoring();
643                 add_preview_and_start_loading(*preview_ptr, *this, buffer);
644
645                 break;
646         }
647         }
648 }
649
650
651 void InsetExternal::fileChanged() const
652 {
653         Buffer const * const buffer = updateFrontend();
654         if (!buffer)
655                 return;
656
657         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
658         BOOST_ASSERT(ptr);
659
660         ptr->removePreview(*buffer);
661         add_preview_and_start_loading(*ptr, *this, *buffer);
662 }
663
664
665 void InsetExternal::write(Buffer const & buffer, ostream & os) const
666 {
667         params_.write(buffer, os);
668 }
669
670
671 void InsetExternal::read(Buffer const & buffer, Lexer & lex)
672 {
673         InsetExternalParams params;
674         if (params.read(buffer, lex))
675                 setParams(params, buffer);
676 }
677
678
679 int InsetExternal::latex(Buffer const & buf, odocstream & os,
680                          OutputParams const & runparams) const
681 {
682         if (params_.draft) {
683                 // FIXME UNICODE
684                 os << "\\fbox{\\ttfamily{}"
685                    << from_utf8(params_.filename.outputFilename(buf.filePath()))
686                    << "}\n";
687                 return 1;
688         }
689
690         // "nice" means that the buffer is exported to LaTeX format but not
691         // run through the LaTeX compiler.
692         // If we're running through the LaTeX compiler, we should write the
693         // generated files in the buffer's temporary directory.
694         bool const external_in_tmpdir = !runparams.nice;
695         bool const dryrun = runparams.dryrun || runparams.inComment;
696
697         // If the template has specified a PDFLaTeX output, then we try and
698         // use that.
699         if (runparams.flavor == OutputParams::PDFLATEX) {
700                 external::Template const * const et_ptr =
701                         external::getTemplatePtr(params_);
702                 if (!et_ptr)
703                         return 0;
704                 external::Template const & et = *et_ptr;
705
706                 external::Template::Formats::const_iterator cit =
707                         et.formats.find("PDFLaTeX");
708
709                 if (cit != et.formats.end()) {
710                         return external::writeExternal(params_, "PDFLaTeX",
711                                                        buf, os,
712                                                        *(runparams.exportdata),
713                                                        external_in_tmpdir,
714                                                        dryrun);
715                 }
716         }
717
718         return external::writeExternal(params_, "LaTeX", buf, os,
719                                        *(runparams.exportdata),
720                                        external_in_tmpdir,
721                                        dryrun);
722 }
723
724
725 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
726                              OutputParams const & runparams) const
727 {
728         os << '\n'; // output external material on a new line
729         external::writeExternal(params_, "Ascii", buf, os,
730                                 *(runparams.exportdata), false,
731                                 runparams.dryrun || runparams.inComment);
732         return PLAINTEXT_NEWLINE;
733 }
734
735
736 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
737                            OutputParams const & runparams) const
738 {
739         return external::writeExternal(params_, "DocBook", buf, os,
740                                        *(runparams.exportdata), false,
741                                        runparams.dryrun || runparams.inComment);
742 }
743
744
745 void InsetExternal::validate(LaTeXFeatures & features) const
746 {
747         if (params_.draft)
748                 return;
749
750         external::Template const * const et_ptr =
751                 external::getTemplatePtr(params_);
752         if (!et_ptr)
753                 return;
754         external::Template const & et = *et_ptr;
755
756         string format;
757         switch (features.runparams().flavor) {
758         case OutputParams::LATEX:
759                 format = "LaTeX";
760                 break;
761         case OutputParams::PDFLATEX:
762                 format = "PDFLaTeX";
763                 break;
764         case OutputParams::XML:
765                 format = "DocBook";
766                 break;
767         }
768         external::Template::Formats::const_iterator cit =
769                 et.formats.find(format);
770         if (cit == et.formats.end())
771                 return;
772
773         // FIXME: We don't need that always
774         features.require("lyxdot");
775
776         vector<string>::const_iterator it  = cit->second.requirements.begin();
777         vector<string>::const_iterator end = cit->second.requirements.end();
778         for (; it != end; ++it)
779                 features.require(*it);
780
781         external::TemplateManager & etm = external::TemplateManager::get();
782
783         it  = cit->second.preambleNames.begin();
784         end = cit->second.preambleNames.end();
785         for (; it != end; ++it) {
786                 string const preamble = etm.getPreambleDefByName(*it);
787                 if (!preamble.empty())
788                         features.addPreambleSnippet(preamble);
789         }
790 }
791
792
793 //
794 // preview stuff
795 //
796
797 namespace {
798
799 bool preview_wanted(InsetExternalParams const & params)
800 {
801         return params.display == external::PreviewDisplay &&
802                 params.filename.isReadableFile();
803 }
804
805
806 docstring const latex_string(InsetExternal const & inset, Buffer const & buffer)
807 {
808         odocstringstream os;
809         // We don't need to set runparams.encoding since it is not used by
810         // latex().
811         OutputParams runparams(0);
812         runparams.flavor = OutputParams::LATEX;
813         inset.latex(buffer, os, runparams);
814         return os.str();
815 }
816
817
818 void add_preview_and_start_loading(RenderMonitoredPreview & renderer,
819                                    InsetExternal const & inset,
820                                    Buffer const & buffer)
821 {
822         InsetExternalParams const & params = inset.params();
823
824         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
825             preview_wanted(params)) {
826                 renderer.setAbsFile(params.filename);
827                 docstring const snippet = latex_string(inset, buffer);
828                 renderer.addPreview(snippet, buffer);
829                 renderer.startLoading(buffer);
830         }
831 }
832
833 } // namespace anon
834
835
836 void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
837 {
838         RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
839         if (!ptr)
840                 return;
841
842         if (preview_wanted(params())) {
843                 ptr->setAbsFile(params_.filename);
844                 docstring const snippet = latex_string(*this, ploader.buffer());
845                 ptr->addPreview(snippet, ploader);
846         }
847 }
848
849
850 /// Mailer stuff
851
852 string const InsetExternalMailer::name_("external");
853
854 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
855         : inset_(inset)
856 {}
857
858
859 string const InsetExternalMailer::inset2string(Buffer const & buffer) const
860 {
861         return params2string(inset_.params(), buffer);
862 }
863
864
865 void InsetExternalMailer::string2params(string const & in,
866                                         Buffer const & buffer,
867                                         InsetExternalParams & params)
868 {
869         params = InsetExternalParams();
870         if (in.empty())
871                 return;
872
873         istringstream data(in);
874         Lexer lex(0,0);
875         lex.setStream(data);
876
877         string name;
878         lex >> name;
879         if (!lex || name != name_)
880                 return print_mailer_error("InsetExternalMailer", in, 1, name_);
881
882         // This is part of the inset proper that is usually swallowed
883         // by Text::readInset
884         string id;
885         lex >> id;
886         if (!lex || id != "External")
887                 return print_mailer_error("InsetBoxMailer", in, 2, "External");
888
889         params.read(buffer, lex);
890 }
891
892
893 string const
894 InsetExternalMailer::params2string(InsetExternalParams const & params,
895                                    Buffer const & buffer)
896 {
897         ostringstream data;
898         data << name_ << ' ';
899         params.write(buffer, data);
900         data << "\\end_inset\n";
901         return data.str();
902 }
903
904 } // namespace lyx