2 * \file render_graphic.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "render_graphic.h"
15 #include "insets/inset.h"
20 #include "metricsinfo.h"
22 #include "frontends/font_metrics.h"
23 #include "frontends/Painter.h"
25 #include "graphics/GraphicsImage.h"
27 #include "support/filetools.h"
29 namespace graphics = lyx::graphics;
31 using lyx::support::AbsolutePath;
32 using lyx::support::OnlyFilename;
38 RenderGraphic::RenderGraphic()
43 RenderGraphic::RenderGraphic(RenderGraphic const & other)
45 loader_(other.loader_),
46 params_(other.params_),
51 auto_ptr<RenderBase> RenderGraphic::clone() const
53 return auto_ptr<RenderBase>(new RenderGraphic(*this));
57 void RenderGraphic::update(graphics::Params const & params)
61 if (!params_.filename.empty()) {
62 BOOST_ASSERT(AbsolutePath(params_.filename));
63 loader_.reset(params_.filename, params_);
68 bool RenderGraphic::hasFileChanged() const
70 unsigned long const new_checksum = loader_.checksum();
71 bool const file_has_changed = checksum_ != new_checksum;
73 checksum_ = new_checksum;
74 return file_has_changed;
78 boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
80 return loader_.connect(slot);
86 bool displayGraphic(graphics::Params const & params)
88 return params.display != graphics::NoDisplay &&
89 lyxrc.display_graphics != graphics::NoDisplay;
93 string const statusMessage(graphics::Params const & params,
94 graphics::ImageStatus status)
96 if (!displayGraphic(params))
97 return _("Not shown.");
100 case graphics::WaitingToLoad:
101 return _("Not shown.");
102 case graphics::Loading:
103 return _("Loading...");
104 case graphics::Converting:
105 return _("Converting to loadable format...");
106 case graphics::Loaded:
107 return _("Loaded into memory. Generating pixmap...");
108 case graphics::ScalingEtc:
109 return _("Scaling etc...");
110 case graphics::Ready:
111 return _("Ready to display");
112 case graphics::ErrorNoFile:
113 return _("No file found!");
114 case graphics::ErrorConverting:
115 return _("Error converting to loadable format");
116 case graphics::ErrorLoading:
117 return _("Error loading file into memory");
118 case graphics::ErrorGeneratingPixmap:
119 return _("Error generating the pixmap");
120 case graphics::ErrorUnknown:
121 return _("No image");
127 bool readyToDisplay(graphics::Loader const & loader)
129 if (!loader.image() || loader.status() != graphics::Ready)
131 return loader.image()->isDrawable();
137 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
139 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
141 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
145 dim.wid = loader_.image()->getWidth() +
146 2 * InsetOld::TEXT_TO_INSET_OFFSET;
150 LyXFont msgFont(mi.base.font);
151 msgFont.setFamily(LyXFont::SANS_FAMILY);
153 string const justname = OnlyFilename(params_.filename);
154 if (!justname.empty()) {
155 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
156 font_width = font_metrics::width(justname, msgFont);
159 string const msg = statusMessage(params_, loader_.status());
161 msgFont.setSize(LyXFont::SIZE_TINY);
162 font_width = std::max(font_width,
163 font_metrics::width(msg, msgFont));
166 dim.wid = std::max(50, font_width + 15);
173 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
175 if (displayGraphic(params_)) {
176 if (loader_.status() == graphics::WaitingToLoad)
177 loader_.startLoading();
178 if (!loader_.monitoring())
179 loader_.startMonitoring();
182 // This will draw the graphics. If the graphics has not been
183 // loaded yet, we draw just a rectangle.
185 if (displayGraphic(params_) && readyToDisplay(loader_)) {
186 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
188 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
193 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
195 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
199 // Print the file name.
200 LyXFont msgFont = pi.base.font;
201 msgFont.setFamily(LyXFont::SANS_FAMILY);
202 string const justname = OnlyFilename(params_.filename);
204 if (!justname.empty()) {
205 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
206 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
207 y - font_metrics::maxAscent(msgFont) - 4,
211 // Print the message.
212 string const msg = statusMessage(params_, loader_.status());
214 msgFont.setSize(LyXFont::SIZE_TINY);
215 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
216 y - 4, msg, msgFont);