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"
19 #include "MetricsInfo.h"
21 #include "frontends/FontMetrics.h"
22 #include "frontends/Painter.h"
24 #include "graphics/GraphicsImage.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/gettext.h"
30 #include "support/bind.h"
37 RenderGraphic::RenderGraphic(Inset const * inset)
39 loader_.connect(bind(&Inset::updateFrontend, inset));
43 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
44 : RenderBase(other), loader_(other.loader_), params_(other.params_)
46 loader_.connect(bind(&Inset::updateFrontend, inset));
50 RenderBase * RenderGraphic::clone(Inset const * inset) const
52 return new RenderGraphic(*this, inset);
55 void RenderGraphic::reload() const
60 void RenderGraphic::update(graphics::Params const & params)
64 if (!params_.filename.empty())
65 loader_.reset(params_.filename, params_);
71 bool displayGraphic(graphics::Params const & params)
73 return params.display && lyxrc.display_graphics;
77 docstring const statusMessage(graphics::Params const & params,
78 graphics::ImageStatus status)
82 if (!displayGraphic(params))
83 ret = _("Not shown.");
86 case graphics::WaitingToLoad:
87 ret = _("Not shown.");
89 case graphics::Loading:
90 ret = _("Loading...");
92 case graphics::Converting:
93 ret = _("Converting to loadable format...");
95 case graphics::Loaded:
96 ret = _("Loaded into memory. Generating pixmap...");
98 case graphics::ScalingEtc:
99 ret = _("Scaling etc...");
101 case graphics::Ready:
102 ret = _("Ready to display");
104 case graphics::ErrorNoFile:
105 ret = _("No file found!");
107 case graphics::ErrorConverting:
108 ret = _("Error converting to loadable format");
110 case graphics::ErrorLoading:
111 ret = _("Error loading file into memory");
113 case graphics::ErrorGeneratingPixmap:
114 ret = _("Error generating the pixmap");
116 case graphics::ErrorUnknown:
126 bool readyToDisplay(graphics::Loader const & loader)
128 if (!loader.image() || loader.status() != graphics::Ready)
130 return loader.image()->isDrawable();
136 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
138 if (displayGraphic(params_)) {
139 if (loader_.status() == graphics::WaitingToLoad)
140 loader_.startLoading();
141 if (!loader_.monitoring())
142 loader_.startMonitoring();
145 bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_);
147 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
148 dim.asc = loader_.image()->height();
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);
181 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
183 // This will draw the graphics. If the graphics has not been
184 // loaded yet, we draw just a rectangle.
185 int const x1 = x + Inset::TEXT_TO_INSET_OFFSET;
186 int const y1 = y - dim_.asc;
187 int const w = dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET;
188 int const h = dim_.height();
190 if (displayGraphic(params_) && readyToDisplay(loader_))
191 pi.pain.image(x1, y1, w, h, *loader_.image());
194 Color c = pi.change_.changed() ? pi.change_.color() : Color_foreground;
195 pi.pain.rectangle(x1, y1, w, h, c);
197 // Print the file name.
198 FontInfo msgFont = pi.base.font;
199 msgFont.setPaintColor(c);
200 msgFont.setFamily(SANS_FAMILY);
201 string const justname = params_.filename.onlyFileName();
203 if (!justname.empty()) {
204 msgFont.setSize(FONT_SIZE_FOOTNOTE);
205 pi.pain.text(x1 + 6, y - theFontMetrics(msgFont).maxAscent() - 4,
206 from_utf8(justname), msgFont);
209 // Print the message.
210 docstring const msg = statusMessage(params_, loader_.status());
212 msgFont.setSize(FONT_SIZE_TINY);
213 pi.pain.text(x1 + 6, y - 4, msg, msgFont);
216 pi.change_.paintCue(pi, x1, y1, x1 + w, y1 + h);