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"
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::onlyFilename;
40 RenderGraphic::RenderGraphic(Inset const * inset)
42 loader_.connect(boost::bind(&LyX::updateInset,
43 boost::cref(LyX::cref()), inset));
47 RenderGraphic::RenderGraphic(RenderGraphic const & other,
50 loader_(other.loader_),
51 params_(other.params_)
53 loader_.connect(boost::bind(&LyX::updateInset,
54 boost::cref(LyX::cref()), inset));
58 RenderBase * RenderGraphic::clone(Inset const * inset) const
60 return new RenderGraphic(*this, inset);
64 void RenderGraphic::update(graphics::Params const & params)
68 if (!params_.filename.empty()) {
69 loader_.reset(params_.filename, params_);
76 bool displayGraphic(graphics::Params const & params)
78 return params.display != graphics::NoDisplay &&
79 lyxrc.display_graphics != graphics::NoDisplay;
83 docstring const statusMessage(graphics::Params const & params,
84 graphics::ImageStatus status)
88 if (!displayGraphic(params))
89 ret = _("Not shown.");
92 case graphics::WaitingToLoad:
93 ret = _("Not shown.");
95 case graphics::Loading:
96 ret = _("Loading...");
98 case graphics::Converting:
99 ret = _("Converting to loadable format...");
101 case graphics::Loaded:
102 ret = _("Loaded into memory. Generating pixmap...");
104 case graphics::ScalingEtc:
105 ret = _("Scaling etc...");
107 case graphics::Ready:
108 ret = _("Ready to display");
110 case graphics::ErrorNoFile:
111 ret = _("No file found!");
113 case graphics::ErrorConverting:
114 ret = _("Error converting to loadable format");
116 case graphics::ErrorLoading:
117 ret = _("Error loading file into memory");
119 case graphics::ErrorGeneratingPixmap:
120 ret = _("Error generating the pixmap");
122 case graphics::ErrorUnknown:
132 bool readyToDisplay(graphics::Loader const & loader)
134 if (!loader.image() || loader.status() != graphics::Ready)
136 return loader.image()->isDrawable();
142 bool RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
144 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
146 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
150 dim.wid = loader_.image()->getWidth() +
151 2 * Inset::TEXT_TO_INSET_OFFSET;
155 Font msgFont(mi.base.font);
156 msgFont.setFamily(Font::SANS_FAMILY);
159 docstring const justname =
160 from_utf8(onlyFilename(params_.filename.absFilename()));
161 if (!justname.empty()) {
162 msgFont.setSize(Font::SIZE_FOOTNOTE);
163 font_width = theFontMetrics(msgFont)
167 docstring const msg = statusMessage(params_, loader_.status());
169 msgFont.setSize(Font::SIZE_TINY);
170 font_width = std::max(font_width,
171 theFontMetrics(msgFont).width(msg));
174 dim.wid = std::max(50, font_width + 15);
177 bool const changed = dim_ != dim;
183 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
185 if (displayGraphic(params_)) {
186 if (loader_.status() == graphics::WaitingToLoad)
187 loader_.startLoading();
188 if (!loader_.monitoring())
189 loader_.startMonitoring();
192 // This will draw the graphics. If the graphics has not been
193 // loaded yet, we draw just a rectangle.
195 if (displayGraphic(params_) && readyToDisplay(loader_)) {
196 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
198 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
203 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
205 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
209 // Print the file name.
210 Font msgFont = pi.base.font;
211 msgFont.setFamily(Font::SANS_FAMILY);
212 string const justname = onlyFilename(params_.filename.absFilename());
214 if (!justname.empty()) {
215 msgFont.setSize(Font::SIZE_FOOTNOTE);
216 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
217 y - theFontMetrics(msgFont).maxAscent() - 4,
218 from_utf8(justname), msgFont);
221 // Print the message.
222 docstring const msg = statusMessage(params_, loader_.status());
224 msgFont.setSize(Font::SIZE_TINY);
225 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
226 y - 4, msg, msgFont);