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>
35 RenderGraphic::RenderGraphic(Inset const * inset)
37 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
41 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
42 : RenderBase(other), loader_(other.loader_), params_(other.params_)
44 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
48 RenderBase * RenderGraphic::clone(Inset const * inset) const
50 return new RenderGraphic(*this, inset);
54 void RenderGraphic::update(graphics::Params const & params)
58 if (!params_.filename.empty())
59 loader_.reset(params_.filename, params_);
65 bool displayGraphic(graphics::Params const & params)
67 return params.display != graphics::NoDisplay &&
68 lyxrc.display_graphics != graphics::NoDisplay;
72 docstring const statusMessage(graphics::Params const & params,
73 graphics::ImageStatus status)
77 if (!displayGraphic(params))
78 ret = _("Not shown.");
81 case graphics::WaitingToLoad:
82 ret = _("Not shown.");
84 case graphics::Loading:
85 ret = _("Loading...");
87 case graphics::Converting:
88 ret = _("Converting to loadable format...");
90 case graphics::Loaded:
91 ret = _("Loaded into memory. Generating pixmap...");
93 case graphics::ScalingEtc:
94 ret = _("Scaling etc...");
97 ret = _("Ready to display");
99 case graphics::ErrorNoFile:
100 ret = _("No file found!");
102 case graphics::ErrorConverting:
103 ret = _("Error converting to loadable format");
105 case graphics::ErrorLoading:
106 ret = _("Error loading file into memory");
108 case graphics::ErrorGeneratingPixmap:
109 ret = _("Error generating the pixmap");
111 case graphics::ErrorUnknown:
121 bool readyToDisplay(graphics::Loader const & loader)
123 if (!loader.image() || loader.status() != graphics::Ready)
125 return loader.image()->isDrawable();
131 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
133 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
135 dim.asc = image_ready ? loader_.image()->height() : 50;
139 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
143 FontInfo msgFont(mi.base.font);
144 msgFont.setFamily(SANS_FAMILY);
147 docstring const justname = from_utf8(params_.filename.onlyFileName());
148 if (!justname.empty()) {
149 msgFont.setSize(FONT_SIZE_FOOTNOTE);
150 font_width = theFontMetrics(msgFont).width(justname);
153 docstring const msg = statusMessage(params_, loader_.status());
155 msgFont.setSize(FONT_SIZE_TINY);
156 font_width = std::max(font_width,
157 theFontMetrics(msgFont).width(msg));
160 dim.wid = std::max(50, font_width + 15);
167 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
169 if (displayGraphic(params_)) {
170 if (loader_.status() == graphics::WaitingToLoad)
171 loader_.startLoading();
172 if (!loader_.monitoring())
173 loader_.startMonitoring();
176 // This will draw the graphics. If the graphics has not been
177 // loaded yet, we draw just a rectangle.
179 if (displayGraphic(params_) && readyToDisplay(loader_)) {
180 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
182 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
187 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
189 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
193 // Print the file name.
194 FontInfo msgFont = pi.base.font;
195 msgFont.setFamily(SANS_FAMILY);
196 std::string const justname = params_.filename.onlyFileName();
198 if (!justname.empty()) {
199 msgFont.setSize(FONT_SIZE_FOOTNOTE);
200 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
201 y - theFontMetrics(msgFont).maxAscent() - 4,
202 from_utf8(justname), msgFont);
205 // Print the message.
206 docstring const msg = statusMessage(params_, loader_.status());
208 msgFont.setSize(FONT_SIZE_TINY);
209 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
210 y - 4, msg, msgFont);