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/gettext.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>
36 RenderGraphic::RenderGraphic(Inset const * inset)
38 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
42 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
43 : RenderBase(other), loader_(other.loader_), params_(other.params_)
45 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
49 RenderBase * RenderGraphic::clone(Inset const * inset) const
51 return new RenderGraphic(*this, inset);
55 void RenderGraphic::update(graphics::Params const & params)
59 if (!params_.filename.empty())
60 loader_.reset(params_.filename, params_);
66 bool displayGraphic(graphics::Params const & params)
68 return params.display != graphics::NoDisplay &&
69 lyxrc.display_graphics != graphics::NoDisplay;
73 docstring const statusMessage(graphics::Params const & params,
74 graphics::ImageStatus status)
78 if (!displayGraphic(params))
79 ret = _("Not shown.");
82 case graphics::WaitingToLoad:
83 ret = _("Not shown.");
85 case graphics::Loading:
86 ret = _("Loading...");
88 case graphics::Converting:
89 ret = _("Converting to loadable format...");
91 case graphics::Loaded:
92 ret = _("Loaded into memory. Generating pixmap...");
94 case graphics::ScalingEtc:
95 ret = _("Scaling etc...");
98 ret = _("Ready to display");
100 case graphics::ErrorNoFile:
101 ret = _("No file found!");
103 case graphics::ErrorConverting:
104 ret = _("Error converting to loadable format");
106 case graphics::ErrorLoading:
107 ret = _("Error loading file into memory");
109 case graphics::ErrorGeneratingPixmap:
110 ret = _("Error generating the pixmap");
112 case graphics::ErrorUnknown:
122 bool readyToDisplay(graphics::Loader const & loader)
124 if (!loader.image() || loader.status() != graphics::Ready)
126 return loader.image()->isDrawable();
132 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
134 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
136 dim.asc = image_ready ? loader_.image()->height() : 50;
140 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
144 FontInfo msgFont(mi.base.font);
145 msgFont.setFamily(SANS_FAMILY);
148 docstring const justname = from_utf8(params_.filename.onlyFileName());
149 if (!justname.empty()) {
150 msgFont.setSize(FONT_SIZE_FOOTNOTE);
151 font_width = theFontMetrics(msgFont).width(justname);
154 docstring const msg = statusMessage(params_, loader_.status());
156 msgFont.setSize(FONT_SIZE_TINY);
157 font_width = max(font_width,
158 theFontMetrics(msgFont).width(msg));
161 dim.wid = max(50, font_width + 15);
168 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
170 if (displayGraphic(params_)) {
171 if (loader_.status() == graphics::WaitingToLoad)
172 loader_.startLoading();
173 if (!loader_.monitoring())
174 loader_.startMonitoring();
177 // This will draw the graphics. If the graphics has not been
178 // loaded yet, we draw just a rectangle.
180 if (displayGraphic(params_) && readyToDisplay(loader_)) {
181 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
183 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
188 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
190 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
194 // Print the file name.
195 FontInfo msgFont = pi.base.font;
196 msgFont.setFamily(SANS_FAMILY);
197 string const justname = params_.filename.onlyFileName();
199 if (!justname.empty()) {
200 msgFont.setSize(FONT_SIZE_FOOTNOTE);
201 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
202 y - theFontMetrics(msgFont).maxAscent() - 4,
203 from_utf8(justname), msgFont);
206 // Print the message.
207 docstring const msg = statusMessage(params_, loader_.status());
209 msgFont.setSize(FONT_SIZE_TINY);
210 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
211 y - 4, msg, msgFont);