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 bool GraphicInset::hasFileChanged() const
55 unsigned long const new_checksum = loader_.checksum();
56 bool const file_has_changed = checksum_ != new_checksum;
58 checksum_ = new_checksum;
59 return file_has_changed;
63 void GraphicInset::view(BufferView * bv) const
66 view_ = bv->owner()->view();
70 BufferView * GraphicInset::view() const
72 return view_.lock().get();
76 boost::signals::connection GraphicInset::connect(slot_type const & slot) const
78 return loader_.connect(slot);
82 void GraphicInset::setNoDisplayMessage(string const & str)
84 nodisplay_message_ = str;
88 string const GraphicInset::statusMessage() const
90 switch (loader_.status()) {
91 case grfx::WaitingToLoad:
92 return _("Not shown.");
94 return _("Loading...");
95 case grfx::Converting:
96 return _("Converting to loadable format...");
98 return _("Loaded into memory. Must now generate pixmap.");
99 case grfx::ScalingEtc:
100 return _("Scaling etc...");
102 return _("Ready to display");
103 case grfx::ErrorNoFile:
104 return _("No file found!");
105 case grfx::ErrorConverting:
106 return _("Error converting to loadable format");
107 case grfx::ErrorLoading:
108 return _("Error loading file into memory");
109 case grfx::ErrorGeneratingPixmap:
110 return _("Error generating the pixmap");
111 case grfx::ErrorUnknown:
112 return _("No image");
118 GraphicInset::DisplayType GraphicInset::displayType() const
120 if (params_.display == grfx::NoDisplay && !nodisplay_message_.empty())
121 return NODISPLAY_MESSAGE;
123 if (!loader_.image() || loader_.status() != grfx::Ready)
124 return STATUS_MESSAGE;
126 return loader_.image()->isDrawable() ? IMAGE : STATUS_MESSAGE;
130 void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const
132 DisplayType type = displayType();
134 dim.asc = (type == IMAGE) ? loader_.image()->getHeight() : 50;
139 dim.wid = loader_.image()->getWidth() +
140 2 * Inset::TEXT_TO_INSET_OFFSET;
147 LyXFont msgFont(mi.base.font);
148 msgFont.setFamily(LyXFont::SANS_FAMILY);
150 string const justname = OnlyFilename(params_.filename);
151 if (!justname.empty()) {
152 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
153 font_width = font_metrics::width(justname, msgFont);
156 string const msg = statusMessage();
158 msgFont.setSize(LyXFont::SIZE_TINY);
159 font_width = std::max(font_width,
160 font_metrics::width(msg, msgFont));
163 dim.wid = std::max(50, font_width + 15);
167 case NODISPLAY_MESSAGE:
171 LyXFont msgFont(mi.base.font);
172 msgFont.setFamily(LyXFont::SANS_FAMILY);
173 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
174 font_width = font_metrics::width(nodisplay_message_, msgFont);
176 dim.wid = std::max(50, font_width + 15);
185 void GraphicInset::draw(PainterInfo & pi, int x, int y) const
187 // Cache the BufferView.
191 // Comment this out and see if anything goes wrong.
192 // The explanation for why it _was_ needed once upon a time is below.
194 // MakeAbsPath returns filename_ unchanged if it is absolute
196 string const file_with_path =
197 MakeAbsPath(params_.filename, view_->buffer()->filePath());
199 // A 'paste' operation creates a new inset with the correct filepath,
200 // but then the 'old' inset stored in the 'copy' operation is actually
201 // added to the buffer.
203 // Thus, pasting a graphic into a new buffer with different
204 // buffer->filePath() will result in the image being displayed in LyX even
205 // though the relative path now points at nothing at all. Subsequent
206 // loading of the file into LyX will therefore fail.
208 // We should ensure that the filepath is correct.
209 if (file_with_path != loader_.filename()) {
210 params_.filename = file_with_path;
215 if (params_.display != grfx::NoDisplay &&
216 loader_.status() == grfx::WaitingToLoad)
217 loader_.startLoading();
219 if (params_.display != grfx::NoDisplay && !loader_.monitoring())
220 loader_.startMonitoring();
222 // This will draw the graphics. If the graphics has not been loaded yet,
223 // we draw just a rectangle.
225 switch (displayType()) {
228 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
230 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
238 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
240 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
241 dim_.asc + dim_.des);
243 // Print the file name.
244 LyXFont msgFont = pi.base.font;
245 msgFont.setFamily(LyXFont::SANS_FAMILY);
246 string const justname = OnlyFilename(params_.filename);
248 if (!justname.empty()) {
249 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
250 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
251 y - font_metrics::maxAscent(msgFont) - 4,
255 // Print the message.
256 string const msg = statusMessage();
258 msgFont.setSize(LyXFont::SIZE_TINY);
259 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
260 y - 4, msg, msgFont);
265 case NODISPLAY_MESSAGE:
267 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
269 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
270 dim_.asc + dim_.des);
272 LyXFont msgFont = pi.base.font;
273 msgFont.setFamily(LyXFont::SANS_FAMILY);
274 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
275 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
276 y - font_metrics::maxAscent(msgFont) - 4,
277 nodisplay_message_, msgFont);