2 * \file RenderGraphic.cpp
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 "RenderGraphic.h"
15 #include "insets/Inset.h"
20 #include "MetricsInfo.h"
22 #include "frontends/FontMetrics.h"
23 #include "frontends/Painter.h"
25 #include "graphics/GraphicsImage.h"
27 #include "support/filetools.h"
29 #include <boost/bind.hpp>
34 using support::onlyFilename;
39 RenderGraphic::RenderGraphic(Inset const * inset)
41 loader_.connect(boost::bind(&LyX::updateInset,
42 boost::cref(LyX::cref()), inset));
46 RenderGraphic::RenderGraphic(RenderGraphic const & other,
49 loader_(other.loader_),
50 params_(other.params_)
52 loader_.connect(boost::bind(&LyX::updateInset,
53 boost::cref(LyX::cref()), inset));
57 RenderBase * RenderGraphic::clone(Inset const * inset) const
59 return new RenderGraphic(*this, inset);
63 void RenderGraphic::update(graphics::Params const & params)
67 if (!params_.filename.empty()) {
68 loader_.reset(params_.filename, params_);
75 bool displayGraphic(graphics::Params const & params)
77 return params.display != graphics::NoDisplay &&
78 lyxrc.display_graphics != graphics::NoDisplay;
82 docstring const statusMessage(graphics::Params const & params,
83 graphics::ImageStatus status)
87 if (!displayGraphic(params))
88 ret = _("Not shown.");
91 case graphics::WaitingToLoad:
92 ret = _("Not shown.");
94 case graphics::Loading:
95 ret = _("Loading...");
97 case graphics::Converting:
98 ret = _("Converting to loadable format...");
100 case graphics::Loaded:
101 ret = _("Loaded into memory. Generating pixmap...");
103 case graphics::ScalingEtc:
104 ret = _("Scaling etc...");
106 case graphics::Ready:
107 ret = _("Ready to display");
109 case graphics::ErrorNoFile:
110 ret = _("No file found!");
112 case graphics::ErrorConverting:
113 ret = _("Error converting to loadable format");
115 case graphics::ErrorLoading:
116 ret = _("Error loading file into memory");
118 case graphics::ErrorGeneratingPixmap:
119 ret = _("Error generating the pixmap");
121 case graphics::ErrorUnknown:
131 bool readyToDisplay(graphics::Loader const & loader)
133 if (!loader.image() || loader.status() != graphics::Ready)
135 return loader.image()->isDrawable();
141 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
143 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
145 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
149 dim.wid = loader_.image()->getWidth() +
150 2 * Inset::TEXT_TO_INSET_OFFSET;
154 FontInfo msgFont(mi.base.font);
155 msgFont.setFamily(SANS_FAMILY);
158 docstring const justname =
159 from_utf8(onlyFilename(params_.filename.absFilename()));
160 if (!justname.empty()) {
161 msgFont.setSize(FONT_SIZE_FOOTNOTE);
162 font_width = theFontMetrics(msgFont)
166 docstring const msg = statusMessage(params_, loader_.status());
168 msgFont.setSize(FONT_SIZE_TINY);
169 font_width = std::max(font_width,
170 theFontMetrics(msgFont).width(msg));
173 dim.wid = std::max(50, font_width + 15);
180 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
182 if (displayGraphic(params_)) {
183 if (loader_.status() == graphics::WaitingToLoad)
184 loader_.startLoading();
185 if (!loader_.monitoring())
186 loader_.startMonitoring();
189 // This will draw the graphics. If the graphics has not been
190 // loaded yet, we draw just a rectangle.
192 if (displayGraphic(params_) && readyToDisplay(loader_)) {
193 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
195 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
200 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
202 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
206 // Print the file name.
207 FontInfo msgFont = pi.base.font;
208 msgFont.setFamily(SANS_FAMILY);
209 string const justname = onlyFilename(params_.filename.absFilename());
211 if (!justname.empty()) {
212 msgFont.setSize(FONT_SIZE_FOOTNOTE);
213 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
214 y - theFontMetrics(msgFont).maxAscent() - 4,
215 from_utf8(justname), msgFont);
218 // Print the message.
219 docstring const msg = statusMessage(params_, loader_.status());
221 msgFont.setSize(FONT_SIZE_TINY);
222 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
223 y - 4, msg, msgFont);