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));
44 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
45 : RenderBase(other), loader_(other.loader_), params_(other.params_)
47 loader_.connect(boost::bind(&Inset::updateFrontend, inset));
51 RenderBase * RenderGraphic::clone(Inset const * inset) const
53 return new RenderGraphic(*this, inset);
57 void RenderGraphic::update(graphics::Params const & params)
61 if (!params_.filename.empty())
62 loader_.reset(params_.filename, params_);
68 bool displayGraphic(graphics::Params const & params)
70 return params.display != graphics::NoDisplay &&
71 lyxrc.display_graphics != graphics::NoDisplay;
75 docstring const statusMessage(graphics::Params const & params,
76 graphics::ImageStatus status)
80 if (!displayGraphic(params))
81 ret = _("Not shown.");
84 case graphics::WaitingToLoad:
85 ret = _("Not shown.");
87 case graphics::Loading:
88 ret = _("Loading...");
90 case graphics::Converting:
91 ret = _("Converting to loadable format...");
93 case graphics::Loaded:
94 ret = _("Loaded into memory. Generating pixmap...");
96 case graphics::ScalingEtc:
97 ret = _("Scaling etc...");
100 ret = _("Ready to display");
102 case graphics::ErrorNoFile:
103 ret = _("No file found!");
105 case graphics::ErrorConverting:
106 ret = _("Error converting to loadable format");
108 case graphics::ErrorLoading:
109 ret = _("Error loading file into memory");
111 case graphics::ErrorGeneratingPixmap:
112 ret = _("Error generating the pixmap");
114 case graphics::ErrorUnknown:
124 bool readyToDisplay(graphics::Loader const & loader)
126 if (!loader.image() || loader.status() != graphics::Ready)
128 return loader.image()->isDrawable();
134 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
136 bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
138 dim.asc = image_ready ? loader_.image()->height() : 50;
142 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
146 FontInfo msgFont(mi.base.font);
147 msgFont.setFamily(SANS_FAMILY);
150 docstring const justname = from_utf8(params_.filename.onlyFileName());
151 if (!justname.empty()) {
152 msgFont.setSize(FONT_SIZE_FOOTNOTE);
153 font_width = theFontMetrics(msgFont).width(justname);
156 docstring const msg = statusMessage(params_, loader_.status());
158 msgFont.setSize(FONT_SIZE_TINY);
159 font_width = max(font_width,
160 theFontMetrics(msgFont).width(msg));
163 dim.wid = max(50, font_width + 15);
170 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
172 if (displayGraphic(params_)) {
173 if (loader_.status() == graphics::WaitingToLoad)
174 loader_.startLoading();
175 if (!loader_.monitoring())
176 loader_.startMonitoring();
179 // This will draw the graphics. If the graphics has not been
180 // loaded yet, we draw just a rectangle.
182 if (displayGraphic(params_) && readyToDisplay(loader_)) {
183 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
185 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
190 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
192 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
196 // Print the file name.
197 FontInfo msgFont = pi.base.font;
198 msgFont.setFamily(SANS_FAMILY);
199 string const justname = params_.filename.onlyFileName();
201 if (!justname.empty()) {
202 msgFont.setSize(FONT_SIZE_FOOTNOTE);
203 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
204 y - theFontMetrics(msgFont).maxAscent() - 4,
205 from_utf8(justname), msgFont);
208 // Print the message.
209 docstring const msg = statusMessage(params_, loader_.status());
211 msgFont.setSize(FONT_SIZE_TINY);
212 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
213 y - 4, msg, msgFont);