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"
17 #include "support/FileName.h"
18 #include "support/filetools.h"
19 #include "support/gettext.h"
22 #include "MetricsInfo.h"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "graphics/GraphicsImage.h"
29 #include "support/filetools.h"
31 #include <boost/bind.hpp>
38 RenderGraphic::RenderGraphic(Inset const * inset)
40 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
41 icon_.connect(boost::bind(&Inset::updateFrontend, inset));
45 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
46 : RenderBase(other), loader_(other.loader_), icon_(other.icon_),
47 params_(other.params_)
49 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
50 icon_.connect(boost::bind(&Inset::updateFrontend, inset));
54 RenderBase * RenderGraphic::clone(Inset const * inset) const
56 return new RenderGraphic(*this, inset);
60 void RenderGraphic::update(graphics::Params const & params)
64 if (!params_.filename.empty())
65 loader_.reset(params_.filename, params_);
66 // If icon is set to empty, icon_ will not be reset to empty
67 // but will not be displayed. This is to avoid repeated loading
68 // of the same icon when figure status changes.
69 if (!params_.icon.empty()) {
70 support::FileName const icon = support::libFileSearch("images/",
72 if (!icon.empty()) // using an empty bounding box
73 icon_.reset(icon, graphics::Params());
80 bool displayGraphic(graphics::Params const & params)
82 return params.display != graphics::NoDisplay &&
83 lyxrc.display_graphics != graphics::NoDisplay;
87 docstring const statusMessage(graphics::Params const & params,
88 graphics::ImageStatus status)
92 if (!displayGraphic(params))
93 ret = _("Not shown.");
96 case graphics::WaitingToLoad:
97 ret = _("Not shown.");
99 case graphics::Loading:
100 ret = _("Loading...");
102 case graphics::Converting:
103 ret = _("Converting to loadable format...");
105 case graphics::Loaded:
106 ret = _("Loaded into memory. Generating pixmap...");
108 case graphics::ScalingEtc:
109 ret = _("Scaling etc...");
111 case graphics::Ready:
112 ret = _("Ready to display");
114 case graphics::ErrorNoFile:
115 ret = _("No file found!");
117 case graphics::ErrorConverting:
118 ret = _("Error converting to loadable format");
120 case graphics::ErrorLoading:
121 ret = _("Error loading file into memory");
123 case graphics::ErrorGeneratingPixmap:
124 ret = _("Error generating the pixmap");
126 case graphics::ErrorUnknown:
136 bool readyToDisplay(graphics::Loader const & loader)
138 if (!loader.image() || loader.status() != graphics::Ready)
140 return loader.image()->isDrawable();
146 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
148 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
150 dim.asc = image_ready ? loader_.image()->height() : 50;
154 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
158 FontInfo msgFont(mi.base.font);
159 msgFont.setFamily(SANS_FAMILY);
162 docstring const justname = from_utf8(params_.filename.onlyFileName());
163 if (!justname.empty()) {
164 msgFont.setSize(FONT_SIZE_FOOTNOTE);
165 font_width = theFontMetrics(msgFont).width(justname);
168 docstring const msg = statusMessage(params_, loader_.status());
170 msgFont.setSize(FONT_SIZE_TINY);
171 font_width = max(font_width,
172 theFontMetrics(msgFont).width(msg));
175 dim.wid = max(50, font_width + 15);
182 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
184 if (displayGraphic(params_)) {
185 if (loader_.status() == graphics::WaitingToLoad)
186 loader_.startLoading();
187 if (!loader_.monitoring())
188 loader_.startMonitoring();
189 if (icon_.status() == graphics::WaitingToLoad)
190 icon_.startLoading();
191 if (!icon_.monitoring())
192 icon_.startMonitoring();
195 // This will draw the graphics. If the graphics has not been
196 // loaded yet, we draw just a rectangle.
198 if (displayGraphic(params_) && readyToDisplay(loader_)) {
199 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
201 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
206 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
208 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
212 // Print the file name.
213 FontInfo msgFont = pi.base.font;
214 msgFont.setFamily(SANS_FAMILY);
215 string const justname = params_.filename.onlyFileName();
217 if (!justname.empty()) {
218 msgFont.setSize(FONT_SIZE_FOOTNOTE);
219 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
220 y - theFontMetrics(msgFont).maxAscent() - 4,
221 from_utf8(justname), msgFont);
224 // Print the message.
225 docstring const msg = statusMessage(params_, loader_.status());
227 msgFont.setSize(FONT_SIZE_TINY);
228 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
229 y - 4, msg, msgFont);
232 if (!params_.icon.empty() && readyToDisplay(icon_))
233 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET, y - dim_.asc,
234 10, 10, *icon_.image());