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/graphicinset.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 GraphicInset::GraphicInset()
34 GraphicInset::GraphicInset(GraphicInset const & other)
35 : loader_(other.loader_),
36 params_(other.params_),
37 nodisplay_message_(other.nodisplay_message_),
42 void GraphicInset::update(grfx::Params const & params)
46 if (!params_.filename.empty()) {
47 lyx::Assert(AbsolutePath(params_.filename));
48 loader_.reset(params_.filename, params_);
53 grfx::Params const & GraphicInset::params() const
59 bool GraphicInset::hasFileChanged() const
61 unsigned long const new_checksum = loader_.checksum();
62 bool const file_has_changed = checksum_ != new_checksum;
64 checksum_ = new_checksum;
65 return file_has_changed;
69 void GraphicInset::view(BufferView * bv) const
72 view_ = bv->owner()->view();
76 BufferView * GraphicInset::view() const
78 return view_.lock().get();
82 boost::signals::connection GraphicInset::connect(slot_type const & slot) const
84 return loader_.connect(slot);
88 void GraphicInset::setNoDisplayMessage(string const & str)
90 nodisplay_message_ = str;
94 string const GraphicInset::statusMessage() const
96 switch (loader_.status()) {
97 case grfx::WaitingToLoad:
98 return _("Not shown.");
100 return _("Loading...");
101 case grfx::Converting:
102 return _("Converting to loadable format...");
104 return _("Loaded into memory. Must now generate pixmap.");
105 case grfx::ScalingEtc:
106 return _("Scaling etc...");
108 return _("Ready to display");
109 case grfx::ErrorNoFile:
110 return _("No file found!");
111 case grfx::ErrorConverting:
112 return _("Error converting to loadable format");
113 case grfx::ErrorLoading:
114 return _("Error loading file into memory");
115 case grfx::ErrorGeneratingPixmap:
116 return _("Error generating the pixmap");
117 case grfx::ErrorUnknown:
118 return _("No image");
124 GraphicInset::DisplayType GraphicInset::displayType() const
126 if (params_.display == grfx::NoDisplay && !nodisplay_message_.empty())
127 return NODISPLAY_MESSAGE;
129 if (!loader_.image() || loader_.status() != grfx::Ready)
130 return STATUS_MESSAGE;
132 return loader_.image()->isDrawable() ? IMAGE : STATUS_MESSAGE;
136 void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const
138 DisplayType type = displayType();
140 dim.asc = (type == IMAGE) ? loader_.image()->getHeight() : 50;
145 dim.wid = loader_.image()->getWidth() +
146 2 * Inset::TEXT_TO_INSET_OFFSET;
153 LyXFont msgFont(mi.base.font);
154 msgFont.setFamily(LyXFont::SANS_FAMILY);
156 string const justname = OnlyFilename(params_.filename);
157 if (!justname.empty()) {
158 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
159 font_width = font_metrics::width(justname, msgFont);
162 string const msg = statusMessage();
164 msgFont.setSize(LyXFont::SIZE_TINY);
165 font_width = std::max(font_width,
166 font_metrics::width(msg, msgFont));
169 dim.wid = std::max(50, font_width + 15);
173 case NODISPLAY_MESSAGE:
177 LyXFont msgFont(mi.base.font);
178 msgFont.setFamily(LyXFont::SANS_FAMILY);
179 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
180 font_width = font_metrics::width(nodisplay_message_, msgFont);
182 dim.wid = std::max(50, font_width + 15);
191 void GraphicInset::draw(PainterInfo & pi, int x, int y) const
193 // Cache the BufferView.
197 // Comment this out and see if anything goes wrong.
198 // The explanation for why it _was_ needed once upon a time is below.
200 // MakeAbsPath returns filename_ unchanged if it is absolute
202 string const file_with_path =
203 MakeAbsPath(params_.filename, view_->buffer()->filePath());
205 // A 'paste' operation creates a new inset with the correct filepath,
206 // but then the 'old' inset stored in the 'copy' operation is actually
207 // added to the buffer.
209 // Thus, pasting a graphic into a new buffer with different
210 // buffer->filePath() will result in the image being displayed in LyX even
211 // though the relative path now points at nothing at all. Subsequent
212 // loading of the file into LyX will therefore fail.
214 // We should ensure that the filepath is correct.
215 if (file_with_path != loader_.filename()) {
216 params_.filename = file_with_path;
221 if (params_.display != grfx::NoDisplay &&
222 loader_.status() == grfx::WaitingToLoad)
223 loader_.startLoading();
225 if (params_.display != grfx::NoDisplay && !loader_.monitoring())
226 loader_.startMonitoring();
228 // This will draw the graphics. If the graphics has not been loaded yet,
229 // we draw just a rectangle.
231 switch (displayType()) {
234 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
236 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
244 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
246 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
247 dim_.asc + dim_.des);
249 // Print the file name.
250 LyXFont msgFont = pi.base.font;
251 msgFont.setFamily(LyXFont::SANS_FAMILY);
252 string const justname = OnlyFilename(params_.filename);
254 if (!justname.empty()) {
255 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
256 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
257 y - font_metrics::maxAscent(msgFont) - 4,
261 // Print the message.
262 string const msg = statusMessage();
264 msgFont.setSize(LyXFont::SIZE_TINY);
265 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
266 y - 4, msg, msgFont);
271 case NODISPLAY_MESSAGE:
273 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
275 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
276 dim_.asc + dim_.des);
278 LyXFont msgFont = pi.base.font;
279 msgFont.setFamily(LyXFont::SANS_FAMILY);
280 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
281 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
282 y - font_metrics::maxAscent(msgFont) - 4,
283 nodisplay_message_, msgFont);