]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphics.C
rotatefix patch
[lyx.git] / src / insets / insetgraphics.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995-2001 the LyX Team.
7  *           
8  *           This file Copyright 2000 Baruch Even.
9  * ====================================================== */
10
11 /*
12 Major tasks:
13         * Switch to convert the images in the background, this requires work on
14                 the converter, the systemcontroller and the graphics cache.
15
16 Minor tasks:
17     * Pop up a dialog if the widget version is higher than what we accept.
18         * Provide sed/awk/C code to downgrade from InsetGraphics to FigInset(?)
19         
20 */
21
22 /*
23 Known BUGS:
24     
25     * If the image is from the clipart, and the document is moved to another
26        directory, the user is screwed. Need a way to handle it.
27        This amounts to a problem of when to use relative or absolute file paths
28        We should probably use what the user asks to use... but when he chooses
29        by the file dialog we normally get an absolute path and this may not be 
30        what the user meant.
31
32         * If we are trying to create a file in a read-only directory and there
33                 are graphics that need converting, the converting will fail because
34                 it is done in-place, into the same directory as the original image.
35                 This needs to be fixed in the src/converter.C file
36                 [ This is presumed to be fixed, needs testing.]
37
38         * We do not dither or resize the image in a WYSIWYM way, we load it at
39                 its original size and color, resizing is done in the final output,
40                 but not in the LyX window.
41
42         * The scale option is only handled for the horizontal part, the vertical
43                 part will not work. For now it is also shown only for horizontal
44                 resizing on the form.
45
46         * EPS figures are not fully detected, they may have a lot of possible
47                 suffixes so we need to read the file and detect if it's EPS or not.
48                 [Implemented, need testing]
49                 
50 TODO Before initial production release:
51     * Replace insetfig everywhere
52         * Search for comments of the form
53             // INSET_GRAPHICS: remove this when InsetFig is thrown.
54           And act upon them. Make sure not to remove InsetFig code for the 
55                   1.2.0 release, only afterwards, after deployment shows InsetGraphics
56                   to be ok.
57  
58 TODO Extended features:
59  
60     * Advanced Latex tab folder.
61     * Add support for more features so that it will be better than insetfig.
62         * Keep aspect ratio radio button
63         * Support for complete control over the latex parameters for TeXperts
64         * What advanced features the users want to do?
65             Implement them in a non latex dependent way, but a logical way.
66             LyX should translate it to latex or any other fitting format.
67     * Add a way to roll the image file into the file format.
68     * When loading, if the image is not found in the expected place, try
69        to find it in the clipart, or in the same directory with the image.
70     * Keep a tab on the image file, if it changes, update the lyx view.
71         * The image choosing dialog could show thumbnails of the image formats
72                 it knows of, thus selection based on the image instead of based on
73                 filename.
74         * Add support for the 'picins' package.
75         * Add support for the 'picinpar' package.
76         * Improve support for 'subfigure' - Allow to set the various options
77                 that are possible.
78         * Add resizing by percentage of image size (50%, 150%) - usefull for two
79                 images of different size to be resized where they both should have
80                 the same scale compared to each other.
81  */
82
83 /* NOTES:
84  *
85  * Intentions:
86  *  This is currently a moving target, I'm trying stuff and learning what
87  *  is needed and how to accomplish it, since there is no predefined goal or
88  *  way to go I invent it as I go.
89  *
90  *  My current intention is for seperation from LaTeX, the basic needs are 
91  *  resizing and rotating, displaying on screen in various depths and printing
92  *  conversion of depths (independent of the display depth). For this I'll 
93  *  provide a simple interface.
94  *
95  *  The medium level includes clipping of the image, but in a limited way.
96  *
97  *  For the LaTeX gurus I'll provide a complete control over the output, but
98  *  this is latex dependent and guru dependent so I'd rather avoid doing this
99  *  for the normal user. This stuff includes clipping, special image size
100  *  specifications (\textwidth\minus 2in) which I see no way to generalize
101  *  to non-latex specific way.
102  *
103  * Used packages:
104  *  'graphicx' for the graphics inclusion.
105  *  'subfigure' for the subfigures.
106  *
107  * Fileformat:
108  *
109  * Current version is 1 (inset file format version), when changing it
110  * it should be changed in the Write() function when writing in one place
111  * and when reading one should change the version check and the error message.
112  *
113  * The filename is kept in  the lyx file in a relative way, so as to allow
114  * moving the document file and its images with no problem.
115  *
116  * Conversions:
117  *   Postscript output means EPS figures.
118  *
119  *   PDF output is best done with PDF figures if it's a direct conversion
120  *   or PNG figures otherwise.
121  *      Image format
122  *      from        to
123  *      EPS         epstopdf
124  *      JPG/PNG     direct
125  *      PDF         direct
126  *      others      PNG
127  */
128
129 #include <config.h> 
130
131 #ifdef __GNUG__
132 #pragma implementation
133 #endif 
134
135 #include "insets/insetgraphics.h"
136 #include "insets/insetgraphicsParams.h"
137 #include "graphics/GraphicsCache.h"
138 #include "graphics/GraphicsCacheItem.h"
139
140 #include "frontends/Dialogs.h"
141 #include "LyXView.h"
142 #include "buffer.h"
143 #include "BufferView.h"
144 #include "converter.h"
145 #include "frontends/support/LyXImage.h"
146 #include "Painter.h"
147 #include "lyx_gui_misc.h"
148 #include "support/FileInfo.h"
149 #include "support/filetools.h"
150 #include "support/lyxlib.h"
151 #include "lyxtext.h"
152 #include "lyxrc.h"
153 #include "font.h" // For the lyxfont class.
154 #include "fstream" // for ifstream in isEPS
155 #include <algorithm> // For the std::max
156 #include "support/lyxmanip.h"
157 #include "debug.h"
158 #include "gettext.h"
159
160 extern string system_tempdir;
161
162 using std::ifstream;
163 using std::ostream;
164 using std::endl;
165
166
167 // This function is a utility function
168 // ... that should be with ChangeExtension ...
169 inline
170 string const RemoveExtension(string const & filename)
171 {
172         return ChangeExtension(filename, string());
173 }
174
175
176 // Initialize only those variables that do not have a constructor.
177 InsetGraphics::InsetGraphics()
178         : cacheHandle(0), imageLoaded(false)
179 {}
180
181
182 InsetGraphics::InsetGraphics(InsetGraphics const & ig, bool same_id)
183         : Inset(), SigC::Object()
184         , cacheHandle(ig.cacheHandle)
185         , imageLoaded(ig.imageLoaded)
186 {
187         setParams(ig.getParams());
188         if (same_id)
189                 id_ = ig.id_;
190 }
191
192
193 InsetGraphics::~InsetGraphics()
194 {
195         // Emits the hide signal to the dialog connected (if any)
196         hideDialog();
197 }
198
199
200 string const
201 InsetGraphics::statusMessage() const
202 {
203         string msg;
204
205         if (cacheHandle.get()) {
206                 switch (cacheHandle->getImageStatus()) {
207                 case GraphicsCacheItem::UnknownError:
208                         msg = _("Unknown Error");
209                         break;
210
211                 case GraphicsCacheItem::Loading:
212                         msg = _("Loading...");
213                         break;
214
215                 case GraphicsCacheItem::ErrorReading:
216                         msg = _("Error reading");
217                         break;
218
219                 case GraphicsCacheItem::ErrorConverting:
220                         msg = _("Error converting");
221                         break;
222
223                 case GraphicsCacheItem::Loaded:
224                         // No message to write.
225                         break;
226                 }
227         }
228
229         return msg;
230 }
231
232
233 int InsetGraphics::ascent(BufferView *, LyXFont const &) const
234 {
235         LyXImage * pixmap = 0;
236         if (cacheHandle.get() && (pixmap = cacheHandle->getImage()))
237                 return pixmap->getHeight();
238         else
239                 return 50;
240 }
241
242
243 int InsetGraphics::descent(BufferView *, LyXFont const &) const
244 {
245         // this is not true if viewport is used and clip is not.
246         return 0;
247 }
248
249
250 int InsetGraphics::width(BufferView *, LyXFont const & font) const
251 {
252         LyXImage * pixmap = 0;
253         
254         if (cacheHandle.get() && (pixmap = cacheHandle->getImage()))
255                 return pixmap->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         Painter & paint = bv->painter();
284
285         int ldescent = descent(bv, font);
286         int lascent = ascent(bv, font);
287         int lwidth = width(bv, font);
288
289         // Make sure x is updated upon exit from this routine
290         int old_x = int(x);
291         x += lwidth;
292
293         // This will draw the graphics. If the graphics has not been loaded yet,
294         // we draw just a rectangle.
295         if (imageLoaded) {
296
297                 paint.image(old_x + 2, baseline - lascent,
298                             lwidth - 4, lascent + ldescent,
299                             cacheHandle->getImage());
300         } else {
301                 
302                 // Get the image status, default to unknown error.
303                 GraphicsCacheItem::ImageStatus status = GraphicsCacheItem::UnknownError;
304                 if (lyxrc.display_graphics != "no" &&
305                     params.display != InsetGraphicsParams::NONE &&
306                     cacheHandle.get())
307                         status = cacheHandle->getImageStatus();
308                 
309                 // Check if the image is now ready.
310                 if (status == GraphicsCacheItem::Loaded) {
311                         imageLoaded = true;
312
313                         // Tell BufferView we need to be updated!
314                         bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
315                         return;
316                 }
317
318                 paint.rectangle(old_x + 2, baseline - lascent,
319                                 lwidth - 4,
320                                 lascent + ldescent);
321
322                 // Print the file name.
323                 LyXFont msgFont(font);
324                 msgFont.setFamily(LyXFont::SANS_FAMILY);
325
326                 string const justname = OnlyFilename (params.filename);
327                 if (!justname.empty()) {
328                         msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
329                         paint.text(old_x + 8, 
330                                    baseline - lyxfont::maxAscent(msgFont) - 4,
331                                    justname, msgFont);
332                 }
333
334                 // Print the message.
335                 string const msg = statusMessage();
336                 if (!msg.empty()) {
337                         msgFont.setSize(LyXFont::SIZE_TINY);
338                         paint.text(old_x + 8, baseline - 4, msg, msgFont);
339                 }
340         }
341 }
342
343
344 void InsetGraphics::edit(BufferView *bv, int, int, unsigned int)
345 {
346         bv->owner()->getDialogs()->showGraphics(this);
347 }
348
349
350 void InsetGraphics::edit(BufferView * bv, bool)
351 {
352         edit(bv, 0, 0, 0);
353 }
354
355
356 Inset::EDITABLE InsetGraphics::editable() const
357 {
358         return IS_EDITABLE;
359 }
360
361
362 void InsetGraphics::write(Buffer const * buf, ostream & os) const
363 {
364         os << "Graphics FormatVersion 1\n";
365
366         params.Write(buf, os);
367 }
368
369
370 void InsetGraphics::read(Buffer const * buf, LyXLex & lex)
371 {
372         string const token = lex.getString();
373
374         if (token == "Graphics")
375                 readInsetGraphics(buf, lex);
376         else if (token == "Figure") // Compatibility reading of FigInset figures.
377                 readFigInset(buf, lex);
378         else
379                 lyxerr[Debug::INFO] << "Not a Graphics or Figure inset!\n";
380
381         updateInset();
382 }
383
384 void InsetGraphics::readInsetGraphics(Buffer const * buf, LyXLex & lex)
385 {
386         bool finished = false;
387
388         while (lex.isOK() && !finished) {
389                 lex.next();
390
391                 string const token = lex.getString();
392                 lyxerr[Debug::INFO] << "Token: '" << token << '\'' 
393                                     << std::endl;
394
395                 if (token.empty()) {
396                         continue;
397                 } else if (token == "\\end_inset") {
398                         finished = true;
399                 } else if (token == "FormatVersion") {
400                         lex.next();
401                         int version = lex.getInteger();
402                         if (version > 1)
403                                 lyxerr
404                                 << "This document was created with a newer Graphics widget"
405                                 ", You should use a newer version of LyX to read this"
406                                 " file."
407                                 << std::endl;
408                         // TODO: Possibly open up a dialog?
409                 }
410                 else {
411                         if (! params.Read(buf, lex, token))
412                                 lyxerr << "Unknown token, " << token << ", skipping." 
413                                         << std::endl;
414                 }
415         }
416 }
417
418
419 void InsetGraphics::readFigInset(Buffer const * buf, LyXLex & lex)
420 {
421         bool finished = false;
422         
423         while (lex.isOK() && !finished) {
424                 lex.next();
425
426                 string const token = lex.getString();
427                 lyxerr[Debug::INFO] << "Token: " << token << endl;
428                 
429                 if (token.empty())
430                         continue;
431                 else if (token == "\\end_inset") {
432                         finished = true;
433                 } else if (token == "file") {
434                         if (lex.next()) {
435                                 string const name = lex.getString();
436                                 string const path = OnlyPath(buf->fileName());
437                                 params.filename = MakeAbsPath(name, path);
438                         }
439                 } else if (token == "extra") {
440                         if (lex.next());
441                         // kept for backwards compability. Delete in 0.13.x
442                 } else if (token == "subcaption") {
443                         if (lex.eatLine())
444                                 params.subcaptionText = lex.getString();
445                 } else if (token == "label") {
446                         if (lex.next());
447                         // kept for backwards compability. Delete in 0.13.x
448                 } else if (token == "angle") {
449                         if (lex.next())
450                                 params.rotateAngle = lex.getFloat();
451                 } else if (token == "size") {
452                         // Size of image on screen is ignored in InsetGraphics, just eat
453                         // the input.
454                         if (lex.next()) {
455                                 lex.getInteger();
456                         }
457                         if (lex.next()) {
458                                 lex.getInteger();
459                         }
460                 } else if (token == "flags") {
461                         InsetGraphicsParams::DisplayType tmp = InsetGraphicsParams::COLOR;
462                         if (lex.next())
463                                 switch (lex.getInteger()) {
464                                 case 1: tmp = InsetGraphicsParams::MONOCHROME; break;
465                                 case 2: tmp = InsetGraphicsParams::GRAYSCALE; break;
466                                 }
467                         params.display = tmp;
468                 } else if (token == "subfigure") {
469                         params.subcaption = true;
470                 } else if (token == "width") {
471                         if (lex.next()) {
472                                 params.widthResize = static_cast<InsetGraphicsParams::Resize>(lex.getInteger());
473                         }
474                         if (lex.next()) {
475                                 params.widthSize = lex.getFloat();
476                         }
477                 } else if (token == "height") {
478                         if (lex.next()) {
479                                 params.heightResize = static_cast<InsetGraphicsParams::Resize>(lex.getInteger());
480                         }
481                         if (lex.next()) {
482                                 params.heightSize = lex.getFloat();
483                         }
484                 }
485         }
486 }
487
488
489 namespace {
490
491 void formatResize(ostream & os, string const & key,
492                   InsetGraphicsParams::Resize resizeType, double size)
493 {
494         switch (resizeType) {
495         case InsetGraphicsParams::DEFAULT_SIZE:
496                 break;
497
498         case InsetGraphicsParams::CM:
499                 os << key << '=' << size << "cm,";
500                 break;
501
502         case InsetGraphicsParams::INCH:
503                 os << key << '=' << size << "in,";
504                 break;
505
506         case InsetGraphicsParams::PERCENT_PAGE:
507                 os << key << '=' << size / 100 << "\\text" << key << ',';
508                 break;
509
510         case InsetGraphicsParams::PERCENT_COLUMN:
511                 os << key << '=' << size / 100 << "\\column" << key << ',';
512                 break;
513
514         case InsetGraphicsParams::SCALE:
515                 os << "scale" << '=' << size/100 << ',';
516         }
517 }
518
519 } // namespace anon
520
521
522 string const
523 InsetGraphics::createLatexOptions() const
524 {
525         // Calculate the options part of the command, we must do it to a string
526         // stream since we might have a trailing comma that we would like to remove
527         // before writing it to the output stream.
528         ostringstream options;
529
530         formatResize(options, "width", params.widthResize, params.widthSize);
531         formatResize(options, "height", params.heightResize, params.heightSize);
532
533         // Make sure it's not very close to zero, a float can be effectively
534         // zero but not exactly zero.
535         if (!lyx::float_equal(params.rotateAngle, 0, 0.001)) {
536                 options << "angle="
537                         << params.rotateAngle << ',';
538         }
539
540         string opts = options.str().c_str();
541         opts = strip(opts, ',');
542
543         return opts;
544 }
545
546 namespace {
547
548 enum FileType {
549         EPS,
550         PNG,
551         JPEG,
552         GIF,
553         PDF,
554         UNKNOWN
555 };
556
557 bool isEPS(string const & filename)
558 {
559         if (filename.empty() || !IsFileReadable(filename)) return false;
560
561         ifstream ifs(filename.c_str());
562
563         if (!ifs) return false; // Couldn't open file...
564
565         bool is_eps = false; // Have we recognized the file as EPS?
566         string to_find = "%!PS-Adobe-"; // The string we use to recognize
567         int const max_attempts = 500; // Maximum strings to read to attempt recognition
568         int count = 0; // Counter of attempts.
569         string str;
570         for (; count < max_attempts; ++count) {
571                 if (ifs.eof()) {
572                         lyxerr[Debug::INFO] << "InsetGraphics (isEPS)"
573                                 " End of file reached and it wasn't found to be EPS!" << endl;
574                         break;
575                 }
576
577                 ifs >> str;
578                 if (str.find(to_find)) {
579                         is_eps = true;
580                         break;
581                 }
582         }
583
584         return is_eps;
585 }
586
587 enum FileType classifyFileType(string const & filename, string const & suffix)
588 {
589         if (suffix == "png")
590                 return PNG;
591         else if (suffix == "jpg" || suffix == "jpeg")
592                 return JPEG;
593         else if (suffix == "gif")
594                 return GIF;
595         else if (suffix == "pdf")
596                 return PDF;
597         else if (isEPS(filename))
598                 return EPS;
599
600         return UNKNOWN;
601 }
602
603 string decideOutputImageFormat(string const & suffix, enum FileType type)
604 {
605         // lyxrc.pdf_mode means:
606         // Are we creating a PDF or a PS file?
607         // (Should actually mean, are we using latex or pdflatex).
608         
609         if (lyxrc.pdf_mode) {
610                 if (type == EPS || type == EPS || type == PDF)
611                         return "pdf";
612                 else if (type == JPEG)
613                         return suffix;
614                 else
615                         return "png";
616         }
617
618         // If it's postscript, we always do eps.
619         // There are many suffixes that are actually EPS (ask Garst for example)
620         // so we detect if it's an EPS by looking in the file, if it is, we return
621         // the same suffix of the file so it won't be converted.
622         if (type == EPS)
623                 return suffix;
624         
625         return "eps";
626 }
627
628 } // Anon. namespace
629
630 string const 
631 InsetGraphics::prepareFile(Buffer const *buf) const
632 {
633
634         // do_convert = Do we need to convert the file?
635         // nice = Do we create a nice version?
636         //        This is used when exporting the latex file only.
637         // 
638         // 
639         // if (!do_convert)
640         //   return original filename
641         // 
642         // if (!nice)
643         //   convert_place = temp directory
644         //   return new filename in temp directory
645         // else
646         //   convert_place = original file directory
647         //   return original filename without the extension
648         //
649         
650         // Get the extension (format) of the original file.
651         string const extension = GetExtension(params.filename);
652         FileType type = classifyFileType(params.filename, extension);
653         
654         // Are we creating a PDF or a PS file?
655         // (Should actually mean, are we usind latex or pdflatex).
656         string const image_target = decideOutputImageFormat(extension, type);
657
658         if (extension == image_target)
659                 return params.filename;
660
661         string outfile;
662         if (!buf->niceFile) {
663                 string const temp = AddName(buf->tmppath, params.filename);
664                 outfile = RemoveExtension(temp);
665                 
666                 //lyxerr << "buf::tmppath = " << buf->tmppath << "\n";
667                 //lyxerr << "filename = " << params.filename << "\n";
668                 //lyxerr << "temp = " << temp << "\n";
669                 //lyxerr << "outfile = " << outfile << endl;
670         } else {
671                 string const path = OnlyPath(buf->fileName());
672                 string const relname = MakeRelPath(params.filename, path);
673                 outfile = RemoveExtension(relname);
674         }
675
676         converters.convert(buf, params.filename, outfile, extension, image_target);
677         
678         return outfile;
679 }
680
681
682 int InsetGraphics::latex(Buffer const *buf, ostream & os,
683                          bool /*fragile*/, bool/*fs*/) const
684 {
685         // MISSING: We have to decide how to do the order of the options
686         // that is dependent of order, like width, height, angle. Should
687         // we rotate before scale? Should we let the user decide?
688         // bool rot_before_scale; ?
689
690         // (BE) As a first step we should do a scale before rotate since this is
691         // more like the natural thought of how to do it.
692         // (BE) I believe that a priority list presented to the user with
693         // a default order would be the best, though it would be better to
694         // hide such a thing in an "Advanced options" dialog.
695         // (BE) This should go an advanced LaTeX options dialog.
696
697         // If there is no file specified, just output a message about it in
698         // the latex output.
699         if (params.filename.empty()) {
700                 os  << "\\fbox{\\rule[-0.5in]{0pt}{1in}"
701                         << _("empty figure path")
702                         << "}\n";
703
704                 return 1; // One end of line marker added to the stream.
705         }
706
707         // Keep count of newlines that we issued.
708         int newlines = 0;
709
710         // This variables collect all the latex code that should be before and
711         // after the actual includegraphics command.
712         string before;
713         string after;
714
715         // Do we want subcaptions?
716         if (params.subcaption) {
717                 before += "\\subfigure[" + params.subcaptionText + "]{";
718                 after = '}' + after;
719         }
720
721         // We never use the starred form, we use the "clip" option instead.
722         os << before << "\\includegraphics";
723
724         // Write the options if there are any.
725         string const opts = createLatexOptions();
726         if (!opts.empty()) {
727                 os << '[' << opts << ']';
728         }
729
730         // Make the filename relative to the lyx file
731         // and remove the extension so the LaTeX will use whatever is
732         // appropriate (when there are several versions in different formats)
733         string const filename = prepareFile(buf);
734         
735         os << '{' << filename << '}' << after;
736
737         // Return how many newlines we issued.
738         return newlines;
739 }
740
741
742 int InsetGraphics::ascii(Buffer const *, ostream &, int) const
743 {
744         // No graphics in ascii output. Possible to use gifscii to convert
745         // images to ascii approximation.
746         
747         // 1. Convert file to ascii using gifscii
748         // 2. Read ascii output file and add it to the output stream.
749         
750         return 0;
751 }
752
753
754 int InsetGraphics::linuxdoc(Buffer const *, ostream &) const
755 {
756         // No graphics in LinuxDoc output. Should check how/what to add.
757         return 0;
758 }
759
760
761 // For explanation on inserting graphics into DocBook checkout:
762 // http://linuxdoc.org/LDP/LDP-Author-Guide/inserting-pictures.html
763 // See also the docbook guide at http://www.docbook.org/
764 int InsetGraphics::docbook(Buffer const * buf, ostream & os) const
765 {
766         // Change the path to be relative to the main file.
767         string const buffer_dir = OnlyPath(buf->fileName());
768         string const filename = RemoveExtension(
769                                    MakeRelPath(params.filename, buffer_dir));
770
771         // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will 
772         // need to switch to MediaObject. However, for now this is sufficient and 
773         // easier to use.
774         os << "<graphic fileref=\"" << filename << "\"></graphic>";
775         return 0;
776 }
777
778
779 void InsetGraphics::validate(LaTeXFeatures & features) const
780 {
781         // If we have no image, we should not require anything.
782         if (params.filename.empty())
783                 return ;
784
785         features.require("graphicx");
786
787         if (params.subcaption)
788                 features.require("subfigure");
789 }
790
791
792 // Update the inset after parameters changed (read from file or changed in
793 // dialog.
794 void InsetGraphics::updateInset() const
795 {
796         GraphicsCache & gc = GraphicsCache::getInstance();
797         boost::shared_ptr<GraphicsCacheItem> temp(0);
798
799         // We do it this way so that in the face of some error, we will still
800         // be in a valid state.
801         if (!params.filename.empty() &&
802             lyxrc.display_graphics != "no" &&
803             params.display != InsetGraphicsParams::NONE) {
804                 temp = gc.addFile(params.filename);
805         }
806
807         // Mark the image as unloaded so that it gets updated.
808         imageLoaded = false;
809
810         cacheHandle = temp;
811 }
812
813
814 bool InsetGraphics::setParams(InsetGraphicsParams const & p)
815 {
816         // If nothing is changed, just return and say so.
817         if (params == p)
818                 return false;
819
820         // Copy the new parameters.
821         params = p;
822
823         // Update the inset with the new parameters.
824         updateInset();
825
826         // We have changed data, report it.
827         return true;
828 }
829
830
831 InsetGraphicsParams InsetGraphics::getParams() const
832 {
833         return params;
834 }
835
836
837 Inset * InsetGraphics::clone(Buffer const &, bool same_id) const
838 {
839         return new InsetGraphics(*this, same_id);
840 }