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 BufferView * GraphicInset::view() const
65 return view_.lock().get();
69 boost::signals::connection GraphicInset::connect(slot_type const & slot) const
71 return loader_.connect(slot);
75 void GraphicInset::setNoDisplayMessage(string const & str)
77 nodisplay_message_ = str;
81 string const GraphicInset::statusMessage() const
83 switch (loader_.status()) {
84 case grfx::WaitingToLoad:
85 return _("Not shown.");
87 return _("Loading...");
88 case grfx::Converting:
89 return _("Converting to loadable format...");
91 return _("Loaded into memory. Must now generate pixmap.");
92 case grfx::ScalingEtc:
93 return _("Scaling etc...");
95 return _("Ready to display");
96 case grfx::ErrorNoFile:
97 return _("No file found!");
98 case grfx::ErrorConverting:
99 return _("Error converting to loadable format");
100 case grfx::ErrorLoading:
101 return _("Error loading file into memory");
102 case grfx::ErrorGeneratingPixmap:
103 return _("Error generating the pixmap");
104 case grfx::ErrorUnknown:
105 return _("No image");
111 GraphicInset::DisplayType GraphicInset::displayType() const
113 if (params_.display == grfx::NoDisplay && !nodisplay_message_.empty())
114 return NODISPLAY_MESSAGE;
116 if (!loader_.image() || loader_.status() != grfx::Ready)
117 return STATUS_MESSAGE;
119 return loader_.image()->isDrawable() ? IMAGE : STATUS_MESSAGE;
123 void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const
125 DisplayType type = displayType();
127 dim.asc = (type == IMAGE) ? loader_.image()->getHeight() : 50;
132 dim.wid = loader_.image()->getWidth() +
133 2 * Inset::TEXT_TO_INSET_OFFSET;
140 LyXFont msgFont(mi.base.font);
141 msgFont.setFamily(LyXFont::SANS_FAMILY);
143 string const justname = OnlyFilename(params_.filename);
144 if (!justname.empty()) {
145 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
146 font_width = font_metrics::width(justname, msgFont);
149 string const msg = statusMessage();
151 msgFont.setSize(LyXFont::SIZE_TINY);
152 font_width = std::max(font_width,
153 font_metrics::width(msg, msgFont));
156 dim.wid = std::max(50, font_width + 15);
160 case NODISPLAY_MESSAGE:
164 LyXFont msgFont(mi.base.font);
165 msgFont.setFamily(LyXFont::SANS_FAMILY);
166 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
167 font_width = font_metrics::width(nodisplay_message_, msgFont);
169 dim.wid = std::max(50, font_width + 15);
178 void GraphicInset::draw(PainterInfo & pi, int x, int y) const
181 view_ = pi.base.bv->owner()->view();
184 // Comment this out and see if anything goes wrong.
185 // The explanation for why it _was_ needed once upon a time is below.
187 // MakeAbsPath returns filename_ unchanged if it is absolute
189 string const file_with_path =
190 MakeAbsPath(params_.filename, view_->buffer()->filePath());
192 // A 'paste' operation creates a new inset with the correct filepath,
193 // but then the 'old' inset stored in the 'copy' operation is actually
194 // added to the buffer.
196 // Thus, pasting a graphic into a new buffer with different
197 // buffer->filePath() will result in the image being displayed in LyX even
198 // though the relative path now points at nothing at all. Subsequent
199 // loading of the file into LyX will therefore fail.
201 // We should ensure that the filepath is correct.
202 if (file_with_path != loader_.filename()) {
203 params_.filename = file_with_path;
208 if (params_.display != grfx::NoDisplay &&
209 loader_.status() == grfx::WaitingToLoad)
210 loader_.startLoading();
212 if (params_.display != grfx::NoDisplay && !loader_.monitoring())
213 loader_.startMonitoring();
215 // This will draw the graphics. If the graphics has not been loaded yet,
216 // we draw just a rectangle.
218 switch (displayType()) {
221 pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
223 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
231 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
233 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
234 dim_.asc + dim_.des);
236 // Print the file name.
237 LyXFont msgFont = pi.base.font;
238 msgFont.setFamily(LyXFont::SANS_FAMILY);
239 string const justname = OnlyFilename(params_.filename);
241 if (!justname.empty()) {
242 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
243 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
244 y - font_metrics::maxAscent(msgFont) - 4,
248 // Print the message.
249 string const msg = statusMessage();
251 msgFont.setSize(LyXFont::SIZE_TINY);
252 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
253 y - 4, msg, msgFont);
258 case NODISPLAY_MESSAGE:
260 pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
262 dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
263 dim_.asc + dim_.des);
265 LyXFont msgFont = pi.base.font;
266 msgFont.setFamily(LyXFont::SANS_FAMILY);
267 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
268 pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
269 y - font_metrics::maxAscent(msgFont) - 4,
270 nodisplay_message_, msgFont);