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"
20 #include "MetricsInfo.h"
22 #include "frontends/FontMetrics.h"
23 #include "frontends/Painter.h"
25 #include "graphics/GraphicsImage.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/gettext.h"
31 #include "support/bind.h"
38 RenderGraphic::RenderGraphic(Inset const * inset)
39 : loader_(inset->buffer().fileName())
41 loader_.connect(bind(&Inset::updateFrontend, inset));
45 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
46 : RenderBase(other), loader_(other.loader_), params_(other.params_)
48 loader_.connect(bind(&Inset::updateFrontend, inset));
52 RenderBase * RenderGraphic::clone(Inset const * inset) const
54 return new RenderGraphic(*this, inset);
57 void RenderGraphic::reload() const
62 void RenderGraphic::update(graphics::Params const & params)
66 if (!params_.filename.empty())
67 loader_.reset(params_.filename, params_);
73 bool displayGraphic(graphics::Params const & params)
75 return params.display && lyxrc.display_graphics;
79 docstring const statusMessage(graphics::Params const & params,
80 graphics::ImageStatus status)
84 if (!displayGraphic(params))
85 ret = _("Not shown.");
88 case graphics::WaitingToLoad:
89 ret = _("Not shown.");
91 case graphics::Loading:
92 ret = _("Loading...");
94 case graphics::Converting:
95 ret = _("Converting to loadable format...");
97 case graphics::Loaded:
98 ret = _("Loaded into memory. Generating pixmap...");
100 case graphics::ScalingEtc:
101 ret = _("Scaling etc...");
103 case graphics::Ready:
104 ret = _("Ready to display");
106 case graphics::ErrorNoFile:
107 ret = _("No file found!");
109 case graphics::ErrorConverting:
110 ret = _("Error converting to loadable format");
112 case graphics::ErrorLoading:
113 ret = _("Error loading file into memory");
115 case graphics::ErrorGeneratingPixmap:
116 ret = _("Error generating the pixmap");
118 case graphics::ErrorUnknown:
128 bool readyToDisplay(graphics::Loader const & loader)
130 if (!loader.image() || loader.status() != graphics::Ready)
132 return loader.image()->isDrawable();
138 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
140 if (displayGraphic(params_)) {
141 if (loader_.status() == graphics::WaitingToLoad)
142 loader_.startLoading();
143 if (!loader_.monitoring())
144 loader_.startMonitoring();
145 loader_.checkModifiedAsync();
148 bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_);
150 dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
151 dim.asc = loader_.image()->height();
162 FontInfo msgFont(mi.base.font);
163 msgFont.setFamily(SANS_FAMILY);
166 docstring const justname = from_utf8(params_.filename.onlyFileName());
167 if (!justname.empty()) {
168 msgFont.setSize(FOOTNOTE_SIZE);
169 font_width = theFontMetrics(msgFont).width(justname);
170 font_height = theFontMetrics(msgFont).maxHeight();
173 docstring const msg = statusMessage(params_, loader_.status());
175 msgFont.setSize(TINY_SIZE);
176 font_width = max(font_width,
177 theFontMetrics(msgFont).width(msg));
178 font_height += theFontMetrics(msgFont).maxAscent();
179 dim.des = theFontMetrics(msgFont).maxDescent();
182 dim.wid = max(50, font_width + 15);
183 dim.asc = max(50, font_height + 15);
189 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
191 // This will draw the graphics. If the graphics has not been
192 // loaded yet, we draw just a rectangle.
193 int const x1 = x + Inset::TEXT_TO_INSET_OFFSET;
194 int const y1 = y - dim_.asc;
195 int const w = dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET;
196 int const h = dim_.height();
198 if (displayGraphic(params_) && readyToDisplay(loader_))
199 pi.pain.image(x1, y1, w, h, *loader_.image());
202 Color c = pi.change_.changed() ? pi.change_.color() : Color_foreground;
203 pi.pain.rectangle(x1, y1, w, h, c);
205 // Print the file name.
206 FontInfo msgFont = pi.base.font;
207 msgFont.setPaintColor(c);
208 msgFont.setFamily(SANS_FAMILY);
209 string const justname = params_.filename.onlyFileName();
211 if (!justname.empty()) {
212 msgFont.setSize(FOOTNOTE_SIZE);
213 pi.pain.text(x1 + 6, y - theFontMetrics(msgFont).maxAscent() - 4,
214 from_utf8(justname), msgFont);
217 // Print the message.
218 docstring const msg = statusMessage(params_, loader_.status());
220 msgFont.setSize(TINY_SIZE);
221 pi.pain.text(x1 + 6, y - 4, msg, msgFont);
224 pi.change_.paintCue(pi, x1, y1, x1 + w, y1 + h);