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 : inset_(inset), loader_(inset->buffer().fileName())
41 loader_.connect(bind(&Inset::updateFrontend, inset));
45 RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
46 : RenderBase(other), inset_(inset), 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() + inset_->leftOffset(mi.base.bv)
151 + inset_->rightOffset(mi.base.bv);
152 dim.asc = loader_.image()->height();
163 FontInfo msgFont(mi.base.font);
164 msgFont.setFamily(SANS_FAMILY);
167 docstring const justname = from_utf8(params_.filename.onlyFileName());
168 if (!justname.empty()) {
169 msgFont.setSize(FOOTNOTE_SIZE);
170 font_width = theFontMetrics(msgFont).width(justname);
171 font_height = theFontMetrics(msgFont).maxHeight();
174 docstring const msg = statusMessage(params_, loader_.status());
176 msgFont.setSize(TINY_SIZE);
177 font_width = max(font_width,
178 theFontMetrics(msgFont).width(msg));
179 font_height += theFontMetrics(msgFont).maxAscent();
180 dim.des = theFontMetrics(msgFont).maxDescent();
183 dim.wid = max(50, font_width + 15);
184 dim.asc = max(50, font_height + 15);
190 void RenderGraphic::draw(PainterInfo & pi, int x, int y, bool const darkmode) const
192 // This will draw the graphics. If the graphics has not been
193 // loaded yet, we draw just a rectangle.
194 int const x1 = x + inset_->leftOffset(pi.base.bv);
195 int const y1 = y - dim_.asc;
196 int const w = dim_.wid - inset_->leftOffset(pi.base.bv) - inset_->rightOffset(pi.base.bv);
197 int const h = dim_.height();
199 if (displayGraphic(params_) && readyToDisplay(loader_))
200 pi.pain.image(x1, y1, w, h, *loader_.image(), darkmode);
203 Color c = pi.change.changed() ? pi.change.color() : Color_foreground;
204 pi.pain.rectangle(x1, y1, w, h, c);
206 // Print the file name.
207 FontInfo msgFont = pi.base.font;
208 msgFont.setPaintColor(c);
209 msgFont.setFamily(SANS_FAMILY);
210 string const justname = params_.filename.onlyFileName();
212 if (!justname.empty()) {
213 msgFont.setSize(FOOTNOTE_SIZE);
214 pi.pain.text(x1 + 6, y - theFontMetrics(msgFont).maxAscent() - 4,
215 from_utf8(justname), msgFont);
218 // Print the message.
219 docstring const msg = statusMessage(params_, loader_.status());
221 msgFont.setSize(TINY_SIZE);
222 pi.pain.text(x1 + 6, y - 4, msg, msgFont);
225 pi.change.paintCue(pi, x1, y1, x1 + w, y1 + h);