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