2 * \file render_graphic.C
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 "render_graphic.h"
15 #include "insets/inset.h"
19 #include "metricsinfo.h"
21 #include "frontends/font_metrics.h"
22 #include "frontends/Painter.h"
24 #include "graphics/GraphicsImage.h"
26 #include "support/filetools.h"
28 namespace graphics = lyx::graphics;
30 using lyx::support::AbsolutePath;
31 using lyx::support::OnlyFilename;
36 RenderGraphic::RenderGraphic()
41 RenderGraphic::RenderGraphic(RenderGraphic const & other)
43 loader_(other.loader_),
44 params_(other.params_),
49 RenderBase * RenderGraphic::clone() const
51 return new RenderGraphic(*this);
55 void RenderGraphic::update(graphics::Params const & params)
59 if (!params_.filename.empty()) {
60 BOOST_ASSERT(AbsolutePath(params_.filename));
61 loader_.reset(params_.filename, params_);
66 bool RenderGraphic::hasFileChanged() const
68 unsigned long const new_checksum = loader_.checksum();
69 bool const file_has_changed = checksum_ != new_checksum;
71 checksum_ = new_checksum;
72 return file_has_changed;
76 boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
78 return loader_.connect(slot);
84 string const statusMessage(graphics::ImageStatus status)
87 case graphics::WaitingToLoad:
88 return _("Not shown.");
89 case graphics::Loading:
90 return _("Loading...");
91 case graphics::Converting:
92 return _("Converting to loadable format...");
93 case graphics::Loaded:
94 return _("Loaded into memory. Must now generate pixmap.");
95 case graphics::ScalingEtc:
96 return _("Scaling etc...");
98 return _("Ready to display");
99 case graphics::ErrorNoFile:
100 return _("No file found!");
101 case graphics::ErrorConverting:
102 return _("Error converting to loadable format");
103 case graphics::ErrorLoading:
104 return _("Error loading file into memory");
105 case graphics::ErrorGeneratingPixmap:
106 return _("Error generating the pixmap");
107 case graphics::ErrorUnknown:
108 return _("No image");
114 bool readyToDisplay(graphics::Loader const & loader)
116 if (!loader.image() || loader.status() != graphics::Ready)
118 return loader.image()->isDrawable();
124 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
126 bool image_ready = readyToDisplay(loader_);
128 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
132 dim.wid = loader_.image()->getWidth() +
133 2 * InsetOld::TEXT_TO_INSET_OFFSET;
137 LyXFont msgFont(mi.base.font);
138 msgFont.setFamily(LyXFont::SANS_FAMILY);
140 string const justname = OnlyFilename(params_.filename);
141 if (!justname.empty()) {
142 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
143 font_width = font_metrics::width(justname, msgFont);
146 string const msg = statusMessage(loader_.status());
148 msgFont.setSize(LyXFont::SIZE_TINY);
149 font_width = std::max(font_width,
150 font_metrics::width(msg, msgFont));
153 dim.wid = std::max(50, font_width + 15);
160 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
162 if (params_.display != graphics::NoDisplay &&
163 loader_.status() == graphics::WaitingToLoad)
164 loader_.startLoading();
166 if (params_.display != graphics::NoDisplay &&
167 !loader_.monitoring())
168 loader_.startMonitoring();
170 // This will draw the graphics. If the graphics has not been loaded yet,
171 // we draw just a rectangle.
173 if (readyToDisplay(loader_)) {
174 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
176 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
181 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
183 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
187 // Print the file name.
188 LyXFont msgFont = pi.base.font;
189 msgFont.setFamily(LyXFont::SANS_FAMILY);
190 string const justname = OnlyFilename(params_.filename);
192 if (!justname.empty()) {
193 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
194 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
195 y - font_metrics::maxAscent(msgFont) - 4,
199 // Print the message.
200 string const msg = statusMessage(loader_.status());
202 msgFont.setSize(LyXFont::SIZE_TINY);
203 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
204 y - 4, msg, msgFont);