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 RenderInset::RenderInset()
33 RenderInset::RenderInset(RenderInset const &)
35 // Cached variables are not copied
39 RenderInset::~RenderInset()
43 RenderInset & RenderInset::operator=(RenderInset const &)
45 // Cached variables are not copied
50 BufferView * RenderInset::view() const
52 return view_.lock().get();
56 ButtonRenderer::ButtonRenderer()
61 RenderInset * ButtonRenderer::clone() const
63 return new ButtonRenderer(*this);
67 void ButtonRenderer::update(string const & text, bool editable)
74 void ButtonRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
76 lyx::Assert(mi.base.bv);
78 LyXFont font(LyXFont::ALL_SANE);
82 font_metrics::buttonText(text_, font, dim.wid, dim.asc, dim.des);
84 font_metrics::rectText(text_, font, dim.wid, dim.asc, dim.des);
90 void ButtonRenderer::draw(PainterInfo & pi, int x, int y) const
92 lyx::Assert(pi.base.bv);
93 view_ = pi.base.bv->owner()->view();
95 // Draw it as a box with the LaTeX text
96 LyXFont font(LyXFont::ALL_SANE);
97 font.setColor(LColor::command).decSize();
100 pi.pain.buttonText(x + 2, y, text_, font);
102 pi.pain.rectText(x + 2, y, text_, font,
103 LColor::commandbg, LColor::commandframe);
108 GraphicRenderer::GraphicRenderer()
113 GraphicRenderer::GraphicRenderer(GraphicRenderer const & other)
114 : RenderInset(other),
115 loader_(other.loader_),
116 params_(other.params_),
121 RenderInset * GraphicRenderer::clone() const
123 return new GraphicRenderer(*this);
127 void GraphicRenderer::update(grfx::Params const & params)
131 if (!params_.filename.empty()) {
132 lyx::Assert(AbsolutePath(params_.filename));
133 loader_.reset(params_.filename, params_);
138 bool GraphicRenderer::hasFileChanged() const
140 unsigned long const new_checksum = loader_.checksum();
141 bool const file_has_changed = checksum_ != new_checksum;
142 if (file_has_changed)
143 checksum_ = new_checksum;
144 return file_has_changed;
148 boost::signals::connection GraphicRenderer::connect(slot_type const & slot) const
150 return loader_.connect(slot);
154 string const GraphicRenderer::statusMessage() const
156 switch (loader_.status()) {
157 case grfx::WaitingToLoad:
158 return _("Not shown.");
160 return _("Loading...");
161 case grfx::Converting:
162 return _("Converting to loadable format...");
164 return _("Loaded into memory. Must now generate pixmap.");
165 case grfx::ScalingEtc:
166 return _("Scaling etc...");
168 return _("Ready to display");
169 case grfx::ErrorNoFile:
170 return _("No file found!");
171 case grfx::ErrorConverting:
172 return _("Error converting to loadable format");
173 case grfx::ErrorLoading:
174 return _("Error loading file into memory");
175 case grfx::ErrorGeneratingPixmap:
176 return _("Error generating the pixmap");
177 case grfx::ErrorUnknown:
178 return _("No image");
184 bool GraphicRenderer::readyToDisplay() const
186 if (!loader_.image() || loader_.status() != grfx::Ready)
188 return loader_.image()->isDrawable();
192 void GraphicRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
194 bool image_ready = readyToDisplay();
196 dim.asc = image_ready ? loader_.image()->getHeight() : 50;
200 dim.wid = loader_.image()->getWidth() +
201 2 * Inset::TEXT_TO_INSET_OFFSET;
205 LyXFont msgFont(mi.base.font);
206 msgFont.setFamily(LyXFont::SANS_FAMILY);
208 string const justname = OnlyFilename(params_.filename);
209 if (!justname.empty()) {
210 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
211 font_width = font_metrics::width(justname, msgFont);
214 string const msg = statusMessage();
216 msgFont.setSize(LyXFont::SIZE_TINY);
217 font_width = std::max(font_width,
218 font_metrics::width(msg, msgFont));
221 dim.wid = std::max(50, font_width + 15);
228 void GraphicRenderer::draw(PainterInfo & pi, int x, int y) const
230 lyx::Assert(pi.base.bv);
231 view_ = pi.base.bv->owner()->view();
234 // Comment this out and see if anything goes wrong.
235 // The explanation for why it _was_ needed once upon a time is below.
237 // MakeAbsPath returns filename_ unchanged if it is absolute
239 string const file_with_path =
240 MakeAbsPath(params_.filename, view_->buffer()->filePath());
242 // A 'paste' operation creates a new inset with the correct filepath,
243 // but then the 'old' inset stored in the 'copy' operation is actually
244 // added to the buffer.
246 // Thus, pasting a graphic into a new buffer with different
247 // buffer->filePath() will result in the image being displayed in LyX even
248 // though the relative path now points at nothing at all. Subsequent
249 // loading of the file into LyX will therefore fail.
251 // We should ensure that the filepath is correct.
252 if (file_with_path != loader_.filename()) {
253 params_.filename = file_with_path;
258 if (params_.display != grfx::NoDisplay &&
259 loader_.status() == grfx::WaitingToLoad)
260 loader_.startLoading();
262 if (params_.display != grfx::NoDisplay && !loader_.monitoring())
263 loader_.startMonitoring();
265 // This will draw the graphics. If the graphics has not been loaded yet,
266 // we draw just a rectangle.
268 if (readyToDisplay()) {
269 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
271 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
276 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
278 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
279 dim_.asc + dim_.des);
281 // Print the file name.
282 LyXFont msgFont = pi.base.font;
283 msgFont.setFamily(LyXFont::SANS_FAMILY);
284 string const justname = OnlyFilename(params_.filename);
286 if (!justname.empty()) {
287 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
288 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
289 y - font_metrics::maxAscent(msgFont) - 4,
293 // Print the message.
294 string const msg = statusMessage();
296 msgFont.setSize(LyXFont::SIZE_TINY);
297 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
298 y - 4, msg, msgFont);