]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphics.C
87dbd920d89176a95c0fb90f200c601a689a8e38
[lyx.git] / src / insets / insetgraphics.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995-2002 the LyX Team.
7  *
8  * \author Baruch Even
9  * \author Herbert Voss <voss@lyx.org>
10  * ====================================================== */
11
12 /*
13 Known BUGS:
14
15     * If the image is from the clipart, and the document is moved to another
16       directory, the user is screwed. Need a way to handle it.
17       This amounts to a problem of when to use relative or absolute file paths
18       We should probably use what the user asks to use... but when he chooses
19       by the file dialog we normally get an absolute path and this may not be
20       what the user meant.
21
22       Note that browseRelFile in helper_funcs.* provides a file name
23       which is relative if it is at reference path (here puffer path)
24       level or below, and an absolute path if the file name is not a
25       `natural' relative file name. In any case,
26               MakeAbsPath(filename, buf->filePath())
27       is guaranteed to provide the correct absolute path. This is what is
28       done know for include insets. Feel free to ask me -- JMarc
29       14/01/2002
30
31 TODO Before initial production release:
32
33     * What advanced features the users want to do?
34       Implement them in a non latex dependent way, but a logical way.
35       LyX should translate it to latex or any other fitting format.
36     * Add a way to roll the image file into the file format.
37     * When loading, if the image is not found in the expected place, try
38       to find it in the clipart, or in the same directory with the image.
39     * Keep a tab on the image file, if it changes, update the lyx view.
40     * The image choosing dialog could show thumbnails of the image formats
41       it knows of, thus selection based on the image instead of based on
42       filename.
43     * Add support for the 'picins' package.
44     * Add support for the 'picinpar' package.
45     * Improve support for 'subfigure' - Allow to set the various options
46       that are possible.
47 */
48
49 /* NOTES:
50  * Fileformat:
51  * Current version is 1 (inset file format version), when changing it
52  * it should be changed in the Write() function when writing in one place
53  * and when reading one should change the version check and the error message.
54  * The filename is kept in  the lyx file in a relative way, so as to allow
55  * moving the document file and its images with no problem.
56  *
57  *
58  * Conversions:
59  *   Postscript output means EPS figures.
60  *
61  *   PDF output is best done with PDF figures if it's a direct conversion
62  *   or PNG figures otherwise.
63  *      Image format
64  *      from        to
65  *      EPS         epstopdf
66  *      PS          ps2pdf
67  *      JPG/PNG     direct
68  *      PDF         direct
69  *      others      PNG
70  */
71
72 #include <config.h>
73
74 #ifdef __GNUG__
75 #pragma implementation
76 #endif
77
78 #include "insets/insetgraphics.h"
79 #include "insets/insetgraphicsParams.h"
80
81 #include "graphics/GraphicsCache.h"
82 #include "graphics/GraphicsImage.h"
83
84 #include "LyXView.h"
85 #include "lyxtext.h"
86 #include "buffer.h"
87 #include "BufferView.h"
88 #include "converter.h"
89 #include "Painter.h"
90 #include "lyxrc.h"
91 #include "font.h"    // For the lyxfont class.
92 #include "debug.h"
93 #include "gettext.h"
94 #include "LaTeXFeatures.h"
95
96 #include "frontends/Dialogs.h"
97 #include "frontends/controllers/helper_funcs.h" // getVectorFromString
98
99 #include "support/LAssert.h"
100 #include "support/filetools.h"
101 #include "support/lyxalgo.h" // lyx::count
102 #include "support/path.h"
103
104 #include <algorithm> // For the std::max
105
106 extern string system_tempdir;
107
108 using std::ostream;
109 using std::endl;
110
111 ///////////////////////////////////////////////////////////////////////////
112 int const VersionNumber = 1;
113 ///////////////////////////////////////////////////////////////////////////
114
115 namespace {
116
117 // This function is a utility function
118 // ... that should be with ChangeExtension ...
119 inline
120 string const RemoveExtension(string const & filename)
121 {
122         return ChangeExtension(filename, string());
123 }
124
125 } // namespace anon
126
127
128 namespace {
129
130 string const unique_id()
131 {
132         static unsigned int seed = 1000;
133
134         ostringstream ost;
135         ost << "graph" << ++seed;
136
137         // Needed if we use lyxstring.
138         return ost.str().c_str();
139 }
140
141 } // namespace anon
142
143
144 InsetGraphics::InsetGraphics()
145         : graphic_label(unique_id()),
146           cached_status_(grfx::ErrorUnknown), cache_filled_(false), old_asc(0)
147
148 {}
149
150
151 InsetGraphics::InsetGraphics(InsetGraphics const & ig,
152                              string const & filepath,
153                              bool same_id)
154         : Inset(ig, same_id),
155           SigC::Object(),
156           graphic_label(unique_id()),
157           cached_status_(grfx::ErrorUnknown), cache_filled_(false), old_asc(0)
158 {
159         setParams(ig.params(), filepath);
160 }
161
162
163 InsetGraphics::~InsetGraphics()
164 {
165         cached_image_.reset(0);
166         grfx::GCache & gc = grfx::GCache::get();
167         gc.remove(*this);
168
169         // Emits the hide signal to the dialog connected (if any)
170         hideDialog();
171 }
172
173
174 string const InsetGraphics::statusMessage() const
175 {
176         string msg;
177
178         switch (cached_status_) {
179         case grfx::WaitingToLoad:
180                 msg = _("Waiting for draw request to start loading...");
181                 break;
182         case grfx::Loading:
183                 msg = _("Loading...");
184                 break;
185         case grfx::Converting:
186                 msg = _("Converting to loadable format...");
187                 break;
188         case grfx::ScalingEtc:
189                 msg = _("Loaded. Scaling etc...");
190                 break;
191         case grfx::ErrorNoFile:
192                 msg = _("No file found!");
193                 break;
194         case grfx::ErrorLoading:
195                 msg = _("Error loading file into memory");
196                 break;
197         case grfx::ErrorConverting:
198                 msg = _("Error converting to loadable format");
199                 break;
200         case grfx::ErrorScalingEtc:
201                 msg = _("Error scaling etc");
202                 break;
203         case grfx::ErrorUnknown:
204                 msg = _("No image");
205                 break;
206         case grfx::Loaded:
207                 msg = _("Loaded but not displaying");
208                 break;
209         }
210
211         return msg;
212 }
213
214
215 void InsetGraphics::setCache() const
216 {
217         if (cache_filled_)
218                 return;
219
220         grfx::GCache & gc = grfx::GCache::get();
221         cached_status_ = gc.status(*this);
222         cached_image_  = gc.image(*this);
223 }
224
225
226 bool InsetGraphics::drawImage() const
227 {
228         setCache();
229         Pixmap const pixmap =
230                 (cached_status_ == grfx::Loaded && cached_image_.get() != 0) ?
231                 cached_image_->getPixmap() : 0;
232
233         return pixmap != 0;
234 }
235
236
237 int InsetGraphics::ascent(BufferView *, LyXFont const &) const
238 {
239         old_asc = 50;
240         if (drawImage())
241                 old_asc = cached_image_->getHeight();
242         return old_asc;
243 }
244
245
246 int InsetGraphics::descent(BufferView *, LyXFont const &) const
247 {
248         return 0;
249 }
250
251
252 int InsetGraphics::width(BufferView *, LyXFont const & font) const
253 {
254         if (drawImage())
255                 return cached_image_->getWidth();
256         else {
257                 int font_width = 0;
258
259                 LyXFont msgFont(font);
260                 msgFont.setFamily(LyXFont::SANS_FAMILY);
261
262                 string const justname = OnlyFilename (params().filename);
263                 if (!justname.empty()) {
264                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
265                         font_width = lyxfont::width(justname, msgFont);
266                 }
267
268                 string const msg = statusMessage();
269                 if (!msg.empty()) {
270                         msgFont.setSize(LyXFont::SIZE_TINY);
271                         int const msg_width = lyxfont::width(msg, msgFont);
272                         font_width = std::max(font_width, msg_width);
273                 }
274
275                 return std::max(50, font_width + 15);
276         }
277 }
278
279
280 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
281                          int baseline, float & x, bool) const
282 {
283         int oasc = old_asc;
284         grfx::ImageStatus old_status_ = cached_status_;
285
286         int ldescent = descent(bv, font);
287         int lascent  = ascent(bv, font);
288         int lwidth   = width(bv, font);
289
290         // we may have changed while someone other was drawing us so better
291         // to not draw anything as we surely call to redraw ourself soon.
292         // This is not a nice thing to do and should be fixed properly somehow.
293         // But I still don't know the best way to go. So let's do this like this
294         // for now (Jug 20020311)
295         if (lascent != oasc) {
296 //              lyxerr << "IG(" << this << "): " << x << endl;
297                 return;
298         }
299
300         // Make sure now that x is updated upon exit from this routine
301         int old_x = int(x);
302         x += lwidth;
303
304         // Initiate the loading of the graphics file
305         if (cached_status_ == grfx::WaitingToLoad) {
306                 grfx::GCache & gc = grfx::GCache::get();
307                 gc.startLoading(*this);
308         }
309
310         // This will draw the graphics. If the graphics has not been loaded yet,
311         // we draw just a rectangle.
312         Painter & paint = bv->painter();
313
314         if (drawImage()) {
315 //              lyxerr << "IG(" << this << "): " << old_x << endl;
316                 paint.image(old_x + 2, baseline - lascent,
317                             lwidth - 4, lascent + ldescent,
318                             *cached_image_.get());
319
320         } else {
321
322                 paint.rectangle(old_x + 2, baseline - lascent,
323                                 lwidth - 4,
324                                 lascent + ldescent);
325
326                 // Print the file name.
327                 LyXFont msgFont(font);
328                 msgFont.setFamily(LyXFont::SANS_FAMILY);
329                 string const justname = OnlyFilename (params().filename);
330                 if (!justname.empty()) {
331                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
332                         paint.text(old_x + 8,
333                                    baseline - lyxfont::maxAscent(msgFont) - 4,
334                                    justname, msgFont);
335                 }
336
337                 // Print the message.
338                 string const msg = statusMessage();
339                 if (!msg.empty()) {
340                         msgFont.setSize(LyXFont::SIZE_TINY);
341                         paint.text(old_x + 8, baseline - 4, msg, msgFont);
342                 }
343         }
344
345         // the status message may mean we changed size, so indicate
346         // we need a row redraw
347         if (old_status_ != grfx::ErrorUnknown && old_status_ != cached_status_) {
348                 bv->getLyXText()->status(bv, LyXText::CHANGED_IN_DRAW);
349         }
350
351         // Reset the cache, ready for the next draw request
352         cached_status_ = grfx::ErrorUnknown;
353         cached_image_.reset(0);
354         cache_filled_ = false;
355 }
356
357
358 // Update the inset after parameters changed (read from file or changed in
359 // dialog. The grfx::GCache makes the decisions about whether or not to draw
360 // (interogates lyxrc, ascertains whether file exists etc)
361 void InsetGraphics::updateInset(string const & filepath) const
362 {
363         grfx::GCache & gc = grfx::GCache::get();
364         gc.update(*this, filepath);
365 }
366
367
368 void InsetGraphics::edit(BufferView *bv, int, int, unsigned int)
369 {
370         bv->owner()->getDialogs()->showGraphics(this);
371 }
372
373
374 void InsetGraphics::edit(BufferView * bv, bool)
375 {
376         edit(bv, 0, 0, 0);
377 }
378
379
380 Inset::EDITABLE InsetGraphics::editable() const
381 {
382         return IS_EDITABLE;
383 }
384
385
386 void InsetGraphics::write(Buffer const *, ostream & os) const
387 {
388         os << "Graphics FormatVersion " << VersionNumber << '\n';
389         params().Write(os);
390 }
391
392
393 void InsetGraphics::read(Buffer const * buf, LyXLex & lex)
394 {
395         string const token = lex.getString();
396
397         if (token == "Graphics")
398                 readInsetGraphics(lex);
399         else if (token == "Figure") // Compatibility reading of FigInset figures.
400                 readFigInset(lex);
401         else
402                 lyxerr[Debug::GRAPHICS] << "Not a Graphics or Figure inset!\n";
403
404         updateInset(buf->filePath());
405 }
406
407
408 void InsetGraphics::readInsetGraphics(LyXLex & lex)
409 {
410         bool finished = false;
411
412         while (lex.isOK() && !finished) {
413                 lex.next();
414
415                 string const token = lex.getString();
416                 lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
417                                     << std::endl;
418
419                 if (token.empty()) {
420                         continue;
421                 } else if (token == "\\end_inset") {
422                         finished = true;
423                 } else if (token == "FormatVersion") {
424                         lex.next();
425                         int version = lex.getInteger();
426                         if (version > VersionNumber)
427                                 lyxerr
428                                 << "This document was created with a newer Graphics widget"
429                                 ", You should use a newer version of LyX to read this"
430                                 " file."
431                                 << std::endl;
432                         // TODO: Possibly open up a dialog?
433                 }
434                 else {
435                         if (! params_.Read(lex, token))
436                                 lyxerr << "Unknown token, " << token << ", skipping."
437                                         << std::endl;
438                 }
439         }
440 }
441
442 // FormatVersion < 1.0  (LyX < 1.2)
443 void InsetGraphics::readFigInset(LyXLex & lex)
444 {
445         std::vector<string> const oldUnits =
446                 getVectorFromString("pt,cm,in,p%,c%");
447         bool finished = false;
448         // set the display default
449         if (lyxrc.display_graphics == "mono")
450             params_.display = InsetGraphicsParams::MONOCHROME;
451         else if (lyxrc.display_graphics == "gray")
452             params_.display = InsetGraphicsParams::GRAYSCALE;
453         else if (lyxrc.display_graphics == "color")
454             params_.display = InsetGraphicsParams::COLOR;
455         else
456             params_.display = InsetGraphicsParams::NONE;
457         while (lex.isOK() && !finished) {
458                 lex.next();
459
460                 string const token = lex.getString();
461                 lyxerr[Debug::GRAPHICS] << "Token: " << token << endl;
462
463                 if (token.empty())
464                         continue;
465                 else if (token == "\\end_inset") {
466                         finished = true;
467                 } else if (token == "file") {
468                         if (lex.next()) {
469                                 params_.filename = lex.getString();
470                         }
471                 } else if (token == "extra") {
472                         if (lex.next());
473                         // kept for backwards compability. Delete in 0.13.x
474                 } else if (token == "subcaption") {
475                         if (lex.eatLine())
476                                 params_.subcaptionText = lex.getString();
477                         params_.subcaption = true;
478                 } else if (token == "label") {
479                         if (lex.next());
480                         // kept for backwards compability. Delete in 0.13.x
481                 } else if (token == "angle") {
482                         if (lex.next())
483                                 params_.rotate = true;
484                                 params_.rotateAngle = lex.getFloat();
485                 } else if (token == "size") {
486                         if (lex.next())
487                                 params_.lyxwidth = LyXLength(lex.getString()+"pt");
488                         if (lex.next())
489                                 params_.lyxheight = LyXLength(lex.getString()+"pt");
490                 } else if (token == "flags") {
491                         if (lex.next())
492                                 switch (lex.getInteger()) {
493                                 case 1: params_.display = InsetGraphicsParams::MONOCHROME;
494                                     break;
495                                 case 2: params_.display = InsetGraphicsParams::GRAYSCALE;
496                                     break;
497                                 case 3: params_.display = InsetGraphicsParams::COLOR;
498                                     break;
499                                 }
500                 } else if (token == "subfigure") {
501                         params_.subcaption = true;
502                 } else if (token == "width") {
503                     if (lex.next()) {
504                         int i = lex.getInteger();
505                         if (lex.next()) {
506                             if (i == 5) {
507                                 params_.scale = lex.getInteger();
508                                 params_.size_type = InsetGraphicsParams::SCALE;
509                             } else {
510                                 params_.width = LyXLength(lex.getString()+oldUnits[i]);
511                                 params_.size_type = InsetGraphicsParams::WH;
512                             }
513                         }
514                     }
515                 } else if (token == "height") {
516                     if (lex.next()) {
517                         int i = lex.getInteger();
518                         if (lex.next()) {
519                             params_.height = LyXLength(lex.getString()+oldUnits[i]);
520                             params_.size_type = InsetGraphicsParams::WH;
521                         }
522                     }
523                 }
524         }
525 }
526
527 string const InsetGraphics::createLatexOptions() const
528 {
529         // Calculate the options part of the command, we must do it to a string
530         // stream since we might have a trailing comma that we would like to remove
531         // before writing it to the output stream.
532         ostringstream options;
533         if (!params().bb.empty())
534             options << "  bb=" << strip(params().bb) << ",\n";
535         if (params().draft)
536             options << "  draft,\n";
537         if (params().clip)
538             options << "  clip,\n";
539         if (params().size_type == InsetGraphicsParams::WH) {
540             if (!params().width.zero())
541                 options << "  width=" << params().width.asLatexString() << ",\n";
542             if (!params().height.zero())
543                 options << "  height=" << params().height.asLatexString() << ",\n";
544         } else if (params().size_type == InsetGraphicsParams::SCALE) {
545             if (params().scale > 0)
546                 options << "  scale=" << double(params().scale)/100.0 << ",\n";
547         }
548         if (params().keepAspectRatio)
549             options << "  keepaspectratio,\n";
550         // Make sure it's not very close to zero, a float can be effectively
551         // zero but not exactly zero.
552         if (!lyx::float_equal(params().rotateAngle, 0, 0.001) && params().rotate) {
553             options << "  angle=" << params().rotateAngle << ",\n";
554             if (!params().rotateOrigin.empty()) {
555                 options << "  origin=" << params().rotateOrigin[0];
556                 if (contains(params().rotateOrigin,"Top"))
557                     options << 't';
558                 else if (contains(params().rotateOrigin,"Bottom"))
559                     options << 'b';
560                 else if (contains(params().rotateOrigin,"Baseline"))
561                     options << 'B';
562                 options << ",\n";
563             }
564         }
565         if (!params().special.empty())
566             options << params().special << ",\n";
567         string opts = options.str().c_str();
568         return opts.substr(0,opts.size()-2);    // delete last ",\n"
569 }
570
571 namespace {
572 string findTargetFormat(string const & suffix)
573 {
574         // lyxrc.pdf_mode means:
575         // Are we creating a PDF or a PS file?
576         // (Should actually mean, are we using latex or pdflatex).
577         lyxerr[Debug::GRAPHICS] << "decideOutput: lyxrc.pdf_mode = "
578                             << lyxrc.pdf_mode << std::endl;
579         if (lyxrc.pdf_mode) {
580                 if (contains(suffix,"ps") || suffix == "pdf")
581                         return "pdf";
582                 else if (suffix == "jpg")
583                         return suffix;
584                 else
585                         return "png";
586         }
587         // If it's postscript, we always do eps.
588         lyxerr[Debug::GRAPHICS] << "decideOutput: we have PostScript mode\n";
589         if (suffix != "ps")
590             return "eps";
591         else
592             return "ps";
593 }
594
595 } // Anon. namespace
596
597
598 string const InsetGraphics::prepareFile(Buffer const *buf) const
599 {
600         // do_convert = Do we need to convert the file?
601         // nice = Do we create a nice version?
602         //        This is used when exporting the latex file only.
603         // if (!do_convert)
604         //   return original filename
605         // if (!nice)
606         //   convert_place = temp directory
607         //   return new filename in temp directory
608         // else
609         //   convert_place = original file directory
610         //   return original filename without the extension
611         //
612         // if it's a zipped one, than let LaTeX do the rest!!!
613         string filename_  = params().filename;
614         bool const zipped = zippedFile(filename_);
615
616         if ((zipped && params().noUnzip) || buf->niceFile) {
617                 lyxerr[Debug::GRAPHICS] << "don't unzip file or export latex"
618                                     << filename_ << endl;
619                 return filename_;
620         }
621
622         // Enable these helper functions to find the file if it is stored as
623         // a relative path.
624         Path p(buf->filePath());
625
626         if (zipped)
627                 filename_ = unzipFile(filename_);
628
629         string const from = getExtFromContents(filename_);
630         string const to   = findTargetFormat(from);
631
632         if (from == to) {
633                 // No conversion needed!
634                 return filename_;
635         }
636
637         string const temp = AddName(buf->tmppath, filename_);
638         string const outfile_base = RemoveExtension(temp);
639
640         lyxerr[Debug::GRAPHICS] << "tempname = " << temp << "\n";
641         lyxerr[Debug::GRAPHICS] << "buf::tmppath = " << buf->tmppath << "\n";
642         lyxerr[Debug::GRAPHICS] << "filename_ = " << filename_ << "\n";
643         lyxerr[Debug::GRAPHICS] << "outfile_base = " << outfile_base << endl;
644
645         converters.convert(buf, filename_, outfile_base, from, to);
646         return outfile_base;
647 }
648
649
650 namespace {
651
652 string const latexify(string const & str)
653 {
654         ostringstream out;
655
656         string::const_iterator it  = str.begin();
657         string::const_iterator end = str.end();
658
659         for (; it != end; ++it) {
660                 switch (*it) {
661                         
662                 case ('$'):
663                 case ('&'):
664                 case ('%'):
665                 case ('#'):
666                 case ('_'):
667                 case ('{'):
668                 case ('}'):
669                         out << '\\' << *it;
670                         break;
671
672                 case ('~'):
673                 case ('^'):
674                         out << '\\' << *it << "{}";
675                         break;
676
677                 case ('\\'):
678                         out << "\textbackslash ";
679                         break;
680
681                 default:
682                         out << *it;
683                         break;
684                 }
685         }
686         
687         return out.str().c_str();
688 }
689  
690 } // namespace anon
691
692
693 int InsetGraphics::latex(Buffer const *buf, ostream & os,
694                          bool /*fragile*/, bool/*fs*/) const
695 {
696         // If there is no file specified, just output a message about it in
697         // the latex output.
698         if (params().filename.empty()) {
699                 os  << "\\fbox{\\rule[-0.5in]{0pt}{1in}"
700                         << _("empty figure path") << "}\n";
701                 return 1; // One end-of-line marker added to the stream.
702         }
703
704         // Enable these helper functions to find the file if it is stored as
705         // a relative path.
706         Path p(buf->filePath());
707
708         // Ditto if the file is not there.
709         if (!IsFileReadable(params().filename)) {
710                 os  << "\\fbox{\\rule[-0.5in]{0pt}{1in}"
711                     << latexify(MakeRelPath(params().filename, buf->filePath()))
712                     << _(" not found") << "}\n";
713                 return 1; // One end-of-line marker added to the stream.
714         }
715         // These variables collect all the latex code that should be before and
716         // after the actual includegraphics command.
717         string before;
718         string after;
719         // Do we want subcaptions?
720         if (params().subcaption) {
721                 before += "\\subfigure[" + params().subcaptionText + "]{";
722                 after = '}';
723         }
724         // We never use the starred form, we use the "clip" option instead.
725         before += "\\includegraphics";
726         // Write the options if there are any.
727         string const opts = createLatexOptions();
728         if (!opts.empty()) {
729                 before += ("[%\n" + opts +']');
730         }
731         // Make the filename relative to the lyx file
732         // and remove the extension so the LaTeX will use whatever is
733         // appropriate (when there are several versions in different formats)
734         string const latex_str = before + '{' + prepareFile(buf) + '}' + after;
735         os << latex_str;
736
737         // Return how many newlines we issued.
738         int const newlines =
739                 int(lyx::count(latex_str.begin(), latex_str.end(),'\n') + 1);
740
741         return newlines;
742 }
743
744
745 int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
746 {
747         // No graphics in ascii output. Possible to use gifscii to convert
748         // images to ascii approximation.
749         // 1. Convert file to ascii using gifscii
750         // 2. Read ascii output file and add it to the output stream.
751         // at least we send the filename
752         os << '<' << _("Graphic file:") << params().filename << ">\n";
753         return 0;
754 }
755
756
757 int InsetGraphics::linuxdoc(Buffer const *, ostream &) const
758 {
759         // No graphics in LinuxDoc output. Should check how/what to add.
760         return 0;
761 }
762
763
764 // For explanation on inserting graphics into DocBook checkout:
765 // http://linuxdoc.org/LDP/LDP-Author-Guide/inserting-pictures.html
766 // See also the docbook guide at http://www.docbook.org/
767 int InsetGraphics::docbook(Buffer const *, ostream & os) const
768 {
769         // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
770         // need to switch to MediaObject. However, for now this is sufficient and
771         // easier to use.
772         os << "<graphic fileref=\"&" << graphic_label << ";\">";
773         return 0;
774 }
775
776
777 void InsetGraphics::validate(LaTeXFeatures & features) const
778 {
779         // If we have no image, we should not require anything.
780         if (params().filename.empty())
781                 return ;
782
783         features.includeFile(graphic_label, RemoveExtension(params_.filename));
784
785         features.require("graphicx");
786
787         if (params().subcaption)
788                 features.require("subfigure");
789 }
790
791
792 bool InsetGraphics::setParams(InsetGraphicsParams const & p,
793                               string const & filepath)
794 {
795         // If nothing is changed, just return and say so.
796         if (params() == p && !p.filename.empty()) {
797                 return false;
798         }
799
800         // Copy the new parameters.
801         params_ = p;
802
803         // Update the inset with the new parameters.
804         updateInset(filepath);
805
806         // We have changed data, report it.
807         return true;
808 }
809
810
811 InsetGraphicsParams const & InsetGraphics::params() const
812 {
813         return params_;
814 }
815
816
817 Inset * InsetGraphics::clone(Buffer const & buffer, bool same_id) const
818 {
819         return new InsetGraphics(*this, buffer.filePath(), same_id);
820 }