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"
16 #include "BufferView.h"
18 #include "metricsinfo.h"
20 #include "frontends/font_metrics.h"
21 #include "frontends/LyXView.h"
22 #include "frontends/Painter.h"
24 #include "graphics/GraphicsImage.h"
26 #include "support/filetools.h"
29 using namespace lyx::support;
32 RenderInset::RenderInset()
36 RenderInset::RenderInset(RenderInset const &)
38 // Cached variables are not copied
42 RenderInset::~RenderInset()
46 RenderInset & RenderInset::operator=(RenderInset const &)
48 // Cached variables are not copied
53 BufferView * RenderInset::view() const
55 return view_.lock().get();
59 ButtonRenderer::ButtonRenderer()
64 RenderInset * ButtonRenderer::clone() const
66 return new ButtonRenderer(*this);
70 void ButtonRenderer::update(string const & text, bool editable)
77 void ButtonRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
81 LyXFont font(LyXFont::ALL_SANE);
85 font_metrics::buttonText(text_, font, dim.wid, dim.asc, dim.des);
87 font_metrics::rectText(text_, font, dim.wid, dim.asc, dim.des);
93 void ButtonRenderer::draw(PainterInfo & pi, int x, int y) const
96 view_ = pi.base.bv->owner()->view();
98 // Draw it as a box with the LaTeX text
99 LyXFont font(LyXFont::ALL_SANE);
100 font.setColor(LColor::command).decSize();
103 pi.pain.buttonText(x + 2, y, text_, font);
105 pi.pain.rectText(x + 2, y, text_, font,
106 LColor::commandbg, LColor::commandframe);
111 GraphicRenderer::GraphicRenderer()
116 GraphicRenderer::GraphicRenderer(GraphicRenderer const & other)
117 : RenderInset(other),
118 loader_(other.loader_),
119 params_(other.params_),
124 RenderInset * GraphicRenderer::clone() const
126 return new GraphicRenderer(*this);
130 void GraphicRenderer::update(lyx::graphics::Params const & params)
134 if (!params_.filename.empty()) {
135 Assert(AbsolutePath(params_.filename));
136 loader_.reset(params_.filename, params_);
141 bool GraphicRenderer::hasFileChanged() const
143 unsigned long const new_checksum = loader_.checksum();
144 bool const file_has_changed = checksum_ != new_checksum;
145 if (file_has_changed)
146 checksum_ = new_checksum;
147 return file_has_changed;
151 boost::signals::connection GraphicRenderer::connect(slot_type const & slot) const
153 return loader_.connect(slot);
157 string const GraphicRenderer::statusMessage() const
159 switch (loader_.status()) {
160 case lyx::graphics::WaitingToLoad:
161 return _("Not shown.");
162 case lyx::graphics::Loading:
163 return _("Loading...");
164 case lyx::graphics::Converting:
165 return _("Converting to loadable format...");
166 case lyx::graphics::Loaded:
167 return _("Loaded into memory. Must now generate pixmap.");
168 case lyx::graphics::ScalingEtc:
169 return _("Scaling etc...");
170 case lyx::graphics::Ready:
171 return _("Ready to display");
172 case lyx::graphics::ErrorNoFile:
173 return _("No file found!");
174 case lyx::graphics::ErrorConverting:
175 return _("Error converting to loadable format");
176 case lyx::graphics::ErrorLoading:
177 return _("Error loading file into memory");
178 case lyx::graphics::ErrorGeneratingPixmap:
179 return _("Error generating the pixmap");
180 case lyx::graphics::ErrorUnknown:
181 return _("No image");
187 bool GraphicRenderer::readyToDisplay() const
189 if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
191 return loader_.image()->isDrawable();
195 void GraphicRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
197 bool image_ready = readyToDisplay();
199 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
203 dim.wid = loader_.image()->getWidth() +
204 2 * InsetOld::TEXT_TO_INSET_OFFSET;
208 LyXFont msgFont(mi.base.font);
209 msgFont.setFamily(LyXFont::SANS_FAMILY);
211 string const justname = OnlyFilename(params_.filename);
212 if (!justname.empty()) {
213 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
214 font_width = font_metrics::width(justname, msgFont);
217 string const msg = statusMessage();
219 msgFont.setSize(LyXFont::SIZE_TINY);
220 font_width = std::max(font_width,
221 font_metrics::width(msg, msgFont));
224 dim.wid = std::max(50, font_width + 15);
231 void GraphicRenderer::draw(PainterInfo & pi, int x, int y) const
234 view_ = pi.base.bv->owner()->view();
237 // Comment this out and see if anything goes wrong.
238 // The explanation for why it _was_ needed once upon a time is below.
240 // MakeAbsPath returns filename_ unchanged if it is absolute
242 string const file_with_path =
243 MakeAbsPath(params_.filename, view_->buffer()->filePath());
245 // A 'paste' operation creates a new inset with the correct filepath,
246 // but then the 'old' inset stored in the 'copy' operation is actually
247 // added to the buffer.
249 // Thus, pasting a graphic into a new buffer with different
250 // buffer->filePath() will result in the image being displayed in LyX even
251 // though the relative path now points at nothing at all. Subsequent
252 // loading of the file into LyX will therefore fail.
254 // We should ensure that the filepath is correct.
255 if (file_with_path != loader_.filename()) {
256 params_.filename = file_with_path;
261 if (params_.display != lyx::graphics::NoDisplay &&
262 loader_.status() == lyx::graphics::WaitingToLoad)
263 loader_.startLoading();
265 if (params_.display != lyx::graphics::NoDisplay &&
266 !loader_.monitoring())
267 loader_.startMonitoring();
269 // This will draw the graphics. If the graphics has not been loaded yet,
270 // we draw just a rectangle.
272 if (readyToDisplay()) {
273 pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
275 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
280 pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
282 dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
283 dim_.asc + dim_.des);
285 // Print the file name.
286 LyXFont msgFont = pi.base.font;
287 msgFont.setFamily(LyXFont::SANS_FAMILY);
288 string const justname = OnlyFilename(params_.filename);
290 if (!justname.empty()) {
291 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
292 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
293 y - font_metrics::maxAscent(msgFont) - 4,
297 // Print the message.
298 string const msg = statusMessage();
300 msgFont.setSize(LyXFont::SIZE_TINY);
301 pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
302 y - 4, msg, msgFont);