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"
21 #include "metricsinfo.h"
23 #include "frontends/Application.h"
24 #include "frontends/FontLoader.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "graphics/GraphicsImage.h"
30 #include "support/filetools.h"
32 #include <boost/bind.hpp>
34 namespace graphics = lyx::graphics;
37 using lyx::support::absolutePath;
38 using lyx::support::onlyFilename;
44 RenderGraphic::RenderGraphic(InsetBase const * inset)
46 loader_.connect(boost::bind(&LyX::updateInset,
47 boost::cref(LyX::cref()), inset));
51 RenderGraphic::RenderGraphic(RenderGraphic const & other,
52 InsetBase const * inset)
54 loader_(other.loader_),
55 params_(other.params_)
57 loader_.connect(boost::bind(&LyX::updateInset,
58 boost::cref(LyX::cref()), inset));
62 auto_ptr<RenderBase> RenderGraphic::clone(InsetBase const * inset) const
64 return auto_ptr<RenderBase>(new RenderGraphic(*this, inset));
68 void RenderGraphic::update(graphics::Params const & params)
72 if (!params_.filename.empty()) {
73 BOOST_ASSERT(absolutePath(params_.filename));
74 loader_.reset(params_.filename, params_);
81 bool displayGraphic(graphics::Params const & params)
83 return params.display != graphics::NoDisplay &&
84 lyxrc.display_graphics != graphics::NoDisplay;
88 string const statusMessage(graphics::Params const & params,
89 graphics::ImageStatus status)
93 if (!displayGraphic(params))
94 ret = _("Not shown.");
97 case graphics::WaitingToLoad:
98 ret = _("Not shown.");
100 case graphics::Loading:
101 ret = _("Loading...");
103 case graphics::Converting:
104 ret = _("Converting to loadable format...");
106 case graphics::Loaded:
107 ret = _("Loaded into memory. Generating pixmap...");
109 case graphics::ScalingEtc:
110 ret = _("Scaling etc...");
112 case graphics::Ready:
113 ret = _("Ready to display");
115 case graphics::ErrorNoFile:
116 ret = _("No file found!");
118 case graphics::ErrorConverting:
119 ret = _("Error converting to loadable format");
121 case graphics::ErrorLoading:
122 ret = _("Error loading file into memory");
124 case graphics::ErrorGeneratingPixmap:
125 ret = _("Error generating the pixmap");
127 case graphics::ErrorUnknown:
134 return lyx::to_utf8(ret);
138 bool readyToDisplay(graphics::Loader const & loader)
140 if (!loader.image() || loader.status() != graphics::Ready)
142 return loader.image()->isDrawable();
148 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
150 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
152 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
156 dim.wid = loader_.image()->getWidth() +
157 2 * InsetOld::TEXT_TO_INSET_OFFSET;
161 LyXFont msgFont(mi.base.font);
162 msgFont.setFamily(LyXFont::SANS_FAMILY);
164 string const justname = onlyFilename(params_.filename);
165 docstring djust(justname.begin(), justname.end());
166 if (!justname.empty()) {
167 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
168 font_width = theApp->fontLoader().metrics(msgFont)
172 string const msg = statusMessage(params_, loader_.status());
174 docstring dmsg(msg.begin(), msg.end());
175 msgFont.setSize(LyXFont::SIZE_TINY);
176 font_width = std::max(font_width, theApp->fontLoader()
177 .metrics(msgFont).width(dmsg));
180 dim.wid = std::max(50, font_width + 15);
187 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
189 if (displayGraphic(params_)) {
190 if (loader_.status() == graphics::WaitingToLoad)
191 loader_.startLoading();
192 if (!loader_.monitoring())
193 loader_.startMonitoring();
196 // This will draw the graphics. If the graphics has not been
197 // loaded yet, we draw just a rectangle.
199 if (displayGraphic(params_) && readyToDisplay(loader_)) {
200 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
202 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
207 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
209 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
213 // Print the file name.
214 LyXFont msgFont = pi.base.font;
215 msgFont.setFamily(LyXFont::SANS_FAMILY);
216 string const justname = onlyFilename(params_.filename);
218 if (!justname.empty()) {
219 docstring djust(justname.begin(), justname.end());
220 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
221 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
222 y - theApp->fontLoader().metrics(msgFont).maxAscent() - 4,
226 // Print the message.
227 string const msg = statusMessage(params_, loader_.status());
229 docstring dmsg(msg.begin(), msg.end());
230 msgFont.setSize(LyXFont::SIZE_TINY);
231 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
232 y - 4, dmsg, msgFont);