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()
30 : old_ascent_(0), checksum_(0)
34 GraphicInset::GraphicInset(GraphicInset const & other)
35 : loader_(other.loader_),
36 params_(other.params_),
37 nodisplay_message_(other.nodisplay_message_),
43 void GraphicInset::update(grfx::Params const & params)
45 if (!params.filename.empty()) {
46 lyx::Assert(AbsolutePath(params.filename));
47 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 BufferView * GraphicInset::view() const
71 return view_.lock().get();
75 boost::signals::connection GraphicInset::connect(slot_type const & slot) const
77 return loader_.connect(slot);
81 void GraphicInset::setNoDisplayMessage(string const & str)
83 nodisplay_message_ = str;
87 string const GraphicInset::statusMessage() const
89 switch (loader_.status()) {
90 case grfx::WaitingToLoad:
91 return _("Not shown.");
93 return _("Loading...");
94 case grfx::Converting:
95 return _("Converting to loadable format...");
97 return _("Loaded into memory. Must now generate pixmap.");
98 case grfx::ScalingEtc:
99 return _("Scaling etc...");
101 return _("Ready to display");
102 case grfx::ErrorNoFile:
103 return _("No file found!");
104 case grfx::ErrorConverting:
105 return _("Error converting to loadable format");
106 case grfx::ErrorLoading:
107 return _("Error loading file into memory");
108 case grfx::ErrorGeneratingPixmap:
109 return _("Error generating the pixmap");
110 case grfx::ErrorUnknown:
111 return _("No image");
117 GraphicInset::DisplayType GraphicInset::displayType() const
119 if (params_.display == grfx::NoDisplay && !nodisplay_message_.empty())
120 return NODISPLAY_MESSAGE;
122 if (!loader_.image() || loader_.status() != grfx::Ready)
123 return STATUS_MESSAGE;
125 return loader_.image()->isDrawable() ? IMAGE : STATUS_MESSAGE;
129 void GraphicInset::metrics(MetricsInfo & mi, Dimension & dim) const
131 DisplayType type = displayType();
133 old_ascent_ = (type == IMAGE) ? loader_.image()->getHeight() : 50;
135 dim.asc = old_ascent_;
140 dim.wid = loader_.image()->getWidth() +
141 2 * Inset::TEXT_TO_INSET_OFFSET;
148 LyXFont msgFont(mi.base.font);
149 msgFont.setFamily(LyXFont::SANS_FAMILY);
151 string const justname = OnlyFilename(params_.filename);
152 if (!justname.empty()) {
153 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
154 font_width = font_metrics::width(justname, msgFont);
157 string const msg = statusMessage();
159 msgFont.setSize(LyXFont::SIZE_TINY);
160 font_width = std::max(font_width,
161 font_metrics::width(msg, msgFont));
164 dim.wid = std::max(50, font_width + 15);
168 case NODISPLAY_MESSAGE:
172 LyXFont msgFont(mi.base.font);
173 msgFont.setFamily(LyXFont::SANS_FAMILY);
174 msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
175 font_width = font_metrics::width(nodisplay_message_, msgFont);
177 dim.wid = std::max(50, font_width + 15);
186 void GraphicInset::draw(PainterInfo & pi, int x, int y) const
188 BufferView * bv = pi.base.bv;
189 view_ = bv->owner()->view();
192 // MakeAbsPath returns filename_ unchanged if it is absolute
194 string const file_with_path =
195 MakeAbsPath(params_.filename, bv->buffer()->filePath());
197 // A 'paste' operation creates a new inset with the correct filepath,
198 // but then the 'old' inset stored in the 'copy' operation is actually
199 // added to the buffer.
201 // Thus, pasting a graphic into a new buffer with different
202 // buffer->filePath() will result in the image being displayed in LyX even
203 // though the relative path now points at nothing at all. Subsequent
204 // loading of the file into LyX will therefore fail.
206 // We should ensure that the filepath is correct.
207 if (file_with_path != loader_.filename()) {
208 params_.filename = file_with_path;
213 // we may have changed while someone other was drawing us so better
214 // to not draw anything as we surely call to redraw ourself soon.
215 // This is not a nice thing to do and should be fixed properly somehow.
216 // But I still don't know the best way to go. So let's do this like this
217 // for now (Jug 20020311)
218 if (dim_.asc != old_ascent_)
221 if (params_.display != grfx::NoDisplay &&
222 loader_.status() == grfx::WaitingToLoad)
223 loader_.startLoading();
225 if (!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);