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 "insets/renderers.h"
15 #include "insets/inset.h"
17 #include "BufferView.h"
20 #include "metricsinfo.h"
22 #include "frontends/font_metrics.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
26 #include "graphics/GraphicsImage.h"
28 #include "support/filetools.h"
30 using lyx::support::AbsolutePath;
31 using lyx::support::OnlyFilename;
34 RenderInset::RenderInset()
38 RenderInset::RenderInset(RenderInset const &)
40 // Cached variables are not copied
44 RenderInset::~RenderInset()
48 RenderInset & RenderInset::operator=(RenderInset const &)
50 // Cached variables are not copied
55 BufferView * RenderInset::view() const
57 return view_.lock().get();
61 ButtonRenderer::ButtonRenderer()
66 RenderInset * ButtonRenderer::clone() const
68 return new ButtonRenderer(*this);
72 void ButtonRenderer::update(string const & text, bool editable)
79 void ButtonRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
81 BOOST_ASSERT(mi.base.bv);
83 LyXFont font(LyXFont::ALL_SANE);
87 font_metrics::buttonText(text_, font, dim.wid, dim.asc, dim.des);
89 font_metrics::rectText(text_, font, dim.wid, dim.asc, dim.des);
95 void ButtonRenderer::draw(PainterInfo & pi, int x, int y) const
97 BOOST_ASSERT(pi.base.bv);
98 view_ = pi.base.bv->owner()->view();
100 // Draw it as a box with the LaTeX text
101 LyXFont font(LyXFont::ALL_SANE);
102 font.setColor(LColor::command);
106 pi.pain.buttonText(x + 2, y, text_, font);
108 pi.pain.rectText(x + 2, y, text_, font,
109 LColor::commandbg, LColor::commandframe);
114 GraphicRenderer::GraphicRenderer()
119 GraphicRenderer::GraphicRenderer(GraphicRenderer const & other)
120 : RenderInset(other),
121 loader_(other.loader_),
122 params_(other.params_),
127 RenderInset * GraphicRenderer::clone() const
129 return new GraphicRenderer(*this);
133 void GraphicRenderer::update(lyx::graphics::Params const & params)
137 if (!params_.filename.empty()) {
138 BOOST_ASSERT(AbsolutePath(params_.filename));
139 loader_.reset(params_.filename, params_);
144 bool GraphicRenderer::hasFileChanged() const
146 unsigned long const new_checksum = loader_.checksum();
147 bool const file_has_changed = checksum_ != new_checksum;
148 if (file_has_changed)
149 checksum_ = new_checksum;
150 return file_has_changed;
154 boost::signals::connection GraphicRenderer::connect(slot_type const & slot) const
156 return loader_.connect(slot);
160 string const GraphicRenderer::statusMessage() const
162 switch (loader_.status()) {
163 case lyx::graphics::WaitingToLoad:
164 return _("Not shown.");
165 case lyx::graphics::Loading:
166 return _("Loading...");
167 case lyx::graphics::Converting:
168 return _("Converting to loadable format...");
169 case lyx::graphics::Loaded:
170 return _("Loaded into memory. Must now generate pixmap.");
171 case lyx::graphics::ScalingEtc:
172 return _("Scaling etc...");
173 case lyx::graphics::Ready:
174 return _("Ready to display");
175 case lyx::graphics::ErrorNoFile:
176 return _("No file found!");
177 case lyx::graphics::ErrorConverting:
178 return _("Error converting to loadable format");
179 case lyx::graphics::ErrorLoading:
180 return _("Error loading file into memory");
181 case lyx::graphics::ErrorGeneratingPixmap:
182 return _("Error generating the pixmap");
183 case lyx::graphics::ErrorUnknown:
184 return _("No image");
190 bool GraphicRenderer::readyToDisplay() const
192 if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
194 return loader_.image()->isDrawable();
198 void GraphicRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
200 bool image_ready = readyToDisplay();
202 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
206 dim.wid = loader_.image()->getWidth() +
207 2 * InsetOld::TEXT_TO_INSET_OFFSET;
211 LyXFont msgFont(mi.base.font);
212 msgFont.setFamily(LyXFont::SANS_FAMILY);
214 string const justname = OnlyFilename(params_.filename);
215 if (!justname.empty()) {
216 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
217 font_width = font_metrics::width(justname, msgFont);
220 string const msg = statusMessage();
222 msgFont.setSize(LyXFont::SIZE_TINY);
223 font_width = std::max(font_width,
224 font_metrics::width(msg, msgFont));
227 dim.wid = std::max(50, font_width + 15);
234 void GraphicRenderer::draw(PainterInfo & pi, int x, int y) const
236 BOOST_ASSERT(pi.base.bv);
237 view_ = pi.base.bv->owner()->view();
240 // Comment this out and see if anything goes wrong.
241 // The explanation for why it _was_ needed once upon a time is below.
243 // MakeAbsPath returns filename_ unchanged if it is absolute
245 string const file_with_path =
246 MakeAbsPath(params_.filename, view_->buffer()->filePath());
248 // A 'paste' operation creates a new inset with the correct filepath,
249 // but then the 'old' inset stored in the 'copy' operation is actually
250 // added to the buffer.
252 // Thus, pasting a graphic into a new buffer with different
253 // buffer->filePath() will result in the image being displayed in LyX even
254 // though the relative path now points at nothing at all. Subsequent
255 // loading of the file into LyX will therefore fail.
257 // We should ensure that the filepath is correct.
258 if (file_with_path != loader_.filename()) {
259 params_.filename = file_with_path;
264 if (params_.display != lyx::graphics::NoDisplay &&
265 loader_.status() == lyx::graphics::WaitingToLoad)
266 loader_.startLoading();
268 if (params_.display != lyx::graphics::NoDisplay &&
269 !loader_.monitoring())
270 loader_.startMonitoring();
272 // This will draw the graphics. If the graphics has not been loaded yet,
273 // we draw just a rectangle.
275 if (readyToDisplay()) {
276 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
278 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
283 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
285 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
289 // Print the file name.
290 LyXFont msgFont = pi.base.font;
291 msgFont.setFamily(LyXFont::SANS_FAMILY);
292 string const justname = OnlyFilename(params_.filename);
294 if (!justname.empty()) {
295 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
296 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
297 y - font_metrics::maxAscent(msgFont) - 4,
301 // Print the message.
302 string const msg = statusMessage();
304 msgFont.setSize(LyXFont::SIZE_TINY);
305 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
306 y - 4, msg, msgFont);