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/font_metrics.h"
24 #include "frontends/Painter.h"
26 #include "graphics/GraphicsImage.h"
28 #include "support/filetools.h"
30 #include <boost/bind.hpp>
32 namespace graphics = lyx::graphics;
34 using lyx::support::AbsolutePath;
35 using lyx::support::OnlyFilename;
41 RenderGraphic::RenderGraphic(InsetBase const * inset)
43 loader_.connect(boost::bind(&LyX::updateInset,
44 boost::cref(LyX::cref()), inset));
48 RenderGraphic::RenderGraphic(RenderGraphic const & other,
49 InsetBase const * inset)
51 loader_(other.loader_),
52 params_(other.params_)
54 loader_.connect(boost::bind(&LyX::updateInset,
55 boost::cref(LyX::cref()), inset));
59 auto_ptr<RenderBase> RenderGraphic::clone(InsetBase const * inset) const
61 return auto_ptr<RenderBase>(new RenderGraphic(*this, inset));
65 void RenderGraphic::update(graphics::Params const & params)
69 if (!params_.filename.empty()) {
70 BOOST_ASSERT(AbsolutePath(params_.filename));
71 loader_.reset(params_.filename, params_);
78 bool displayGraphic(graphics::Params const & params)
80 return params.display != graphics::NoDisplay &&
81 lyxrc.display_graphics != graphics::NoDisplay;
85 string const statusMessage(graphics::Params const & params,
86 graphics::ImageStatus status)
88 if (!displayGraphic(params))
89 return _("Not shown.");
92 case graphics::WaitingToLoad:
93 return _("Not shown.");
94 case graphics::Loading:
95 return _("Loading...");
96 case graphics::Converting:
97 return _("Converting to loadable format...");
98 case graphics::Loaded:
99 return _("Loaded into memory. Generating pixmap...");
100 case graphics::ScalingEtc:
101 return _("Scaling etc...");
102 case graphics::Ready:
103 return _("Ready to display");
104 case graphics::ErrorNoFile:
105 return _("No file found!");
106 case graphics::ErrorConverting:
107 return _("Error converting to loadable format");
108 case graphics::ErrorLoading:
109 return _("Error loading file into memory");
110 case graphics::ErrorGeneratingPixmap:
111 return _("Error generating the pixmap");
112 case graphics::ErrorUnknown:
113 return _("No image");
119 bool readyToDisplay(graphics::Loader const & loader)
121 if (!loader.image() || loader.status() != graphics::Ready)
123 return loader.image()->isDrawable();
129 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
131 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
133 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
137 dim.wid = loader_.image()->getWidth() +
138 2 * InsetOld::TEXT_TO_INSET_OFFSET;
142 LyXFont msgFont(mi.base.font);
143 msgFont.setFamily(LyXFont::SANS_FAMILY);
145 string const justname = OnlyFilename(params_.filename);
146 if (!justname.empty()) {
147 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
148 font_width = font_metrics::width(justname, msgFont);
151 string const msg = statusMessage(params_, loader_.status());
153 msgFont.setSize(LyXFont::SIZE_TINY);
154 font_width = std::max(font_width,
155 font_metrics::width(msg, msgFont));
158 dim.wid = std::max(50, font_width + 15);
165 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
167 if (displayGraphic(params_)) {
168 if (loader_.status() == graphics::WaitingToLoad)
169 loader_.startLoading();
170 if (!loader_.monitoring())
171 loader_.startMonitoring();
174 // This will draw the graphics. If the graphics has not been
175 // loaded yet, we draw just a rectangle.
177 if (displayGraphic(params_) && readyToDisplay(loader_)) {
178 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
180 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
185 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
187 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
191 // Print the file name.
192 LyXFont msgFont = pi.base.font;
193 msgFont.setFamily(LyXFont::SANS_FAMILY);
194 string const justname = OnlyFilename(params_.filename);
196 if (!justname.empty()) {
197 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
198 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
199 y - font_metrics::maxAscent(msgFont) - 4,
203 // Print the message.
204 string const msg = statusMessage(params_, loader_.status());
206 msgFont.setSize(LyXFont::SIZE_TINY);
207 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
208 y - 4, msg, msgFont);