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/FontMetrics.h"
24 #include "frontends/Painter.h"
26 #include "graphics/GraphicsImage.h"
28 #include "support/filetools.h"
30 #include <boost/bind.hpp>
35 using support::absolutePath;
36 using support::onlyFilename;
42 RenderGraphic::RenderGraphic(InsetBase const * inset)
44 loader_.connect(boost::bind(&LyX::updateInset,
45 boost::cref(LyX::cref()), inset));
49 RenderGraphic::RenderGraphic(RenderGraphic const & other,
50 InsetBase const * inset)
52 loader_(other.loader_),
53 params_(other.params_)
55 loader_.connect(boost::bind(&LyX::updateInset,
56 boost::cref(LyX::cref()), inset));
60 auto_ptr<RenderBase> RenderGraphic::clone(InsetBase const * inset) const
62 return auto_ptr<RenderBase>(new RenderGraphic(*this, inset));
66 void RenderGraphic::update(graphics::Params const & params)
70 if (!params_.filename.empty()) {
71 BOOST_ASSERT(absolutePath(params_.filename));
72 loader_.reset(params_.filename, params_);
79 bool displayGraphic(graphics::Params const & params)
81 return params.display != graphics::NoDisplay &&
82 lyxrc.display_graphics != graphics::NoDisplay;
86 docstring const statusMessage(graphics::Params const & params,
87 graphics::ImageStatus status)
91 if (!displayGraphic(params))
92 ret = _("Not shown.");
95 case graphics::WaitingToLoad:
96 ret = _("Not shown.");
98 case graphics::Loading:
99 ret = _("Loading...");
101 case graphics::Converting:
102 ret = _("Converting to loadable format...");
104 case graphics::Loaded:
105 ret = _("Loaded into memory. Generating pixmap...");
107 case graphics::ScalingEtc:
108 ret = _("Scaling etc...");
110 case graphics::Ready:
111 ret = _("Ready to display");
113 case graphics::ErrorNoFile:
114 ret = _("No file found!");
116 case graphics::ErrorConverting:
117 ret = _("Error converting to loadable format");
119 case graphics::ErrorLoading:
120 ret = _("Error loading file into memory");
122 case graphics::ErrorGeneratingPixmap:
123 ret = _("Error generating the pixmap");
125 case graphics::ErrorUnknown:
135 bool readyToDisplay(graphics::Loader const & loader)
137 if (!loader.image() || loader.status() != graphics::Ready)
139 return loader.image()->isDrawable();
145 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
147 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
149 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
153 dim.wid = loader_.image()->getWidth() +
154 2 * InsetOld::TEXT_TO_INSET_OFFSET;
158 LyXFont msgFont(mi.base.font);
159 msgFont.setFamily(LyXFont::SANS_FAMILY);
162 docstring const justname =
163 from_utf8(onlyFilename(params_.filename));
164 if (!justname.empty()) {
165 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
166 font_width = theFontMetrics(msgFont)
170 docstring const msg = statusMessage(params_, loader_.status());
172 msgFont.setSize(LyXFont::SIZE_TINY);
173 font_width = std::max(font_width,
174 theFontMetrics(msgFont).width(msg));
177 dim.wid = std::max(50, font_width + 15);
184 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
186 if (displayGraphic(params_)) {
187 if (loader_.status() == graphics::WaitingToLoad)
188 loader_.startLoading();
189 if (!loader_.monitoring())
190 loader_.startMonitoring();
193 // This will draw the graphics. If the graphics has not been
194 // loaded yet, we draw just a rectangle.
196 if (displayGraphic(params_) && readyToDisplay(loader_)) {
197 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
199 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
204 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
206 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
210 // Print the file name.
211 LyXFont msgFont = pi.base.font;
212 msgFont.setFamily(LyXFont::SANS_FAMILY);
213 string const justname = onlyFilename(params_.filename);
215 if (!justname.empty()) {
216 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
217 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
218 y - theFontMetrics(msgFont).maxAscent() - 4,
219 from_utf8(justname), msgFont);
222 // Print the message.
223 docstring const msg = statusMessage(params_, loader_.status());
225 msgFont.setSize(LyXFont::SIZE_TINY);
226 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
227 y - 4, msg, msgFont);