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 // MakeAbsPath returns filename_ unchanged if it is absolute
199 string const file_with_path =
200 MakeAbsPath(params_.filename, view_->buffer()->filePath());
202 // A 'paste' operation creates a new inset with the correct filepath,
203 // but then the 'old' inset stored in the 'copy' operation is actually
204 // added to the buffer.
206 // Thus, pasting a graphic into a new buffer with different
207 // buffer->filePath() will result in the image being displayed in LyX even
208 // though the relative path now points at nothing at all. Subsequent
209 // loading of the file into LyX will therefore fail.
211 // We should ensure that the filepath is correct.
212 if (file_with_path != loader_.filename()) {
213 params_.filename = file_with_path;
218 if (params_.display != grfx::NoDisplay &&
219 loader_.status() == grfx::WaitingToLoad)
220 loader_.startLoading();
222 if (!loader_.monitoring())
223 loader_.startMonitoring();
225 // This will draw the graphics. If the graphics has not been loaded yet,
226 // we draw just a rectangle.
228 switch (displayType()) {
231 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
233 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
241 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
243 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
244 dim_.asc + dim_.des);
246 // Print the file name.
247 LyXFont msgFont = pi.base.font;
248 msgFont.setFamily(LyXFont::SANS_FAMILY);
249 string const justname = OnlyFilename(params_.filename);
251 if (!justname.empty()) {
252 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
253 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
254 y - font_metrics::maxAscent(msgFont) - 4,
258 // Print the message.
259 string const msg = statusMessage();
261 msgFont.setSize(LyXFont::SIZE_TINY);
262 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
263 y - 4, msg, msgFont);
268 case NODISPLAY_MESSAGE:
270 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
272 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
273 dim_.asc + dim_.des);
275 LyXFont msgFont = pi.base.font;
276 msgFont.setFamily(LyXFont::SANS_FAMILY);
277 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
278 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
279 y - font_metrics::maxAscent(msgFont) - 4,
280 nodisplay_message_, msgFont);